mirror of
https://github.com/3b1b/manim.git
synced 2025-08-05 16:49:03 +00:00
Small cleanups to the shader files
This commit is contained in:
parent
00dcc14df1
commit
e882356264
5 changed files with 7 additions and 48 deletions
|
@ -1,6 +1,8 @@
|
|||
// This file is not a shader, it's just a set of
|
||||
// functions meant to be inserted into other shaders.
|
||||
|
||||
// Must be inserted in a context with a definition for modify_distance_for_endpoints
|
||||
|
||||
// All of this is with respect to a curve that's been rotated/scaled
|
||||
// so that b0 = (0, 0) and b1 = (1, 0). That is, b2 entirely
|
||||
// determines the shape of the curve
|
||||
|
@ -16,36 +18,6 @@ vec2 bezier(float t, vec2 b2){
|
|||
);
|
||||
}
|
||||
|
||||
void compute_C_and_grad_C(float a, float b, vec2 p, out float Cxy, out vec2 grad_Cxy){
|
||||
// Curve has the implicit form x = a*y + b*sqrt(y), which is also
|
||||
// 0 = -x^2 + 2axy + b^2 y - a^2 y^2.
|
||||
Cxy = -p.x*p.x + 2 * a * p.x*p.y + b*b * p.y - a*a * p.y*p.y;
|
||||
|
||||
// Approximate distance to curve using the gradient of -x^2 + 2axy + b^2 y - a^2 y^2
|
||||
grad_Cxy = vec2(
|
||||
-2 * p.x + 2 * a * p.y, // del C / del x
|
||||
2 * a * p.x + b*b - 2 * a*a * p.y // del C / del y
|
||||
);
|
||||
}
|
||||
|
||||
// This function is flawed.
|
||||
float cheap_dist_to_curve(vec2 p, vec2 b2){
|
||||
float a = (b2.x - 2.0) / b2.y;
|
||||
float b = sign(b2.y) * 2.0 / sqrt(abs(b2.y));
|
||||
float x = p.x;
|
||||
float y = p.y;
|
||||
|
||||
// Curve has the implicit form x = a*y + b*sqrt(y), which is also
|
||||
// 0 = -x^2 + 2axy + b^2 y - a^2 y^2.
|
||||
float Cxy = -x * x + 2 * a * x * y + sign(b2.y) * b * b * y - a * a * y * y;
|
||||
|
||||
// Approximate distance to curve using the gradient of -x^2 + 2axy + b^2 y - a^2 y^2
|
||||
vec2 grad_Cxy = 2 * vec2(
|
||||
-x + a * y, // del C / del x
|
||||
a * x + b * b / 2 - a * a * y // del C / del y
|
||||
);
|
||||
return abs(Cxy / length(grad_Cxy));
|
||||
}
|
||||
|
||||
float cube_root(float x){
|
||||
return sign(x) * pow(abs(x), 1.0 / 3.0);
|
||||
|
@ -114,10 +86,9 @@ float dist_to_point_on_curve(vec2 p, float t, vec2 b2){
|
|||
}
|
||||
|
||||
|
||||
float min_dist_to_curve(vec2 p, vec2 b2, float degree, bool quick_approx){
|
||||
float min_dist_to_curve(vec2 p, vec2 b2, float degree){
|
||||
// Check if curve is really a a line
|
||||
if(degree == 1) return dist_to_line(p, b2);
|
||||
if(quick_approx) return cheap_dist_to_curve(p, b2);
|
||||
|
||||
// Try finding the exact sdf by solving the equation
|
||||
// (d/dt) dist^2(t) = 0, which amount to the following
|
||||
|
|
|
@ -10,11 +10,6 @@ in float bezier_degree;
|
|||
|
||||
out vec4 frag_color;
|
||||
|
||||
const float FILL_INSIDE = 0;
|
||||
const float FILL_OUTSIDE = 1;
|
||||
const float FILL_ALL = 2;
|
||||
|
||||
|
||||
// Needed for quadratic_bezier_distance insertion below
|
||||
float modify_distance_for_endpoints(vec2 p, float dist, float t){
|
||||
return dist;
|
||||
|
@ -28,7 +23,7 @@ float modify_distance_for_endpoints(vec2 p, float dist, float t){
|
|||
float sdf(){
|
||||
// For really flat curves, just take the distance to the curve
|
||||
if(bezier_degree < 2 || abs(uv_b2.y / uv_b2.x) < uv_anti_alias_width){
|
||||
return min_dist_to_curve(uv_coords, uv_b2, bezier_degree, false);
|
||||
return min_dist_to_curve(uv_coords, uv_b2, bezier_degree);
|
||||
}
|
||||
// This converts uv_coords to a space where the bezier points sit on
|
||||
// (0, 0), (1/2, 0) and (1, 1), so that the curve can be expressed implicityly
|
||||
|
|
|
@ -21,16 +21,9 @@ out vec2 uv_b2;
|
|||
|
||||
out float bezier_degree;
|
||||
|
||||
const float FILL_INSIDE = 0;
|
||||
const float FILL_OUTSIDE = 1;
|
||||
const float FILL_ALL = 2;
|
||||
|
||||
const float SQRT5 = 2.236068;
|
||||
|
||||
|
||||
// To my knowledge, there is no notion of #include for shaders,
|
||||
// so to share functionality between this and others, the caller
|
||||
// replaces this line with the contents of named file
|
||||
// in manim replaces this line with the contents of named file
|
||||
#INSERT quadratic_bezier_geometry_functions.glsl
|
||||
#INSERT scale_and_shift_point_for_frame.glsl
|
||||
|
||||
|
|
|
@ -84,7 +84,7 @@ void main() {
|
|||
if (uv_stroke_width == 0) discard;
|
||||
|
||||
frag_color = color;
|
||||
float dist_to_curve = min_dist_to_curve(uv_coords, uv_b2, bezier_degree, false);
|
||||
float dist_to_curve = min_dist_to_curve(uv_coords, uv_b2, bezier_degree);
|
||||
// An sdf for the region around the curve we wish to color.
|
||||
float signed_dist = abs(dist_to_curve) - 0.5 * uv_stroke_width;
|
||||
frag_color.a *= smoothstep(0.5, -0.5, signed_dist / uv_anti_alias_width);
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
// Assumes theese uniforms exist in the surrounding context
|
||||
// Assumes the following uniforms exist in the surrounding context:
|
||||
// uniform float scale;
|
||||
// uniform float aspect_ratio;
|
||||
// uniform float frame_center;
|
||||
|
|
Loading…
Add table
Reference in a new issue