From 5803a005986e762ed45539decaca55c8c4f6cccd Mon Sep 17 00:00:00 2001 From: Grant Sanderson Date: Thu, 26 Jan 2023 20:14:22 -0800 Subject: [PATCH] Use smaller fill_texture, adjusting winding-fill blending hack as is necessary --- .../shaders/quadratic_bezier_fill/frag.glsl | 4 ++-- .../shaders/quadratic_bezier_fill/geom.glsl | 2 +- manimlib/utils/shaders.py | 19 ++++++++++++++----- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/manimlib/shaders/quadratic_bezier_fill/frag.glsl b/manimlib/shaders/quadratic_bezier_fill/frag.glsl index 352ad949..1db2bc88 100644 --- a/manimlib/shaders/quadratic_bezier_fill/frag.glsl +++ b/manimlib/shaders/quadratic_bezier_fill/frag.glsl @@ -30,9 +30,9 @@ void main() { is changed to -alpha / (1 - alpha). This has a singularity at alpha = 1, so we cap it at a value very close to 1. Effectively, the purpose of this cap is to make sure the original fragment color can be recovered even after - blending with an alpha = 1 color. + blending with an (alpha = 1) color. */ - float a = 0.999 * frag_color.a; + float a = 0.98 * frag_color.a; if(winding && orientation < 0) a = -a / (1 - a); frag_color.a = a; diff --git a/manimlib/shaders/quadratic_bezier_fill/geom.glsl b/manimlib/shaders/quadratic_bezier_fill/geom.glsl index 242f8621..9f1e8ad6 100644 --- a/manimlib/shaders/quadratic_bezier_fill/geom.glsl +++ b/manimlib/shaders/quadratic_bezier_fill/geom.glsl @@ -38,7 +38,7 @@ void emit_triangle(vec3 points[3], vec4 v_color[3]){ color = v_color[i]; point = points[i]; // Pure black will be used to discard fragments later - if(winding && color.rgb == vec3(0.0)) color.rgb += vec3(0.01); + if(winding && color.rgb == vec3(0.0)) color.rgb += vec3(3.0 / 256); gl_Position = get_gl_Position(points[i]); EmitVertex(); } diff --git a/manimlib/utils/shaders.py b/manimlib/utils/shaders.py index 1556015b..6fd3c03f 100644 --- a/manimlib/utils/shaders.py +++ b/manimlib/utils/shaders.py @@ -9,6 +9,7 @@ import numpy as np from manimlib.constants import DEFAULT_PIXEL_HEIGHT from manimlib.constants import DEFAULT_PIXEL_WIDTH +from manimlib.utils.customization import get_customization from manimlib.utils.directories import get_shader_dir from manimlib.utils.file_ops import find_file @@ -107,10 +108,13 @@ def get_fill_palette(ctx) -> Tuple[Framebuffer, VertexArray]: Creates a texture, loaded into a frame buffer, and a vao which can display that texture as a simple quad onto a screen. """ - size = (2 * DEFAULT_PIXEL_WIDTH, 2 * DEFAULT_PIXEL_HEIGHT) + cam_config = get_customization()['camera_resolutions'] + res_name = cam_config['default_resolution'] + size = tuple(map(int, cam_config[res_name].split("x"))) + # Important to make sure dtype is floating point (not fixed point) # so that alpha values can be negative and are not clipped - texture = ctx.texture(size=size, components=4, dtype='f4') + texture = ctx.texture(size=size, components=4, dtype='f2') depth_buffer = ctx.depth_renderbuffer(size) # TODO, currently not used texture_fbo = ctx.framebuffer(texture, depth_buffer) @@ -134,7 +138,9 @@ def get_fill_palette(ctx) -> Tuple[Framebuffer, VertexArray]: uniform float h_nudge; in vec2 v_textcoord; - out vec4 frag_color; + out vec4 color; + + const float MIN_RGB = 2.0 / 256; void main() { // Apply poor man's anti-aliasing @@ -142,12 +148,15 @@ def get_fill_palette(ctx) -> Tuple[Framebuffer, VertexArray]: vec2 tc1 = v_textcoord + vec2(0, h_nudge); vec2 tc2 = v_textcoord + vec2(v_nudge, 0); vec2 tc3 = v_textcoord + vec2(v_nudge, h_nudge); - frag_color = + color = 0.25 * texture(Texture, tc0) + 0.25 * texture(Texture, tc1) + 0.25 * texture(Texture, tc2) + 0.25 * texture(Texture, tc3); - if(distance(frag_color.rgb, vec3(0.0)) < 1e-3) discard; + if(abs(color.r) < MIN_RGB && abs(color.g) < MIN_RGB && abs(color.b) < MIN_RGB) + discard; + // Counteract scaling in quadratic_bezier_frag + color = color / 0.98; //TODO, set gl_FragDepth; } ''',