From 8b8655a6f3c46c59150901245cb6e8d0b42b55e2 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Sun, 1 Nov 2026 12:07:48 +0700 Subject: [PATCH 1/2] Displacement in the path tracers Rendering mode traces displaced geometry when the display has displacement on, so path-traced renders show the viewport's silhouettes and the displaced surface shadows itself. - render::displace: the viewport's dense subdivision of the mesh, each vertex moved along its normal by its set's height (box-filtered to the mip the vertex shader samples) times the height strength, at the viewport's scale (world length per UV / 16) - trace_geometry gives the GPU and CPU tracers the displaced mesh and BVH; Rendering mode, texturecraft-cli render --samples and MCP viewport_render use it - Rendering mode keeps the traced geometry until the mesh, a set's content or a height strength changes (display tweaks don't rebuild it) - subdivide::vertices shared by the renderer's mesh upload and displacement - test: a uniform height lifts the dense plane by height x strength x world per UV / 16 --- README.md | 2 +- crates/automation/src/render.rs | 6 +- crates/render/src/displace.rs | 164 ++++++++++++++++++++++++++++++ crates/render/src/lib.rs | 1 + crates/render/src/renderer.rs | 9 +- crates/render/src/subdivide.rs | 14 +++ crates/ui-egui/src/render_mode.rs | 25 ++++- docs/features.md | 6 +- 8 files changed, 211 insertions(+), 16 deletions(-) create mode 100644 crates/render/src/displace.rs diff --git a/README.md b/README.md index 2c0f010c..da93263f 100644 --- a/README.md +++ b/README.md @@ -191,7 +191,7 @@ from our code against public documentation. - hardware ray tracing in Rendering mode; - pen tilt (the windowing layer doesn't report it yet); - Alembic files in the older HDF5 container, and Alembic animation (the first sample is read); -- displacement in shadows and the path tracer (the viewport displaces); +- displacement in the viewport's key-light shadows (the viewport and the path tracers displace); - a native-speaker review of the translations; - the first signed release on every platform. diff --git a/crates/automation/src/render.rs b/crates/automation/src/render.rs index cbc834f5..e98ccbf9 100644 --- a/crates/automation/src/render.rs +++ b/crates/automation/src/render.rs @@ -4,6 +4,7 @@ use rayon::prelude::*; use texturecraft_engine::compose::{DisplayKind, all_tiles, tile_bytes}; use texturecraft_engine::{Session, ViewMode}; +use texturecraft_render::displace::trace_geometry; use texturecraft_render::gpu_trace::GpuTracer; use texturecraft_render::trace::{TraceDisplay, TraceScene, TraceSet, TraceSettings, Tracer}; use texturecraft_render::{Frame, Shade, ViewKind}; @@ -112,7 +113,7 @@ impl Gpu { /// The session's 3D view as the path tracers see it, and how Rendering mode shows it (denoised). fn trace_inputs(s: &mut Session) -> (TraceScene, TraceDisplay) { s.update(); - let sets = s + let sets: Vec = s .doc .project .texture_sets @@ -122,7 +123,8 @@ fn trace_inputs(s: &mut Session) -> (TraceScene, TraceDisplay) { .collect(); let d = &s.doc.project.display; let env_image = d.environment_resource().and_then(|n| s.images.get(n)); - let scene = TraceScene::for_display(s.mesh.clone(), s.bvh.clone(), sets, d, env_image.map(|i| i.as_ref())); + let (mesh, bvh) = trace_geometry(&s.mesh, &s.bvh, &sets, d); + let scene = TraceScene::for_display(mesh, bvh, sets, d, env_image.map(|i| i.as_ref())); (scene, TraceDisplay { denoise: true, ..TraceDisplay::from_display(d) }) } diff --git a/crates/render/src/displace.rs b/crates/render/src/displace.rs new file mode 100644 index 00000000..287898c7 --- /dev/null +++ b/crates/render/src/displace.rs @@ -0,0 +1,164 @@ +//! Displacement for the path tracers: the mesh's dense copy (the one the viewport displaces in its +//! vertex shader) moved along its normals by the height channel, as the viewport moves it, so +//! Rendering mode shows the same silhouettes and the displaced surface casts its own shadows. + +use std::sync::Arc; + +use texturecraft_core::Channel; +use texturecraft_core::project::DisplaySettings; +use texturecraft_geom::glam::{Vec2, Vec3, Vec4}; +use texturecraft_geom::{Bvh, Mesh, SubMesh}; + +use crate::subdivide::{self, Draw, Vertex}; +use crate::trace::TraceSet; + +/// The geometry the path tracers trace for `display`: displaced when the display displaces and a +/// set has height to displace by, else `mesh` and `bvh` as they are. +pub fn trace_geometry(mesh: &Arc, bvh: &Arc, sets: &[TraceSet], display: &DisplaySettings) -> (Arc, Arc) { + match display.displacement.then(|| displaced(mesh, sets)).flatten() { + Some(m) => { + let bvh = Arc::new(Bvh::new(&m)); + (Arc::new(m), bvh) + } + None => (mesh.clone(), bvh.clone()), + } +} + +/// `mesh` subdivided like the viewport's dense copy and displaced by the height of the set over +/// each material (None when no set has height and a height strength). +pub fn displaced(mesh: &Mesh, sets: &[TraceSet]) -> Option { + if !sets.iter().any(|s| s.height_strength != 0.0 && s.composite.channels.contains_key(&Channel::Height)) { + return None; + } + let verts = subdivide::vertices(mesh); + let draws: Vec = if mesh.submeshes.is_empty() { + vec![(0, u32::try_from(mesh.indices.len()).ok()?, 0)] + } else { + mesh.submeshes.iter().map(|s| (s.start, s.count, s.material)).collect() + }; + let (lo, hi) = mesh.bounds(); + let world_per_uv = subdivide::world_per_uv(&verts, &mesh.indices).unwrap_or(((hi - lo).length() * 0.5).max(1e-3)); + let (dense, indices, dense_draws) = subdivide::subdivide(&verts, &mesh.indices, &draws, subdivide::level_for(mesh.triangle_count())); + // UV distance between neighbouring vertices: the viewport samples the mip that matches it. + let spacing = 1.0 / ((indices.len() / 3).max(1) as f32 * 0.5).sqrt(); + let heights: Vec> = sets.iter().map(|s| Heights::new(s, spacing)).collect(); + // Every dense vertex belongs to one triangle grid, so to one draw. + let mut material = vec![usize::MAX; dense.len()]; + for &(start, count, m) in &dense_draws { + for i in indices.get(start as usize..(start as usize).saturating_add(count as usize)).unwrap_or_default() { + if let Some(slot) = material.get_mut(*i as usize) { + *slot = m; + } + } + } + let lift = |v: &Vertex, m: usize| -> Vec3 { + let p = Vec3::from(v.pos); + let Some((set, h)) = sets.iter().zip(&heights).find(|(s, _)| s.material == m) else { return p }; + let n = Vec3::from(v.nrm).normalize_or_zero(); + p + n * (h.as_ref().map_or(0.0, |h| h.sample(Vec2::from(v.uv))) * set.height_strength * world_per_uv / 16.0) + }; + let positions: Vec = dense.iter().zip(&material).map(|(v, m)| lift(v, *m)).collect(); + let names = |m: usize| mesh.submeshes.iter().find(|s| s.material == m).map(|s| s.name.clone()).unwrap_or_default(); + Some(Mesh { + positions, + normals: dense.iter().map(|v| Vec3::from(v.nrm)).collect(), + tangents: dense.iter().map(|v| Vec4::from(v.tan)).collect(), + uvs: dense.iter().map(|v| Vec2::from(v.uv)).collect(), + colors: Vec::new(), + submeshes: if mesh.submeshes.is_empty() { + Vec::new() + } else { + dense_draws.iter().map(|&(start, count, m)| SubMesh { name: names(m), material: m, start, count }).collect() + }, + indices, + materials: mesh.materials.clone(), + }) +} + +/// A set's height, box-filtered by `k` texels (the mip level the viewport samples). +struct Heights { + w: usize, + h: usize, + data: Vec, +} + +impl Heights { + fn new(s: &TraceSet, spacing: f32) -> Option { + if s.height_strength == 0.0 || !s.composite.channels.contains_key(&Channel::Height) { + return None; + } + let (w0, h0) = (s.composite.resolution.max(1), s.composite.height.max(1)); + let mip = (w0 as f32 * spacing).max(1.0).log2().floor().clamp(0.0, 6.0) as u32; + let k = 1u32 << mip; + let (w, h) = ((w0 / k).max(1), (h0 / k).max(1)); + let mut data = Vec::with_capacity((w * h) as usize); + for y in 0..h { + for x in 0..w { + let mut sum = 0.0; + for dy in 0..k { + for dx in 0..k { + sum += s.composite.value(Channel::Height, (x * k + dx).min(w0 - 1), (y * k + dy).min(h0 - 1))[0]; + } + } + data.push(sum / (k * k) as f32); + } + } + Some(Heights { w: w as usize, h: h as usize, data }) + } + + /// Bilinear at `uv`, clamped at the edges (the viewport's height sampler). + fn sample(&self, uv: Vec2) -> f32 { + let at = |x: isize, y: isize| { + let (x, y) = (x.clamp(0, self.w as isize - 1) as usize, y.clamp(0, self.h as isize - 1) as usize); + self.data.get(y * self.w + x).copied().unwrap_or(0.0) + }; + let (fx, fy) = (uv.x * self.w as f32 - 0.5, uv.y * self.h as f32 - 0.5); + if !(fx.is_finite() && fy.is_finite()) { + return 0.0; + } + let (x0, y0) = (fx.floor(), fy.floor()); + let (tx, ty) = (fx - x0, fy - y0); + let (x0, y0) = (x0 as isize, y0 as isize); + let top = at(x0, y0) * (1.0 - tx) + at(x0 + 1, y0) * tx; + let bottom = at(x0, y0 + 1) * (1.0 - tx) + at(x0 + 1, y0 + 1) * tx; + top * (1.0 - ty) + bottom * ty + } +} + +#[cfg(test)] +mod tests { + use texturecraft_compose::Composite; + use texturecraft_geom::primitives; + + use super::*; + + /// A plane's set with a uniform height. + fn set(height: f32, strength: f32) -> TraceSet { + let mut composite = Composite::new(&[Channel::BaseColor, Channel::Height], 64); + if let Some(img) = composite.channels.get_mut(&Channel::Height) { + for (x, y) in texturecraft_compose::all_tiles(64, 64) { + img.tile_mut(x, y, &[texturecraft_texel::to_u16_signed(height)]); + } + } + TraceSet { material: 0, composite, height_strength: strength } + } + + #[test] + fn uniform_height_lifts_the_dense_plane_along_its_normal() { + let plane = primitives::plane(2.0, 1); + assert!(displaced(&plane, &[set(0.5, 0.0)]).is_none(), "no strength, nothing to displace"); + assert!(displaced(&plane, &[]).is_none()); + let m = displaced(&plane, &[set(0.5, 2.0)]).unwrap(); + assert!(m.triangle_count() > plane.triangle_count()); + assert_eq!((m.positions.len(), m.uvs.len(), m.normals.len()), (m.tangents.len(), m.positions.len(), m.positions.len())); + // world per UV is 2 (a 2 × 2 plane over the UV square): 0.5 × 2 × 2 / 16. + let n = plane.normals[0]; + let lift = m.positions.iter().map(|p| p.dot(n)).collect::>(); + assert!(lift.iter().all(|d| (d - 0.125).abs() < 1e-3), "{:?}", &lift[..4]); + let off = DisplaySettings::default(); + let (mesh, bvh) = (Arc::new(plane), Arc::new(Bvh::new(&primitives::plane(2.0, 1)))); + assert!(Arc::ptr_eq(&trace_geometry(&mesh, &bvh, &[set(0.5, 2.0)], &off).0, &mesh), "off unless the display displaces"); + let on = DisplaySettings { displacement: true, ..DisplaySettings::default() }; + assert!(!Arc::ptr_eq(&trace_geometry(&mesh, &bvh, &[set(0.5, 2.0)], &on).0, &mesh)); + } +} diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index 41f23131..01df6c30 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -13,6 +13,7 @@ #![forbid(unsafe_code)] #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +pub mod displace; #[cfg(not(target_arch = "wasm32"))] pub mod gpu_bake; pub mod gpu_trace; diff --git a/crates/render/src/renderer.rs b/crates/render/src/renderer.rs index 20080f31..547d4946 100644 --- a/crates/render/src/renderer.rs +++ b/crates/render/src/renderer.rs @@ -1010,14 +1010,7 @@ impl Renderer { if self.mesh.as_ref().is_some_and(|m| m.id == id) { return; } - let verts: Vec = (0..mesh.positions.len()) - .map(|i| Vertex { - pos: mesh.positions[i].to_array(), - nrm: mesh.normals.get(i).map(|n| n.to_array()).unwrap_or([0.0, 1.0, 0.0]), - tan: mesh.tangents.get(i).map(|t| t.to_array()).unwrap_or([1.0, 0.0, 0.0, 1.0]), - uv: mesh.uvs.get(i).map(|u| u.to_array()).unwrap_or([0.0, 0.0]), - }) - .collect(); + let verts = subdivide::vertices(mesh); let mk = |label: &str, data: &[u8], usage: wgpu::BufferUsages| upload_buffer(device, queue, label, data, usage); let mut idx_bytes = bytemuck::cast_slice::(&mesh.indices).to_vec(); idx_bytes.resize(idx_bytes.len().next_multiple_of(4), 0); diff --git a/crates/render/src/subdivide.rs b/crates/render/src/subdivide.rs index c420aad8..d08a82f2 100644 --- a/crates/render/src/subdivide.rs +++ b/crates/render/src/subdivide.rs @@ -23,6 +23,20 @@ pub(crate) const DENSE_TRIANGLES: usize = 1 << 19; /// Finest split: 64 × 64 segments per edge (for very coarse meshes such as a cube). const MAX_LEVEL: u32 = 6; +/// The vertices of `mesh` as the viewport draws them (missing attributes get defaults). +pub(crate) fn vertices(mesh: &texturecraft_geom::Mesh) -> Vec { + mesh.positions + .iter() + .enumerate() + .map(|(i, p)| Vertex { + pos: p.to_array(), + nrm: mesh.normals.get(i).map(|n| n.to_array()).unwrap_or([0.0, 1.0, 0.0]), + tan: mesh.tangents.get(i).map(|t| t.to_array()).unwrap_or([1.0, 0.0, 0.0, 1.0]), + uv: mesh.uvs.get(i).map(|u| u.to_array()).unwrap_or([0.0, 0.0]), + }) + .collect() +} + /// Subdivision level for a mesh of `triangles` (0: dense enough as it is). pub(crate) fn level_for(triangles: usize) -> u32 { (0..=MAX_LEVEL).rev().find(|l| triangles.saturating_mul(1 << (2 * l)) <= DENSE_TRIANGLES).unwrap_or(0) diff --git a/crates/ui-egui/src/render_mode.rs b/crates/ui-egui/src/render_mode.rs index 0e474fb8..8228f51a 100644 --- a/crates/ui-egui/src/render_mode.rs +++ b/crates/ui-egui/src/render_mode.rs @@ -14,7 +14,8 @@ use std::sync::{Mutex, PoisonError}; use eframe::egui_wgpu::{self, CallbackResources, CallbackTrait, ScreenDescriptor}; use egui::{Color32, ColorImage, Rect, RichText, TextureHandle, TextureOptions, Ui, vec2}; -use texturecraft_engine::geom::OrbitCamera; +use texturecraft_engine::geom::{Bvh, Mesh, OrbitCamera}; +use texturecraft_render::displace::trace_geometry; use texturecraft_render::gpu_trace::GpuTracer; use texturecraft_render::readback::Readback; use texturecraft_render::trace::{TraceDisplay, TraceScene, TraceSet, TraceSettings, Tracer}; @@ -145,6 +146,9 @@ struct TraceState { resolved: Option, } +/// Traced geometry and the key of what it was built from. +type Geometry = (String, Arc, Arc); + /// Rendering mode state kept by the app. #[derive(Default)] pub struct RenderView { @@ -156,6 +160,9 @@ pub struct RenderView { shown: Option<(u32, u32, Vec)>, samples: u32, elapsed: f64, + /// The traced geometry (displaced or not) and what it was built from: changes to the display + /// alone don't rebuild it. + geometry: Mutex>, } /// Viewport resolution of a CPU render (the web renders at half size: it has one thread). @@ -167,7 +174,7 @@ fn render_size(rect: Rect) -> (u32, u32) { fn trace_scene(app: &TextureCraftApp) -> TraceScene { let s = &app.session; let d = &s.doc.project.display; - let sets = s + let sets: Vec = s .doc .project .texture_sets @@ -176,7 +183,19 @@ fn trace_scene(app: &TextureCraftApp) -> TraceScene { .map(|(ts, c)| TraceSet { material: ts.material, composite: c.composite.clone(), height_strength: ts.atlas_height_strength() }) .collect(); let env_image = d.environment_resource().and_then(|n| s.images.get(n)); - TraceScene::for_display(s.mesh.clone(), s.bvh.clone(), sets, d, env_image.map(|i| i.as_ref())) + let revisions: Vec = s.caches.iter().map(|c| c.revision).collect(); + let strengths: Vec = sets.iter().map(|t| t.height_strength).collect(); + let key = format!("{}|{revisions:?}|{strengths:?}|{}", s.mesh_id, d.displacement); + let mut cached = app.render.geometry.lock().unwrap_or_else(PoisonError::into_inner); + let (mesh, bvh) = match cached.as_ref() { + Some((k, m, b)) if *k == key => (m.clone(), b.clone()), + _ => { + let (m, b) = trace_geometry(&s.mesh, &s.bvh, &sets, d); + *cached = Some((key, m.clone(), b.clone())); + (m, b) + } + }; + TraceScene::for_display(mesh, bvh, sets, d, env_image.map(|i| i.as_ref())) } fn settings(app: &TextureCraftApp) -> TraceSettings { diff --git a/docs/features.md b/docs/features.md index eef951df..e266545a 100644 --- a/docs/features.md +++ b/docs/features.md @@ -75,12 +75,14 @@ Everything TextureCraft does today, by area. What's still missing is in [`ROADMA ## The viewport - Physically based shading (metallic/roughness or specular/glossiness) with clear coat, sheen, - anisotropy, subsurface scattering, transmission and parallax occlusion; image-based lighting from + anisotropy, subsurface scattering, transmission, parallax occlusion and **displacement** from the + height channel; image-based lighting from six procedural studio and outdoor environments or **your own HDR/EXR panoramas**; ACES or linear tone mapping, key-light shadows, bloom, vignette, sharpen, grain, **depth of field** and **saved cameras** (also imported from glTF, FBX, COLLADA and USD files). - **Rendering mode** (F10): a progressive, **path-traced** view of your textures with multi-bounce - light, environment importance sampling, a denoiser and one-click PNG save, traced on the GPU + light, environment importance sampling, displaced geometry, a denoiser and one-click PNG save, + traced on the GPU (compute shaders, about 25× the CPU tracer, desktop and WebGPU) or on the CPU. - View any channel, any baked mesh map, or a UV checker; Alt+drag to orbit, middle-drag to pan, wheel to zoom, F to frame, M / C / B to cycle views. From e2637115dff1eb748ec7a24c9176a298de604efe Mon Sep 17 00:00:00 2001 From: bflatastic Date: Sun, 1 Nov 2026 12:17:27 +0700 Subject: [PATCH 2/2] Displacement in the viewport's key-light shadows The shadow pass draws the dense copy and displaces it like the 3D view, so a displaced surface shadows itself instead of being shadowed as if it were still smooth. - shaders/displace.wgsl: displace_by_height, shared by mesh.wgsl and shadow.wgsl - the shadow pipeline takes the set bind group (height and sampler) and a displacement vec4 (on, world units per unit of height with the height strength, vertex spacing) - while it displaces, the cached shadow map is re-rendered when the set textures change - test: with flat height, displaced shadows match the plain mesh's --- README.md | 1 - crates/render/src/renderer.rs | 69 +++++++++++++++---------- crates/render/src/shaders/displace.wgsl | 9 ++++ crates/render/src/shaders/mesh.wgsl | 8 +-- crates/render/src/shaders/shadow.wgsl | 20 +++++-- crates/render/tests/headless.rs | 15 ++++++ docs/features.md | 2 +- 7 files changed, 86 insertions(+), 38 deletions(-) create mode 100644 crates/render/src/shaders/displace.wgsl diff --git a/README.md b/README.md index da93263f..6cf2da04 100644 --- a/README.md +++ b/README.md @@ -191,7 +191,6 @@ from our code against public documentation. - hardware ray tracing in Rendering mode; - pen tilt (the windowing layer doesn't report it yet); - Alembic files in the older HDF5 container, and Alembic animation (the first sample is read); -- displacement in the viewport's key-light shadows (the viewport and the path tracers displace); - a native-speaker review of the translations; - the first signed release on every platform. diff --git a/crates/render/src/renderer.rs b/crates/render/src/renderer.rs index 547d4946..5fd4ac14 100644 --- a/crates/render/src/renderer.rs +++ b/crates/render/src/renderer.rs @@ -427,8 +427,9 @@ struct ViewTargets { struct ShadowMap { view: wgpu::TextureView, size: u32, - /// What the map was rendered for: mesh id, light direction bits, visible sets. - key: Option<(u64, [u32; 3])>, + /// What the map was rendered for: mesh id, light direction bits, displacement (its + /// parameters' bits and, when on, the content revision), visible sets. + key: Option<(u64, [u32; 3], [u32; 4], u64)>, visible: Vec, vp: Mat4, buf: wgpu::Buffer, @@ -730,10 +731,10 @@ impl Renderer { let shader = |label: &str, source: &str| { device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some(label), source: wgpu::ShaderSource::Wgsl(source.into()) }) }; - let module = shader("tc_mesh", include_str!("shaders/mesh.wgsl")); + let module = shader("tc_mesh", concat!(include_str!("shaders/displace.wgsl"), include_str!("shaders/mesh.wgsl"))); let util_module = shader("tc_util", include_str!("shaders/util.wgsl")); let post_module = shader("tc_post", concat!(include_str!("shaders/util.wgsl"), include_str!("shaders/post.wgsl"), include_str!("shaders/fx.wgsl"))); - let shadow_module = shader("tc_shadow", include_str!("shaders/shadow.wgsl")); + let shadow_module = shader("tc_shadow", concat!(include_str!("shaders/displace.wgsl"), include_str!("shaders/shadow.wgsl"))); let bgl = |label: &str, entries: &[wgpu::BindGroupLayoutEntry]| { device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { label: Some(label), entries }) }; @@ -782,7 +783,7 @@ impl Renderer { let bg_layout = pl("tc_bg", &[Some(&globals_bgl)]); let blit_layout = pl("tc_blit", &[Some(&blit_bgl)]); let fx_layout = pl("tc_fx", &[Some(&fx_bgl)]); - let shadow_layout = pl("tc_shadow", &[Some(&shadow_bgl)]); + let shadow_layout = pl("tc_shadow", &[Some(&shadow_bgl), Some(&set_bgl)]); let vb = [Some(wgpu::VertexBufferLayout { array_stride: std::mem::size_of::() as u64, step_mode: wgpu::VertexStepMode::Vertex, @@ -888,7 +889,7 @@ impl Renderer { }); let shadow_buf = device.create_buffer(&wgpu::BufferDescriptor { label: Some("tc_shadow"), - size: std::mem::size_of::<[[f32; 4]; 4]>() as u64, + size: std::mem::size_of::<[[f32; 4]; 5]>() as u64, usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, mapped_at_creation: false, }); @@ -1420,11 +1421,22 @@ impl Renderer { } /// Keep the key-light shadow map current; returns its view-projection. `light` points to - /// the light (world space). Re-renders only when the mesh, light or visible sets changed. + /// the light (world space), `disp` is the shadow pass's displacement (see shadow.wgsl). + /// Re-renders only when the mesh, light, displacement or visible sets changed (and, when it + /// displaces, the set textures). /// shortcut: casters are fully opaque (the dithered opacity cut-out is ignored) and the map is /// one shared buffer, so two 3D views with different lights in one frame would share the last /// light; upgrade to a per-view map if split 3D views get their own lights. - fn update_shadow(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, encoder: &mut wgpu::CommandEncoder, light: Vec3, visible: &[bool]) -> Option { + #[allow(clippy::too_many_arguments)] + fn update_shadow( + &mut self, + device: &wgpu::Device, + queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, + light: Vec3, + disp: [f32; 4], + visible: &[bool], + ) -> Option { let l = light.try_normalize()?; let (id, center, radius) = self.mesh.as_ref().map(|m| (m.id, m.center, m.radius))?; if self.shadow.size != SHADOW_SIZE { @@ -1435,14 +1447,18 @@ impl Renderer { v.globals_bg = None; } } - let key = (id, l.to_array().map(f32::to_bits)); + let displacing = disp[0] > 0.5; + let key = (id, l.to_array().map(f32::to_bits), disp.map(f32::to_bits), if displacing { self.content_rev } else { 0 }); if self.shadow.key == Some(key) && self.shadow.visible == visible { return Some(self.shadow.vp); } // An orthographic light frustum around the mesh's bounding sphere. let up = if l.y.abs() > 0.99 { Vec3::Z } else { Vec3::Y }; let vp = Mat4::orthographic_rh(-radius, radius, -radius, radius, radius * 0.5, radius * 3.5) * Mat4::look_at_rh(center + l * radius * 2.0, center, up); - queue.write_buffer(&self.shadow.buf, 0, bytemuck::bytes_of(&vp.to_cols_array_2d())); + let mut raw = [[0.0f32; 4]; 5]; + raw[..4].copy_from_slice(&vp.to_cols_array_2d()); + raw[4] = disp; + queue.write_buffer(&self.shadow.buf, 0, bytemuck::bytes_of(&raw)); let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { label: Some("tc_shadow"), color_attachments: &[], @@ -1457,12 +1473,12 @@ impl Renderer { }); pass.set_pipeline(&self.shadow_pipeline); pass.set_bind_group(0, &self.shadow.bg, &[]); - // shortcut: casters are the base mesh (not displaced); a displaced shadow pass would - // need the set textures in shadow.wgsl. if let Some(m) = &self.mesh { - pass.set_vertex_buffer(0, m.geo.vb.slice(..)); - pass.set_index_buffer(m.geo.ib.slice(..), wgpu::IndexFormat::Uint32); - for (range, _) in self.drawable(&m.geo.draws, visible, None) { + let geo = m.dense.as_ref().filter(|_| displacing).unwrap_or(&m.geo); + pass.set_vertex_buffer(0, geo.vb.slice(..)); + pass.set_index_buffer(geo.ib.slice(..), wgpu::IndexFormat::Uint32); + for (range, set) in self.drawable(&geo.draws, visible, None) { + pass.set_bind_group(1, &set.bind_group, &[]); pass.draw_indexed(range, 0, 0..1); } } @@ -1517,7 +1533,18 @@ impl Renderer { let (sin, cos) = s.env_rotation_deg.to_radians().sin_cos(); let [lx, ly, lz, lw] = s.light; let light = Vec3::new(cos * lx + sin * lz, ly, cos * lz - sin * lx); - let shadow_vp = if !data && s.shadows && lw > 0.0 { self.update_shadow(device, queue, encoder, light, &frame.visible) } else { None }; + let displace = s.displacement && frame.kind == ViewKind::Scene; + if displace { + self.ensure_dense(device, queue); + } + let disp = match self.mesh.as_ref().filter(|_| displace) { + // Height-to-normal's slope: strength 1 tilts 45° for a height change of 1 over 1/16 of + // the UV square; the vertex shader samples the mip whose texels match the vertex spacing. + Some(m) => [1.0, m.world_per_uv / 16.0, 1.0 / (m.dense.as_ref().unwrap_or(&m.geo).triangles.max(1) as f32 * 0.5).sqrt(), 0.0], + None => [0.0; 4], + }; + let shadow_disp = [disp[0], disp[1] * s.height_strength, disp[2], 0.0]; + let shadow_vp = if !data && s.shadows && lw > 0.0 { self.update_shadow(device, queue, encoder, light, shadow_disp, &frame.visible) } else { None }; let radius = self.mesh.as_ref().map_or(1.0, |m| m.radius); // Background rays come from a perspective camera even in orthographic views (an ortho // ray grid would show a single flat environment color). @@ -1529,16 +1556,6 @@ impl Renderer { let sss = s.sss_blur.filter(|m| scene_fx && m.iter().any(|v| *v > 0.0)); let fx_on = dof.is_some() || sss.is_some(); let refraction = s.refraction && !data && frame.kind == ViewKind::Scene && self.mesh.is_some(); - let displace = s.displacement && frame.kind == ViewKind::Scene; - if displace { - self.ensure_dense(device, queue); - } - let disp = match self.mesh.as_ref().filter(|_| displace) { - // Height-to-normal's slope: strength 1 tilts 45° for a height change of 1 over 1/16 of - // the UV square; the vertex shader samples the mip whose texels match the vertex spacing. - Some(m) => [1.0, m.world_per_uv / 16.0, 1.0 / (m.dense.as_ref().unwrap_or(&m.geo).triangles.max(1) as f32 * 0.5).sqrt(), 0.0], - None => [0.0; 4], - }; let cam = &frame.camera; let half_h = (cam.fov.clamp(5.0, 150.0).to_radians() * 0.5).tan(); let height = frame.size[1].max(1) as f32; diff --git a/crates/render/src/shaders/displace.wgsl b/crates/render/src/shaders/displace.wgsl new file mode 100644 index 00000000..8e9ea9d8 --- /dev/null +++ b/crates/render/src/shaders/displace.wgsl @@ -0,0 +1,9 @@ +// Displacement shared by the 3D view (mesh.wgsl) and the key-light shadow pass (shadow.wgsl). + +// `p` moved along normal `n` by `scale` times the height at `uv`, read at the mip whose texels +// match the vertex spacing `spacing` (UV units). +fn displace_by_height(p: vec3, n: vec3, uv: vec2, height: texture_2d, smp: sampler, scale: f32, spacing: f32) -> vec3 { + let dims = vec2(textureDimensions(height)); + let h = textureSampleLevel(height, smp, uv, max(log2(dims.x * spacing), 0.0)).r; + return p + n * inverseSqrt(max(dot(n, n), 1e-12)) * (h * scale); +} diff --git a/crates/render/src/shaders/mesh.wgsl b/crates/render/src/shaders/mesh.wgsl index 6f818677..ec6bcdce 100644 --- a/crates/render/src/shaders/mesh.wgsl +++ b/crates/render/src/shaders/mesh.wgsl @@ -61,16 +61,12 @@ struct VOut { @location(3) uv: vec2, }; -// The vertex moved along its normal by the height channel (displacement), at the mip whose texels -// match the vertex spacing. +// The vertex moved along its normal by the height channel (displacement). fn displaced(v: VIn) -> vec3 { if (g.disp.x < 0.5) { return v.pos; } - let dims = vec2(textureDimensions(t_height)); - let h = textureSampleLevel(t_height, samp, v.uv, max(log2(dims.x * g.disp.z), 0.0)).r; - let n = v.nrm * inverseSqrt(max(dot(v.nrm, v.nrm), 1e-12)); - return v.pos + n * (h * g.mode.z * g.disp.y); + return displace_by_height(v.pos, v.nrm, v.uv, t_height, samp, g.mode.z * g.disp.y, g.disp.z); } @vertex diff --git a/crates/render/src/shaders/shadow.wgsl b/crates/render/src/shaders/shadow.wgsl index 7f17495b..34aee0ae 100644 --- a/crates/render/src/shaders/shadow.wgsl +++ b/crates/render/src/shaders/shadow.wgsl @@ -1,8 +1,20 @@ -// Depth-only key-light shadow map pass. +// Depth-only key-light shadow map pass, displaced like the 3D view when it displaces. -@group(0) @binding(0) var light_vp: mat4x4; +struct ShadowPass { + light_vp: mat4x4, + disp: vec4, // x displacement on (1), y world units per unit of height, z vertex spacing in UV units, w - +}; + +@group(0) @binding(0) var sp: ShadowPass; +// The set bind group of mesh.wgsl: only its height and sampler. +@group(1) @binding(3) var t_height: texture_2d; +@group(1) @binding(9) var samp: sampler; @vertex -fn vs_shadow(@location(0) pos: vec3) -> @builtin(position) vec4 { - return light_vp * vec4(pos, 1.0); +fn vs_shadow(@location(0) pos: vec3, @location(1) nrm: vec3, @location(3) uv: vec2) -> @builtin(position) vec4 { + var p = pos; + if (sp.disp.x > 0.5) { + p = displace_by_height(pos, nrm, uv, t_height, samp, sp.disp.y, sp.disp.z); + } + return sp.light_vp * vec4(p, 1.0); } diff --git a/crates/render/tests/headless.rs b/crates/render/tests/headless.rs index 172f69a0..8ef1b897 100644 --- a/crates/render/tests/headless.rs +++ b/crates/render/tests/headless.rs @@ -164,6 +164,21 @@ fn post_effects() { assert_eq!(all.px.len(), base.px.len()); } +/// The shadow pass draws the displaced (dense) mesh when the view displaces: with flat height it +/// casts the same shadows as the plain mesh. +#[test] +fn displaced_shadows_match_flat_height() { + let Some((mut h, frame)) = setup() else { return }; + let lit = shaded(&frame, |s| { + s.light = LIGHT; + s.shadows = true; + }); + let plain = render(&mut h, &lit, "shadows_plain"); + let displaced = render(&mut h, &shaded(&lit, |s| s.displacement = true), "shadows_displaced"); + let off = plain.px.chunks(4).zip(displaced.px.chunks(4)).filter(|(a, b)| a.iter().zip(*b).any(|(x, y)| x.abs_diff(*y) > 8)).count(); + assert!(off < 480 * 360 / 100, "{off} pixels differ"); +} + #[test] fn shadows_darken_the_key_light() { let Some((mut h, frame)) = setup() else { return }; diff --git a/docs/features.md b/docs/features.md index e266545a..a071609a 100644 --- a/docs/features.md +++ b/docs/features.md @@ -75,7 +75,7 @@ Everything TextureCraft does today, by area. What's still missing is in [`ROADMA ## The viewport - Physically based shading (metallic/roughness or specular/glossiness) with clear coat, sheen, - anisotropy, subsurface scattering, transmission, parallax occlusion and **displacement** from the + anisotropy, subsurface scattering, transmission, parallax occlusion and **displacement** (with its own shadows) from the height channel; image-based lighting from six procedural studio and outdoor environments or **your own HDR/EXR panoramas**; ACES or linear tone mapping, key-light shadows, bloom, vignette, sharpen, grain, **depth of field** and