use crate::nodes::node::Type;
use crate::nodes::tsl::{call, float, length, shader_fn, texture_uv, to_var, uv, vec4_join};
use crate::nodes::NodeRef;
use crate::textures::Texture;
pub fn chromatic_aberration(
map: &Texture,
strength: NodeRef,
center: NodeRef,
scale: NodeRef,
) -> NodeRef {
let map = map.clone();
let def = shader_fn(
Some("ChromaticAberrationShader"),
vec![
("uv", Type::Vec2),
("strength", Type::F32),
("center", Type::Vec2),
("scale", Type::F32),
],
Type::Vec4,
move |args| {
let (uv, strength, center, scale) = (
args[0].clone(),
args[1].clone(),
args[2].clone(),
args[3].clone(),
);
let offset = to_var(None, uv.sub(center.clone()));
let distance = length(offset.clone());
let step = scale.mul(float(0.02)).mul(strength.clone());
let red_scale = float(1.0).add(step.clone());
let green_scale = float(1.0);
let blue_scale = float(1.0).sub(step);
let aberration_strength = to_var(None, strength.mul(distance));
let radial = offset.mul(aberration_strength);
let channel_uv = |channel_scale: NodeRef, chromatic: f64| {
center
.add(offset.mul(channel_scale))
.add(radial.mul(float(chromatic)))
};
let r = to_var(None, texture_uv(&map, channel_uv(red_scale, 0.01)));
let g = to_var(None, texture_uv(&map, channel_uv(green_scale, 0.0)));
let b = to_var(None, texture_uv(&map, channel_uv(blue_scale, -0.01)));
let a = to_var(None, texture_uv(&map, uv));
vec4_join(vec![r.x(), g.y(), b.z(), a.w()])
},
);
call(&def, vec![uv(), strength, center, scale])
}