use std::rc::Rc;
use crate::materials::MeshBasicNodeMaterial;
use crate::nodes::frame::register_texture_update;
use crate::nodes::node::{SettableValue, Type};
use crate::nodes::tsl::{
abs, block, exp, float, luminance, max, texture_uv, to_const, to_var, uniform_settable, uv,
vec2,
};
use crate::nodes::{NodeRef, NodeUpdate, NodeUpdateType};
use crate::objects::QuadMesh;
use crate::renderer::{RenderTarget, RenderTargetOptions, Renderer};
use crate::textures::{Texture, TextureFilter, TextureType};
pub fn bilateral_blur(
map: &Texture,
direction: Option<NodeRef>,
sigma: u32,
sigma_color: f64,
) -> BilateralBlurNode {
BilateralBlurNode::new(map, direction, sigma, sigma_color)
}
pub struct BilateralBlurNode(Rc<BilateralBlurState>);
pub(crate) struct BilateralBlurState {
map: Texture,
horizontal: RenderTarget,
vertical: RenderTarget,
horizontal_quad: QuadMesh,
vertical_quad: QuadMesh,
inv_size: SettableValue,
resolution_scale: f64,
node: NodeRef,
}
fn spatial_coefficients(kernel_radius: u32) -> Vec<f64> {
let sigma = kernel_radius as f64 / 3.0;
(0..kernel_radius)
.map(|i| {
let i = i as f64;
0.39894 * (-0.5 * i * i / (sigma * sigma)).exp() / sigma
})
.collect()
}
fn scaled_size(size: u32, resolution_scale: f64) -> u32 {
((size as f64 * resolution_scale).round() as u32).max(1)
}
impl BilateralBlurNode {
pub fn new(map: &Texture, direction: Option<NodeRef>, sigma: u32, sigma_color: f64) -> Self {
let texture_type = match map.format() {
wgpu::TextureFormat::Rgba16Float => TextureType::HalfFloat,
_ => TextureType::UnsignedByte,
};
let target = || {
RenderTarget::new_with_options(
1,
1,
RenderTargetOptions {
texture_type,
samples: 0,
depth_buffer: false,
min_filter: TextureFilter::Linear,
mag_filter: TextureFilter::Linear,
},
)
.expect("three-rs: a bilateral blur target is a colour type")
};
let horizontal = target();
let vertical = target();
let (inv_size_node, inv_size) = uniform_settable(Type::Vec2, vec![0.0, 0.0]);
let direction = match direction {
Some(direction) => direction.to(Type::Vec2),
None => vec2(1.0, 1.0),
};
let quad = |map: &Texture, pass_direction: [f64; 2]| {
let (pass_direction, _) = uniform_settable(Type::Vec2, pass_direction.to_vec());
let mut material = MeshBasicNodeMaterial::new();
material.name = "Bilateral_blur";
material.fragment_node = Some(blur(
map,
direction.clone(),
pass_direction,
inv_size_node.clone(),
sigma,
sigma_color,
));
QuadMesh::new(material)
};
let horizontal_quad = quad(map, [1.0, 0.0]);
let vertical_quad = quad(&horizontal.texture(), [0.0, 1.0]);
let node = to_var(None, texture_uv(&vertical.texture(), uv()));
let state = Rc::new(BilateralBlurState {
map: map.clone(),
horizontal,
vertical,
horizontal_quad,
vertical_quad,
inv_size,
resolution_scale: 1.0,
node,
});
register_texture_update(state.vertical.texture().id(), &state);
Self(state)
}
pub fn node(&self) -> NodeRef {
self.0.node.clone()
}
pub fn texture(&self) -> Texture {
self.0.vertical.texture()
}
#[doc(hidden)]
pub fn quad_materials(&self) -> Vec<&MeshBasicNodeMaterial> {
vec![
&self.0.horizontal_quad.material,
&self.0.vertical_quad.material,
]
}
pub fn set_size(&self, width: u32, height: u32) {
self.0.set_size(width, height);
}
}
impl BilateralBlurState {
fn set_size(&self, width: u32, height: u32) {
let width = scaled_size(width, self.resolution_scale);
let height = scaled_size(height, self.resolution_scale);
self.inv_size
.set(vec![1.0 / width as f64, 1.0 / height as f64]);
self.horizontal.set_size(width, height);
self.vertical.set_size(width, height);
}
}
impl NodeUpdate for BilateralBlurState {
fn update_before_type(&self) -> NodeUpdateType {
NodeUpdateType::Frame
}
fn update_before(&self, renderer: &mut Renderer) -> bool {
if let Some(input) = crate::nodes::frame::texture_update(self.map.id()) {
renderer.update_before_node(&input);
}
let mut renderer = renderer.reset_state();
let (width, height) = self.map.size();
self.set_size(width, height);
renderer.set_render_target(Some(self.horizontal.clone()));
renderer.render_quad(&self.horizontal_quad);
renderer.set_render_target(Some(self.vertical.clone()));
renderer.render_quad(&self.vertical_quad);
drop(renderer);
true
}
}
fn blur(
map: &Texture,
direction: NodeRef,
pass_direction: NodeRef,
inv_size: NodeRef,
sigma: u32,
sigma_color: f64,
) -> NodeRef {
let uv_node = uv();
let kernel_size = sigma * 2 + 3;
let spatial = spatial_coefficients(kernel_size);
let direction = direction.mul(pass_direction);
let center_color = to_var(None, texture_uv(map, uv_node.clone()));
let center_luminance = to_var(None, luminance(center_color.xyz()));
let weight_sum = to_var(None, float(spatial[0]));
let color_sum = to_var(None, center_color.mul(float(spatial[0])));
let color_sigma_factor = to_const(None, float(-0.5).div(float(sigma_color * sigma_color)));
let mut statements = vec![
center_color.clone(),
center_luminance.clone(),
weight_sum.clone(),
color_sum.clone(),
color_sigma_factor.clone(),
];
for (i, &w) in spatial.iter().enumerate().skip(1) {
let x = float(i as f64);
let spatial_weight = float(w);
let uv_offset = to_var(None, direction.clone().mul(inv_size.clone().mul(x)));
let sample1 = to_var(
None,
texture_uv(map, uv_node.clone().add(uv_offset.clone())),
);
let sample2 = to_var(None, texture_uv(map, uv_node.clone().sub(uv_offset)));
let diff1 = abs(luminance(sample1.xyz()).sub(center_luminance.clone()));
let diff2 = abs(luminance(sample2.xyz()).sub(center_luminance.clone()));
let color_weight1 = to_var(
None,
exp(diff1.clone().mul(diff1).mul(color_sigma_factor.clone())),
);
let color_weight2 = to_var(
None,
exp(diff2.clone().mul(diff2).mul(color_sigma_factor.clone())),
);
let bilateral_weight1 = spatial_weight.clone().mul(color_weight1.clone());
let bilateral_weight2 = spatial_weight.mul(color_weight2.clone());
statements.push(color_weight1);
statements.push(color_weight2);
statements.push(color_sum.add_assign(sample1.mul(bilateral_weight1.clone())));
statements.push(color_sum.add_assign(sample2.mul(bilateral_weight2.clone())));
statements.push(weight_sum.add_assign(bilateral_weight1));
statements.push(weight_sum.add_assign(bilateral_weight2));
}
block(statements, color_sum.div(max(weight_sum, float(0.0001))))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sigma_4_matches_the_dumped_coefficients() {
let c = spatial_coefficients(4 * 2 + 3);
assert_eq!(c.len(), 11);
assert_eq!(c[0], 0.1088018181818182);
assert_eq!(c[1], 0.10482978744722705);
assert_eq!(c[10], 0.002639315969304918);
}
#[test]
fn set_size_rounds_and_clamps_to_one() {
assert_eq!(scaled_size(400, 1.0), 400);
assert_eq!(scaled_size(0, 1.0), 1);
}
}