Move around where colormap code lives

This commit is contained in:
Grant Sanderson 2021-01-10 08:35:06 -08:00
parent e98ebfe9e1
commit 92b0953081
3 changed files with 20 additions and 29 deletions

View file

@ -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

View file

@ -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)

View file

@ -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,