mirror of
https://github.com/ManimCommunity/manim.git
synced 2026-06-22 10:01:47 +00:00
addded relative import support for shaders and first working square (fill is broken)
This commit is contained in:
parent
6151860418
commit
d169cd556f
35 changed files with 394 additions and 356 deletions
|
|
@ -77,6 +77,7 @@ class Camera:
|
|||
frame_rate: float | None = None,
|
||||
**kwargs,
|
||||
):
|
||||
logger.warning("Camera is deprecated. Please use OpenGLCamera instead.")
|
||||
self.background_image = background_image
|
||||
self.frame_center = frame_center
|
||||
self.image_mode = image_mode
|
||||
|
|
|
|||
|
|
@ -237,12 +237,10 @@ class OpenGLMobject:
|
|||
|
||||
Gets called upon creation"""
|
||||
self.uniforms = {
|
||||
"is_fixed_in_frame": np.array(
|
||||
float(self.is_fixed_in_frame), dtype=UNIFORM_DTYPE
|
||||
),
|
||||
"gloss": np.array(self.gloss, dtype=UNIFORM_DTYPE),
|
||||
"shadow": np.array(self.shadow, dtype=UNIFORM_DTYPE),
|
||||
"reflectiveness": np.array(self.reflectiveness, dtype=UNIFORM_DTYPE),
|
||||
"is_fixed_in_frame": float(self.is_fixed_in_frame),
|
||||
"gloss": float(self.gloss),
|
||||
"shadow": float(self.shadow),
|
||||
"reflectiveness": float(self.reflectiveness),
|
||||
}
|
||||
|
||||
def init_colors(self):
|
||||
|
|
@ -2311,7 +2309,7 @@ class OpenGLMobject:
|
|||
|
||||
def set_reflectiveness(self, reflectiveness: float, recurse: bool = True):
|
||||
for mob in self.get_family(recurse):
|
||||
mob.uniforms["reflectiveness"] = np.asarray(reflectiveness)
|
||||
mob.uniforms["reflectiveness"] = float(reflectiveness)
|
||||
return self
|
||||
|
||||
def get_shadow(self) -> np.ndarray:
|
||||
|
|
@ -2319,7 +2317,7 @@ class OpenGLMobject:
|
|||
|
||||
def set_shadow(self, shadow: float, recurse: bool = True):
|
||||
for mob in self.get_family(recurse):
|
||||
mob.uniforms["shadow"] = np.asarray(shadow)
|
||||
mob.uniforms["shadow"] = float(shadow)
|
||||
return self
|
||||
|
||||
def get_gloss(self) -> np.ndarray:
|
||||
|
|
@ -2327,7 +2325,7 @@ class OpenGLMobject:
|
|||
|
||||
def set_gloss(self, gloss: float, recurse: bool = True):
|
||||
for mob in self.get_family(recurse):
|
||||
mob.uniforms["gloss"] = np.asarray(gloss)
|
||||
mob.uniforms["gloss"] = float(gloss)
|
||||
return self
|
||||
|
||||
# Background rectangle
|
||||
|
|
@ -2891,20 +2889,20 @@ class OpenGLMobject:
|
|||
|
||||
@affects_shader_info_id
|
||||
def fix_in_frame(self) -> Self:
|
||||
self.uniforms["is_fixed_in_frame"] = np.asarray(1.0)
|
||||
self.uniforms["is_fixed_in_frame"] = float(1.0)
|
||||
self.is_fixed_in_frame = True
|
||||
return self
|
||||
|
||||
@affects_shader_info_id
|
||||
def fix_orientation(self) -> Self:
|
||||
self.uniforms["is_fixed_orientation"] = np.asarray(1.0)
|
||||
self.uniforms["is_fixed_orientation"] = float(1.0)
|
||||
self.is_fixed_orientation = True
|
||||
self.fixed_orientation_center = tuple(self.get_center())
|
||||
return self
|
||||
|
||||
@affects_shader_info_id
|
||||
def unfix_from_frame(self) -> Self:
|
||||
self.uniforms["is_fixed_in_frame"] = np.asarray(0.0)
|
||||
self.uniforms["is_fixed_in_frame"] = float(0.0)
|
||||
self.is_fixed_in_frame = False
|
||||
return self
|
||||
|
||||
|
|
|
|||
|
|
@ -92,7 +92,7 @@ class OpenGLVMobject(OpenGLMobject):
|
|||
background_image_file: str | None = None,
|
||||
long_lines: bool = False,
|
||||
joint_type: LineJointType = LineJointType.AUTO,
|
||||
flat_stroke: bool = True,
|
||||
flat_stroke: bool = False,
|
||||
# Measured in pixel widths
|
||||
anti_alias_width: float = 1.0,
|
||||
**kwargs,
|
||||
|
|
@ -147,15 +147,9 @@ class OpenGLVMobject(OpenGLMobject):
|
|||
|
||||
def init_uniforms(self):
|
||||
super().init_uniforms()
|
||||
self.uniforms["anti_alias_width"] = np.asarray(
|
||||
self.anti_alias_width, dtype=UNIFORM_DTYPE
|
||||
)
|
||||
self.uniforms["joint_type"] = np.asarray(
|
||||
self.joint_type.value, dtype=UNIFORM_DTYPE
|
||||
)
|
||||
self.uniforms["flat_stroke"] = np.asarray(
|
||||
float(self.flat_stroke), dtype=UNIFORM_DTYPE
|
||||
)
|
||||
self.uniforms["anti_alias_width"] = float(self.anti_alias_width)
|
||||
self.uniforms["joint_type"] = float(self.joint_type.value)
|
||||
self.uniforms["flat_stroke"] = float(self.flat_stroke)
|
||||
|
||||
# These are here just to make type checkers happy
|
||||
def get_family(self, recurse: bool = True) -> list[OpenGLVMobject]: # type: ignore
|
||||
|
|
@ -453,7 +447,7 @@ class OpenGLVMobject(OpenGLMobject):
|
|||
|
||||
def set_flat_stroke(self, flat_stroke: bool = True, recurse: bool = True):
|
||||
for mob in self.get_family(recurse):
|
||||
mob.uniforms["flat_stroke"] = np.asarray(float(flat_stroke))
|
||||
mob.uniforms["flat_stroke"] = float(flat_stroke)
|
||||
return self
|
||||
|
||||
def get_flat_stroke(self) -> bool:
|
||||
|
|
@ -461,11 +455,11 @@ class OpenGLVMobject(OpenGLMobject):
|
|||
|
||||
def set_joint_type(self, joint_type: LineJointType, recurse: bool = True):
|
||||
for mob in self.get_family(recurse):
|
||||
mob.uniforms["joint_type"] = np.asarray(joint_type.value)
|
||||
mob.uniforms["joint_type"] = float(joint_type.value)
|
||||
return self
|
||||
|
||||
def get_joint_type(self) -> LineJointType:
|
||||
return LineJointType(int(self.uniforms["joint_type"][0]))
|
||||
return LineJointType(int(self.uniforms["joint_type"]))
|
||||
|
||||
# Points
|
||||
def set_anchors_and_handles(self, anchors1, handles, anchors2):
|
||||
|
|
@ -1457,7 +1451,7 @@ class OpenGLVMobject(OpenGLMobject):
|
|||
if not self.needs_new_triangulation:
|
||||
return self.triangulation
|
||||
|
||||
points = self.get_points()
|
||||
points = self.points
|
||||
|
||||
if len(points) <= 1:
|
||||
self.triangulation = np.zeros(0, dtype="i4")
|
||||
|
|
|
|||
|
|
@ -493,7 +493,7 @@ class OpenGLCamera:
|
|||
value = tuple(value)
|
||||
shader[name].value = value
|
||||
else:
|
||||
logger.warning(f"Uniform {name} not found in shader {shader}")
|
||||
logger.debug(f"Uniform {name} not found in shader {shader}")
|
||||
|
||||
def refresh_perspective_uniforms(self) -> None:
|
||||
frame = self.frame
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ __all__ = [
|
|||
|
||||
|
||||
def get_shader_code_from_file(file_path: Path) -> str:
|
||||
# TODO: Is this code used ?
|
||||
if file_path in file_path_to_code_map:
|
||||
return file_path_to_code_map[file_path]
|
||||
source = file_path.read_text()
|
||||
|
|
|
|||
|
|
@ -29,10 +29,11 @@ def find_file(file_name: Path, directories: list[Path]) -> Path:
|
|||
return file_name
|
||||
possible_paths = (directory / file_name for directory in directories)
|
||||
for path in possible_paths:
|
||||
logger.debug(f"Searching for {file_name} in {path}")
|
||||
if path.exists():
|
||||
return path
|
||||
else:
|
||||
logger.debug(f"{path} does not exist.")
|
||||
logger.debug(f"shader_wrapper.py::find_file() : {path} does not exist.")
|
||||
raise OSError(f"{file_name} not Found")
|
||||
|
||||
|
||||
|
|
@ -136,9 +137,14 @@ class ShaderWrapper:
|
|||
|
||||
def init_program_code(self):
|
||||
def get_code(name: str) -> str | None:
|
||||
return get_shader_code_from_file(
|
||||
self.shader_folder / f"{name}.glsl",
|
||||
)
|
||||
path = self.shader_folder / f"{name}.glsl"
|
||||
logger.debug(f"Reading {name}.glsl shader code from {path.absolute()}")
|
||||
code = get_shader_code_from_file(path)
|
||||
if code is not None:
|
||||
logger.debug(
|
||||
f"=============================================\n{code}\n============================================="
|
||||
)
|
||||
return code
|
||||
|
||||
self.program_code = {
|
||||
"vertex_shader": get_code("vert"),
|
||||
|
|
@ -190,13 +196,13 @@ filename_to_code_map: dict = {}
|
|||
def get_shader_code_from_file(filename: Path) -> str | None:
|
||||
if filename in filename_to_code_map:
|
||||
return filename_to_code_map[filename]
|
||||
|
||||
try:
|
||||
filepath = find_file(
|
||||
filename,
|
||||
directories=[get_shader_dir(), Path("/")],
|
||||
)
|
||||
except OSError:
|
||||
logger.warning(f"Could not find shader file {filename}")
|
||||
return None
|
||||
|
||||
result = filepath.read_text()
|
||||
|
|
@ -206,17 +212,25 @@ def get_shader_code_from_file(filename: Path) -> str | None:
|
|||
# passing to ctx.program for compiling
|
||||
# Replace "#INSERT " lines with relevant code
|
||||
insertions = re.findall(
|
||||
r"^#include ../include/.*\.glsl$",
|
||||
r"^#include.*",
|
||||
result,
|
||||
flags=re.MULTILINE,
|
||||
)
|
||||
for line in insertions:
|
||||
include_path = line.strip().replace("#include", "")
|
||||
include_path = include_path.replace('"', "")
|
||||
path = (filepath.parent / Path(include_path.strip())).resolve()
|
||||
logger.debug(f"Trying to get code from: {path} to include in {filepath.name}")
|
||||
inserted_code = get_shader_code_from_file(
|
||||
Path() / "include" / line.replace("#include ../include/", ""),
|
||||
path,
|
||||
)
|
||||
if inserted_code is None:
|
||||
return None
|
||||
result = result.replace(line, inserted_code)
|
||||
|
||||
result = result.replace(
|
||||
line,
|
||||
f"// Start include of: {include_path}\n\n{inserted_code}\n\n// End include of: {include_path}",
|
||||
)
|
||||
filename_to_code_map[filename] = result
|
||||
return result
|
||||
|
||||
|
|
|
|||
|
|
@ -2,14 +2,15 @@
|
|||
|
||||
out vec4 frag_color;
|
||||
|
||||
void main() {
|
||||
vec2 st = gl_FragCoord.xy / vec2(854, 360);
|
||||
vec3 color = vec3(0.0);
|
||||
void main()
|
||||
{
|
||||
vec2 st = gl_FragCoord.xy / vec2(854, 360);
|
||||
vec3 color = vec3(0.0);
|
||||
|
||||
st *= 3.0;
|
||||
st = fract(st);
|
||||
st *= 3.0;
|
||||
st = fract(st);
|
||||
|
||||
color = vec3(st, 0.0);
|
||||
color = vec3(st, 0.0);
|
||||
|
||||
frag_color = vec4(color, 1.0);
|
||||
frag_color = vec4(color, 1.0);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,43 +3,45 @@
|
|||
uniform vec2 u_resolution;
|
||||
out vec4 frag_color;
|
||||
|
||||
|
||||
#define PI 3.14159265358979323846
|
||||
|
||||
vec2 rotate2D(vec2 _st, float _angle){
|
||||
vec2 rotate2D(vec2 _st, float _angle)
|
||||
{
|
||||
_st -= 0.5;
|
||||
_st = mat2(cos(_angle),-sin(_angle),
|
||||
sin(_angle),cos(_angle)) * _st;
|
||||
_st = mat2(cos(_angle), -sin(_angle), sin(_angle), cos(_angle)) * _st;
|
||||
_st += 0.5;
|
||||
return _st;
|
||||
}
|
||||
|
||||
vec2 tile(vec2 _st, float _zoom){
|
||||
vec2 tile(vec2 _st, float _zoom)
|
||||
{
|
||||
_st *= _zoom;
|
||||
return fract(_st);
|
||||
}
|
||||
|
||||
float box(vec2 _st, vec2 _size, float _smoothEdges){
|
||||
_size = vec2(0.5)-_size*0.5;
|
||||
vec2 aa = vec2(_smoothEdges*0.5);
|
||||
vec2 uv = smoothstep(_size,_size+aa,_st);
|
||||
uv *= smoothstep(_size,_size+aa,vec2(1.0)-_st);
|
||||
return uv.x*uv.y;
|
||||
float box(vec2 _st, vec2 _size, float _smoothEdges)
|
||||
{
|
||||
_size = vec2(0.5) - _size * 0.5;
|
||||
vec2 aa = vec2(_smoothEdges * 0.5);
|
||||
vec2 uv = smoothstep(_size, _size + aa, _st);
|
||||
uv *= smoothstep(_size, _size + aa, vec2(1.0) - _st);
|
||||
return uv.x * uv.y;
|
||||
}
|
||||
|
||||
void main(void){
|
||||
vec2 st = gl_FragCoord.xy/u_resolution.xy;
|
||||
void main(void)
|
||||
{
|
||||
vec2 st = gl_FragCoord.xy / u_resolution.xy;
|
||||
vec3 color = vec3(0.0);
|
||||
|
||||
// Divide the space in 4
|
||||
st = tile(st,4.);
|
||||
st = tile(st, 4.);
|
||||
|
||||
// Use a matrix to rotate the space 45 degrees
|
||||
st = rotate2D(st,PI*0.25);
|
||||
st = rotate2D(st, PI * 0.25);
|
||||
|
||||
// Draw a square
|
||||
color = vec3(box(st,vec2(0.7),0.01));
|
||||
color = vec3(box(st, vec2(0.7), 0.01));
|
||||
// color = vec3(st,0.0);
|
||||
|
||||
frag_color = vec4(color,1.0);
|
||||
frag_color = vec4(color, 1.0);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,56 +4,65 @@ uniform vec3 u_resolution;
|
|||
uniform float u_time;
|
||||
out vec4 frag_color;
|
||||
|
||||
vec3 palette(float d){
|
||||
return mix(vec3(0.2,0.7,0.9),vec3(1.,0.,1.),d);
|
||||
vec3 palette(float d)
|
||||
{
|
||||
return mix(vec3(0.2, 0.7, 0.9), vec3(1., 0., 1.), d);
|
||||
}
|
||||
|
||||
vec2 rotate(vec2 p,float a){
|
||||
float c = cos(a);
|
||||
vec2 rotate(vec2 p, float a)
|
||||
{
|
||||
float c = cos(a);
|
||||
float s = sin(a);
|
||||
return p*mat2(c,s,-s,c);
|
||||
return p * mat2(c, s, -s, c);
|
||||
}
|
||||
|
||||
float map(vec3 p){
|
||||
for( int i = 0; i<8; ++i){
|
||||
float t = u_time*0.1;
|
||||
p.xz =rotate(p.xz,t);
|
||||
p.xy =rotate(p.xy,t*1.89);
|
||||
float map(vec3 p)
|
||||
{
|
||||
for (int i = 0; i < 8; ++i)
|
||||
{
|
||||
float t = u_time * 0.1;
|
||||
p.xz = rotate(p.xz, t);
|
||||
p.xy = rotate(p.xy, t * 1.89);
|
||||
p.xz = abs(p.xz);
|
||||
p.xz-=.5;
|
||||
}
|
||||
return dot(sign(p),p)/5.;
|
||||
p.xz -= .5;
|
||||
}
|
||||
return dot(sign(p), p) / 5.;
|
||||
}
|
||||
|
||||
vec4 rm (vec3 ro, vec3 rd){
|
||||
vec4 rm(vec3 ro, vec3 rd)
|
||||
{
|
||||
float t = 0.;
|
||||
vec3 col = vec3(0.);
|
||||
float d;
|
||||
for(float i =0.; i<64.; i++){
|
||||
vec3 p = ro + rd*t;
|
||||
d = map(p)*.5;
|
||||
if(d<0.02){
|
||||
for (float i = 0.; i < 64.; i++)
|
||||
{
|
||||
vec3 p = ro + rd * t;
|
||||
d = map(p) * .5;
|
||||
if (d < 0.02)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if(d>100.){
|
||||
break;
|
||||
if (d > 100.)
|
||||
{
|
||||
break;
|
||||
}
|
||||
//col+=vec3(0.6,0.8,0.8)/(400.*(d));
|
||||
col+=palette(length(p)*.1)/(400.*(d));
|
||||
t+=d;
|
||||
// col+=vec3(0.6,0.8,0.8)/(400.*(d));
|
||||
col += palette(length(p) * .1) / (400. * (d));
|
||||
t += d;
|
||||
}
|
||||
return vec4(col,1./(d*100.));
|
||||
return vec4(col, 1. / (d * 100.));
|
||||
}
|
||||
|
||||
void main(void){
|
||||
vec2 uv = (gl_FragCoord.xy-(u_resolution.xy/2.))/u_resolution.x;
|
||||
vec3 ro = vec3(0.,0.,-50.);
|
||||
ro.xz = rotate(ro.xz,u_time);
|
||||
void main(void)
|
||||
{
|
||||
vec2 uv = (gl_FragCoord.xy - (u_resolution.xy / 2.)) / u_resolution.x;
|
||||
vec3 ro = vec3(0., 0., -50.);
|
||||
ro.xz = rotate(ro.xz, u_time);
|
||||
vec3 cf = normalize(-ro);
|
||||
vec3 cs = normalize(cross(cf,vec3(0.,1.,0.)));
|
||||
vec3 cu = normalize(cross(cf,cs));
|
||||
vec3 uuv = ro+cf*3. + uv.x*cs + uv.y*cu;
|
||||
vec3 rd = normalize(uuv-ro);
|
||||
vec4 col = rm(ro,rd);
|
||||
vec3 cs = normalize(cross(cf, vec3(0., 1., 0.)));
|
||||
vec3 cu = normalize(cross(cf, cs));
|
||||
vec3 uuv = ro + cf * 3. + uv.x * cs + uv.y * cu;
|
||||
vec3 rd = normalize(uuv - ro);
|
||||
vec4 col = rm(ro, rd);
|
||||
frag_color = vec4(col.xyz, 1);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,19 +1,17 @@
|
|||
///// INSERT COLOR_MAP FUNCTION HERE /////
|
||||
|
||||
vec4 add_light(vec4 color,
|
||||
vec3 point,
|
||||
vec3 unit_normal,
|
||||
vec3 light_coords,
|
||||
float gloss,
|
||||
float shadow){
|
||||
vec4 add_light(vec4 color, vec3 point, vec3 unit_normal, vec3 light_coords, float gloss, float shadow)
|
||||
{
|
||||
///// INSERT COLOR FUNCTION HERE /////
|
||||
// The line above may be replaced by arbitrary code snippets, as per
|
||||
// the method Mobject.set_color_by_code
|
||||
if(gloss == 0.0 && shadow == 0.0) return color;
|
||||
if (gloss == 0.0 && shadow == 0.0)
|
||||
return color;
|
||||
|
||||
// TODO, do we actually want this? It effectively treats surfaces as two-sided
|
||||
if(unit_normal.z < 0){
|
||||
unit_normal *= -1;
|
||||
if (unit_normal.z < 0)
|
||||
{
|
||||
unit_normal *= -1;
|
||||
}
|
||||
|
||||
// TODO, read this in as a uniform?
|
||||
|
|
@ -26,18 +24,11 @@ vec4 add_light(vec4 color,
|
|||
float shine = gloss * exp(-3 * pow(1 - dot_prod, 2));
|
||||
float dp2 = dot(normalize(to_light), unit_normal);
|
||||
float darkening = mix(1, max(dp2, 0), shadow);
|
||||
return vec4(
|
||||
darkening * mix(color.rgb, vec3(1.0), shine),
|
||||
color.a
|
||||
);
|
||||
return vec4(darkening * mix(color.rgb, vec3(1.0), shine), color.a);
|
||||
}
|
||||
|
||||
vec4 finalize_color(vec4 color,
|
||||
vec3 point,
|
||||
vec3 unit_normal,
|
||||
vec3 light_coords,
|
||||
float gloss,
|
||||
float shadow){
|
||||
vec4 finalize_color(vec4 color, vec3 point, vec3 unit_normal, vec3 light_coords, float gloss, float shadow)
|
||||
{
|
||||
// Put insertion here instead
|
||||
return add_light(color, point, unit_normal, light_coords, gloss, shadow);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,16 +3,19 @@
|
|||
// uniform vec3 camera_offset;
|
||||
// uniform mat3 camera_rotation;
|
||||
|
||||
vec3 rotate_point_into_frame(vec3 point){
|
||||
if(bool(is_fixed_in_frame)){
|
||||
vec3 rotate_point_into_frame(vec3 point)
|
||||
{
|
||||
if (bool(is_fixed_in_frame))
|
||||
{
|
||||
return point;
|
||||
}
|
||||
return camera_rotation * point;
|
||||
}
|
||||
|
||||
|
||||
vec3 position_point_into_frame(vec3 point){
|
||||
if(bool(is_fixed_in_frame)){
|
||||
vec3 position_point_into_frame(vec3 point)
|
||||
{
|
||||
if (bool(is_fixed_in_frame))
|
||||
{
|
||||
return point;
|
||||
}
|
||||
return rotate_point_into_frame(point - camera_offset);
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
uniform vec4 u_color;
|
||||
out vec4 frag_color;
|
||||
|
||||
void main() {
|
||||
frag_color = u_color;
|
||||
void main()
|
||||
{
|
||||
frag_color = u_color;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,8 @@ in vec4 in_vert;
|
|||
uniform mat4 u_model_view_matrix;
|
||||
uniform mat4 u_projection_matrix;
|
||||
|
||||
void main() {
|
||||
void main()
|
||||
{
|
||||
vec4 camera_space_vertex = u_model_view_matrix * in_vert;
|
||||
vec4 clip_space_vertex = u_projection_matrix * camera_space_vertex;
|
||||
gl_Position = clip_space_vertex;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
#version 330
|
||||
|
||||
#include ../ include / camera_uniform_declarations.glsl
|
||||
#include "../include/camera_uniform_declarations.glsl"
|
||||
|
||||
in vec4 color;
|
||||
in float fill_all; // Either 0 or 1
|
||||
|
|
@ -20,7 +20,7 @@ float modify_distance_for_endpoints(vec2 p, float dist, float t)
|
|||
return dist;
|
||||
}
|
||||
|
||||
#include ../ include / quadratic_bezier_distance.glsl
|
||||
#include "../include/quadratic_bezier_distance.glsl"
|
||||
|
||||
float sdf()
|
||||
{
|
||||
|
|
@ -40,7 +40,8 @@ float sdf()
|
|||
// as y = x^2.
|
||||
mat2 to_simple_space = mat2(v2, 0, 2 - u2, 4 * v2);
|
||||
vec2 p = to_simple_space * uv_coords;
|
||||
// Sign takes care of whether we should be filling the inside or outside of curve.
|
||||
// Sign takes care of whether we should be filling the inside or outside of
|
||||
// curve.
|
||||
float sgn = orientation * sign(v2);
|
||||
float Fp = (p.x * p.x - p.y);
|
||||
if (sgn * Fp <= 0)
|
||||
|
|
|
|||
|
|
@ -36,10 +36,14 @@ out float bezier_degree;
|
|||
vec3 unit_normal;
|
||||
|
||||
// Analog of import for manim only
|
||||
#include ../include/quadratic_bezier_geometry_functions.glsl
|
||||
#include ../include/get_gl_Position.glsl
|
||||
#include ../include/get_unit_normal.glsl
|
||||
#include ../include/finalize_color.glsl
|
||||
// Comment to prevent formatting
|
||||
#include "../include/quadratic_bezier_geometry_functions.glsl"
|
||||
//
|
||||
#include "../include/get_gl_Position.glsl"
|
||||
//
|
||||
#include "../include/get_unit_normal.glsl"
|
||||
//
|
||||
#include "../include/finalize_color.glsl"
|
||||
|
||||
void emit_vertex_wrapper(vec3 point, int index)
|
||||
{
|
||||
|
|
@ -67,7 +71,8 @@ void emit_pentagon(vec3[3] points, vec3 normal)
|
|||
// Tangent vectors
|
||||
vec3 t01 = normalize(p1 - p0);
|
||||
vec3 t12 = normalize(p2 - p1);
|
||||
// Vectors perpendicular to the curve in the plane of the curve pointing outside the curve
|
||||
// Vectors perpendicular to the curve in the plane of the curve pointing
|
||||
// outside the curve
|
||||
vec3 p0_perp = cross(t01, normal);
|
||||
vec3 p2_perp = cross(t12, normal);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
#version 330
|
||||
|
||||
#include ../include/camera_uniform_declarations.glsl
|
||||
#include "../include/camera_uniform_declarations.glsl"
|
||||
|
||||
in vec3 point;
|
||||
in float orientation;
|
||||
|
|
@ -10,15 +10,15 @@ in float vert_index;
|
|||
out vec3 bp; // Bezier control point
|
||||
out float v_orientation;
|
||||
out vec4 v_color;
|
||||
out float v_vert_index
|
||||
out float v_vert_index;
|
||||
|
||||
// Analog of import for manim only
|
||||
#include ../include/position_point_into_frame.glsl
|
||||
#include "../include/position_point_into_frame.glsl"
|
||||
|
||||
void main()
|
||||
{
|
||||
bp = position_point_into_frame(point);
|
||||
v_orientation = orientation;
|
||||
v_color = color;
|
||||
v_vert_index = vert_index
|
||||
}
|
||||
v_vert_index = vert_index;
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
#version 330
|
||||
|
||||
#include ../include/camera_uniform_declarations.glsl
|
||||
#include "../include/camera_uniform_declarations.glsl"
|
||||
|
||||
in vec2 uv_coords;
|
||||
in vec2 uv_b2;
|
||||
|
|
@ -20,25 +20,25 @@ in float bezier_degree;
|
|||
|
||||
out vec4 frag_color;
|
||||
|
||||
|
||||
float cross2d(vec2 v, vec2 w){
|
||||
float cross2d(vec2 v, vec2 w)
|
||||
{
|
||||
return v.x * w.y - w.x * v.y;
|
||||
}
|
||||
|
||||
|
||||
float modify_distance_for_endpoints(vec2 p, float dist, float t){
|
||||
float modify_distance_for_endpoints(vec2 p, float dist, float t)
|
||||
{
|
||||
float buff = 0.5 * uv_stroke_width - uv_anti_alias_width;
|
||||
// Check the beginning of the curve
|
||||
if(t == 0){
|
||||
if (t == 0)
|
||||
{
|
||||
// Clip the start
|
||||
if(has_prev == 0) return max(dist, -p.x + buff);
|
||||
if (has_prev == 0)
|
||||
return max(dist, -p.x + buff);
|
||||
// Bevel start
|
||||
if(bevel_start == 1){
|
||||
if (bevel_start == 1)
|
||||
{
|
||||
float a = angle_from_prev;
|
||||
mat2 rot = mat2(
|
||||
cos(a), sin(a),
|
||||
-sin(a), cos(a)
|
||||
);
|
||||
mat2 rot = mat2(cos(a), sin(a), -sin(a), cos(a));
|
||||
// Dist for intersection of two lines
|
||||
float bevel_d = max(abs(p.y), abs((rot * p).y));
|
||||
// Dist for union of this intersection with the real curve
|
||||
|
|
@ -47,30 +47,29 @@ float modify_distance_for_endpoints(vec2 p, float dist, float t){
|
|||
return max(min(dist, bevel_d), dist / 2);
|
||||
}
|
||||
// Otherwise, start will be rounded off
|
||||
}else if(t == 1){
|
||||
}
|
||||
else if (t == 1)
|
||||
{
|
||||
// Check the end of the curve
|
||||
// TODO, too much code repetition
|
||||
vec2 v21 = (bezier_degree == 2) ? vec2(1, 0) - uv_b2 : vec2(-1, 0);
|
||||
float len_v21 = length(v21);
|
||||
if(len_v21 == 0){
|
||||
if (len_v21 == 0)
|
||||
{
|
||||
v21 = -uv_b2;
|
||||
len_v21 = length(v21);
|
||||
}
|
||||
|
||||
float perp_dist = dot(p - uv_b2, v21) / len_v21;
|
||||
if(has_next == 0) return max(dist, -perp_dist + buff);
|
||||
if (has_next == 0)
|
||||
return max(dist, -perp_dist + buff);
|
||||
// Bevel end
|
||||
if(bevel_end == 1){
|
||||
if (bevel_end == 1)
|
||||
{
|
||||
float a = -angle_to_next;
|
||||
mat2 rot = mat2(
|
||||
cos(a), sin(a),
|
||||
-sin(a), cos(a)
|
||||
);
|
||||
mat2 rot = mat2(cos(a), sin(a), -sin(a), cos(a));
|
||||
vec2 v21_unit = v21 / length(v21);
|
||||
float bevel_d = max(
|
||||
abs(cross2d(p - uv_b2, v21_unit)),
|
||||
abs(cross2d((rot * (p - uv_b2)), v21_unit))
|
||||
);
|
||||
float bevel_d = max(abs(cross2d(p - uv_b2, v21_unit)), abs(cross2d((rot * (p - uv_b2)), v21_unit)));
|
||||
return max(min(dist, bevel_d), dist / 2);
|
||||
}
|
||||
// Otherwise, end will be rounded off
|
||||
|
|
@ -78,12 +77,12 @@ float modify_distance_for_endpoints(vec2 p, float dist, float t){
|
|||
return dist;
|
||||
}
|
||||
|
||||
#include "../include/quadratic_bezier_distance.glsl"
|
||||
|
||||
#include ../include/quadratic_bezier_distance.glsl
|
||||
|
||||
|
||||
void main() {
|
||||
if (uv_stroke_width == 0) discard;
|
||||
void main()
|
||||
{
|
||||
if (uv_stroke_width == 0)
|
||||
discard;
|
||||
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;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#version 330
|
||||
|
||||
layout (triangles) in;
|
||||
layout (triangle_strip, max_vertices = 5) out;
|
||||
layout(triangles) in;
|
||||
layout(triangle_strip, max_vertices = 5) out;
|
||||
|
||||
// Needed for get_gl_Position
|
||||
uniform vec2 frame_shape;
|
||||
|
|
@ -12,7 +12,7 @@ uniform float is_fixed_in_frame;
|
|||
uniform float anti_alias_width;
|
||||
uniform float flat_stroke;
|
||||
|
||||
//Needed for lighting
|
||||
// Needed for lighting
|
||||
uniform vec3 light_source_position;
|
||||
uniform vec3 camera_position;
|
||||
uniform float joint_type;
|
||||
|
|
@ -52,54 +52,61 @@ const float BEVEL_JOINT = 2;
|
|||
const float MITER_JOINT = 3;
|
||||
const float PI = 3.141592653;
|
||||
|
||||
#include ../include/quadratic_bezier_geometry_functions.glsl
|
||||
#include ../include/get_gl_Position.glsl
|
||||
#include ../include/get_unit_normal.glsl
|
||||
#include ../include/finalize_color.glsl
|
||||
#include "../include/finalize_color.glsl"
|
||||
#include "../include/get_gl_Position.glsl"
|
||||
#include "../include/get_unit_normal.glsl"
|
||||
#include "../include/quadratic_bezier_geometry_functions.glsl"
|
||||
|
||||
|
||||
void flatten_points(in vec3[3] points, out vec2[3] flat_points){
|
||||
for(int i = 0; i < 3; i++){
|
||||
void flatten_points(in vec3[3] points, out vec2[3] flat_points)
|
||||
{
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
float sf = perspective_scale_factor(points[i].z, focal_distance);
|
||||
flat_points[i] = sf * points[i].xy;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
float angle_between_vectors(vec2 v1, vec2 v2){
|
||||
float angle_between_vectors(vec2 v1, vec2 v2)
|
||||
{
|
||||
float v1_norm = length(v1);
|
||||
float v2_norm = length(v2);
|
||||
if(v1_norm == 0 || v2_norm == 0) return 0.0;
|
||||
if (v1_norm == 0 || v2_norm == 0)
|
||||
return 0.0;
|
||||
float dp = dot(v1, v2) / (v1_norm * v2_norm);
|
||||
float angle = acos(clamp(dp, -1.0, 1.0));
|
||||
float sn = sign(cross2d(v1, v2));
|
||||
return sn * angle;
|
||||
}
|
||||
|
||||
|
||||
bool find_intersection(vec2 p0, vec2 v0, vec2 p1, vec2 v1, out vec2 intersection){
|
||||
bool find_intersection(vec2 p0, vec2 v0, vec2 p1, vec2 v1, out vec2 intersection)
|
||||
{
|
||||
// Find the intersection of a line passing through
|
||||
// p0 in the direction v0 and one passing through p1 in
|
||||
// the direction p1.
|
||||
// That is, find a solutoin to p0 + v0 * t = p1 + v1 * s
|
||||
float det = -v0.x * v1.y + v1.x * v0.y;
|
||||
if(det == 0) return false;
|
||||
if (det == 0)
|
||||
return false;
|
||||
float t = cross2d(p0 - p1, v1) / det;
|
||||
intersection = p0 + v0 * t;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void create_joint(float angle, vec2 unit_tan, float buff,
|
||||
vec2 static_c0, out vec2 changing_c0,
|
||||
vec2 static_c1, out vec2 changing_c1){
|
||||
void create_joint(float angle, vec2 unit_tan, float buff, vec2 static_c0, out vec2 changing_c0, vec2 static_c1,
|
||||
out vec2 changing_c1)
|
||||
{
|
||||
float shift;
|
||||
if(abs(angle) < 1e-3){
|
||||
if (abs(angle) < 1e-3)
|
||||
{
|
||||
// No joint
|
||||
shift = 0;
|
||||
}else if(joint_type == MITER_JOINT){
|
||||
}
|
||||
else if (joint_type == MITER_JOINT)
|
||||
{
|
||||
shift = buff * (-1.0 - cos(angle)) / sin(angle);
|
||||
}else{
|
||||
}
|
||||
else
|
||||
{
|
||||
// For a Bevel joint
|
||||
shift = buff * (1.0 - cos(angle)) / sin(angle);
|
||||
}
|
||||
|
|
@ -107,11 +114,11 @@ void create_joint(float angle, vec2 unit_tan, float buff,
|
|||
changing_c1 = static_c1 + shift * unit_tan;
|
||||
}
|
||||
|
||||
|
||||
// This function is responsible for finding the corners of
|
||||
// a bounding region around the bezier curve, which can be
|
||||
// emitted as a triangle fan
|
||||
int get_corners(vec2 controls[3], int degree, float stroke_widths[3], out vec2 corners[5]){
|
||||
int get_corners(vec2 controls[3], int degree, float stroke_widths[3], out vec2 corners[5])
|
||||
{
|
||||
vec2 p0 = controls[0];
|
||||
vec2 p1 = controls[1];
|
||||
vec2 p2 = controls[2];
|
||||
|
|
@ -122,8 +129,8 @@ int get_corners(vec2 controls[3], int degree, float stroke_widths[3], out vec2 c
|
|||
vec2 v01 = -v10;
|
||||
vec2 v21 = -v12;
|
||||
|
||||
vec2 p0_perp = vec2(-v01.y, v01.x); // Pointing to the left of the curve from p0
|
||||
vec2 p2_perp = vec2(-v12.y, v12.x); // Pointing to the left of the curve from p2
|
||||
vec2 p0_perp = vec2(-v01.y, v01.x); // Pointing to the left of the curve from p0
|
||||
vec2 p2_perp = vec2(-v12.y, v12.x); // Pointing to the left of the curve from p2
|
||||
|
||||
// aaw is the added width given around the polygon for antialiasing.
|
||||
// In case the normal is faced away from (0, 0, 1), the vector to the
|
||||
|
|
@ -140,86 +147,82 @@ int get_corners(vec2 controls[3], int degree, float stroke_widths[3], out vec2 c
|
|||
vec2 c3 = p2 - buff2 * p2_perp + aaw2 * v12;
|
||||
|
||||
// Account for previous and next control points
|
||||
if(has_prev > 0) create_joint(angle_from_prev, v01, buff0, c0, c0, c1, c1);
|
||||
if(has_next > 0) create_joint(angle_to_next, v21, buff2, c3, c3, c2, c2);
|
||||
if (has_prev > 0)
|
||||
create_joint(angle_from_prev, v01, buff0, c0, c0, c1, c1);
|
||||
if (has_next > 0)
|
||||
create_joint(angle_to_next, v21, buff2, c3, c3, c2, c2);
|
||||
|
||||
// Linear case is the simplest
|
||||
if(degree == 1){
|
||||
// The order of corners should be for a triangle_strip. Last entry is a dummy
|
||||
if (degree == 1)
|
||||
{
|
||||
// The order of corners should be for a triangle_strip. Last entry is a
|
||||
// dummy
|
||||
corners = vec2[5](c0, c1, c3, c2, vec2(0.0));
|
||||
return 4;
|
||||
}
|
||||
// Otherwise, form a pentagon around the curve
|
||||
float orientation = sign(cross2d(v01, v12)); // Positive for ccw curves
|
||||
if(orientation > 0) corners = vec2[5](c0, c1, p1, c2, c3);
|
||||
else corners = vec2[5](c1, c0, p1, c3, c2);
|
||||
float orientation = sign(cross2d(v01, v12)); // Positive for ccw curves
|
||||
if (orientation > 0)
|
||||
corners = vec2[5](c0, c1, p1, c2, c3);
|
||||
else
|
||||
corners = vec2[5](c1, c0, p1, c3, c2);
|
||||
// Replace corner[2] with convex hull point accounting for stroke width
|
||||
find_intersection(corners[0], v01, corners[4], v21, corners[2]);
|
||||
return 5;
|
||||
}
|
||||
|
||||
|
||||
void set_adjascent_info(vec2 c0, vec2 tangent,
|
||||
int degree,
|
||||
vec2 adj[3],
|
||||
out float bevel,
|
||||
out float angle
|
||||
){
|
||||
void set_adjascent_info(vec2 c0, vec2 tangent, int degree, vec2 adj[3], out float bevel, out float angle)
|
||||
{
|
||||
bool linear_adj = (angle_between_vectors(adj[1] - adj[0], adj[2] - adj[1]) < 1e-3);
|
||||
angle = angle_between_vectors(c0 - adj[1], tangent);
|
||||
// Decide on joint type
|
||||
bool one_linear = (degree == 1 || linear_adj);
|
||||
bool should_bevel = (
|
||||
(joint_type == AUTO_JOINT && one_linear) ||
|
||||
joint_type == BEVEL_JOINT
|
||||
);
|
||||
bool should_bevel = ((joint_type == AUTO_JOINT && one_linear) || joint_type == BEVEL_JOINT);
|
||||
bevel = should_bevel ? 1.0 : 0.0;
|
||||
}
|
||||
|
||||
|
||||
void find_joint_info(vec2 controls[3], vec2 prev[3], vec2 next[3], int degree){
|
||||
void find_joint_info(vec2 controls[3], vec2 prev[3], vec2 next[3], int degree)
|
||||
{
|
||||
float tol = 1e-6;
|
||||
|
||||
// Made as floats not bools so they can be passed to the frag shader
|
||||
has_prev = float(distance(prev[2], controls[0]) < tol);
|
||||
has_next = float(distance(next[0], controls[2]) < tol);
|
||||
|
||||
if(bool(has_prev)){
|
||||
if (bool(has_prev))
|
||||
{
|
||||
vec2 tangent = controls[1] - controls[0];
|
||||
set_adjascent_info(
|
||||
controls[0], tangent, degree, prev,
|
||||
bevel_start, angle_from_prev
|
||||
);
|
||||
set_adjascent_info(controls[0], tangent, degree, prev, bevel_start, angle_from_prev);
|
||||
}
|
||||
if(bool(has_next)){
|
||||
if (bool(has_next))
|
||||
{
|
||||
vec2 tangent = controls[1] - controls[2];
|
||||
set_adjascent_info(
|
||||
controls[2], tangent, degree, next,
|
||||
bevel_end, angle_to_next
|
||||
);
|
||||
set_adjascent_info(controls[2], tangent, degree, next, bevel_end, angle_to_next);
|
||||
angle_to_next *= -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void main() {
|
||||
void main()
|
||||
{
|
||||
// Convert control points to a standard form if they are linear or null
|
||||
vec3 controls[3];
|
||||
vec3 prev[3];
|
||||
vec3 next[3];
|
||||
unit_normal = get_unit_normal(controls);
|
||||
bezier_degree = get_reduced_control_points(vec3[3](bp[0], bp[1], bp[2]), controls);
|
||||
if(bezier_degree == 0.0) return; // Null curve
|
||||
if (bezier_degree == 0.0)
|
||||
return; // Null curve
|
||||
int degree = int(bezier_degree);
|
||||
get_reduced_control_points(vec3[3](prev_bp[0], prev_bp[1], prev_bp[2]), prev);
|
||||
get_reduced_control_points(vec3[3](next_bp[0], next_bp[1], next_bp[2]), next);
|
||||
|
||||
|
||||
// Adjust stroke width based on distance from the camera
|
||||
float scaled_strokes[3];
|
||||
for(int i = 0; i < 3; i++){
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
float sf = perspective_scale_factor(controls[i].z, focal_distance);
|
||||
if(bool(flat_stroke)){
|
||||
if (bool(flat_stroke))
|
||||
{
|
||||
vec3 to_cam = normalize(vec3(0.0, 0.0, focal_distance) - controls[i]);
|
||||
sf *= abs(dot(unit_normal, to_cam));
|
||||
}
|
||||
|
|
@ -227,8 +230,9 @@ void main() {
|
|||
}
|
||||
|
||||
// Control points are projected to the xy plane before drawing, which in turn
|
||||
// gets tranlated to a uv plane. The z-coordinate information will be remembered
|
||||
// by what's sent out to gl_Position, and by how it affects the lighting and stroke width
|
||||
// gets tranlated to a uv plane. The z-coordinate information will be
|
||||
// remembered by what's sent out to gl_Position, and by how it affects the
|
||||
// lighting and stroke width
|
||||
vec2 flat_controls[3];
|
||||
vec2 flat_prev[3];
|
||||
vec2 flat_next[3];
|
||||
|
|
@ -243,7 +247,8 @@ void main() {
|
|||
int n_corners = get_corners(flat_controls, degree, scaled_strokes, corners);
|
||||
|
||||
int index_map[5] = int[5](0, 0, 1, 2, 2);
|
||||
if(n_corners == 4) index_map[2] = 2;
|
||||
if (n_corners == 4)
|
||||
index_map[2] = 2;
|
||||
|
||||
// Find uv conversion matrix
|
||||
mat3 xy_to_uv = get_xy_to_uv(flat_controls[0], flat_controls[1]);
|
||||
|
|
@ -252,26 +257,16 @@ void main() {
|
|||
uv_b2 = (xy_to_uv * vec3(flat_controls[2], 1.0)).xy;
|
||||
|
||||
// Emit each corner
|
||||
for(int i = 0; i < n_corners; i++){
|
||||
for (int i = 0; i < n_corners; i++)
|
||||
{
|
||||
uv_coords = (xy_to_uv * vec3(corners[i], 1.0)).xy;
|
||||
uv_stroke_width = scaled_strokes[index_map[i]] / scale_factor;
|
||||
// Apply some lighting to the color before sending out.
|
||||
// vec3 xyz_coords = vec3(corners[i], controls[index_map[i]].z);
|
||||
vec3 xyz_coords = vec3(corners[i], controls[index_map[i]].z);
|
||||
color = finalize_color(
|
||||
v_color[index_map[i]],
|
||||
xyz_coords,
|
||||
unit_normal,
|
||||
light_source_position,
|
||||
camera_position,
|
||||
reflectiveness,
|
||||
gloss,
|
||||
shadow
|
||||
);
|
||||
gl_Position = vec4(
|
||||
get_gl_Position(vec3(corners[i], 0.0)).xy,
|
||||
get_gl_Position(controls[index_map[i]]).zw
|
||||
);
|
||||
color = finalize_color(v_color[index_map[i]], xyz_coords, unit_normal, light_source_position, camera_position,
|
||||
reflectiveness, gloss, shadow);
|
||||
gl_Position = vec4(get_gl_Position(vec3(corners[i], 0.0)).xy, get_gl_Position(controls[index_map[i]]).zw);
|
||||
EmitVertex();
|
||||
}
|
||||
EndPrimitive();
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
#version 330
|
||||
|
||||
#include ../include/camera_uniform_declarations.glsl
|
||||
#include "../include/camera_uniform_declarations.glsl"
|
||||
|
||||
in vec3 point;
|
||||
in vec3 prev_point;
|
||||
|
|
@ -19,7 +19,7 @@ out vec4 v_color;
|
|||
|
||||
const float STROKE_WIDTH_CONVERSION = 0.01;
|
||||
|
||||
#include ../include/position_point_into_frame.glsl
|
||||
#include "../include/position_point_into_frame.glsl"
|
||||
|
||||
void main()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,13 +1,14 @@
|
|||
#version 330
|
||||
|
||||
#include ../include/camera_uniform_declarations.glsl
|
||||
#include "./include/camera_uniform_declarations.glsl"
|
||||
|
||||
in vec3 point;
|
||||
|
||||
// Analog of import for manim only
|
||||
#include ../include/get_gl_Position.glsl
|
||||
#include ../include/position_point_into_frame.glsl
|
||||
#include "./include/get_gl_Position.glsl"
|
||||
#include "./include/position_point_into_frame.glsl"
|
||||
|
||||
void main(){
|
||||
void main()
|
||||
{
|
||||
gl_Position = get_gl_Position(position_point_into_frame(point));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,15 +10,9 @@ in vec4 v_color;
|
|||
|
||||
out vec4 frag_color;
|
||||
|
||||
#include ../include/finalize_color.glsl
|
||||
#include "../include/finalize_color.glsl"
|
||||
|
||||
void main() {
|
||||
frag_color = finalize_color(
|
||||
v_color,
|
||||
xyz_coords,
|
||||
normalize(v_normal),
|
||||
light_source_position,
|
||||
gloss,
|
||||
shadow
|
||||
);
|
||||
void main()
|
||||
{
|
||||
frag_color = finalize_color(v_color, xyz_coords, normalize(v_normal), light_source_position, gloss, shadow);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
#version 330
|
||||
|
||||
#include ../include/camera_uniform_declarations.glsl
|
||||
#include "../include/camera_uniform_declarations.glsl"
|
||||
|
||||
in vec3 point;
|
||||
in vec3 du_point;
|
||||
|
|
@ -11,11 +11,12 @@ out vec3 xyz_coords;
|
|||
out vec3 v_normal;
|
||||
out vec4 v_color;
|
||||
|
||||
#include ../include/position_point_into_frame.glsl
|
||||
#include ../include/get_gl_Position.glsl
|
||||
#include ../include/get_rotated_surface_unit_normal_vector.glsl
|
||||
#include "../include/get_gl_Position.glsl"
|
||||
#include "../include/get_rotated_surface_unit_normal_vector.glsl"
|
||||
#include "../include/position_point_into_frame.glsl"
|
||||
|
||||
void main(){
|
||||
void main()
|
||||
{
|
||||
xyz_coords = position_point_into_frame(point);
|
||||
v_normal = get_rotated_surface_unit_normal_vector(point, du_point, dv_point);
|
||||
v_color = color;
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ in vec4 v_color;
|
|||
|
||||
out vec4 frag_color;
|
||||
|
||||
void main() {
|
||||
void main()
|
||||
{
|
||||
frag_color = v_color;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,8 @@ in vec4 in_color;
|
|||
|
||||
out vec4 v_color;
|
||||
|
||||
void main() {
|
||||
void main()
|
||||
{
|
||||
v_color = in_color;
|
||||
gl_Position = vec4(in_vert, 0.0, 1.0);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,29 +14,21 @@ in float v_opacity;
|
|||
|
||||
out vec4 frag_color;
|
||||
|
||||
#include ../include/finalize_color.glsl
|
||||
#include "../include/finalize_color.glsl"
|
||||
|
||||
const float dark_shift = 0.2;
|
||||
|
||||
void main() {
|
||||
void main()
|
||||
{
|
||||
vec4 color = texture(LightTexture, v_im_coords);
|
||||
if(num_textures == 2.0){
|
||||
if (num_textures == 2.0)
|
||||
{
|
||||
vec4 dark_color = texture(DarkTexture, v_im_coords);
|
||||
float dp = dot(
|
||||
normalize(light_source_position - xyz_coords),
|
||||
normalize(v_normal)
|
||||
);
|
||||
float dp = dot(normalize(light_source_position - xyz_coords), normalize(v_normal));
|
||||
float alpha = smoothstep(-dark_shift, dark_shift, dp);
|
||||
color = mix(dark_color, color, alpha);
|
||||
}
|
||||
|
||||
frag_color = finalize_color(
|
||||
color,
|
||||
xyz_coords,
|
||||
normalize(v_normal),
|
||||
light_source_position,
|
||||
gloss,
|
||||
shadow
|
||||
);
|
||||
frag_color = finalize_color(color, xyz_coords, normalize(v_normal), light_source_position, gloss, shadow);
|
||||
frag_color.a = v_opacity;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
#version 330
|
||||
|
||||
#include ../include/camera_uniform_declarations.glsl
|
||||
#include "../include/camera_uniform_declarations.glsl"
|
||||
|
||||
in vec3 point;
|
||||
in vec3 du_point;
|
||||
|
|
@ -13,11 +13,12 @@ out vec3 v_normal;
|
|||
out vec2 v_im_coords;
|
||||
out float v_opacity;
|
||||
|
||||
#include ../include/position_point_into_frame.glsl
|
||||
#include ../include/get_gl_Position.glsl
|
||||
#include ../include/get_rotated_surface_unit_normal_vector.glsl
|
||||
#include "../include/get_gl_Position.glsl"
|
||||
#include "../include/get_rotated_surface_unit_normal_vector.glsl"
|
||||
#include "../include/position_point_into_frame.glsl"
|
||||
|
||||
void main(){
|
||||
void main()
|
||||
{
|
||||
xyz_coords = position_point_into_frame(point);
|
||||
v_normal = get_rotated_surface_unit_normal_vector(point, du_point, dv_point);
|
||||
v_im_coords = im_coords;
|
||||
|
|
|
|||
|
|
@ -12,23 +12,18 @@ in vec2 point;
|
|||
|
||||
out vec4 frag_color;
|
||||
|
||||
#include ../include/finalize_color.glsl
|
||||
#include "../include/finalize_color.glsl"
|
||||
|
||||
void main() {
|
||||
void main()
|
||||
{
|
||||
vec2 diff = point - center;
|
||||
float dist = length(diff);
|
||||
float signed_dist = dist - point_radius;
|
||||
if (signed_dist > 0.5 * anti_alias_width){
|
||||
if (signed_dist > 0.5 * anti_alias_width)
|
||||
{
|
||||
discard;
|
||||
}
|
||||
vec3 normal = vec3(diff / point_radius, sqrt(1 - (dist * dist) / (point_radius * point_radius)));
|
||||
frag_color = finalize_color(
|
||||
color,
|
||||
vec3(point.xy, 0.0),
|
||||
normal,
|
||||
light_source_position,
|
||||
gloss,
|
||||
shadow
|
||||
);
|
||||
frag_color = finalize_color(color, vec3(point.xy, 0.0), normal, light_source_position, gloss, shadow);
|
||||
frag_color.a *= smoothstep(0.5, -0.5, signed_dist / anti_alias_width);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#version 330
|
||||
|
||||
layout (points) in;
|
||||
layout (triangle_strip, max_vertices = 4) out;
|
||||
layout(points) in;
|
||||
layout(triangle_strip, max_vertices = 4) out;
|
||||
|
||||
// Needed for get_gl_Position
|
||||
uniform vec2 frame_shape;
|
||||
|
|
@ -20,9 +20,10 @@ out float point_radius;
|
|||
out vec2 center;
|
||||
out vec2 point;
|
||||
|
||||
#include ../include/get_gl_Position.glsl
|
||||
#include "../include/get_gl_Position.glsl"
|
||||
|
||||
void main() {
|
||||
void main()
|
||||
{
|
||||
color = v_color[0];
|
||||
point_radius = v_point_radius[0];
|
||||
center = v_point[0].xy;
|
||||
|
|
@ -30,7 +31,8 @@ void main() {
|
|||
point_radius = v_point_radius[0] / max(1.0 - v_point[0].z / focal_distance / frame_shape.y, 0.0);
|
||||
float rpa = point_radius + anti_alias_width;
|
||||
|
||||
for(int i = 0; i < 4; i++){
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
// To account for perspective
|
||||
|
||||
int x_index = 2 * (i % 2) - 1;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
#version 330
|
||||
|
||||
#include ../include/camera_uniform_declarations.glsl
|
||||
#include "../include/camera_uniform_declarations.glsl"
|
||||
|
||||
in vec3 point;
|
||||
in vec4 color;
|
||||
|
|
@ -11,9 +11,10 @@ out vec3 v_point;
|
|||
out float v_point_radius;
|
||||
out vec4 v_color;
|
||||
|
||||
#include ../include/position_point_into_frame.glsl
|
||||
#include "../include/position_point_into_frame.glsl"
|
||||
|
||||
void main(){
|
||||
void main()
|
||||
{
|
||||
v_point = position_point_into_frame(point);
|
||||
v_point_radius = point_radius;
|
||||
v_color = color;
|
||||
|
|
|
|||
|
|
@ -6,11 +6,15 @@ flat in int v_texture_mode;
|
|||
|
||||
out vec4 frag_color;
|
||||
|
||||
void main() {
|
||||
void main()
|
||||
{
|
||||
float curve_func = v_texture_coords[0] * v_texture_coords[0] - v_texture_coords[1];
|
||||
if (v_texture_mode * curve_func >= 0.0) {
|
||||
if (v_texture_mode * curve_func >= 0.0)
|
||||
{
|
||||
frag_color = v_color;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
discard;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,7 +12,8 @@ out vec4 v_color;
|
|||
out vec2 v_texture_coords;
|
||||
flat out int v_texture_mode;
|
||||
|
||||
void main() {
|
||||
void main()
|
||||
{
|
||||
v_color = in_color;
|
||||
v_texture_coords = texture_coords;
|
||||
v_texture_mode = texture_mode;
|
||||
|
|
|
|||
|
|
@ -10,36 +10,38 @@ out vec4 frag_color;
|
|||
|
||||
// https://www.shadertoy.com/view/ltXSDB
|
||||
// Test if point p crosses line (a, b), returns sign of result
|
||||
float testCross(vec2 a, vec2 b, vec2 p) {
|
||||
return sign((b.y-a.y) * (p.x-a.x) - (b.x-a.x) * (p.y-a.y));
|
||||
float testCross(vec2 a, vec2 b, vec2 p)
|
||||
{
|
||||
return sign((b.y - a.y) * (p.x - a.x) - (b.x - a.x) * (p.y - a.y));
|
||||
}
|
||||
|
||||
// Determine which side we're on (using barycentric parameterization)
|
||||
float signBezier(vec2 A, vec2 B, vec2 C, vec2 p)
|
||||
{
|
||||
vec2 a = C - A, b = B - A, c = p - A;
|
||||
vec2 bary = vec2(c.x*b.y-b.x*c.y,a.x*c.y-c.x*a.y) / (a.x*b.y-b.x*a.y);
|
||||
vec2 bary = vec2(c.x * b.y - b.x * c.y, a.x * c.y - c.x * a.y) / (a.x * b.y - b.x * a.y);
|
||||
vec2 d = vec2(bary.y * 0.5, 0.0) + 1.0 - bary.x - bary.y;
|
||||
return mix(sign(d.x * d.x - d.y), mix(-1.0, 1.0,
|
||||
step(testCross(A, B, p) * testCross(B, C, p), 0.0)),
|
||||
step((d.x - d.y), 0.0)) * testCross(A, C, B);
|
||||
return mix(sign(d.x * d.x - d.y), mix(-1.0, 1.0, step(testCross(A, B, p) * testCross(B, C, p), 0.0)),
|
||||
step((d.x - d.y), 0.0)) *
|
||||
testCross(A, C, B);
|
||||
}
|
||||
|
||||
// Solve cubic equation for roots
|
||||
vec3 solveCubic(float a, float b, float c)
|
||||
{
|
||||
float p = b - a*a / 3.0, p3 = p*p*p;
|
||||
float q = a * (2.0*a*a - 9.0*b) / 27.0 + c;
|
||||
float d = q*q + 4.0*p3 / 27.0;
|
||||
float p = b - a * a / 3.0, p3 = p * p * p;
|
||||
float q = a * (2.0 * a * a - 9.0 * b) / 27.0 + c;
|
||||
float d = q * q + 4.0 * p3 / 27.0;
|
||||
float offset = -a / 3.0;
|
||||
if(d >= 0.0) {
|
||||
if (d >= 0.0)
|
||||
{
|
||||
float z = sqrt(d);
|
||||
vec2 x = (vec2(z, -z) - q) / 2.0;
|
||||
vec2 uv = sign(x)*pow(abs(x), vec2(1.0/3.0));
|
||||
vec2 uv = sign(x) * pow(abs(x), vec2(1.0 / 3.0));
|
||||
return vec3(offset + uv.x + uv.y);
|
||||
}
|
||||
float v = acos(-sqrt(-27.0 / p3) * q / 2.0) / 3.0;
|
||||
float m = cos(v), n = sin(v)*1.732050808;
|
||||
float m = cos(v), n = sin(v) * 1.732050808;
|
||||
return vec3(m + m, -n - m, n - m) * sqrt(-p / 3.0) + offset;
|
||||
}
|
||||
|
||||
|
|
@ -48,38 +50,46 @@ float sdBezier(vec2 A, vec2 B, vec2 C, vec2 p)
|
|||
{
|
||||
B = mix(B + vec2(1e-4), B, abs(sign(B * 2.0 - A - C)));
|
||||
vec2 a = B - A, b = A - B * 2.0 + C, c = a * 2.0, d = A - p;
|
||||
vec3 k = vec3(3.*dot(a,b),2.*dot(a,a)+dot(d,b),dot(d,a)) / dot(b,b);
|
||||
vec3 k = vec3(3. * dot(a, b), 2. * dot(a, a) + dot(d, b), dot(d, a)) / dot(b, b);
|
||||
vec3 t = clamp(solveCubic(k.x, k.y, k.z), 0.0, 1.0);
|
||||
vec2 pos = A + (c + b*t.x)*t.x;
|
||||
vec2 pos = A + (c + b * t.x) * t.x;
|
||||
float dis = length(pos - p);
|
||||
pos = A + (c + b*t.y)*t.y;
|
||||
pos = A + (c + b * t.y) * t.y;
|
||||
dis = min(dis, length(pos - p));
|
||||
pos = A + (c + b*t.z)*t.z;
|
||||
pos = A + (c + b * t.z) * t.z;
|
||||
dis = min(dis, length(pos - p));
|
||||
return dis * signBezier(A, B, C, p);
|
||||
}
|
||||
|
||||
// https://www.shadertoy.com/view/llcfR7
|
||||
float dLine(vec2 p1, vec2 p2, vec2 x) {
|
||||
vec4 colA = vec4(clamp (5.0 - length (x - p1), 0.0, 1.0));
|
||||
vec4 colB = vec4(clamp (5.0 - length (x - p2), 0.0, 1.0));
|
||||
float dLine(vec2 p1, vec2 p2, vec2 x)
|
||||
{
|
||||
vec4 colA = vec4(clamp(5.0 - length(x - p1), 0.0, 1.0));
|
||||
vec4 colB = vec4(clamp(5.0 - length(x - p2), 0.0, 1.0));
|
||||
|
||||
vec2 a_p1 = x - p1;
|
||||
vec2 p2_p1 = p2 - p1;
|
||||
float h = clamp (dot (a_p1, p2_p1) / dot (p2_p1, p2_p1), 0.0, 1.0);
|
||||
return length (a_p1 - p2_p1 * h);
|
||||
float h = clamp(dot(a_p1, p2_p1) / dot(p2_p1, p2_p1), 0.0, 1.0);
|
||||
return length(a_p1 - p2_p1 * h);
|
||||
}
|
||||
|
||||
void main() {
|
||||
void main()
|
||||
{
|
||||
float distance;
|
||||
if (v_degree == 2.0) {
|
||||
if (v_degree == 2.0)
|
||||
{
|
||||
distance = sdBezier(uv_curve[0], uv_curve[1], uv_curve[2], uv_point);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
distance = dLine(uv_curve[0], uv_curve[2], uv_point);
|
||||
}
|
||||
if (abs(distance) < v_thickness) {
|
||||
if (abs(distance) < v_thickness)
|
||||
{
|
||||
frag_color = v_color;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
discard;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,7 +15,8 @@ out vec2 uv_point;
|
|||
out vec2[3] uv_curve;
|
||||
out vec4 v_color;
|
||||
|
||||
int get_degree(in vec3 points[3], out vec3 normal) {
|
||||
int get_degree(in vec3 points[3], out vec3 normal)
|
||||
{
|
||||
float length_threshold = 1e-6;
|
||||
float angle_threshold = 5e-2;
|
||||
|
||||
|
|
@ -24,31 +25,38 @@ int get_degree(in vec3 points[3], out vec3 normal) {
|
|||
|
||||
float dot_prod = clamp(dot(normalize(v01), normalize(v12)), -1, 1);
|
||||
bool aligned = acos(dot_prod) < angle_threshold;
|
||||
bool distinct_01 = length(v01) > length_threshold; // v01 is considered nonzero
|
||||
bool distinct_12 = length(v12) > length_threshold; // v12 is considered nonzero
|
||||
bool distinct_01 = length(v01) > length_threshold; // v01 is considered nonzero
|
||||
bool distinct_12 = length(v12) > length_threshold; // v12 is considered nonzero
|
||||
int num_distinct = int(distinct_01) + int(distinct_12);
|
||||
|
||||
bool quadratic = (num_distinct == 2) && !aligned;
|
||||
bool linear = (num_distinct == 1) || ((num_distinct == 2) && aligned);
|
||||
bool constant = (num_distinct == 0);
|
||||
|
||||
if (quadratic) {
|
||||
if (quadratic)
|
||||
{
|
||||
// If the curve is quadratic pass a normal vector to the caller.
|
||||
normal = normalize(cross(v01, v12));
|
||||
return 2;
|
||||
} else if (linear) {
|
||||
}
|
||||
else if (linear)
|
||||
{
|
||||
return 1;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// https://iquilezles.org/www/articles/bezierbbox/bezierbbox.htm
|
||||
vec4 bboxBezier(in vec2 p0, in vec2 p1, in vec2 p2) {
|
||||
vec4 bboxBezier(in vec2 p0, in vec2 p1, in vec2 p2)
|
||||
{
|
||||
vec2 mi = min(p0, p2);
|
||||
vec2 ma = max(p0, p2);
|
||||
|
||||
if (p1.x < mi.x || p1.x > ma.x || p1.y < mi.y || p1.y > ma.y) {
|
||||
if (p1.x < mi.x || p1.x > ma.x || p1.y < mi.y || p1.y > ma.y)
|
||||
{
|
||||
vec2 t = clamp((p0 - p1) / (p0 - 2.0 * p1 + p2), 0.0, 1.0);
|
||||
vec2 s = 1.0 - t;
|
||||
vec2 q = s * s * p0 + 2.0 * s * t * p1 + t * t * p2;
|
||||
|
|
@ -59,16 +67,19 @@ vec4 bboxBezier(in vec2 p0, in vec2 p1, in vec2 p2) {
|
|||
return vec4(mi, ma);
|
||||
}
|
||||
|
||||
vec2 convert_to_uv(vec3 x_unit, vec3 y_unit, vec3 point) {
|
||||
vec2 convert_to_uv(vec3 x_unit, vec3 y_unit, vec3 point)
|
||||
{
|
||||
return vec2(dot(point, x_unit), dot(point, y_unit));
|
||||
}
|
||||
|
||||
vec3 convert_from_uv(vec3 translation, vec3 x_unit, vec3 y_unit, vec2 point) {
|
||||
vec3 convert_from_uv(vec3 translation, vec3 x_unit, vec3 y_unit, vec2 point)
|
||||
{
|
||||
vec3 untranslated_point = point[0] * x_unit + point[1] * y_unit;
|
||||
return untranslated_point + translation;
|
||||
}
|
||||
|
||||
void main() {
|
||||
void main()
|
||||
{
|
||||
float thickness_multiplier = 0.004;
|
||||
v_color = in_color;
|
||||
|
||||
|
|
@ -78,26 +89,33 @@ void main() {
|
|||
vec3 tile_x_unit = normalize(current_curve[2] - current_curve[0]);
|
||||
vec3 unit_normal;
|
||||
vec3 tile_y_unit;
|
||||
if (v_degree == 0) {
|
||||
if (v_degree == 0)
|
||||
{
|
||||
tile_y_unit = vec3(0.0, 0.0, 0.0);
|
||||
} else if (v_degree == 1) {
|
||||
}
|
||||
else if (v_degree == 1)
|
||||
{
|
||||
// Since the curve forms a straight line there's no way to compute a normal.
|
||||
unit_normal = manim_unit_normal;
|
||||
|
||||
tile_y_unit = cross(unit_normal, tile_x_unit);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
// Prefer to use a computed normal vector rather than the one from manim.
|
||||
unit_normal = computed_normal;
|
||||
|
||||
// Ensure tile_y_unit is pointing toward p1 from p0.
|
||||
tile_y_unit = cross(unit_normal, tile_x_unit);
|
||||
if (dot(tile_y_unit, current_curve[1] - current_curve[0]) < 0) {
|
||||
if (dot(tile_y_unit, current_curve[1] - current_curve[0]) < 0)
|
||||
{
|
||||
tile_y_unit *= -1;
|
||||
}
|
||||
}
|
||||
|
||||
// Project the curve onto the tile.
|
||||
for(int i = 0; i < 3; i++) {
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
uv_curve[i] = convert_to_uv(tile_x_unit, tile_y_unit, current_curve[i]);
|
||||
}
|
||||
|
||||
|
|
@ -117,9 +135,7 @@ void main() {
|
|||
tile_x_vec += 2 * v_thickness * tile_x_unit;
|
||||
tile_y_vec += 2 * v_thickness * tile_y_unit;
|
||||
|
||||
vec3 tile_point = tile_origin + \
|
||||
tile_coordinate[0] * tile_x_vec + \
|
||||
tile_coordinate[1] * tile_y_vec;
|
||||
vec3 tile_point = tile_origin + tile_coordinate[0] * tile_x_vec + tile_coordinate[1] * tile_y_vec;
|
||||
gl_Position = u_projection_matrix * u_model_view_matrix * vec4(tile_point, 1.0);
|
||||
uv_point = convert_to_uv(tile_x_unit, tile_y_unit, tile_point);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
in vec4 v_color;
|
||||
out vec4 frag_color;
|
||||
|
||||
void main() {
|
||||
frag_color = v_color;
|
||||
void main()
|
||||
{
|
||||
frag_color = v_color;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,8 @@ in vec4 in_vert;
|
|||
in vec4 in_color;
|
||||
out vec4 v_color;
|
||||
|
||||
void main() {
|
||||
void main()
|
||||
{
|
||||
v_color = in_color;
|
||||
gl_Position = u_projection_matrix * u_view_matrix * u_model_matrix * in_vert;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue