Make gloss and shadow uniforms

This commit is contained in:
Grant Sanderson 2020-06-28 12:13:25 -07:00
parent 1cab58ac49
commit 8af879236e
11 changed files with 54 additions and 71 deletions

View file

@ -1143,8 +1143,17 @@ class Mobject(Container):
pass # To implement in subclass pass # To implement in subclass
def interpolate_light_style(self, mobject1, mobject2, alpha): def interpolate_light_style(self, mobject1, mobject2, alpha):
self.set_gloss(interpolate(mobject1.get_gloss(), mobject2.get_gloss(), alpha)) g0 = self.get_gloss()
self.set_shadow(interpolate(mobject1.get_shadow(), mobject2.get_shadow(), alpha)) g1 = mobject1.get_gloss()
g2 = mobject2.get_gloss()
if not (g0 == g1 == g2):
self.set_gloss(interpolate(g1, g2, alpha))
s0 = self.get_shadow()
s1 = mobject1.get_shadow()
s2 = mobject2.get_shadow()
if not (s0 == s1 == s2):
self.set_shadow(interpolate(s1, s2, alpha))
return self return self
def become_partial(self, mobject, a, b): def become_partial(self, mobject, a, b):
@ -1258,6 +1267,8 @@ class Mobject(Container):
def get_shader_uniforms(self): def get_shader_uniforms(self):
return { return {
"is_fixed_in_frame": float(self.is_fixed_in_frame), "is_fixed_in_frame": float(self.is_fixed_in_frame),
"gloss": self.gloss,
"shadow": self.shadow,
} }
def get_shader_data(self): def get_shader_data(self):

View file

@ -35,8 +35,6 @@ class ParametricSurface(Mobject):
('du_point', np.float32, (3,)), ('du_point', np.float32, (3,)),
('dv_point', np.float32, (3,)), ('dv_point', np.float32, (3,)),
('color', np.float32, (4,)), ('color', np.float32, (4,)),
('gloss', np.float32, (1,)),
('shadow', np.float32, (1,)),
] ]
} }
@ -133,8 +131,6 @@ class ParametricSurface(Mobject):
data["point"] = s_points[tri_indices] data["point"] = s_points[tri_indices]
data["du_point"] = du_points[tri_indices] data["du_point"] = du_points[tri_indices]
data["dv_point"] = dv_points[tri_indices] data["dv_point"] = dv_points[tri_indices]
data["gloss"] = self.gloss
data["shadow"] = self.shadow
self.fill_in_shader_color_info(data) self.fill_in_shader_color_info(data)
return data return data
@ -170,8 +166,6 @@ class TexturedSurface(ParametricSurface):
('dv_point', np.float32, (3,)), ('dv_point', np.float32, (3,)),
('im_coords', np.float32, (2,)), ('im_coords', np.float32, (2,)),
('opacity', np.float32, (1,)), ('opacity', np.float32, (1,)),
('gloss', np.float32, (1,)),
('shadow', np.float32, (1,)),
] ]
} }

View file

