A community-maintained Python framework for creating mathematical animations.
Remove duplicate insert shader directory (#2064)
Ryan McCauley782b567a4642
diff --git a/manim/renderer/shader_wrapper.py b/manim/renderer/shader_wrapper.pyindex 045f8781e..829381d45 100644--- a/manim/renderer/shader_wrapper.py+++ b/manim/renderer/shader_wrapper.py@@ -183,7 +183,7 @@ def get_shader_code_from_file(filename):)for line in insertions:inserted_code = get_shader_code_from_file(- os.path.join("inserts", line.replace("#include ../include/", "")),+ os.path.join("include", line.replace("#include ../include/", "")),)result = result.replace(line, inserted_code)filename_to_code_map[filename] = resultdiff --git a/manim/renderer/shaders/inserts/NOTE.md b/manim/renderer/shaders/inserts/NOTE.mddeleted file mode 100644index 5aada430b..000000000--- a/manim/renderer/shaders/inserts/NOTE.md+++ /dev/null@@ -1,7 +0,0 @@-There seems to be no analog to #include in C++ for OpenGL shaders. While there are other options for sharing code between shaders, a lot of them aren't great, especially if the goal is to have all the logic for which specific bits of code to share handled in the shader file itself. So the way manim currently works is to replace any line which looks like--#INSERT <file_name>--with the code from one of the files in this folder.--The functions in this file often include reference to uniforms which are assumed to be part of the surrounding context into which they are inserted.diff --git a/manim/renderer/shaders/inserts/add_light.glsl b/manim/renderer/shaders/inserts/add_light.glsldeleted file mode 100644index cc6291994..000000000--- a/manim/renderer/shaders/inserts/add_light.glsl+++ /dev/null@@ -1,43 +0,0 @@-///// INSERT COLOR_MAP FUNCTION HERE /////--vec4 add_light(vec4 color,- vec3 point,- vec3 unit_normal,- vec3 light_coords,- float gloss,- float shadow){- ///// INSERT COLOR FUNCTION HERE /////- // The line above may be replaced by arbitrary code snippets, as per- // the method Mobject.set_color_by_code- if(gloss == 0.0 && shadow == 0.0) return color;-- // TODO, do we actually want this? It effectively treats surfaces as two-sided- if(unit_normal.z < 0){- unit_normal *= -1;- }-- // TODO, read this in as a uniform?- float camera_distance = 6;- // Assume everything has already been rotated such that camera is in the z-direction- vec3 to_camera = vec3(0, 0, camera_distance) - point;- vec3 to_light = light_coords - point;- vec3 light_reflection = -to_light + 2 * unit_normal * dot(to_light, unit_normal);- float dot_prod = dot(normalize(light_reflection), normalize(to_camera));- float shine = gloss * exp(-3 * pow(1 - dot_prod, 2));- float dp2 = dot(normalize(to_light), unit_normal);- float darkening = mix(1, max(dp2, 0), shadow);- return vec4(- darkening * mix(color.rgb, vec3(1.0), shine),- color.a- );-}--vec4 finalize_color(vec4 color,- vec3 point,- vec3 unit_normal,- vec3 light_coords,- float gloss,- float shadow){- // Put insertion here instead- return add_light(color, point, unit_normal, light_coords, gloss, shadow);-}diff --git a/manim/renderer/shaders/inserts/camera_uniform_declarations.glsl b/manim/renderer/shaders/inserts/camera_uniform_declarations.glsldeleted file mode 100644index fdeacab85..000000000--- a/manim/renderer/shaders/inserts/camera_uniform_declarations.glsl+++ /dev/null@@ -1,6 +0,0 @@-uniform vec2 frame_shape;-uniform float anti_alias_width;-uniform vec3 camera_center;-uniform mat3 camera_rotation;-uniform float is_fixed_in_frame;-uniform float focal_distance;diff --git a/manim/renderer/shaders/inserts/finalize_color.glsl b/manim/renderer/shaders/inserts/finalize_color.glsldeleted file mode 100644index 26b10376a..000000000--- a/manim/renderer/shaders/inserts/finalize_color.glsl+++ /dev/null@@ -1,51 +0,0 @@-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);- 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,- vec3 point,- vec3 unit_normal,- vec3 light_coords,- float gloss,- float shadow){- if(gloss == 0.0 && shadow == 0.0) return color;-- // TODO, do we actually want this? It effectively treats surfaces as two-sided- if(unit_normal.z < 0){- unit_normal *= -1;- }-- // TODO, read this in as a uniform?- float camera_distance = 6;- // Assume everything has already been rotated such that camera is in the z-direction- vec3 to_camera = vec3(0, 0, camera_distance) - point;- vec3 to_light = light_coords - point;- vec3 light_reflection = -to_light + 2 * unit_normal * dot(to_light, unit_normal);- float dot_prod = dot(normalize(light_reflection), normalize(to_camera));- float shine = gloss * exp(-3 * pow(1 - dot_prod, 2));- float dp2 = dot(normalize(to_light), unit_normal);- float darkening = mix(1, max(dp2, 0), shadow);- return vec4(- darkening * mix(color.rgb, vec3(1.0), shine),- color.a- );-}--vec4 finalize_color(vec4 color,- vec3 point,- vec3 unit_normal,- vec3 light_coords,- float gloss,- float shadow){- ///// INSERT COLOR FUNCTION HERE /////- // The line above may be replaced by arbitrary code snippets, as per- // the method Mobject.set_color_by_code- return add_light(color, point, unit_normal, light_coords, gloss, shadow);-}diff --git a/manim/renderer/shaders/inserts/get_gl_Position.glsl b/manim/renderer/shaders/inserts/get_gl_Position.glsldeleted file mode 100644index 95d3f054b..000000000--- a/manim/renderer/shaders/inserts/get_gl_Position.glsl+++ /dev/null@@ -1,31 +0,0 @@-// Assumes the following uniforms exist in the surrounding context:-// uniform vec2 frame_shape;-// uniform float focal_distance;-// uniform float is_fixed_in_frame;--const vec2 DEFAULT_FRAME_SHAPE = vec2(8.0 * 16.0 / 9.0, 8.0);--float perspective_scale_factor(float z, float focal_distance){- return max(0.0, focal_distance / (focal_distance - z));-}---vec4 get_gl_Position(vec3 point){- vec4 result = vec4(point, 1.0);- if(!bool(is_fixed_in_frame)){- result.x *= 2.0 / frame_shape.x;- result.y *= 2.0 / frame_shape.y;- float psf = perspective_scale_factor(result.z, focal_distance);- if (psf > 0){- result.xy *= psf;- // TODO, what's the better way to do this?- // This is to keep vertices too far out of frame from getting cut.- result.z *= 0.01;- }- } else{- result.x *= 2.0 / DEFAULT_FRAME_SHAPE.x;- result.y *= 2.0 / DEFAULT_FRAME_SHAPE.y;- }- result.z *= -1;- return result;-}diff --git a/manim/renderer/shaders/inserts/get_rotated_surface_unit_normal_vector.glsl b/manim/renderer/shaders/inserts/get_rotated_surface_unit_normal_vector.glsldeleted file mode 100644index 8c6350d7a..000000000--- a/manim/renderer/shaders/inserts/get_rotated_surface_unit_normal_vector.glsl+++ /dev/null@@ -1,16 +0,0 @@-// Assumes the following uniforms exist in the surrounding context:-// uniform vec3 camera_center;-// uniform mat3 camera_rotation;--vec3 get_rotated_surface_unit_normal_vector(vec3 point, vec3 du_point, vec3 dv_point){- vec3 cp = cross(- (du_point - point),- (dv_point - point)- );- if(length(cp) == 0){- // Instead choose a normal to just dv_point - point in the direction of point- vec3 v2 = dv_point - point;- cp = cross(cross(v2, point), v2);- }- return normalize(rotate_point_into_frame(cp));-}diff --git a/manim/renderer/shaders/inserts/get_unit_normal.glsl b/manim/renderer/shaders/inserts/get_unit_normal.glsldeleted file mode 100644index 65bb9c71e..000000000--- a/manim/renderer/shaders/inserts/get_unit_normal.glsl+++ /dev/null@@ -1,22 +0,0 @@-vec3 get_unit_normal(in vec3[3] points){- float tol = 1e-6;- vec3 v1 = normalize(points[1] - points[0]);- vec3 v2 = normalize(points[2] - points[0]);- vec3 cp = cross(v1, v2);- float cp_norm = length(cp);- if(cp_norm < tol){- // Three points form a line, so find a normal vector- // to that line in the plane shared with the z-axis- vec3 k_hat = vec3(0.0, 0.0, 1.0);- vec3 new_cp = cross(cross(v2, k_hat), v2);- float new_cp_norm = length(new_cp);- if(new_cp_norm < tol){- // We only come here if all three points line up- // on the z-axis.- return vec3(0.0, -1.0, 0.0);- // return k_hat;- }- return new_cp / new_cp_norm;- }- return cp / cp_norm;-}diff --git a/manim/renderer/shaders/inserts/position_point_into_frame.glsl b/manim/renderer/shaders/inserts/position_point_into_frame.glsldeleted file mode 100644index 4cef7e38f..000000000--- a/manim/renderer/shaders/inserts/position_point_into_frame.glsl+++ /dev/null@@ -1,19 +0,0 @@-// Assumes the following uniforms exist in the surrounding context:-// uniform float is_fixed_in_frame;-// uniform vec3 camera_center;-// uniform mat3 camera_rotation;--vec3 rotate_point_into_frame(vec3 point){- if(bool(is_fixed_in_frame)){- return point;- }- return camera_rotation * point;-}---vec3 position_point_into_frame(vec3 point){- if(bool(is_fixed_in_frame)){- return point;- }- return rotate_point_into_frame(point - camera_center);-}diff --git a/manim/renderer/shaders/inserts/quadratic_bezier_distance.glsl b/manim/renderer/shaders/inserts/quadratic_bezier_distance.glsldeleted file mode 100644index 847fa4ab6..000000000--- a/manim/renderer/shaders/inserts/quadratic_bezier_distance.glsl+++ /dev/null@@ -1,107 +0,0 @@-// Must be inserted in a context with a definition for modify_distance_for_endpoints--// All of this is with respect to a curve that's been rotated/scaled-// so that b0 = (0, 0) and b1 = (1, 0). That is, b2 entirely-// determines the shape of the curve--vec2 bezier(float t, vec2 b2){- // Quick returns for the 0 and 1 cases- if (t == 0) return vec2(0, 0);- else if (t == 1) return b2;- // Everything else- return vec2(- 2 * t * (1 - t) + b2.x * t*t,- b2.y * t * t- );-}---float cube_root(float x){- return sign(x) * pow(abs(x), 1.0 / 3.0);-}---int cubic_solve(float a, float b, float c, float d, out float roots[3]){- // Normalize so a = 1- b = b / a;- c = c / a;- d = d / a;-- float p = c - b*b / 3.0;- float q = b * (2.0*b*b - 9.0*c) / 27.0 + d;- float p3 = p*p*p;- float disc = q*q + 4.0*p3 / 27.0;- float offset = -b / 3.0;- if(disc >= 0.0){- float z = sqrt(disc);- float u = (-q + z) / 2.0;- float v = (-q - z) / 2.0;- u = cube_root(u);- v = cube_root(v);- roots[0] = offset + u + v;- return 1;- }- float u = sqrt(-p / 3.0);- float v = acos(-sqrt( -27.0 / p3) * q / 2.0) / 3.0;- float m = cos(v);- float n = sin(v) * 1.732050808;-- float all_roots[3] = float[3](- offset + u * (n - m),- offset - u * (n + m),- offset + u * (m + m)- );-- // Only accept roots with a positive derivative- int n_valid_roots = 0;- for(int i = 0; i < 3; i++){- float r = all_roots[i];- if(3*r*r + 2*b*r + c > 0){- roots[n_valid_roots] = r;- n_valid_roots++;- }- }- return n_valid_roots;-}--float dist_to_line(vec2 p, vec2 b2){- float t = clamp(p.x / b2.x, 0, 1);- float dist;- if(t == 0) dist = length(p);- else if(t == 1) dist = distance(p, b2);- else dist = abs(p.y);-- return modify_distance_for_endpoints(p, dist, t);-}---float dist_to_point_on_curve(vec2 p, float t, vec2 b2){- t = clamp(t, 0, 1);- return modify_distance_for_endpoints(- p, length(p - bezier(t, b2)), t- );-}---float min_dist_to_curve(vec2 p, vec2 b2, float degree){- // Check if curve is really a a line- if(degree == 1) return dist_to_line(p, b2);-- // Try finding the exact sdf by solving the equation- // (d/dt) dist^2(t) = 0, which amount to the following- // cubic.- float xm2 = uv_b2.x - 2.0;- float y = uv_b2.y;- float a = xm2*xm2 + y*y;- float b = 3 * xm2;- float c = -(p.x*xm2 + p.y*y) + 2;- float d = -p.x;-- float roots[3];- int n = cubic_solve(a, b, c, d, roots);- // At most 2 roots will have been populated.- float d0 = dist_to_point_on_curve(p, roots[0], b2);- if(n == 1) return d0;- float d1 = dist_to_point_on_curve(p, roots[1], b2);- return min(d0, d1);-}diff --git a/manim/renderer/shaders/inserts/quadratic_bezier_geometry_functions.glsl b/manim/renderer/shaders/inserts/quadratic_bezier_geometry_functions.glsldeleted file mode 100644index d2e3e0fb7..000000000--- a/manim/renderer/shaders/inserts/quadratic_bezier_geometry_functions.glsl+++ /dev/null@@ -1,92 +0,0 @@-float cross2d(vec2 v, vec2 w){- return v.x * w.y - w.x * v.y;-}---mat3 get_xy_to_uv(vec2 b0, vec2 b1){- mat3 shift = mat3(- 1.0, 0.0, 0.0,- 0.0, 1.0, 0.0,- -b0.x, -b0.y, 1.0- );-- float sf = length(b1 - b0);- vec2 I = (b1 - b0) / sf;- vec2 J = vec2(-I.y, I.x);- mat3 rotate = mat3(- I.x, J.x, 0.0,- I.y, J.y, 0.0,- 0.0, 0.0, 1.0- );- return (1 / sf) * rotate * shift;-}---// Orthogonal matrix to convert to a uv space defined so that-// b0 goes to [0, 0] and b1 goes to [1, 0]-mat4 get_xyz_to_uv(vec3 b0, vec3 b1, vec3 unit_normal){- mat4 shift = mat4(- 1, 0, 0, 0,- 0, 1, 0, 0,- 0, 0, 1, 0,- -b0.x, -b0.y, -b0.z, 1- );-- float scale_factor = length(b1 - b0);- vec3 I = (b1 - b0) / scale_factor;- vec3 K = unit_normal;- vec3 J = cross(K, I);- // Transpose (hence inverse) of matrix taking- // i-hat to I, k-hat to unit_normal, and j-hat to their cross- mat4 rotate = mat4(- I.x, J.x, K.x, 0.0,- I.y, J.y, K.y, 0.0,- I.z, J.z, K.z, 0.0,- 0.0, 0.0, 0.0, 1.0- );- return (1 / scale_factor) * rotate * shift;-}---// Returns 0 for null curve, 1 for linear, 2 for quadratic.-// Populates new_points with bezier control points for the curve,-// which for quadratics will be the same, but for linear and null-// might change. The idea is to inform the caller of the degree,-// while also passing tangency information in the linear case.-// float get_reduced_control_points(vec3 b0, vec3 b1, vec3 b2, out vec3 new_points[3]){-float get_reduced_control_points(in vec3 points[3], out vec3 new_points[3]){- float length_threshold = 1e-6;- float angle_threshold = 5e-2;-- vec3 p0 = points[0];- vec3 p1 = points[1];- vec3 p2 = points[2];- vec3 v01 = (p1 - p0);- vec3 v12 = (p2 - p1);-- float dot_prod = clamp(dot(normalize(v01), normalize(v12)), -1, 1);- bool aligned = acos(dot_prod) < angle_threshold;- bool distinct_01 = length(v01) > length_threshold; // v01 is considered nonzero- bool distinct_12 = length(v12) > length_threshold; // v12 is considered nonzero- int n_uniques = int(distinct_01) + int(distinct_12);-- bool quadratic = (n_uniques == 2) && !aligned;- bool linear = (n_uniques == 1) || ((n_uniques == 2) && aligned);- bool constant = (n_uniques == 0);- if(quadratic){- new_points[0] = p0;- new_points[1] = p1;- new_points[2] = p2;- return 2.0;- }else if(linear){- new_points[0] = p0;- new_points[1] = (p0 + p2) / 2.0;- new_points[2] = p2;- return 1.0;- }else{- new_points[0] = p0;- new_points[1] = p0;- new_points[2] = p0;- return 0.0;- }-}