3b1b-manim/manimlib/shaders/inserts/get_gl_Position.glsl

31 lines
997 B
Text
Raw Normal View History

2020-06-01 16:21:18 -07:00
// Assumes the following uniforms exist in the surrounding context:
// uniform vec2 frame_shape;
2020-06-02 16:18:44 -07:00
// uniform float focal_distance;
// uniform float is_fixed_in_frame;
2020-06-09 17:12:52 -07:00
const vec2 DEFAULT_FRAME_SHAPE = vec2(8.0 * 16.0 / 9.0, 8.0);
2020-06-01 16:21:18 -07:00
float perspective_scale_factor(float z, float focal_distance){
2021-01-09 10:29:45 -08:00
return max(0.0, focal_distance / (focal_distance - z));
}
2020-06-01 16:21:18 -07:00
vec4 get_gl_Position(vec3 point){
2020-06-09 17:12:52 -07:00
vec4 result = vec4(point, 1.0);
if(!bool(is_fixed_in_frame)){
2020-06-09 17:12:52 -07:00
result.x *= 2.0 / frame_shape.x;
result.y *= 2.0 / frame_shape.y;
2021-01-09 10:29:45 -08:00
float psf = perspective_scale_factor(result.z, focal_distance);
if (psf > 0){
result.xy *= psf;
// TODO, what's the better way to do this?
// This is to keep vertices too far out of frame from getting cut.
result.z *= 0.01;
}
} else{
2020-06-09 17:12:52 -07:00
result.x *= 2.0 / DEFAULT_FRAME_SHAPE.x;
result.y *= 2.0 / DEFAULT_FRAME_SHAPE.y;
}
2020-06-09 17:12:52 -07:00
result.z *= -1;
2020-06-09 12:34:00 -07:00
return result;
2020-06-01 16:21:18 -07:00
}