@ -12,6 +12,7 @@ from manimlib.utils.bezier import bezier
from manimlib.utils.bezier import get_smooth_quadratic_bezier_handle_points from manimlib.utils.bezier import get_smooth_quadratic_bezier_handle_points
from manimlib.utils.bezier import get_quadratic_approximation_of_cubic from manimlib.utils.bezier import get_quadratic_approximation_of_cubic
from manimlib.utils.bezier import interpolate from manimlib.utils.bezier import interpolate
from manimlib.utils.bezier import set_array_by_interpolation
from manimlib.utils.bezier import integer_interpolate from manimlib.utils.bezier import integer_interpolate
from manimlib.utils.bezier import partial_quadratic_bezier_points from manimlib.utils.bezier import partial_quadratic_bezier_points
from manimlib.utils.color import color_to_rgba from manimlib.utils.color import color_to_rgba
@ -64,9 +65,8 @@ class VMobject(Mobject):
('point', np.float32, (3,)), ('point', np.float32, (3,)),
('unit_normal', np.float32, (3,)), ('unit_normal', np.float32, (3,)),
('color', np.float32, (4,)), ('color', np.float32, (4,)),
('fill_all', np.float32, (1,)), # ('fill_all', np.float32, (1,)),
('gloss', np.float32, (1,)), ('vert_index', np.float32, (1,)),
('shadow', np.float32, (1,)),
], ],
"stroke_dtype": [ "stroke_dtype": [
("point", np.float32, (3,)), ("point", np.float32, (3,)),
@ -75,8 +75,6 @@ class VMobject(Mobject):
('unit_normal', np.float32, (3,)), ('unit_normal', np.float32, (3,)),
("stroke_width", np.float32, (1,)), ("stroke_width", np.float32, (1,)),
("color", np.float32, (4,)), ("color", np.float32, (4,)),
("gloss", np.float32, (1,)),
("shadow", np.float32, (1,)),
] ]
} }
@ -828,10 +826,12 @@ class VMobject(Mobject):
"stroke_width", "stroke_width",
] ]
for attr in attrs: for attr in attrs:
arr = getattr(self, attr) set_array_by_interpolation(
m1a = getattr(mobject1, attr) getattr(self, attr),
m2a = getattr(mobject2, attr) getattr(mobject1, attr),
arr[:] = interpolate(m1a, m2a, alpha) getattr(mobject2, attr),
alpha
)
def pointwise_become_partial(self, vmobject, a, b): def pointwise_become_partial(self, vmobject, a, b):
assert(isinstance(vmobject, VMobject)) assert(isinstance(vmobject, VMobject))
@ -972,8 +972,6 @@ class VMobject(Mobject):
data["unit_normal"] = self.get_unit_normal() data["unit_normal"] = self.get_unit_normal()
data["stroke_width"][:, 0] = stroke_width data["stroke_width"][:, 0] = stroke_width
data["color"] = rgbas data["color"] = rgbas
data["gloss"] = self.gloss
data["shadow"] = self.shadow
return data return data
def lock_triangulation(self, family=True): def lock_triangulation(self, family=True):
@ -1058,12 +1056,7 @@ class VMobject(Mobject):
data["point"] = points[tri_indices] data["point"] = points[tri_indices]
data["unit_normal"] = unit_normal data["unit_normal"] = unit_normal
data["color"] = rgbas data["color"] = rgbas
# Assume the triangulation is such that the first n_points points data["vert_index"][:, 0] = tri_indices
# are on the boundary, and the rest are in the interior
data["fill_all"][:len(points)] = 0
data["fill_all"][len(points):] = 1
data["gloss"] = self.gloss
data["shadow"] = self.shadow
return data return data

View file

