use super::rtt::{convert_to_texture, owned_by};
use super::shape::circle;
use crate::nodes::tsl::{dot, float, mix, screen_uv, time, vec2_join, vec3_join};
use crate::nodes::NodeRef;
pub fn barrel_uv(curvature: impl Into<NodeRef>, coord: impl Into<NodeRef>) -> NodeRef {
let (curvature, coord) = (curvature.into(), coord.into());
let centered = coord.sub(0.5).mul(2.0);
let r2 = dot(centered.clone(), centered.clone());
let distortion = float(1.0).sub(r2.mul(curvature.clone()));
let corner_distortion = float(1.0).sub(curvature.mul(2.0));
centered
.div(distortion)
.mul(corner_distortion)
.mul(0.5)
.add(0.5)
}
pub fn barrel_mask(coord: impl Into<NodeRef>) -> NodeRef {
let coord = coord.into();
let out_of_bounds = coord
.x()
.less_than(0.0)
.or(coord.x().greater_than(1.0))
.or(coord.y().less_than(0.0))
.or(coord.y().greater_than(1.0));
out_of_bounds.select(float(0.0), float(1.0))
}
pub fn color_bleeding(color: impl Into<NodeRef>, amount: impl Into<NodeRef>) -> NodeRef {
let (color, amount) = (color.into(), amount.into());
let input_texture = convert_to_texture(color);
let original = input_texture.sample(screen_uv()).rgb();
let left = |k: f64| {
let offset = if k == 1.0 {
amount.clone()
} else {
amount.clone().mul(k)
};
input_texture
.sample(screen_uv().sub(vec2_join(vec![offset, float(0.0)])))
.rgb()
};
let (left1, left2, left3) = (left(1.0), left(2.0), left(3.0));
let bleed_r = original
.x()
.add(left1.x().mul(0.4))
.add(left2.x().mul(0.2))
.add(left3.x().mul(0.1));
let bleed_g = original
.y()
.add(left1.y().mul(0.25))
.add(left2.y().mul(0.1));
let bleed_b = original.z().add(left1.z().mul(0.15));
let r = bleed_r.div(1.7).clamp(0.0, 1.0);
let g = bleed_g.div(1.35).clamp(0.0, 1.0);
let b = bleed_b.div(1.15).clamp(0.0, 1.0);
owned_by(vec3_join(vec![r, g, b]), input_texture)
}
pub fn scanlines(
color: impl Into<NodeRef>,
intensity: impl Into<NodeRef>,
count: impl Into<NodeRef>,
speed: impl Into<NodeRef>,
coord: impl Into<NodeRef>,
) -> NodeRef {
let (color, intensity, count, speed, coord) = (
color.into(),
intensity.into(),
count.into(),
speed.into(),
coord.into(),
);
let animated_y = coord.y().sub(time().mul(speed));
let scanline = animated_y.mul(count).sin();
let scanline_intensity = scanline.mul(0.5).add(0.5).mul(intensity);
color.mul(float(1.0).sub(scanline_intensity))
}
pub fn vignette(
color: impl Into<NodeRef>,
intensity: impl Into<NodeRef>,
smoothness: impl Into<NodeRef>,
coord: impl Into<NodeRef>,
) -> NodeRef {
let (color, intensity) = (color.into(), intensity.into());
let mask = circle(float(1.42), smoothness, coord);
let vignette_amount = mix(float(1.0).sub(intensity), float(1.0), mask);
color.mul(vignette_amount)
}