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.enable(moderngl.BLEND)
self.ctx.blend_func = moderngl.ONE, moderngl.ONE self.ctx.blend_func = moderngl.ONE, moderngl.ONE
self.ctx.blend_equation = moderngl.FUNC_SUBTRACT 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_func = moderngl.DEFAULT_BLENDING
self.ctx.blend_equation = moderngl.FUNC_ADD self.ctx.blend_equation = moderngl.FUNC_ADD
self.fbo.use() self.fbo.use()

View file

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

View file

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

View file

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