mirror of
https://github.com/3b1b/manim.git
synced 2025-11-13 19:47:49 +00:00
Move around where colormap code lives
This commit is contained in:
parent
e98ebfe9e1
commit
92b0953081
3 changed files with 20 additions and 29 deletions
|
|
@ -1277,13 +1277,10 @@ class Mobject(object):
|
||||||
"""
|
"""
|
||||||
for char in "xyz":
|
for char in "xyz":
|
||||||
glsl_snippet = glsl_snippet.replace(char, "point." + char)
|
glsl_snippet = glsl_snippet.replace(char, "point." + char)
|
||||||
self.replace_shader_code(
|
|
||||||
"///// INSERT COLOR_MAP FUNCTION HERE /////",
|
|
||||||
get_colormap_code(colormap)
|
|
||||||
)
|
|
||||||
self.set_color_by_code(
|
self.set_color_by_code(
|
||||||
"color.rgb = colormap({}, {}, {});".format(
|
"color.rgb = float_to_color({}, {}, {}, {});".format(
|
||||||
glsl_snippet, float(min_value), float(max_value)
|
glsl_snippet, float(min_value), float(max_value),
|
||||||
|
get_colormap_code(colormap)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return self
|
return self
|
||||||
|
|
|
||||||
|
|
@ -154,28 +154,13 @@ def get_shader_code_from_file(filename):
|
||||||
|
|
||||||
|
|
||||||
def get_colormap_code(colormap="viridis"):
|
def get_colormap_code(colormap="viridis"):
|
||||||
code = """
|
rgbs = get_cmap(colormap).colors # Make more general?
|
||||||
const vec3[9] COLOR_MAP_DATA = vec3[9](
|
sparse_rgbs = [
|
||||||
// INSERT DATA //
|
rgbs[int(n)]
|
||||||
);
|
for n in np.linspace(0, len(rgbs) - 1, 9)
|
||||||
vec3 colormap(float value, float min_val, float max_val){
|
|
||||||
float alpha = smoothstep(min_val, max_val, value);
|
|
||||||
int disc_alpha = min(int(alpha * 8), 7);
|
|
||||||
return mix(
|
|
||||||
COLOR_MAP_DATA[disc_alpha],
|
|
||||||
COLOR_MAP_DATA[disc_alpha + 1],
|
|
||||||
8.0 * alpha - disc_alpha
|
|
||||||
);
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
colors = get_cmap(colormap).colors
|
|
||||||
sparse_colors = [
|
|
||||||
colors[int(n)]
|
|
||||||
for n in np.linspace(0, len(colors) - 1, 9)
|
|
||||||
]
|
]
|
||||||
insertion = "".join(
|
data = ",".join(
|
||||||
"vec3({}, {}, {}),".format(*color)
|
"vec3({}, {}, {}),".format(*color)
|
||||||
for color in sparse_colors
|
for color in sparse_rgbs
|
||||||
)
|
)
|
||||||
insertion = insertion[:-1] # Remove final comma
|
return f"vec3[9]({data})"
|
||||||
return code.replace("// INSERT DATA //", insertion)
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,13 @@
|
||||||
///// INSERT COLOR_MAP FUNCTION HERE /////
|
vec3 float_to_color(float value, float min_val, float max_val, vec3[9] colormap_data){
|
||||||
|
float alpha = smoothstep(min_val, max_val, value);
|
||||||
|
int disc_alpha = min(int(alpha * 8), 7);
|
||||||
|
return mix(
|
||||||
|
colormap_data[disc_alpha],
|
||||||
|
colormap_data[disc_alpha + 1],
|
||||||
|
8.0 * alpha - disc_alpha
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
vec4 add_light(vec4 color,
|
vec4 add_light(vec4 color,
|
||||||
vec3 point,
|
vec3 point,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue