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;
|
2021-01-09 10:30:42 -08:00
|
|
|
|
2020-06-01 16:21:18 -07:00
|
|
|
// Needed for get_gl_Position
|
2020-06-08 17:55:41 -07:00
|
|
|
uniform vec2 frame_shape;
|
2022-12-28 18:52:05 -08:00
|
|
|
uniform vec2 pixel_shape;
|
2020-06-02 16:18:44 -07:00
|
|
|
uniform float focal_distance;
|
2020-06-08 20:27:07 -07:00
|
|
|
uniform float is_fixed_in_frame;
|
2021-01-09 22:11:38 -08:00
|
|
|
// Needed for finalize_color
|
2020-06-06 16:55:56 -07:00
|
|
|
uniform vec3 light_source_position;
|
2021-11-08 21:43:57 -08:00
|
|
|
uniform vec3 camera_position;
|
|
|
|
uniform float reflectiveness;
|
2020-06-28 12:13:25 -07:00
|
|
|
uniform float gloss;
|
|
|
|
uniform float shadow;
|
2020-02-03 10:52:39 -08:00
|
|
|
|
2020-02-16 10:53:16 -08:00
|
|
|
in vec3 bp[3];
|
2022-12-23 10:08:23 -07:00
|
|
|
in float v_orientation[3];
|
2020-02-03 10:52:39 -08:00
|
|
|
in vec4 v_color[3];
|
2020-06-28 12:13:25 -07:00
|
|
|
in float v_vert_index[3];
|
2020-02-03 10:52:39 -08:00
|
|
|
|
|
|
|
out vec4 color;
|
2020-05-31 17:02:05 -07:00
|
|
|
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
|
|
|
|
2020-06-03 17:10:33 -07:00
|
|
|
out float orientation;
|
2023-01-09 19:54:09 -08:00
|
|
|
// uv space is where the curve coincides with y = x^2
|
2020-02-03 10:52:39 -08:00
|
|
|
out vec2 uv_coords;
|
2023-01-10 08:54:02 -08:00
|
|
|
out float is_linear;
|
2020-02-03 10:52:39 -08:00
|
|
|
|
2022-12-28 11:17:55 -08:00
|
|
|
vec3 unit_normal;
|
2022-12-23 10:08:23 -07:00
|
|
|
|
2023-01-10 09:49:51 -08:00
|
|
|
const float ANGLE_THRESHOLD = 1e-3;
|
|
|
|
|
2021-01-07 16:16:20 -08:00
|
|
|
|
|
|
|
// Analog of import for manim only
|
2023-01-10 09:53:17 -08:00
|
|
|
#INSERT get_xy_to_uv.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
|
2021-01-09 22:11:38 -08:00
|
|
|
#INSERT finalize_color.glsl
|
2020-02-03 10:52:39 -08:00
|
|
|
|
2020-06-03 17:10:33 -07:00
|
|
|
|
|
|
|
void emit_vertex_wrapper(vec3 point, int index){
|
2021-01-09 22:11:38 -08:00
|
|
|
color = finalize_color(
|
2020-06-06 16:55:56 -07:00
|
|
|
v_color[index],
|
|
|
|
point,
|
2022-12-28 11:17:55 -08:00
|
|
|
unit_normal,
|
2020-06-06 16:55:56 -07:00
|
|
|
light_source_position,
|
2021-11-08 21:43:57 -08:00
|
|
|
camera_position,
|
|
|
|
reflectiveness,
|
2020-06-28 12:13:25 -07:00
|
|
|
gloss,
|
|
|
|
shadow
|
2020-06-06 16:55:56 -07:00
|
|
|
);
|
2023-01-09 19:54:09 -08:00
|
|
|
gl_Position = get_gl_Position(point);
|
2020-06-03 17:10:33 -07:00
|
|
|
EmitVertex();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-03 10:52:39 -08:00
|
|
|
void emit_simple_triangle(){
|
|
|
|
for(int i = 0; i < 3; i++){
|
2020-06-03 17:10:33 -07:00
|
|
|
emit_vertex_wrapper(bp[i], i);
|
2020-02-03 10:52:39 -08:00
|
|
|
}
|
|
|
|
EndPrimitive();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-10 09:49:51 -08:00
|
|
|
void emit_pentagon(vec3[3] points){
|
|
|
|
vec2 p0 = points[0].xy;
|
|
|
|
vec2 p1 = points[1].xy;
|
|
|
|
vec2 p2 = points[2].xy;
|
2023-01-09 19:54:09 -08:00
|
|
|
|
2020-02-03 10:52:39 -08:00
|
|
|
// Tangent vectors
|
2023-01-10 09:49:51 -08:00
|
|
|
vec2 t01 = normalize(p1 - p0);
|
|
|
|
vec2 t12 = normalize(p2 - p1);
|
|
|
|
|
|
|
|
// Vectors perpendicular to the curve in the plane
|
|
|
|
// of the curve pointing outside the curve
|
|
|
|
float cross_prod = cross2d(t01, t12);
|
|
|
|
float sgn = cross_prod >= 0.0 ? 1.0 : -1.0;
|
|
|
|
vec2 p0_perp = sgn * vec2(t01.y, -t01.x);
|
|
|
|
vec2 p2_perp = sgn * vec2(t12.y, -t12.x);
|
|
|
|
|
|
|
|
float angle = asin(clamp(cross_prod, -1, 1));
|
|
|
|
is_linear = float(abs(angle) < ANGLE_THRESHOLD);
|
2020-06-02 16:18:44 -07:00
|
|
|
|
2022-12-28 16:37:44 -08:00
|
|
|
bool fill_inside = orientation > 0.0;
|
2022-12-28 18:52:05 -08:00
|
|
|
float aaw = anti_alias_width * frame_shape.y / pixel_shape.y;
|
2023-01-10 09:49:51 -08:00
|
|
|
vec2 corners[5] = vec2[5](p0, p0, p1, p2, p2);
|
|
|
|
|
|
|
|
if(fill_inside || bool(is_linear)){
|
|
|
|
// Add buffer outside the curve
|
|
|
|
corners[0] += aaw * p0_perp;
|
|
|
|
corners[2] += 0.5 * aaw * (p0_perp + p2_perp);
|
|
|
|
corners[4] += aaw * p2_perp;
|
|
|
|
}
|
|
|
|
if(!fill_inside || bool(is_linear)){
|
|
|
|
// Add buffer inside the curve
|
|
|
|
corners[1] -= aaw * p0_perp;
|
|
|
|
corners[3] -= aaw * p2_perp;
|
2020-06-03 17:10:33 -07:00
|
|
|
}
|
|
|
|
|
2023-01-09 19:54:09 -08:00
|
|
|
// Compute xy_to_uv matrix, and potentially re-evaluate bezier degree
|
2023-01-10 09:49:51 -08:00
|
|
|
mat3 xy_to_uv = get_xy_to_uv(vec2[3](p0, p1, p2), is_linear, is_linear);
|
2023-01-09 19:54:09 -08:00
|
|
|
uv_anti_alias_width = aaw * length(xy_to_uv[0].xy);
|
2023-01-08 23:34:02 -05:00
|
|
|
|
2020-02-03 10:52:39 -08:00
|
|
|
for(int i = 0; i < 5; i++){
|
2020-06-03 17:10:33 -07:00
|
|
|
int j = int(sign(i - 1) + 1); // Maps i = [0, 1, 2, 3, 4] onto j = [0, 0, 1, 2, 2]
|
2023-01-10 09:49:51 -08:00
|
|
|
vec3 corner = vec3(corners[i], points[j].z);
|
|
|
|
uv_coords = (xy_to_uv * vec3(corners[i], 1.0)).xy;
|
2020-06-03 17:10:33 -07:00
|
|
|
emit_vertex_wrapper(corner, j);
|
2020-02-03 10:52:39 -08:00
|
|
|
}
|
|
|
|
EndPrimitive();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void main(){
|
2020-06-28 12:13:25 -07:00
|
|
|
// If vert indices are sequential, don't fill all
|
|
|
|
fill_all = float(
|
|
|
|
(v_vert_index[1] - v_vert_index[0]) != 1.0 ||
|
|
|
|
(v_vert_index[2] - v_vert_index[1]) != 1.0
|
|
|
|
);
|
|
|
|
|
2023-01-10 09:49:51 -08:00
|
|
|
if(bool(fill_all)){
|
2020-02-03 10:52:39 -08:00
|
|
|
emit_simple_triangle();
|
2020-05-31 17:02:05 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-10 09:49:51 -08:00
|
|
|
vec3[3] verts = vec3[3](bp[0], bp[1], bp[2]);
|
|
|
|
unit_normal = get_unit_normal(verts);
|
2022-12-23 10:08:23 -07:00
|
|
|
orientation = v_orientation[0];
|
2021-02-03 13:41:02 -08:00
|
|
|
|
2023-01-10 09:49:51 -08:00
|
|
|
emit_pentagon(verts);
|
2020-02-03 10:52:39 -08:00
|
|
|
}
|
|
|
|
|