3b1b-manim/manimlib/shaders/quadratic_bezier_fill_geom.glsl

110 lines
2.9 KiB
Text
Raw Normal View History

2020-02-03 10:52:39 -08:00
#version 330
layout (triangles) in;
layout (triangle_strip, max_vertices = 5) out;
2020-06-02 16:18:44 -07:00
uniform float anti_alias_width;
2020-06-01 16:21:18 -07:00
// Needed for get_gl_Position
2020-02-03 10:52:39 -08:00
uniform float aspect_ratio;
2020-06-02 16:18:44 -07:00
uniform float focal_distance;
2020-02-03 10:52:39 -08:00
in vec3 bp[3];
2020-02-03 10:52:39 -08:00
in vec4 v_color[3];
in float v_fill_all[3];
2020-06-02 16:18:44 -07:00
in float v_gloss[3];
2020-02-03 10:52:39 -08:00
out vec4 color;
out float fill_all;
2020-02-03 10:52:39 -08:00
out float uv_anti_alias_width;
2020-06-02 16:18:44 -07:00
out vec3 xyz_coords;
out vec3 unit_normal;
2020-02-03 10:52:39 -08:00
// uv space is where b0 = (0, 0), b1 = (1, 0), and transform is orthogonal
out vec2 uv_coords;
out vec2 uv_b2;
out float bezier_degree;
2020-06-02 16:18:44 -07:00
out float gloss;
2020-02-03 10:52:39 -08:00
// To my knowledge, there is no notion of #include for shaders,
// so to share functionality between this and others, the caller
2020-05-31 17:31:03 -07:00
// in manim replaces this line with the contents of named file
2020-02-03 10:52:39 -08:00
#INSERT quadratic_bezier_geometry_functions.glsl
2020-06-01 16:21:18 -07:00
#INSERT get_gl_Position.glsl
2020-06-02 16:18:44 -07:00
#INSERT get_unit_normal.glsl
2020-02-03 10:52:39 -08:00
void emit_simple_triangle(){
for(int i = 0; i < 3; i++){
color = v_color[i];
2020-06-02 16:18:44 -07:00
gloss = v_gloss[i];
xyz_coords = bp[i];
2020-06-01 16:21:18 -07:00
gl_Position = get_gl_Position(bp[i]);
2020-02-03 10:52:39 -08:00
EmitVertex();
}
EndPrimitive();
}
2020-06-02 16:18:44 -07:00
void emit_pentagon(vec3[3] points, vec3 normal){
vec3 p0 = points[0];
vec3 p1 = points[1];
vec3 p2 = points[2];
2020-02-03 10:52:39 -08:00
// Tangent vectors
2020-06-02 16:18:44 -07:00
vec3 t01 = normalize(p1 - p0);
vec3 t12 = normalize(p2 - p1);
// Vectors normal to the curve in the plane of the curve
vec3 n01 = cross(t01, normal);
vec3 n12 = cross(t12, normal);
// Assume you always fill in to the left of the curve
float orient = sign(dot(cross(t01, t12), normal));
bool fill_in = (orient > 0);
float aaw = anti_alias_width / normal.z;
vec3 nudge1 = fill_in ? 0.5 * aaw * (n01 + n12) : vec3(0);
vec3 corners[5] = vec3[5](
p0 + aaw * n01,
p0,
p1 + nudge1,
p2,
p2 + aaw * n12
2020-02-03 10:52:39 -08:00
);
int coords_index_map[5] = int[5](0, 1, 2, 3, 4);
if(!fill_in) coords_index_map = int[5](1, 0, 2, 4, 3);
2020-02-03 10:52:39 -08:00
2020-06-02 16:18:44 -07:00
mat4 xyz_to_uv = get_xyz_to_uv(p0, p1, normal);
uv_b2 = (xyz_to_uv * vec4(p2, 1)).xy;
uv_anti_alias_width = anti_alias_width / length(p1 - p0);
2020-02-03 10:52:39 -08:00
for(int i = 0; i < 5; i++){
2020-06-02 16:18:44 -07:00
vec3 corner = corners[coords_index_map[i]];
xyz_coords = corner;
uv_coords = (xyz_to_uv * vec4(corner, 1)).xy;
2020-02-03 10:52:39 -08:00
// I haven't a clue why an index map doesn't work just
// as well here, but for some reason it doesn't.
2020-06-02 16:18:44 -07:00
int j = int(sign(i - 1) + 1); // Maps 0, 1, 2, 3, 4 onto 0, 0, 1, 2, 2
color = v_color[j];
gloss = v_gloss[j];
gl_Position = get_gl_Position(corner);
2020-02-03 10:52:39 -08:00
EmitVertex();
}
EndPrimitive();
}
void main(){
fill_all = v_fill_all[0];
2020-06-02 16:18:44 -07:00
unit_normal = get_unit_normal(bp[0], bp[1], bp[2]);
2020-02-03 10:52:39 -08:00
if(fill_all == 1){
2020-02-03 10:52:39 -08:00
emit_simple_triangle();
return;
}
2020-06-02 16:18:44 -07:00
vec3 new_bp[3];
bezier_degree = get_reduced_control_points(vec3[3](bp[0], bp[1], bp[2]), new_bp);
if(bezier_degree == 0) return; // Don't emit any vertices
emit_pentagon(new_bp, unit_normal);
2020-02-03 10:52:39 -08:00
}