From b91284b4aad795bfcee945743342fbf66b20b230 Mon Sep 17 00:00:00 2001 From: Grant Sanderson Date: Tue, 9 Jun 2020 12:34:00 -0700 Subject: [PATCH] Small shader fixes --- manimlib/camera/camera.py | 5 +++-- manimlib/shaders/get_gl_Position.glsl | 18 +++++++++--------- .../shaders/quadratic_bezier_fill_frag.glsl | 17 +++++++---------- .../shaders/quadratic_bezier_fill_geom.glsl | 6 +++--- .../shaders/quadratic_bezier_fill_vert.glsl | 2 +- .../shaders/quadratic_bezier_stroke_vert.glsl | 4 ++-- 6 files changed, 25 insertions(+), 27 deletions(-) diff --git a/manimlib/camera/camera.py b/manimlib/camera/camera.py index f1374597..cf7040b9 100644 --- a/manimlib/camera/camera.py +++ b/manimlib/camera/camera.py @@ -148,7 +148,7 @@ class Camera(object): "light_source_position": [-10, 10, 10], "apply_depth_test": False, # Measured in pixel widths, used for vector graphics - "anti_alias_width": 3, + "anti_alias_width": 1.5, # Although vector graphics handle antialiasing fine # without multisampling, for 3d scenes one might want # to set samples to be greater than 0. @@ -371,7 +371,8 @@ class Camera(object): return pw, ph = self.get_pixel_shape() - anti_alias_width = self.anti_alias_width / (ph / 2) + fw, fh = self.frame.get_shape() + anti_alias_width = self.anti_alias_width / (ph / fh) transform = self.frame.get_inverse_camera_position_matrix() light = self.light_source.get_location() transformed_light = np.dot(transform, [*light, 1])[:3] diff --git a/manimlib/shaders/get_gl_Position.glsl b/manimlib/shaders/get_gl_Position.glsl index 62c0fb8b..a84c3951 100644 --- a/manimlib/shaders/get_gl_Position.glsl +++ b/manimlib/shaders/get_gl_Position.glsl @@ -6,19 +6,19 @@ const vec2 DEFAULT_FRAME_SHAPE = vec2(8 * 16 / 9, 8); vec4 get_gl_Position(vec3 point){ + vec4 result = vec4(point, 1); if(!bool(is_fixed_in_frame)){ - point.x *= 2 / frame_shape.x; - point.y *= 2 / frame_shape.y; - point.z /= focal_distance; - point.xy /= max(1 - point.z, 0); + result.x *= 2 / frame_shape.x; + result.y *= 2 / frame_shape.y; + result.z /= focal_distance; + result.xy /= max(1 - point.z, 0); // Todo, does this discontinuity add weirdness? Theoretically, by this point, // the z-coordiante of gl_Position only matter for z-indexing. The reason // for thie line is to avoid agressive clipping of distant points. - if(point.z < 0) point.z *= 0.1; + if(result.z < 0) result.z *= 0.1; } else{ - point.x *= 2 / DEFAULT_FRAME_SHAPE.x; - point.y *= 2 / DEFAULT_FRAME_SHAPE.y; - + result.x *= 2 / DEFAULT_FRAME_SHAPE.x; + result.y *= 2 / DEFAULT_FRAME_SHAPE.y; } - return vec4(point.xy, -point.z, 1); + return result; } \ No newline at end of file diff --git a/manimlib/shaders/quadratic_bezier_fill_frag.glsl b/manimlib/shaders/quadratic_bezier_fill_frag.glsl index 9a6939f8..44c9eb03 100644 --- a/manimlib/shaders/quadratic_bezier_fill_frag.glsl +++ b/manimlib/shaders/quadratic_bezier_fill_frag.glsl @@ -20,9 +20,6 @@ float modify_distance_for_endpoints(vec2 p, float dist, float t){ return dist; } -// To my knowledge, there is no notion of #include for shaders, -// so to share functionality between this and others, the caller -// replaces this line with the contents of quadratic_bezier_sdf.glsl #INSERT quadratic_bezier_distance.glsl @@ -52,13 +49,13 @@ float sdf(){ ); vec2 p = to_simple_space * uv_coords; // Sign takes care of whether we should be filling the inside or outside of curve. - float sn = orientation * sign(v2); - float Fp = sn * (p.x * p.x - p.y); - vec2 grad = vec2( - -2 * p.x * v2, // del C / del u - 4 * v2 - 4 * p.x * (2 - u2) // del C / del v - ); - return Fp / length(grad); + float sgn = orientation * sign(v2); + float Fp = (p.x * p.x - p.y); + if(sgn * Fp < 0){ + return 0; + }else{ + return min_dist_to_curve(uv_coords, uv_b2, bezier_degree); + } } diff --git a/manimlib/shaders/quadratic_bezier_fill_geom.glsl b/manimlib/shaders/quadratic_bezier_fill_geom.glsl index e4b8510b..fbf48708 100644 --- a/manimlib/shaders/quadratic_bezier_fill_geom.glsl +++ b/manimlib/shaders/quadratic_bezier_fill_geom.glsl @@ -110,14 +110,14 @@ void emit_pentagon(vec3[3] points, vec3 normal){ void main(){ fill_all = v_fill_all[0]; - vec3 local_unit_normal = get_unit_normal(vec3[3](bp[0], bp[1], bp[2])); - orientation = sign(dot(v_global_unit_normal[0], local_unit_normal)); - if(fill_all == 1){ emit_simple_triangle(); return; } + vec3 local_unit_normal = get_unit_normal(vec3[3](bp[0], bp[1], bp[2])); + orientation = sign(dot(v_global_unit_normal[0], local_unit_normal)); + vec3 new_bp[3]; bezier_degree = get_reduced_control_points(vec3[3](bp[0], bp[1], bp[2]), new_bp); if(bezier_degree >= 1){ diff --git a/manimlib/shaders/quadratic_bezier_fill_vert.glsl b/manimlib/shaders/quadratic_bezier_fill_vert.glsl index 71b1e713..ce9c3981 100644 --- a/manimlib/shaders/quadratic_bezier_fill_vert.glsl +++ b/manimlib/shaders/quadratic_bezier_fill_vert.glsl @@ -24,7 +24,7 @@ out float v_shadow; void main(){ bp = position_point_into_frame(point); - v_global_unit_normal = normalize(position_point_into_frame(unit_normal)); + v_global_unit_normal = normalize(to_screen_space * vec4(unit_normal, 0)).xyz; v_color = color; v_fill_all = fill_all; v_gloss = gloss; diff --git a/manimlib/shaders/quadratic_bezier_stroke_vert.glsl b/manimlib/shaders/quadratic_bezier_stroke_vert.glsl index 6c06bdc9..94be03a7 100644 --- a/manimlib/shaders/quadratic_bezier_stroke_vert.glsl +++ b/manimlib/shaders/quadratic_bezier_stroke_vert.glsl @@ -25,7 +25,7 @@ out vec4 v_color; out float v_gloss; out float v_shadow; -const float STROKE_WIDTH_CONVERSION = 0.0025; +const float STROKE_WIDTH_CONVERSION = 0.01; // To my knowledge, there is no notion of #include for shaders, // so to share functionality between this and others, the caller @@ -36,7 +36,7 @@ void main(){ bp = position_point_into_frame(point); prev_bp = position_point_into_frame(prev_point); next_bp = position_point_into_frame(next_point); - v_global_unit_normal = normalize(position_point_into_frame(unit_normal)); + v_global_unit_normal = normalize(to_screen_space * vec4(unit_normal, 0)).xyz; v_stroke_width = STROKE_WIDTH_CONVERSION * stroke_width; v_color = color;