use std::cell::RefCell;
use std::rc::Rc;
use three_rs::addons::controls::OrbitControls;
use three_rs::loaders::{GltfLoader, HdrLoader};
use three_rs::materials::{render_output, ToneMapping};
use three_rs::nodes::display::{
convert_to_texture, recurrent_denoise, sharpen, ssr, temporal_reproject, traa,
DenoiseAlphaSource, DenoiseMode, RecurrentDenoiseNode, RecurrentDenoiseOptions, RttNode,
SampleFn, SharpenNode, SsrNode, SsrOptions, TemporalReprojectMode, TemporalReprojectNode,
TemporalReprojectOptions, TraaNode,
};
use three_rs::nodes::mrt;
use three_rs::nodes::node::SettableValue;
use three_rs::nodes::pmrem_node::PmremEnvironment;
use three_rs::nodes::tsl::{
diffuse_color, float, material_metalness_value, material_roughness_value, normal_view,
output_property, pack_normal_to_rgb, saturation, texture_uv, to_var, uniform_settable,
vec2_join, vec4_join,
};
use three_rs::nodes::velocity::velocity;
use three_rs::nodes::Type;
use three_rs::objects::Background;
use three_rs::renderer::cube_render_target;
use three_rs::textures::TextureType;
use three_rs::{
pass, Color, DirectionalLight, PassNode, PerspectiveCamera, RenderPipeline, Renderer,
RendererParameters, Scene,
};
pub const INNER_WIDTH: f64 = 800.0;
pub const INNER_HEIGHT: f64 = 500.0;
pub const DPR: f64 = 1.0;
pub struct SsrParams {
pub quality: f64,
pub mirror_bias: f64,
pub max_distance: f64,
pub intensity: f64,
pub thickness: f64,
pub max_luminance: f64,
pub binary_refine: bool,
pub step_exponent: f64,
pub env_importance_sampling: bool,
pub screen_edge_fade: f64,
pub screen_edge_fade_black: bool,
pub environment_intensity: f64,
}
pub struct TemporalReprojectParams {
pub max_frames: f64,
pub clamp_intensity: f64,
pub flicker_suppression: f64,
pub hit_point_reprojection: bool,
}
pub struct DenoiseParams {
pub enabled: bool,
pub luma_phi: f64,
pub depth_phi: f64,
pub normal_phi: f64,
pub roughness_phi: f64,
pub radius: f64,
pub alpha_phi: f64,
pub strength: f64,
pub adapt: f64,
pub smooth_disocclusions: bool,
pub flicker_suppression: f64,
pub adaptive_trust: f64,
}
pub struct GradingParams {
pub exposure: f64,
pub gamma: f64,
pub contrast: f64,
pub saturation: f64,
}
pub struct Params {
pub roughness: f64,
pub ssr: SsrParams,
pub temporal_reproject: TemporalReprojectParams,
pub denoise: DenoiseParams,
pub grading: GradingParams,
}
#[allow(clippy::approx_constant)]
pub const PARAMS: Params = Params {
roughness: 0.3,
ssr: SsrParams {
quality: 0.25,
mirror_bias: 0.5,
max_distance: 0.4,
intensity: 1.0,
thickness: 0.1,
max_luminance: 35.0,
binary_refine: false,
step_exponent: 3.0,
env_importance_sampling: false,
screen_edge_fade: 0.2,
screen_edge_fade_black: false,
environment_intensity: 3.14,
},
temporal_reproject: TemporalReprojectParams {
max_frames: 16.0,
clamp_intensity: 0.25,
flicker_suppression: 1.0,
hit_point_reprojection: true,
},
denoise: DenoiseParams {
enabled: true,
luma_phi: 0.75,
depth_phi: 20.0,
normal_phi: 0.3,
roughness_phi: 100.0,
radius: 1.5,
alpha_phi: 5.0,
strength: 0.725,
adapt: 0.5,
smooth_disocclusions: true,
flicker_suppression: 1.0,
adaptive_trust: 1.0,
},
grading: GradingParams {
exposure: 1.57,
gamma: 0.89,
contrast: 1.31,
saturation: 1.0,
},
};
fn examples_dir() -> std::path::PathBuf {
three_rs::testing::three_js_dir().join("examples")
}
fn flag(value: bool) -> Vec<f64> {
vec![if value { 1.0 } else { 0.0 }]
}
pub struct App {
pub renderer: Renderer,
pub scene: Rc<RefCell<Scene>>,
pub camera: Rc<RefCell<PerspectiveCamera>>,
pub controls: OrbitControls,
pub environment: PmremEnvironment,
pub scene_pass: PassNode,
pub ssr_node: SsrNode,
pub temporal_reproject_node: TemporalReprojectNode,
pub denoise_node: RecurrentDenoiseNode,
pub graded: RttNode,
pub traa_node: TraaNode,
pub sharpen_node: SharpenNode,
pub gamma: SettableValue,
pub contrast: SettableValue,
pub saturation: SettableValue,
pub render_pipeline: RenderPipeline,
}
#[inline(never)]
fn apply_grading(
source: three_rs::nodes::NodeRef,
contrast: three_rs::nodes::NodeRef,
saturation_adjustment: three_rs::nodes::NodeRef,
gamma: three_rs::nodes::NodeRef,
) -> three_rs::nodes::NodeRef {
let rgb = source.rgb();
let rgb = render_output(vec4_join(vec![rgb, float(1.0)]), ToneMapping::AgX).rgb();
let rgb = rgb.sub(0.5).mul(contrast).add(0.5);
let rgb = saturation(rgb, saturation_adjustment);
let rgb = rgb.max(0.0).pow(float(1.0).div(gamma));
vec4_join(vec![rgb, float(1.0)])
}
pub fn init() -> App {
let mut camera = PerspectiveCamera::new(35.0, INNER_WIDTH / INNER_HEIGHT, 0.1, 8.0);
camera.node.borrow_mut().position.set(
0.9210513838983053,
0.16195074025403253,
0.431687274895316,
);
let mut scene = Scene::new();
let gltf = GltfLoader::load(examples_dir().join("models/gltf/dungeon_warkarma.glb"))
.expect("dungeon_warkarma.glb");
gltf.scene.borrow_mut().scale.multiply_scalar(0.1);
gltf.scene.traverse(&mut |child| {
let mut object = child.borrow_mut();
let Some(mesh) = object.mesh_mut() else {
return;
};
if mesh.material.is_none() && mesh.materials.is_empty() {
return;
}
for material in mesh.material.iter_mut().chain(mesh.materials.iter_mut()) {
material.roughness = PARAMS.roughness;
material.normal_map = None;
material.environment_specular = false;
}
object.cast_shadow = true;
object.receive_shadow = true;
});
scene.add(&gltf.scene);
let mut renderer = Renderer::new(RendererParameters::default()).unwrap();
renderer.set_pixel_ratio(DPR);
renderer.set_size(INNER_WIDTH, INNER_HEIGHT);
renderer.tone_mapping = ToneMapping::AgX;
renderer.tone_mapping_exposure = PARAMS.grading.exposure;
renderer.shadow_map_enabled = true;
let hdr_texture = HdrLoader::new()
.load(examples_dir().join("textures/equirectangular/quarry_01_1k.hdr"))
.expect("quarry_01_1k.hdr");
hdr_texture.set_generate_mipmaps(true);
let background = cube_render_target::from_equirectangular_texture(&mut renderer, &hdr_texture)
.expect("the equirectangular background converts to a cube");
scene.background = Some(Background::CubeTexture(background));
let mut environment = PmremEnvironment::from_equirectangular(&hdr_texture);
environment.update(&mut renderer).unwrap();
scene.environment = Some(environment.handle());
let directional_light = DirectionalLight::new(Color::from_hex(0xffffff), 20.0);
{
let mut object = directional_light.borrow_mut();
object.position.set(-10.9, 2.2, 10.75);
object.cast_shadow = true;
let shadow = object.light_mut().unwrap().shadow.as_mut().unwrap();
shadow.map_size.x = 4096.0;
shadow.map_size.y = 4096.0;
shadow.camera.set_bounds(-1.75, 1.75, 1.75, -1.75);
shadow.camera.set_near(0.1);
shadow.camera.set_far(50.0);
shadow.bias = -0.0005;
}
scene.add(&directional_light);
scene.environment_intensity = 1.0;
let mut controls = OrbitControls::new(&mut camera);
controls.set_element_size(INNER_WIDTH, INNER_HEIGHT);
controls.enable_damping = true;
controls.update(&mut camera, None);
{
let mut node = camera.node.borrow_mut();
node.position
.set(1.259878548682251, 0.5391287340899181, -0.27217301481427114);
node.set_rotation(
-0.3158233106804791,
0.26820684188431526,
0.08637696823742165,
);
}
controls
.target
.set(1.0258536154689288, 0.2746440590977971, -1.0815876858987743);
let scene = Rc::new(RefCell::new(scene));
let camera = Rc::new(RefCell::new(camera));
let mut render_pipeline = RenderPipeline::new();
let scene_pass = pass(scene.clone(), camera.clone());
let mut scene_mrt = mrt(vec![("output", output_property())]);
scene_mrt.set_deferred("diffuseColor", || {
vec4_join(vec![diffuse_color().rgb(), material_metalness_value()])
});
scene_mrt.set_deferred("normal", || {
vec4_join(vec![
pack_normal_to_rgb(normal_view()).rgb(),
material_roughness_value(),
])
});
scene_mrt.set("velocity", velocity());
scene_pass.set_mrt(scene_mrt);
let scene_pass_color = scene_pass.texture_node("output");
let scene_pass_normal = scene_pass.texture_node("normal");
let _ = scene_pass.texture_node("depth");
let _ = scene_pass.texture_node("velocity");
let scene_pass_diffuse = scene_pass.texture_node("diffuseColor");
let normal_texture = scene_pass.texture_named("normal");
normal_texture.set_texture_type(TextureType::UnsignedByte);
let diffuse_texture = scene_pass.texture_named("diffuseColor");
diffuse_texture.set_texture_type(TextureType::UnsignedByte);
let velocity_texture = scene_pass.texture_named("velocity");
let depth_texture = scene_pass.depth_texture();
let scene_normal: SampleFn = {
let normal_texture = normal_texture.clone();
Rc::new(move |coord| texture_uv(&normal_texture, coord).rgb().mul(2.0).sub(1.0))
};
let scene_metal_rough: SampleFn = {
let (diffuse_texture, normal_texture) = (diffuse_texture.clone(), normal_texture.clone());
Rc::new(move |coord: three_rs::nodes::NodeRef| {
vec2_join(vec![
texture_uv(&diffuse_texture, coord.clone()).w(),
texture_uv(&normal_texture, coord).w(),
])
})
};
let scene_diffuse: SampleFn = {
let diffuse_texture = diffuse_texture.clone();
Rc::new(move |coord| texture_uv(&diffuse_texture, coord))
};
let scene_normal_packed: SampleFn = {
let normal_texture = normal_texture.clone();
Rc::new(move |coord| texture_uv(&normal_texture, coord))
};
let ssr_node = ssr(
&scene_pass.texture(),
&depth_texture,
scene_normal,
SsrOptions::new(scene_pass_diffuse.w(), Some(scene_pass_normal.w()))
.with_stochastic(true)
.with_diffuse(scene_diffuse)
.with_environment(&hdr_texture)
.with_env_importance_sampling(PARAMS.ssr.env_importance_sampling)
.with_binary_refine(PARAMS.ssr.binary_refine),
camera.clone(),
);
let temporal_reproject_node = temporal_reproject(
&ssr_node.render_target().texture(),
&depth_texture,
&normal_texture,
&velocity_texture,
camera.clone(),
TemporalReprojectOptions {
mode: TemporalReprojectMode::Specular,
accumulate: false,
..TemporalReprojectOptions::default()
},
);
let denoise_node = recurrent_denoise(
&temporal_reproject_node.texture(),
camera.clone(),
RecurrentDenoiseOptions {
depth: Some(depth_texture.clone()),
normal: Some(scene_normal_packed),
raw: Some(ssr_node.render_target().texture()),
metal_roughness: Some(scene_metal_rough),
mode: DenoiseMode::Specular,
accumulate: true,
..RecurrentDenoiseOptions::default()
},
);
denoise_node.set_alpha_source(DenoiseAlphaSource::RayLength);
ssr_node.set_history(&denoise_node.texture(), &velocity_texture);
temporal_reproject_node.set_history_texture(Some(&denoise_node.texture()));
let denoise_pass_blend = vec4_join(vec![
denoise_node.node().rgb(),
to_var(None, ssr_node.node().w().greater_than(0.0)),
]);
let (gamma_uniform, gamma) = uniform_settable(Type::F32, vec![PARAMS.grading.gamma]);
let (contrast_uniform, contrast) = uniform_settable(Type::F32, vec![PARAMS.grading.contrast]);
let (saturation_uniform, saturation) =
uniform_settable(Type::F32, vec![PARAMS.grading.saturation]);
let lit_color = scene_pass_color.rgb().add(denoise_pass_blend.rgb());
let combined_output_node = vec4_join(vec![lit_color, float(1.0)]);
let graded = convert_to_texture(apply_grading(
combined_output_node,
contrast_uniform,
saturation_uniform,
gamma_uniform,
));
let traa_node = traa(
&graded.texture(),
&depth_texture,
&velocity_texture,
camera.clone(),
);
traa_node.attach(&mut render_pipeline);
temporal_reproject_node.attach(&mut render_pipeline);
let sharpen_node = sharpen(traa_node.node(), 0.0, false);
render_pipeline.output_node = Some(sharpen_node.node());
render_pipeline.output_color_transform = false;
let app = App {
renderer,
scene,
camera,
controls,
environment,
scene_pass,
ssr_node,
temporal_reproject_node,
denoise_node,
graded,
traa_node,
sharpen_node,
gamma,
contrast,
saturation,
render_pipeline,
};
let mut app = app;
apply_params(&app);
apply_post(&mut app);
app
}
pub fn apply_params(app: &App) {
let ssr = &PARAMS.ssr;
let node = &app.ssr_node;
node.quality().set(vec![ssr.quality]);
node.mirror_bias().set(vec![ssr.mirror_bias]);
node.set_step_exponent(ssr.step_exponent);
node.set_binary_refine(ssr.binary_refine);
node.max_distance().set(vec![ssr.max_distance]);
node.intensity().set(vec![ssr.intensity]);
node.thickness().set(vec![ssr.thickness]);
node.max_luminance().set(vec![ssr.max_luminance]);
node.screen_edge_fade().set(vec![ssr.screen_edge_fade]);
node.set_screen_edge_fade_black(ssr.screen_edge_fade_black);
node.environment_intensity()
.set(vec![ssr.environment_intensity]);
let reproject = &PARAMS.temporal_reproject;
let node = &app.temporal_reproject_node;
node.max_frames().set(vec![reproject.max_frames]);
node.clamp_intensity().set(vec![reproject.clamp_intensity]);
node.flicker_suppression()
.set(vec![reproject.flicker_suppression]);
node.hit_point_reprojection()
.set(flag(reproject.hit_point_reprojection));
let denoise = &PARAMS.denoise;
let node = &app.denoise_node;
node.luma_phi().set(vec![denoise.luma_phi]);
node.depth_phi().set(vec![denoise.depth_phi]);
node.normal_phi().set(vec![denoise.normal_phi]);
node.roughness_phi().set(vec![denoise.roughness_phi]);
node.radius()
.set(vec![if denoise.enabled { denoise.radius } else { 0.0 }]);
node.alpha_phi().set(vec![denoise.alpha_phi]);
node.strength().set(vec![denoise.strength]);
node.adapt().set(vec![denoise.adapt]);
node.smooth_disocclusions()
.set(flag(denoise.smooth_disocclusions));
node.flicker_suppression()
.set(vec![denoise.flicker_suppression]);
node.adaptive_trust().set(vec![denoise.adaptive_trust]);
}
pub fn apply_post(app: &mut App) {
let grading = &PARAMS.grading;
app.gamma.set(vec![grading.gamma]);
app.contrast.set(vec![grading.contrast]);
app.saturation.set(vec![grading.saturation]);
app.renderer.tone_mapping = ToneMapping::AgX;
app.renderer.tone_mapping_exposure = grading.exposure;
}
pub fn animate(app: &mut App) {
app.controls.update(&mut app.camera.borrow_mut(), None);
app.render_pipeline.render(&mut app.renderer);
}
pub fn resize(app: &mut App, width: f64, height: f64) {
let mut camera = app.camera.borrow_mut();
camera.aspect = width / height;
camera.update_projection_matrix();
app.renderer.set_size(width, height);
app.controls.set_element_size(width, height);
}
pub fn controls(app: &mut App) -> Option<&mut OrbitControls> {
Some(&mut app.controls)
}
pub fn controls_and_camera(
app: &mut App,
) -> Option<(&mut OrbitControls, std::cell::RefMut<'_, PerspectiveCamera>)> {
Some((&mut app.controls, app.camera.borrow_mut()))
}
fn main() {
three_rs::testing::pin_time(Some(0.0));
let mut app = init();
println!("adapter: {:?}", app.renderer.adapter_info());
animate(&mut app);
let (width, height, pixels) = app.renderer.read_canvas_pixels().unwrap();
let path = std::env::args()
.nth(1)
.unwrap_or_else(|| "target/webgpu_postprocessing_ssr_denoise.png".to_string());
three_rs::testing::write_png(&path, width, height, &pixels);
println!("wrote {path} ({width}x{height})");
}