Change fill shader to use simpler uv space

This commit is contained in:
Grant Sanderson
2023-01-09 19:54:09 -08:00
parent f75df1e26e
commit b4544052d9
2 changed files with 21 additions and 41 deletions

View File

@ -6,38 +6,25 @@ in vec4 color;
in float fill_all; // Either 0 or 1
in float uv_anti_alias_width;
in vec3 xyz_coords;
in float orientation;
in vec2 uv_coords;
in vec2 uv_b2;
in vec2 simp_coords;
in float bezier_degree;
out vec4 frag_color;
#INSERT quadratic_bezier_distance.glsl
float sdf(){
if(bezier_degree < 2){
return abs(uv_coords[1]);
float x0 = uv_coords.x;
float y0 = uv_coords.y;
if(bezier_degree == 1.0){
return abs(y0);
}
float u2 = uv_b2.x;
float v2 = uv_b2.y;
// For really flat curves, just take the distance to x-axis
if(abs(v2 / u2) < 0.1 * uv_anti_alias_width){
return abs(uv_coords[1]);
}
vec2 p = simp_coords;
float Fp = (p.x * p.x - p.y);
// Sign takes care of whether we should be filling the inside or outside of curve.
float sgn = orientation * sign(v2);
if(sgn * Fp <= 0){
float Fxy = y0 - x0 * x0;
if(orientation * Fxy >= 0){
return 0.0;
}else{
return min_dist_to_curve(uv_coords, uv_b2, bezier_degree);
}
return abs(Fxy) / sqrt(1 + 4 * x0 * x0);
}

View File

@ -26,13 +26,9 @@ out vec4 color;
out float fill_all;
out float uv_anti_alias_width;
out vec3 xyz_coords;
out float orientation;
// uv space is where b0 = (0, 0), b1 = (1, 0), and transform is orthogonal
// uv space is where the curve coincides with y = x^2
out vec2 uv_coords;
out vec2 uv_b2;
// These are coordinates in a space where the curve is even simpler, y = x^2
out vec2 simp_coords;
out float bezier_degree;
vec3 unit_normal;
@ -56,8 +52,7 @@ void emit_vertex_wrapper(vec3 point, int index){
gloss,
shadow
);
xyz_coords = point;
gl_Position = get_gl_Position(xyz_coords);
gl_Position = get_gl_Position(point);
EmitVertex();
}
@ -74,6 +69,7 @@ void emit_pentagon(vec3[3] points, vec3 normal){
vec3 p0 = points[0];
vec3 p1 = points[1];
vec3 p2 = points[2];
// Tangent vectors
vec3 t01 = normalize(p1 - p0);
vec3 t12 = normalize(p2 - p1);
@ -112,22 +108,19 @@ void emit_pentagon(vec3[3] points, vec3 normal){
);
}
mat4 xyz_to_uv = get_xyz_to_uv(p0, p1, normal);
uv_b2 = (xyz_to_uv * vec4(p2, 1)).xy;
uv_anti_alias_width = aaw / length(p1 - p0);
// Matrix from the uv space to an even simpler
// one where the curve is equal to y = x^2
mat2 to_simple_space = mat2(
uv_b2.y, 0,
2 - uv_b2.x, 4 * uv_b2.y
// Compute xy_to_uv matrix, and potentially re-evaluate bezier degree
float new_bezier_degree;
mat3 xy_to_uv = get_xy_to_uv(
vec2[3](p0.xy, p1.xy, p2.xy),
bezier_degree,
new_bezier_degree
);
//
bezier_degree = new_bezier_degree;
uv_anti_alias_width = aaw * length(xy_to_uv[0].xy);
for(int i = 0; i < 5; i++){
vec3 corner = corners[i];
uv_coords = (xyz_to_uv * vec4(corner, 1)).xy;
simp_coords = to_simple_space * uv_coords;
uv_coords = (xy_to_uv * vec3(corner.xy, 1.0)).xy;
int j = int(sign(i - 1) + 1); // Maps i = [0, 1, 2, 3, 4] onto j = [0, 0, 1, 2, 2]
emit_vertex_wrapper(corner, j);
}