Push clip plane functionality up to all Mobjects

This commit is contained in:
Grant Sanderson 2024-09-17 17:20:19 -05:00
parent 8eac976c8d
commit 95fca885c9
4 changed files with 21 additions and 25 deletions

View file

@ -142,6 +142,7 @@ class Mobject(object):
self.uniforms: UniformDict = {
"is_fixed_in_frame": 0.0,
"shading": np.array(self.shading, dtype=float),
"clip_plane": np.zeros(4),
}
def init_colors(self):
@ -1946,6 +1947,21 @@ class Mobject(object):
mob.depth_test = False
return self
def set_clip_plane(
self,
vect: Vect3 | None = None,
threshold: float | None = None
) -> Self:
if vect is not None:
self.uniforms["clip_plane"][:3] = vect
if threshold is not None:
self.uniforms["clip_plane"][3] = threshold
return self
def deactivate_clip_plane(self) -> Self:
self.uniforms["clip_plane"][:] = 0
return self
# Shader code manipulation
@affects_data

View file

@ -65,10 +65,6 @@ class Surface(Mobject):
)
self.compute_triangle_indices()
def init_uniforms(self):
super().init_uniforms()
self.uniforms["clip_plane"] = np.zeros(4)
def uv_func(self, u: float, v: float) -> tuple[float, float, float]:
# To be implemented in subclasses
return (u, v, 0.0)
@ -216,21 +212,6 @@ class Surface(Mobject):
self.add_updater(updater)
return self
def set_clip_plane(
self,
vect: Vect3 | None = None,
threshold: float | None = None
) -> Self:
if vect is not None:
self.uniforms["clip_plane"][:3] = vect
if threshold is not None:
self.uniforms["clip_plane"][3] = threshold
return self
def deactivate_clip_plane(self) -> Self:
self.uniforms["clip_plane"][:] = 0
return self
def get_shader_vert_indices(self) -> np.ndarray:
return self.get_triangle_indices()

View file

@ -2,6 +2,7 @@ uniform float is_fixed_in_frame;
uniform mat4 view;
uniform float focal_distance;
uniform vec3 frame_rescale_factors;
uniform vec4 clip_plane;
void emit_gl_Position(vec3 point){
vec4 result = vec4(point, 1.0);
@ -13,4 +14,8 @@ void emit_gl_Position(vec3 point){
// Flip and scale to prevent premature clipping
result.z *= -0.1;
gl_Position = result;
if(clip_plane.xyz != vec3(0.0, 0.0, 0.0)){
gl_ClipDistance[0] = dot(vec4(point, 1.0), clip_plane);
}
}

View file

@ -1,7 +1,5 @@
#version 330
uniform vec4 clip_plane;
in vec3 point;
in vec3 du_point;
in vec3 dv_point;
@ -17,8 +15,4 @@ void main(){
emit_gl_Position(point);
vec3 normal = cross(normalize(du_point - point), normalize(dv_point - point));
v_color = finalize_color(rgba, point, normalize(normal));
if(clip_plane.xyz != vec3(0.0, 0.0, 0.0)){
gl_ClipDistance[0] = dot(vec4(point, 1.0), clip_plane);
}
}