From e4a038c7d715fd74ccb07b7be256299de504be23 Mon Sep 17 00:00:00 2001 From: abdallahsoliman00 Date: Wed, 2 Jul 2025 22:24:38 +0300 Subject: [PATCH] - Added the glow argument to the shading function in mobject - Made the necessary space in finalize_color.glsl to add the shader code to create a glow --- manimlib/mobject/mobject.py | 9 +++++++-- manimlib/shaders/inserts/finalize_color.glsl | 7 +++++-- manimlib/shaders/inserts/get_unit_normal.glsl | 2 +- manimlib/shaders/true_dot/frag.glsl | 2 +- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/manimlib/mobject/mobject.py b/manimlib/mobject/mobject.py index 6fc99055f2..1ab0a88edd 100644 --- a/manimlib/mobject/mobject.py +++ b/manimlib/mobject/mobject.py @@ -80,7 +80,7 @@ def __init__( self, color: ManimColor = DEFAULT_MOBJECT_COLOR, opacity: float = 1.0, - shading: Tuple[float, float, float] = (0.0, 0.0, 0.0), + shading: Tuple[float, float, float, float] = (0.0, 0.0, 0.0, 0.0), # For shaders texture_paths: dict[str, str] | None = None, # If true, the mobject will not get rotated according to camera position @@ -1432,16 +1432,21 @@ def set_shading( reflectiveness: float | None = None, gloss: float | None = None, shadow: float | None = None, + # TODO: `glow` does nothing at the moment; + # still needs to be implemented in the shader code in + # manimlib/shaders/inserts/finalize_color.glsl + glow: float | None = None, recurse: bool = True ) -> Self: """ Larger reflectiveness makes things brighter when facing the light Larger shadow makes faces opposite the light darker Makes parts bright where light gets reflected toward the camera + Glow should add a glowing outline around the silhouette of the mobject """ for mob in self.get_family(recurse): shading = mob.uniforms["shading"] - for i, value in enumerate([reflectiveness, gloss, shadow]): + for i, value in enumerate([reflectiveness, gloss, shadow, glow]): if value is not None: shading[i] = value mob.set_uniform(shading=shading, recurse=False) diff --git a/manimlib/shaders/inserts/finalize_color.glsl b/manimlib/shaders/inserts/finalize_color.glsl index 0656c7d02a..244fa727ed 100644 --- a/manimlib/shaders/inserts/finalize_color.glsl +++ b/manimlib/shaders/inserts/finalize_color.glsl @@ -1,6 +1,6 @@ uniform vec3 light_position; uniform vec3 camera_position; -uniform vec3 shading; +uniform vec4 shading; vec3 float_to_color(float value, float min_val, float max_val, vec3[9] colormap_data){ float alpha = clamp((value - min_val) / (max_val - min_val), 0.0, 1.0); @@ -14,11 +14,14 @@ vec3 float_to_color(float value, float min_val, float max_val, vec3[9] colormap_ vec4 add_light(vec4 color, vec3 point, vec3 unit_normal){ - if(shading == vec3(0.0)) return color; + if(shading == vec4(0.0)) return color; float reflectiveness = shading.x; float gloss = shading.y; float shadow = shading.z; + // glow does noting at the moment; + // shader code needs to be implemented to create a glow around the mobject + float glow = shading.w; vec4 result = color; vec3 to_camera = normalize(camera_position - point); diff --git a/manimlib/shaders/inserts/get_unit_normal.glsl b/manimlib/shaders/inserts/get_unit_normal.glsl index f2327375e4..9616204ec5 100644 --- a/manimlib/shaders/inserts/get_unit_normal.glsl +++ b/manimlib/shaders/inserts/get_unit_normal.glsl @@ -7,7 +7,7 @@ vec3 get_unit_normal(vec3 p0, vec3 p1, vec3 p2){ if(cp_norm > tol) return cp / cp_norm; - // Otherwise, three pionts form a line, so find + // Otherwise, three points form a line, so find // a normal vector to that line in the plane shared // with the z-axis vec3 comb = v1 + v2; diff --git a/manimlib/shaders/true_dot/frag.glsl b/manimlib/shaders/true_dot/frag.glsl index 6f6bf251b9..09bb743a6e 100644 --- a/manimlib/shaders/true_dot/frag.glsl +++ b/manimlib/shaders/true_dot/frag.glsl @@ -26,7 +26,7 @@ void main() { frag_color.a *= pow(1 - r, glow_factor); } - if(shading != vec3(0.0)){ + if(shading != vec4(0.0)){ vec3 point_3d = point + radius * sqrt(1 - r * r) * to_cam; vec3 normal = normalize(point_3d - center); frag_color = finalize_color(frag_color, point_3d, normal);