use std::rc::Rc;
use crate::nodes::node::Type;
use crate::nodes::node::{FnDef, Lazy, NodeRef};
use crate::nodes::tsl::{
call, ceil, dpdx, dpdy, exp2, float, floor, fract, inline_fn, length, log2, max, shader_fn,
to_var, vec2_join, vec3_join,
};
const ALPHA_HASH_SCALE: f64 = 0.05;
fn hash2d(value: NodeRef) -> NodeRef {
thread_local! { static CELL: Lazy<Rc<FnDef>> = const { Lazy::new() }; }
let def = CELL.with(|c| {
c.get(|| {
inline_fn(1, Type::F32, |args| {
let value = args[0].clone();
float(1.0e4)
.mul(
float(17.0)
.mul(value.x())
.add(float(0.1).mul(value.y()))
.sin(),
)
.mul(float(0.1).add(float(13.0).mul(value.y()).add(value.x()).sin().abs()))
.fract()
})
})
});
call(&def, vec![value])
}
fn hash3d(value: NodeRef) -> NodeRef {
thread_local! { static CELL: Lazy<Rc<FnDef>> = const { Lazy::new() }; }
let def = CELL.with(|c| {
c.get(|| {
inline_fn(1, Type::F32, |args| {
let value = args[0].clone();
hash2d(vec2_join(vec![hash2d(value.xy()), value.z()]))
})
})
});
call(&def, vec![value])
}
pub(crate) fn get_alpha_hash_threshold(position: NodeRef) -> NodeRef {
thread_local! { static CELL: Lazy<Rc<FnDef>> = const { Lazy::new() }; }
let def = CELL.with(|c| {
c.get(|| {
shader_fn(
Some("getAlphaHashThreshold"),
vec![("position", Type::Vec3)],
Type::F32,
|args| {
let position = args[0].clone();
let max_deriv = max(length(dpdx(position.xyz())), length(dpdy(position.xyz())));
let pix_scale = to_var(
Some("pixScale"),
float(1.0).div(float(ALPHA_HASH_SCALE).mul(max_deriv)),
);
let pix_scales = vec2_join(vec![
exp2(floor(log2(pix_scale.clone()))),
exp2(ceil(log2(pix_scale.clone()))),
]);
let alpha = vec2_join(vec![
hash3d(floor(pix_scales.x().mul(position.xyz()))),
hash3d(floor(pix_scales.y().mul(position.xyz()))),
]);
let lerp_factor = fract(log2(pix_scale));
let x = lerp_factor
.one_minus()
.mul(alpha.x())
.add(lerp_factor.mul(alpha.y()));
let a = lerp_factor.min(lerp_factor.one_minus());
let one = || float(1.0);
let cases = vec3_join(vec![
x.mul(x.clone())
.div(float(2.0).mul(a.clone()).mul(one().sub(a.clone()))),
x.sub(float(0.5).mul(a.clone())).div(one().sub(a.clone())),
one().sub(
one()
.sub(x.clone())
.mul(one().sub(x.clone()))
.div(float(2.0).mul(a.clone()).mul(one().sub(a.clone()))),
),
]);
let threshold = x.less_than(a.one_minus()).select(
x.less_than(a.clone()).select(cases.x(), cases.y()),
cases.z(),
);
threshold.clamp(float(1.0e-6), float(1.0))
},
)
})
});
call(&def, vec![position])
}