use std::cell::RefCell;
use std::rc::{Rc, Weak};
use crate::addons::simplex_noise::SimplexNoise;
use crate::cameras::PerspectiveCamera;
use crate::nodes::node::{
LiveValue, SettableValue, TextureSource, Type, UniformGroup, UniformSource,
};
use crate::nodes::tsl::{
abs, block, dot, float, get_normal_from_depth, get_view_position, if_else, if_then, int,
loop_options, luminance, mat2_join, max, pass_depth_texture_uv, pi, property, texture_sample,
texture_size, texture_uv, to_const, to_var, uniform, uniform_array_vec3, uniform_settable, uv,
vec2_join, vec3_join, vec4_join, UniformArray,
};
use crate::nodes::NodeRef;
use crate::testing::DeterministicRandom;
use crate::textures::{DepthTexture, MinFilter, Texture, TextureFilter, Wrapping};
use super::SampleFn;
pub fn denoise(
input: &Texture,
depth: &DepthTexture,
normal: Option<SampleFn>,
camera: &Rc<RefCell<PerspectiveCamera>>,
) -> DenoiseNode {
DenoiseNode::new(input, depth, normal, camera)
}
pub struct DenoiseNode {
pub luma_phi: SettableValue,
pub depth_phi: SettableValue,
pub normal_phi: SettableValue,
pub radius: SettableValue,
pub index: SettableValue,
noise: Texture,
node: NodeRef,
}
struct Uniforms {
luma_phi: NodeRef,
depth_phi: NodeRef,
normal_phi: NodeRef,
radius: NodeRef,
index: NodeRef,
resolution: NodeRef,
sample_vectors: UniformArray,
projection_inverse: NodeRef,
}
struct Inputs<'a> {
input: &'a Texture,
depth: &'a DepthTexture,
normal: Option<SampleFn>,
noise: &'a Texture,
u: Uniforms,
}
impl Inputs<'_> {
fn sample_texture(&self, coord: NodeRef) -> NodeRef {
texture_uv(self.input, coord)
}
fn sample_depth(&self, coord: NodeRef) -> NodeRef {
pass_depth_texture_uv(self.depth, coord)
}
fn sample_normal(&self, coord: NodeRef) -> NodeRef {
match &self.normal {
Some(normal) => normal(coord).rgb().normalize(),
None => get_normal_from_depth(coord, self.depth, self.u.projection_inverse.clone()),
}
}
}
impl DenoiseNode {
pub fn new(
input: &Texture,
depth: &DepthTexture,
normal: Option<SampleFn>,
camera: &Rc<RefCell<PerspectiveCamera>>,
) -> Self {
let (luma_phi, luma_phi_value) = uniform_settable(Type::F32, vec![5.0]);
let (depth_phi, depth_phi_value) = uniform_settable(Type::F32, vec![5.0]);
let (normal_phi, normal_phi_value) = uniform_settable(Type::F32, vec![5.0]);
let (radius, radius_value) = uniform_settable(Type::F32, vec![5.0]);
let (index, index_value) = uniform_settable(Type::F32, vec![0.0]);
let map = input.clone();
let resolution = uniform(
UniformSource::Live(LiveValue::new(move || {
let (width, height) = map.size();
vec![f64::from(width), f64::from(height)]
})),
Type::Vec2,
UniformGroup::Object,
None,
);
let camera: Weak<RefCell<PerspectiveCamera>> = Rc::downgrade(camera);
let projection_inverse = uniform(
UniformSource::Live(LiveValue::new(move || {
let camera = camera
.upgrade()
.expect("three-rs: a DenoiseNode outlived its camera");
let m = camera.borrow().projection_matrix_inverse.elements;
m.to_vec()
})),
Type::Mat4,
UniformGroup::Object,
None,
);
let sample_vectors = uniform_array_vec3(&generate_denoise_samples(16, 2, 1.0));
let noise = generate_default_noise(64);
let inputs = Inputs {
input,
depth,
normal,
noise: &noise,
u: Uniforms {
luma_phi,
depth_phi,
normal_phi,
radius,
index,
resolution,
sample_vectors,
projection_inverse,
},
};
let node = denoise_fn(&inputs, uv());
Self {
luma_phi: luma_phi_value,
depth_phi: depth_phi_value,
normal_phi: normal_phi_value,
radius: radius_value,
index: index_value,
noise,
node,
}
}
pub fn node(&self) -> NodeRef {
self.node.clone()
}
pub fn noise_texture(&self) -> &Texture {
&self.noise
}
}
#[inline(never)]
fn denoise_sample(
s: &Inputs,
center: &NodeRef,
view_normal: &NodeRef,
view_position: &NodeRef,
sample_uv: NodeRef,
) -> (Vec<NodeRef>, NodeRef) {
let u = &s.u;
let texel = to_var(None, s.sample_texture(sample_uv.clone()));
let depth = to_var(None, s.sample_depth(sample_uv.clone()));
let normal = to_var(None, s.sample_normal(sample_uv.clone()));
let neighbor_color = texel.rgb();
let view_pos = to_var(
None,
get_view_position(sample_uv, depth.clone(), u.projection_inverse.clone()),
);
let normal_diff = to_var(None, dot(view_normal.clone(), normal.clone()));
let normal_similarity = to_var(
None,
max(normal_diff.clone(), 0.0).pow(u.normal_phi.clone()),
);
let luma_diff = to_var(
None,
abs(luminance(neighbor_color.clone()).sub(luminance(center.clone()))),
);
let luma_similarity = to_var(
None,
max(float(1.0).sub(luma_diff.div(u.luma_phi.clone())), 0.0),
);
let depth_diff = to_var(
None,
abs(dot(
view_position.sub(view_pos.clone()),
view_normal.clone(),
)),
);
let depth_similarity = max(float(1.0).sub(depth_diff.div(u.depth_phi.clone())), 0.0);
let w = luma_similarity
.mul(depth_similarity)
.mul(normal_similarity.clone());
let statements = vec![
texel,
depth,
normal,
view_pos,
normal_diff,
normal_similarity,
luma_diff,
luma_similarity,
depth_diff,
];
(
statements,
vec4_join(vec![neighbor_color.mul(w.clone()), w]),
)
}
#[inline(never)]
fn denoise_fn(s: &Inputs, uv_node: NodeRef) -> NodeRef {
let depth = to_var(None, s.sample_depth(uv_node.clone()));
let view_normal = to_var(None, s.sample_normal(uv_node.clone()));
let texel = to_var(None, s.sample_texture(uv_node.clone()));
let result = property("denoiseResult", Type::Vec4);
let condition = depth
.greater_than_equal(1.0)
.or(dot(view_normal.clone(), view_normal.clone()).equal(0.0));
let branch = if_else(
condition,
vec![result.assign(texel.clone())],
denoise_else(s, &uv_node, &depth, &view_normal, &texel, &result),
);
block(vec![depth, view_normal, texel, branch], result)
}
#[inline(never)]
fn denoise_else(
s: &Inputs,
uv_node: &NodeRef,
depth: &NodeRef,
view_normal: &NodeRef,
texel: &NodeRef,
result: &NodeRef,
) -> Vec<NodeRef> {
let u = &s.u;
let center = vec3_join(vec![texel.rgb()]);
let view_position = to_const(
None,
get_view_position(uv_node.clone(), depth.clone(), u.projection_inverse.clone()),
);
let noise_resolution = texture_size(TextureSource::Texture2D(s.noise.clone()), int(0));
let noise_uv = vec2_join(vec![uv_node.x(), uv_node.y().one_minus()])
.mul(u.resolution.div(noise_resolution));
let noise_texel = to_var(None, texture_sample(s.noise, noise_uv));
let channel = || u.index.mod_(4.0).mul(2.0).mul(pi());
let x = noise_texel.element_node(channel()).sin();
let y = noise_texel.element_node(channel()).cos();
let noise_vec = vec2_join(vec![x, y]);
let rotation_matrix = mat2_join(vec![
noise_vec.x(),
noise_vec.y().negate(),
noise_vec.x(),
noise_vec.y(),
]);
let total_weight = to_var(None, float(1.0));
let denoised = to_var(None, vec3_join(vec![texel.rgb()]));
let taps = loop_options("i", Type::I32, int(0), int(16), "<", |i| {
let sample_dir = u.sample_vectors.element_xyz(i.clone());
let offset = rotation_matrix
.mul(
sample_dir
.xy()
.mul(float(1.0).add(sample_dir.z().mul(u.radius.sub(1.0)))),
)
.div(u.resolution.clone());
let sample_uv = uv_node.add(offset);
let (mut statements, sample_result) =
denoise_sample(s, ¢er, view_normal, &view_position, sample_uv);
statements.push(denoised.add_assign(sample_result.xyz()));
statements.push(total_weight.add_assign(sample_result.w()));
statements
});
vec![
view_position.clone(),
noise_texel,
total_weight.clone(),
denoised.clone(),
taps,
if_then(
total_weight.greater_than(float(0.0)),
vec![denoised.div_assign(total_weight.clone())],
),
result.assign(vec4_join(vec![denoised, texel.a()])),
]
}
pub fn generate_denoise_samples(
num_samples: usize,
num_rings: usize,
radius_exponent: f64,
) -> Vec<[f64; 3]> {
(0..num_samples)
.map(|i| {
let angle =
2.0 * std::f64::consts::PI * num_rings as f64 * i as f64 / num_samples as f64;
let radius = (i as f64 / (num_samples - 1) as f64).powf(radius_exponent);
[angle.cos(), angle.sin(), radius]
})
.collect()
}
pub fn generate_default_noise(size: u32) -> Texture {
let mut random = DeterministicRandom::new();
let simplex = SimplexNoise::new(|| random.next());
let n = size as usize;
let s = f64::from(size);
let mut data = vec![0u8; n * n * 4];
let byte = |v: f64| ((v * 0.5 + 0.5) * 255.0) as u8;
for i in 0..n {
for j in 0..n {
let (x, y) = (i as f64, j as f64);
let at = (i * n + j) * 4;
data[at] = byte(simplex.noise(x, y));
data[at + 1] = byte(simplex.noise(x + s, y));
data[at + 2] = byte(simplex.noise(x, y + s));
data[at + 3] = byte(simplex.noise(x + s, y + s));
}
}
let texture = Texture::new(size, size, Some(data));
texture.set_flip_y(false);
texture.set_generate_mipmaps(false);
texture.set_min_filter(MinFilter::Nearest);
texture.set_mag_filter(TextureFilter::Nearest);
texture.set_wrapping(Wrapping::Repeat, Wrapping::Repeat);
texture
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn denoise_samples_match_three() {
let samples = generate_denoise_samples(16, 2, 1.0);
assert_eq!(samples.len(), 16);
assert_eq!(samples[0], [1.0, 0.0, 0.0]);
let [x, y, r] = samples[2];
assert!((x - (std::f64::consts::PI / 2.0).cos()).abs() < 1e-15);
assert!((y - 1.0).abs() < 1e-15);
assert!((r - 2.0 / 15.0).abs() < 1e-15);
assert_eq!(samples[15][2], 1.0);
}
}