use std::cell::RefCell;
use std::rc::{Rc, Weak};
use crate::cameras::PerspectiveCamera;
use crate::math::Color;
use crate::nodes::node::{LiveValue, TextureSource, Type, UniformGroup, UniformSource};
use crate::nodes::tsl::{
abs, block, const_array_of, float, if_then, int, length, loop_n, mix, pass_depth_texture_uv,
perspective_depth_to_view_z, texture_size, texture_uv, to_const, to_var, uniform, uv, vec2,
vec3, vec4_join, view_z_to_orthographic_depth,
};
use crate::nodes::NodeRef;
use crate::textures::{DepthTexture, Texture};
const POISSON_DISK: [[f64; 2]; 8] = [
[0.493393, 0.394269],
[0.798547, 0.885922],
[0.259143, 0.650754],
[0.605322, 0.023588],
[-0.574681, 0.137452],
[-0.430397, -0.638423],
[-0.849487, -0.366258],
[0.170621, -0.569941],
];
#[derive(Clone)]
#[non_exhaustive]
pub struct DepthAwareBlendOptions {
pub blend_color: NodeRef,
pub edge_radius: NodeRef,
pub edge_strength: NodeRef,
}
impl Default for DepthAwareBlendOptions {
fn default() -> Self {
let white = Color::from_hex(0xffffff);
Self {
blend_color: vec3(white.r, white.g, white.b),
edge_radius: int(2),
edge_strength: float(2.0),
}
}
}
fn camera_reference(
camera: &Rc<RefCell<PerspectiveCamera>>,
read: fn(&PerspectiveCamera) -> f64,
) -> NodeRef {
let camera: Weak<RefCell<PerspectiveCamera>> = Rc::downgrade(camera);
uniform(
UniformSource::Live(LiveValue::new(move || {
let camera = camera
.upgrade()
.expect("three-rs: a depthAwareBlend outlived its camera");
let value = read(&camera.borrow());
vec![value]
})),
Type::F32,
UniformGroup::Object,
None,
)
}
pub fn depth_aware_blend(
base: &Texture,
blend: &Texture,
depth: &DepthTexture,
camera: &Rc<RefCell<PerspectiveCamera>>,
options: DepthAwareBlendOptions,
) -> NodeRef {
let uv_node = uv();
let camera_near = camera_reference(camera, |camera| camera.near);
let camera_far = camera_reference(camera, |camera| camera.far);
let view_z = perspective_depth_to_view_z(
pass_depth_texture_uv(depth, uv_node.clone()),
camera_near.clone(),
camera_far.clone(),
);
let correct_depth =
view_z_to_orthographic_depth(view_z, camera_near.clone(), camera_far.clone());
let push_dir = to_var(None, vec2(0.0, 0.0));
let count = to_var(None, float(0.0));
let resolution = to_const(
None,
texture_size(TextureSource::Texture2D(base.clone()), int(0)).to(Type::IVec2),
);
let pixel_step = vec2(1.0, 1.0).div(resolution.clone());
let poisson_disk = const_array_of(Type::Vec2, POISSON_DISK.iter().flatten().copied().collect());
let taps = loop_n("i", int(8), |i| {
let offset = poisson_disk
.element_node(i.clone())
.mul(options.edge_radius.clone());
let sample_uv = uv_node.clone().add(offset.clone().mul(pixel_step));
let sample_depth = pass_depth_texture_uv(depth, sample_uv);
let sample_view_z =
perspective_depth_to_view_z(sample_depth, camera_near.clone(), camera_far.clone());
let sample_linear_depth =
view_z_to_orthographic_depth(sample_view_z, camera_near.clone(), camera_far.clone());
vec![if_then(
abs(sample_linear_depth.sub(correct_depth.clone()))
.less_than(float(0.05).mul(correct_depth.clone())),
vec![push_dir.add_assign(offset), count.add_assign(float(1.0))],
)]
});
let count_fix = count.assign(count.equal(float(0.0)).select(float(1.0), count.clone()));
let push = push_dir.div_assign(count.clone());
let sample_uv = length(push_dir.clone()).greater_than(float(0.0)).select(
uv_node.clone().add(
options
.edge_strength
.mul(push_dir.clone().div(resolution.clone())),
),
uv_node.clone(),
);
let best_choice = texture_uv(blend, sample_uv).x();
let base_color = texture_uv(base, uv_node);
block(
vec![
push_dir.clone(),
count.clone(),
resolution,
taps,
count_fix,
push,
],
mix(
base_color,
vec4_join(vec![options.blend_color, float(1.0)]),
best_choice,
),
)
}