use std::rc::Rc;
use crate::nodes::node::FnDef;
use crate::nodes::node::Type;
use crate::nodes::tsl::*;
use crate::nodes::NodeRef;
fn trim_segment_alpha() -> Rc<FnDef> {
shader_fn(
None,
vec![("start", Type::Vec4), ("end", Type::Vec4)],
Type::F32,
|args| {
let (start, end) = (args[0].clone(), args[1].clone());
let a = camera_projection_matrix().element(2).element(2);
let b = camera_projection_matrix().element(3).element(2);
let near_estimate = a.greater_than(float(0.0)).select(
b.negate().div(a.add(float(1.0))),
b.mul(float(-0.5)).div(a.clone()),
);
near_estimate.sub(start.z()).div(end.z().sub(start.z()))
},
)
}
fn closest_line_to_line() -> Rc<FnDef> {
shader_fn(
None,
vec![
("p1", Type::Vec3),
("p2", Type::Vec3),
("p3", Type::Vec3),
("p4", Type::Vec3),
],
Type::Vec2,
|args| {
let (p1, p2, p3, p4) = (
args[0].clone(),
args[1].clone(),
args[2].clone(),
args[3].clone(),
);
let p13 = p1.sub(p3.clone());
let p43 = p4.sub(p3);
let p21 = p2.sub(p1);
let d1343 = p13.dot(p43.clone());
let d4321 = p43.dot(p21.clone());
let d1321 = p13.dot(p21.clone());
let d4343 = p43.dot(p43.clone());
let d2121 = p21.dot(p21.clone());
let denom = d2121.mul(d4343.clone()).sub(d4321.mul(d4321.clone()));
let numer = d1343.mul(d4321.clone()).sub(d1321.mul(d4343.clone()));
let mua = numer.div(denom).saturate();
let mub = d1343.add(d4321.mul(mua.clone())).div(d4343).saturate();
vec2_join(vec![mua, mub])
},
)
}
fn world_start() -> NodeRef {
varying_property("worldStart", Type::Vec3, false)
}
fn world_end() -> NodeRef {
varying_property("worldEnd", Type::Vec3, false)
}
fn world_pos() -> NodeRef {
varying_property("worldPos", Type::Vec4, false)
}
fn mvp_line(world_units: bool) -> (Vec<NodeRef>, NodeRef) {
let instance_start = attribute("instanceStart", Type::Vec3);
let instance_end = attribute("instanceEnd", Type::Vec3);
let start = to_var(
Some("start"),
model_view_matrix().mul(vec4_join(vec![instance_start, float(1.0)])),
);
let end = to_var(
Some("end"),
model_view_matrix().mul(vec4_join(vec![instance_end, float(1.0)])),
);
let mut statements = vec![start.clone(), end.clone()];
if world_units {
statements.push(world_start().assign(start.xyz()));
statements.push(world_end().assign(end.xyz()));
}
let perspective = to_const(
None,
camera_projection_matrix()
.element(2)
.element(3)
.equal(float(-1.0)),
);
let trim = trim_segment_alpha();
statements.push(if_then(
perspective.clone(),
vec![if_else_if(
start
.z()
.less_than(float(0.0))
.and(end.z().greater_than(float(0.0))),
vec![end.assign(vec4_join(vec![
mix(
start.xyz(),
end.xyz(),
call(&trim, vec![start.clone(), end.clone()]),
),
end.w(),
]))],
end.z()
.less_than(float(0.0))
.and(start.z().greater_than_equal(float(0.0))),
vec![start.assign(vec4_join(vec![
mix(
end.xyz(),
start.xyz(),
call(&trim, vec![end.clone(), start.clone()]),
),
start.w(),
]))],
)],
));
let clip_start = to_var(None, camera_projection_matrix().mul(start.clone()));
let clip_end = to_var(None, camera_projection_matrix().mul(end.clone()));
let ndc_start = clip_start.xyz().div(clip_start.w());
let ndc_end = clip_end.xyz().div(clip_end.w());
let dir = to_var(None, ndc_end.xy().sub(ndc_start.xy()));
let aspect = to_var(None, viewport().z().div(viewport().w()));
statements.push(dir.x().assign(dir.x().mul(aspect.clone())));
statements.push(dir.assign(dir.normalize()));
let clip = to_var(None, vec4(0.0, 0.0, 0.0, 1.0));
statements.push(clip.clone());
if world_units {
let world_dir = end.xyz().sub(start.xyz()).normalize();
let tmp_fwd = perspective.select(
mix(start.xyz(), end.xyz(), float(0.5)).normalize(),
vec3(0.0, 0.0, -1.0),
);
let world_up = world_dir.cross(tmp_fwd).normalize();
let world_fwd = world_dir.cross(world_up.clone());
let world_pos = world_pos();
let below_half = || position_geometry().y().less_than(float(0.5));
statements.push(world_pos.assign(below_half().select(start.clone(), end.clone())));
let hw = material_line_width().mul(float(0.5));
statements.push(world_pos.add_assign(vec4_join(vec![
position_geometry().x().less_than(float(0.0)).select(
world_up.mul(hw.clone()),
world_up.mul(hw.clone()).negate(),
),
float(0.0),
])));
statements.push(world_pos.add_assign(vec4_join(vec![
below_half().select(
world_dir.mul(hw.clone()).negate(),
world_dir.mul(hw.clone()),
),
float(0.0),
])));
statements
.push(world_pos.add_assign(vec4_join(vec![world_fwd.mul(hw.clone()), float(0.0)])));
statements.push(if_then(
position_geometry()
.y()
.greater_than(float(1.0))
.or(position_geometry().y().less_than(float(0.0))),
vec![world_pos.sub_assign(vec4_join(vec![
world_fwd.mul(float(2.0)).mul(hw),
float(0.0),
]))],
));
statements.push(clip.assign(camera_projection_matrix().mul(world_pos)));
let clip_pose = to_var(None, vec3(0.0, 0.0, 0.0));
statements.push(clip_pose.clone());
statements.push(clip_pose.assign(below_half().select(ndc_start, ndc_end)));
statements.push(clip.z().assign(clip_pose.z().mul(clip.w())));
return (statements, clip);
}
let offset = to_var(Some("offset"), vec2_join(vec![dir.y(), dir.x().negate()]));
statements.push(offset.clone());
statements.push(dir.x().assign(dir.x().div(aspect.clone())));
statements.push(offset.x().assign(offset.x().div(aspect)));
statements.push(
offset.assign(
position_geometry()
.x()
.less_than(float(0.0))
.select(offset.negate(), offset.clone()),
),
);
statements.push(if_else_if(
position_geometry().y().less_than(float(0.0)),
vec![offset.assign(offset.sub(dir.clone()))],
position_geometry().y().greater_than(float(1.0)),
vec![offset.assign(offset.add(dir.clone()))],
));
statements.push(offset.assign(offset.mul(material_line_width())));
statements.push(offset.assign(offset.div(viewport().w().div(screen_dpr()))));
statements.push(
clip.assign(
position_geometry()
.y()
.less_than(float(0.5))
.select(clip_start, clip_end),
),
);
statements.push(offset.assign(offset.mul(clip.w())));
statements.push(clip.assign(clip.add(vec4_join(vec![offset, float(0.0), float(0.0)]))));
(statements, clip)
}
pub(crate) fn setup_position(world_units: bool) -> NodeRef {
let (statements, clip) = mvp_line(world_units);
let local = to_var(
None,
model_world_matrix_inverse()
.mul(camera_world_matrix())
.mul(camera_projection_matrix_inverse())
.mul(clip),
);
position_local().assign(block(statements, local.xyz().div(local.w())))
}
fn alpha_line(alpha_to_coverage: bool, world_units: bool) -> NodeRef {
if world_units {
return alpha_line_world_units(alpha_to_coverage);
}
let v_uv = uv();
let alpha = to_var(Some("alpha"), float(1.0));
let a = v_uv.x();
let b = v_uv
.y()
.greater_than(float(0.0))
.select(v_uv.y().sub(float(1.0)), v_uv.y().add(float(1.0)));
let len2 = a.clone().mul(a).add(b.clone().mul(b));
let body = if alpha_to_coverage {
let dlen = to_var(Some("dlen"), fwidth(len2.clone()));
vec![
dlen.clone(),
if_then(
abs(v_uv.y()).greater_than(float(1.0)),
vec![alpha
.assign(smoothstep(dlen.one_minus(), dlen.add(float(1.0)), len2).one_minus())],
),
]
} else {
vec![if_then(
abs(v_uv.y()).greater_than(float(1.0)),
vec![if_then(len2.greater_than(float(1.0)), vec![discard()])],
)]
};
let mut statements = vec![alpha.clone()];
statements.extend(body);
block(statements, alpha)
}
fn alpha_line_world_units(alpha_to_coverage: bool) -> NodeRef {
let alpha = to_var(Some("alpha"), float(1.0));
let len = to_var(None, float(0.0));
let orthographic = to_const(
None,
camera_projection_matrix()
.element(2)
.element(3)
.not_equal(float(-1.0)),
);
let (world_start, world_end, world_pos) = (world_start(), world_end(), world_pos());
let ortho = {
let line_dir = to_const(None, world_end.xy().sub(world_start.xy()));
let t = world_pos
.xy()
.sub(world_start.xy())
.dot(line_dir.clone())
.div(line_dir.dot(line_dir.clone()))
.saturate();
vec![
line_dir.clone(),
len.assign(length(
world_start.xy().add(line_dir.mul(t)).sub(world_pos.xy()),
)),
]
};
let persp = {
let ray_end = to_const(None, world_pos.xyz().normalize().mul(float(1e5)));
let line_dir = world_end.clone().sub(world_start.clone());
let params = to_const(
None,
call(
&closest_line_to_line(),
vec![
world_start.clone(),
world_end.clone(),
vec3(0.0, 0.0, 0.0),
ray_end.clone(),
],
),
);
let p1 = world_start.add(line_dir.mul(params.x()));
let p2 = ray_end.clone().mul(params.y());
vec![ray_end, params.clone(), len.assign(length(p1.sub(p2)))]
};
let norm = to_const(None, len.div(material_line_width()));
let coverage = if alpha_to_coverage {
let dnorm = to_const(None, fwidth(norm.clone()));
vec![
norm.clone(),
dnorm.clone(),
alpha.assign(
smoothstep(dnorm.negate().add(float(0.5)), dnorm.add(float(0.5)), norm).one_minus(),
),
]
} else {
vec![if_then(norm.greater_than(float(0.5)), vec![discard()])]
};
let mut statements = vec![
alpha.clone(),
len.clone(),
orthographic.clone(),
if_else(orthographic, ortho, persp),
];
statements.extend(coverage);
block(statements, alpha)
}
pub(crate) fn setup_diffuse_color(
alpha_to_coverage: bool,
world_units: bool,
vertex_colors: bool,
fragment: &mut Vec<NodeRef>,
) {
fragment.push(
diffuse_color()
.w()
.mul_assign(alpha_line(alpha_to_coverage, world_units)),
);
if vertex_colors {
let color_start = attribute("instanceColorStart", Type::Vec3);
let color_end = attribute("instanceColorEnd", Type::Vec3);
let instance_color = position_geometry()
.y()
.less_than(float(0.5))
.select(color_start, color_end);
fragment.push(diffuse_color().rgb().mul_assign(instance_color));
}
}