@ -9,13 +9,14 @@ uniform vec2 frame_shape;
uniform float focal_distance; uniform float focal_distance;
uniform float is_fixed_in_frame; uniform float is_fixed_in_frame;
uniform vec3 light_source_position; uniform vec3 light_source_position;
uniform float gloss;
uniform float shadow;
in vec3 bp[3]; in vec3 bp[3];
in vec3 v_global_unit_normal[3]; in vec3 v_global_unit_normal[3];
in vec4 v_color[3]; in vec4 v_color[3];
in float v_fill_all[3]; // in float v_fill_all[3];
in float v_gloss[3]; in float v_vert_index[3];
in float v_shadow[3];
out vec4 color; out vec4 color;
out float fill_all; out float fill_all;
@ -43,8 +44,8 @@ void emit_vertex_wrapper(vec3 point, int index){
point, point,
v_global_unit_normal[index], v_global_unit_normal[index],
light_source_position, light_source_position,
v_gloss[index], gloss,
v_shadow[index] shadow
); );
xyz_coords = point; xyz_coords = point;
gl_Position = get_gl_Position(xyz_coords); gl_Position = get_gl_Position(xyz_coords);
@ -109,8 +110,13 @@ void emit_pentagon(vec3[3] points, vec3 normal){
void main(){ void main(){
fill_all = v_fill_all[0]; // If vert indices are sequential, don't fill all
if(fill_all == 1){ fill_all = float(
(v_vert_index[1] - v_vert_index[0]) != 1.0 ||
(v_vert_index[2] - v_vert_index[1]) != 1.0
);
if(fill_all == 1.0){
emit_simple_triangle(); emit_simple_triangle();
return; return;
} }

View file

@ -6,16 +6,14 @@ uniform float is_fixed_in_frame;
in vec3 point; in vec3 point;
in vec3 unit_normal; in vec3 unit_normal;
in vec4 color; in vec4 color;
in float fill_all; // Either 0 or 1 // in float fill_all; // Either 0 or 1
in float gloss; in float vert_index;
in float shadow;
out vec3 bp; // Bezier control point out vec3 bp; // Bezier control point
out vec3 v_global_unit_normal; out vec3 v_global_unit_normal;
out vec4 v_color; out vec4 v_color;
out float v_fill_all; // out float v_fill_all;
out float v_gloss; out float v_vert_index;
out float v_shadow;
// To my knowledge, there is no notion of #include for shaders, // To my knowledge, there is no notion of #include for shaders,
// so to share functionality between this and others, the caller // so to share functionality between this and others, the caller
@ -26,7 +24,6 @@ void main(){
bp = position_point_into_frame(point); bp = position_point_into_frame(point);
v_global_unit_normal = normalize(to_screen_space * vec4(unit_normal, 0)).xyz; v_global_unit_normal = normalize(to_screen_space * vec4(unit_normal, 0)).xyz;
v_color = color; v_color = color;
v_fill_all = fill_all; // v_fill_all = fill_all;
v_gloss = gloss; v_vert_index = vert_index;
v_shadow = shadow;
} }

View file

@ -10,6 +10,8 @@ uniform float is_fixed_in_frame;
uniform float anti_alias_width; uniform float anti_alias_width;
uniform vec3 light_source_position; uniform vec3 light_source_position;
uniform float joint_type; uniform float joint_type;
uniform float gloss;
uniform float shadow;
in vec3 bp[3]; in vec3 bp[3];
in vec3 prev_bp[3]; in vec3 prev_bp[3];
@ -18,8 +20,6 @@ in vec3 v_global_unit_normal[3];
in vec4 v_color[3]; in vec4 v_color[3];
in float v_stroke_width[3]; in float v_stroke_width[3];
in float v_gloss[3];
in float v_shadow[3];
out vec4 color; out vec4 color;
out float uv_stroke_width; out float uv_stroke_width;
@ -256,8 +256,8 @@ void main() {
xyz_coords, xyz_coords,
v_global_unit_normal[index_map[i]], v_global_unit_normal[index_map[i]],
light_source_position, light_source_position,
v_gloss[index_map[i]], gloss,
v_shadow[index_map[i]] shadow
); );
gl_Position = get_gl_Position(xyz_coords); gl_Position = get_gl_Position(xyz_coords);
EmitVertex(); EmitVertex();

View file

@ -11,8 +11,6 @@ in vec3 unit_normal;
in float stroke_width; in float stroke_width;
in vec4 color; in vec4 color;
in float gloss;
in float shadow;
// Bezier control point // Bezier control point
out vec3 bp; out vec3 bp;
@ -22,8 +20,6 @@ out vec3 v_global_unit_normal;
out float v_stroke_width; out float v_stroke_width;
out vec4 v_color; out vec4 v_color;
out float v_gloss;
out float v_shadow;
const float STROKE_WIDTH_CONVERSION = 0.01; const float STROKE_WIDTH_CONVERSION = 0.01;
@ -40,6 +36,4 @@ void main(){
v_stroke_width = STROKE_WIDTH_CONVERSION * stroke_width; v_stroke_width = STROKE_WIDTH_CONVERSION * stroke_width;
v_color = color; v_color = color;
v_gloss = gloss;
v_shadow = shadow;
} }

View file

@ -4,12 +4,12 @@
// out vec4 frag_color; // out vec4 frag_color;
uniform vec3 light_source_position; uniform vec3 light_source_position;
uniform float gloss;
uniform float shadow;
in vec3 xyz_coords; in vec3 xyz_coords;
in vec3 v_normal; in vec3 v_normal;
in vec4 v_color; in vec4 v_color;
in float v_gloss;
in float v_shadow;
out vec4 frag_color; out vec4 frag_color;
@ -21,7 +21,7 @@ void main() {
xyz_coords, xyz_coords,
normalize(v_normal), normalize(v_normal),
light_source_position, light_source_position,
v_gloss, gloss,
v_shadow shadow
); );
} }

View file

@ -10,14 +10,10 @@ in vec3 point;
in vec3 du_point; in vec3 du_point;
in vec3 dv_point; in vec3 dv_point;
in vec4 color; in vec4 color;
in float gloss;
in float shadow;
out vec3 xyz_coords; out vec3 xyz_coords;
out vec3 v_normal; out vec3 v_normal;
out vec4 v_color; out vec4 v_color;
out float v_gloss;
out float v_shadow;
// These lines will get replaced // These lines will get replaced
#INSERT position_point_into_frame.glsl #INSERT position_point_into_frame.glsl
@ -28,7 +24,5 @@ void main(){
xyz_coords = position_point_into_frame(point); xyz_coords = position_point_into_frame(point);
v_normal = get_rotated_surface_unit_normal_vector(point, du_point, dv_point); v_normal = get_rotated_surface_unit_normal_vector(point, du_point, dv_point);
v_color = color; v_color = color;
v_gloss = gloss;
v_shadow = shadow;
gl_Position = get_gl_Position(xyz_coords); gl_Position = get_gl_Position(xyz_coords);
} }

View file

@ -4,13 +4,13 @@ uniform sampler2D LightTexture;
uniform sampler2D DarkTexture; uniform sampler2D DarkTexture;
uniform float num_textures; uniform float num_textures;
uniform vec3 light_source_position; uniform vec3 light_source_position;
uniform float gloss;
uniform float shadow;
in vec3 xyz_coords; in vec3 xyz_coords;
in vec3 v_normal; in vec3 v_normal;
in vec2 v_im_coords; in vec2 v_im_coords;
in float v_opacity; in float v_opacity;
in float v_gloss;
in float v_shadow;
out vec4 frag_color; out vec4 frag_color;
@ -35,8 +35,8 @@ void main() {
xyz_coords, xyz_coords,
normalize(v_normal), normalize(v_normal),
light_source_position, light_source_position,
v_gloss, gloss,
v_shadow shadow
); );
frag_color.a = v_opacity; frag_color.a = v_opacity;
} }

View file

@ -11,15 +11,11 @@ in vec3 du_point;
in vec3 dv_point; in vec3 dv_point;
in vec2 im_coords; in vec2 im_coords;
in float opacity; in float opacity;
in float gloss;
in float shadow;
out vec3 xyz_coords; out vec3 xyz_coords;
out vec3 v_normal; out vec3 v_normal;
out vec2 v_im_coords; out vec2 v_im_coords;
out float v_opacity; out float v_opacity;
out float v_gloss;
out float v_shadow;
// These lines will get replaced // These lines will get replaced
#INSERT position_point_into_frame.glsl #INSERT position_point_into_frame.glsl
@ -31,7 +27,5 @@ void main(){
v_normal = get_rotated_surface_unit_normal_vector(point, du_point, dv_point); v_normal = get_rotated_surface_unit_normal_vector(point, du_point, dv_point);
v_im_coords = im_coords; v_im_coords = im_coords;
v_opacity = opacity; v_opacity = opacity;
v_gloss = gloss;
v_shadow = shadow;
gl_Position = get_gl_Position(xyz_coords); gl_Position = get_gl_Position(xyz_coords);
} }