Use gl_InstanceID instead of hacking triangle_strip

This commit is contained in:
Grant Sanderson 2023-01-24 13:49:43 -08:00
parent 4774d2bc3b
commit 6e56c31d67
6 changed files with 34 additions and 37 deletions

View file

@ -457,7 +457,10 @@ class Camera(object):
self.ctx.enable(moderngl.BLEND)
self.ctx.blend_func = moderngl.ONE, moderngl.ONE
self.ctx.blend_equation = moderngl.FUNC_SUBTRACT
render_group["vao"].render(int(shader_wrapper.render_primitive))
render_group["vao"].render(
int(shader_wrapper.render_primitive),
instances=2,
)
self.ctx.blend_func = moderngl.DEFAULT_BLENDING
self.ctx.blend_equation = moderngl.FUNC_ADD
self.fbo.use()

View file

@ -1183,7 +1183,6 @@ class VMobject(Mobject):
for submob in family:
if submob.has_fill():
submob.data["base_point"][:] = submob.data["point"][0]
# submob.data["base_color"][:] = submob.data["fill_color"][0]
fill_datas.append(submob.data[fill_names])
# Add dummy
fill_datas.append(submob.data[fill_names][-1:])

View file

@ -1,9 +1,7 @@
#version 330
in vec4 color;
in float fill_all; // Either 0 or 1
in float orientation;
in float fill_all;
in vec2 uv_coords;
out vec4 frag_color;
@ -11,11 +9,9 @@ out vec4 frag_color;
void main() {
if (color.a == 0) discard;
frag_color = color;
if (orientation == 0) return;
float x0 = uv_coords.x;
float y0 = uv_coords.y;
float Fxy = y0 - x0 * x0;
if(orientation * Fxy < 0) discard;
if (bool(fill_all)) return;
float x = uv_coords.x;
float y = uv_coords.y;
if(y - x * x < 0) discard;
}

View file

@ -1,7 +1,7 @@
#version 330
layout (triangles) in;
layout (triangle_strip, max_vertices = 7) out;
layout (triangle_strip, max_vertices = 3) out;
uniform float anti_alias_width;
uniform float pixel_size;
@ -11,11 +11,10 @@ in vec3 verts[3];
in vec4 v_color[3];
in vec3 v_base_point[3];
in float v_vert_index[3];
in float v_inst_id[3];
out vec4 color;
out float orientation;
out float fill_all;
// uv space is where the curve coincides with y = x^2
out vec2 uv_coords;
@ -38,31 +37,29 @@ void main(){
// actually only need every other strip element
if (int(v_vert_index[0]) % 2 == 1) return;
// Curves are marked as eneded when the handle after
// Curves are marked as ended when the handle after
// the first anchor is set equal to that anchor
if (verts[0] == verts[1]) return;
vec3 unit_normal = get_unit_normal(verts[0], verts[1], verts[2]);
// Emit main triangle
orientation = 0.0;
uv_coords = vec2(0.0);
emit_vertex_wrapper(verts[2], v_color[2], unit_normal);
emit_vertex_wrapper(v_base_point[0], v_color[1], unit_normal);
emit_vertex_wrapper(verts[0], v_color[0], unit_normal);
// Emit edge triangle
orientation = 1.0;
uv_coords = vec2(0, 0);
// Two dummies
emit_vertex_wrapper(verts[0], v_color[0], unit_normal);
emit_vertex_wrapper(verts[0], v_color[0], unit_normal);
// Inner corner
uv_coords = vec2(0.5, 0);
emit_vertex_wrapper(verts[1], v_color[1], unit_normal);
// Last corner
uv_coords = vec2(1.0, 1.0);
emit_vertex_wrapper(verts[2], v_color[2], unit_normal);
EndPrimitive();
if(int(v_inst_id[0]) % 2 == 0){
// Emit main triangle
fill_all = float(true);
uv_coords = vec2(0.0);
emit_vertex_wrapper(verts[0], v_color[0], unit_normal);
emit_vertex_wrapper(v_base_point[0], v_color[0], unit_normal);
emit_vertex_wrapper(verts[2], v_color[2], unit_normal);
}else{
// Emit edge triangle
fill_all = float(false);
uv_coords = vec2(0.0, 0.0);
emit_vertex_wrapper(verts[0], v_color[0], unit_normal);
uv_coords = vec2(0.5, 0);
emit_vertex_wrapper(verts[1], v_color[1], unit_normal);
uv_coords = vec2(1.0, 1.0);
emit_vertex_wrapper(verts[2], v_color[2], unit_normal);
EndPrimitive();
}
}

View file

@ -9,10 +9,12 @@ out vec4 v_joint_product;
out vec4 v_color;
out vec3 v_base_point;
out float v_vert_index;
out float v_inst_id;
void main(){
verts = point;
v_color = fill_rgba;
v_base_point = base_point;
v_vert_index = gl_VertexID;
v_inst_id = gl_InstanceID;
}

View file

@ -154,7 +154,7 @@ void main() {
// actually only need every other strip element
if (int(v_vert_index[0]) % 2 == 1) return;
// Curves are marked as eneded when the handle after
// Curves are marked as ended when the handle after
// the first anchor is set equal to that anchor
if (verts[0] == verts[1]) return;