Merge main and fix symmetry review issues

This commit is contained in:
chasse20 committed 2026-11-10 20:22:40 -05:00
commit ca08fb7c3d
269 files changed
+21475 -3068

No files matched your search

+12
View File
@@ -0,0 +1,12 @@
//! What bake mode's 3D view draws while a bake is set up: the high poly, the cage, the UV seams and the low poly
/// Which parts of the bake setup the 3D view draws
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct BakeVisualization {
pub high_poly: bool,
pub cage: bool,
pub uv_seams: bool,
pub low_poly: bool,
/// Draw the cage, seams and low poly of only this texture set
pub only_set: Option<usize>,
}
+9
View File
@@ -0,0 +1,9 @@
/// One texture set's cage on the GPU: three corners per triangle and the wireframe's corner pairs
pub(crate) struct CageGpu {
/// Identifies the corners' order and the edges: a new distance with the same topology only rewrites `corners`
pub(crate) topology: u64,
pub(crate) corners: wgpu::Buffer,
pub(crate) corner_count: u32,
pub(crate) edges: wgpu::Buffer,
pub(crate) edge_index_count: u32,
}
+11
View File
@@ -0,0 +1,11 @@
/// The high poly bake mode draws over the low poly
pub(crate) struct HighPolyGpu {
/// Which mesh the buffers hold
pub(crate) id: u64,
pub(crate) vertices: wgpu::Buffer,
pub(crate) vertex_count: usize,
pub(crate) indices: wgpu::Buffer,
pub(crate) index_count: u32,
/// One value per vertex: 1 where the bake's rays miss it (drawn red), 0 where they reach it
pub(crate) missed: wgpu::Buffer,
}
+5
View File
@@ -13,12 +13,15 @@
#![forbid(unsafe_code)]
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
pub mod bake_visualization;
mod cage_gpu;
pub mod displace;
#[cfg(not(target_arch = "wasm32"))]
pub mod gpu_bake;
pub mod gpu_trace;
#[cfg(not(target_arch = "wasm32"))]
pub mod headless;
mod high_poly_gpu;
pub mod ibl;
pub mod mesh_maps;
pub mod overlay_quad;
@@ -27,7 +30,9 @@ pub mod renderer;
pub mod set_highlight;
mod subdivide;
pub mod trace;
mod uv_seam_gpu;
pub use bake_visualization::BakeVisualization;
pub use overlay_quad::OverlayQuad;
pub use renderer::{COLOR_FORMAT, Frame, MSAA, Renderer, Shade, ViewKind, aux_size, uv_view_scale};
pub use set_highlight::SetHighlight;
+13
View File
@@ -26,6 +26,19 @@ pub fn mesh_map_rgba(baked: Option<&BakedMaps>, m: BakedMap, size: u32, working_
out
}
/// RGBA8 of the neutral value of map `m` (what a map not baked yet shows during a bake: flat
/// normals, white occlusion, mid-gray height ...), `size × size`, encoded like [`mesh_map_rgba`].
pub fn neutral_rgba(m: BakedMap, size: u32) -> Vec<u8> {
let value = m.neutral();
let pixel = [0, 1, 2].map(|k| (value[k].clamp(0.0, 1.0) * 255.0 + 0.5) as u8);
let size = size.max(1);
let mut out = Vec::with_capacity((size * size * 4) as usize);
for _ in 0..size * size {
out.extend_from_slice(&[pixel[0], pixel[1], pixel[2], 255]);
}
out
}
#[cfg(test)]
mod tests {
use super::*;
+388 -100
View File
@@ -30,11 +30,15 @@ use texturecraft_geom::glam::{Mat4, Vec3};
use texturecraft_geom::{Mesh, OrbitCamera};
use texturecraft_texel::TILE;
use crate::bake_visualization::BakeVisualization;
use crate::cage_gpu::CageGpu;
use crate::gpu_trace::{Checked, checked};
use crate::high_poly_gpu::HighPolyGpu;
use crate::ibl;
use crate::overlay_quad::OverlayQuad;
use crate::set_highlight::SetHighlight;
use crate::subdivide::{self, Vertex};
use crate::uv_seam_gpu::UvSeamGpu;
pub const MSAA: u32 = 4;
pub const COLOR_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba8Unorm;
@@ -174,6 +178,8 @@ pub struct Shade {
/// Move the surface by the height channel (3D view): a dense copy of the mesh, displaced in
/// the vertex shader at the height-to-normal strength.
pub displacement: bool,
/// Bake mode's high poly, cage and UV seams over the 3D view
pub bake: Option<BakeVisualization>,
}
/// The viewport's subsurface scattering approximation: the diffuse lighting wraps around the
@@ -260,6 +266,7 @@ impl Default for Shade {
sss_blur: None,
refraction: false,
displacement: false,
bake: None,
}
}
}
@@ -537,6 +544,11 @@ pub struct Renderer {
overlay_pipeline: wgpu::RenderPipeline,
/// The highlighted set's mesh edges, wider and in the highlight color
highlight_pipeline: wgpu::RenderPipeline,
/// Bake mode: the high poly, the cage's faces and wireframe, the low poly's UV seams
bake_high_pipeline: wgpu::RenderPipeline,
cage_pipeline: wgpu::RenderPipeline,
cage_wire_pipeline: wgpu::RenderPipeline,
uv_seam_pipeline: wgpu::RenderPipeline,
bg_pipeline: wgpu::RenderPipeline,
blit_pipeline: wgpu::RenderPipeline,
post_pipeline: wgpu::RenderPipeline,
@@ -570,6 +582,10 @@ pub struct Renderer {
/// accumulation.
content_rev: u64,
mesh: Option<MeshGpu>,
bake_high: Option<HighPolyGpu>,
/// Each texture set's cage, by set index
cages: Vec<Option<CageGpu>>,
uv_seams: Option<UvSeamGpu>,
sets: Vec<SetGpu>,
/// Viewport texture resolution: display textures at 1/2^shift of each set's size (at least
/// as reduced as the device's texture limit needs).
@@ -815,20 +831,31 @@ fn halton(mut i: u32, base: u32) -> f32 {
const CLEAR: wgpu::LoadOp<wgpu::Color> = wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT);
/// Depth bias of the shadow map, per unit of surface slope: keeps lit surfaces from shadowing themselves
const SHADOW_SLOPE_BIAS: f32 = 1.5;
/// Anisotropic filtering of the texture sets' sampler
const MAX_ANISOTROPY: u16 = 8;
/// Size of the environment texture before the first environment loads
const PLACEHOLDER_ENV_SIZE: [u32; 2] = [512, 256];
impl Renderer {
/// `target_format`: the format of the pass [`Renderer::blit`] draws into.
pub fn new(device: &wgpu::Device, queue: &wgpu::Queue, target_format: wgpu::TextureFormat) -> 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", concat!(include_str!("shaders/displace.wgsl"), include_str!("shaders/mesh.wgsl")));
let module =
shader("tc_mesh", concat!(include_str!("shaders/displace.wgsl"), include_str!("shaders/mesh.wgsl"), "\n", include_str!("shaders/bake_view.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", concat!(include_str!("shaders/displace.wgsl"), include_str!("shaders/shadow.wgsl")));
let bgl = |label: &str, entries: &[wgpu::BindGroupLayoutEntry]| {
let bind_group_layout = |label: &str, entries: &[wgpu::BindGroupLayoutEntry]| {
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { label: Some(label), entries })
};
let globals_bgl = bgl(
let globals_bind_group_layout = bind_group_layout(
"tc_globals",
&[
uniform_entry(0),
@@ -845,44 +872,47 @@ impl Renderer {
fragment_entry(4, wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Comparison)),
],
);
// The display textures, the aux texture, the sampler.
// The display textures, the aux texture, the sampler
let textures = DisplayKind::ALL.len() as u32 + 1;
// Displacement reads the height (and the sampler) in the vertex shader.
let height = DisplayKind::ALL.iter().position(|k| *k == DisplayKind::Height).unwrap_or(0) as u32;
// Displacement reads the height (and the sampler) in the vertex shader
let height = DisplayKind::ALL.iter().position(|kind| *kind == DisplayKind::Height).unwrap_or(0) as u32;
let set_entries: Vec<_> = (0..textures)
.map(tex_entry)
.chain(std::iter::once(sampler_entry(textures)))
.map(|mut e| {
if e.binding == height || e.binding == textures {
e.visibility |= wgpu::ShaderStages::VERTEX;
.map(|mut entry| {
if entry.binding == height || entry.binding == textures {
entry.visibility |= wgpu::ShaderStages::VERTEX;
}
e
entry
})
.collect();
let set_bgl = bgl("tc_set", &set_entries);
let refr_bgl = bgl("tc_refr", &[tex_entry(0)]);
let blit_bgl = bgl("tc_blit", &[tex_entry(0), sampler_entry(1), uniform_entry(2)]);
let mip_bgl = bgl("tc_mip", &[tex_entry(0), sampler_entry(1)]);
let fx_bgl = bgl("tc_fx", &[tex_entry(0), sampler_entry(1), uniform_entry(3), tex_entry(4), tex_entry(5), tex_entry(6)]);
let shadow_bgl = bgl("tc_shadow", &[uniform_entry(0)]);
let pl = |label: &str, groups: &[Option<&wgpu::BindGroupLayout>]| {
let set_bind_group_layout = bind_group_layout("tc_set", &set_entries);
let refraction_bind_group_layout = bind_group_layout("tc_refr", &[tex_entry(0)]);
let blit_bind_group_layout = bind_group_layout("tc_blit", &[tex_entry(0), sampler_entry(1), uniform_entry(2)]);
let mip_bind_group_layout = bind_group_layout("tc_mip", &[tex_entry(0), sampler_entry(1)]);
let effect_bind_group_layout =
bind_group_layout("tc_fx", &[tex_entry(0), sampler_entry(1), uniform_entry(3), tex_entry(4), tex_entry(5), tex_entry(6)]);
let shadow_bind_group_layout = bind_group_layout("tc_shadow", &[uniform_entry(0)]);
let pipeline_layout = |label: &str, groups: &[Option<&wgpu::BindGroupLayout>]| {
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: Some(label), bind_group_layouts: groups, immediate_size: 0 })
};
let layout = pl("tc_uv", &[Some(&globals_bgl), Some(&set_bgl)]);
let mesh_layout = pl("tc_mesh", &[Some(&globals_bgl), Some(&set_bgl), Some(&refr_bgl)]);
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), Some(&set_bgl)]);
let vb = [Some(wgpu::VertexBufferLayout {
let layout = pipeline_layout("tc_uv", &[Some(&globals_bind_group_layout), Some(&set_bind_group_layout)]);
let mesh_layout = pipeline_layout("tc_mesh", &[Some(&globals_bind_group_layout), Some(&set_bind_group_layout), Some(&refraction_bind_group_layout)]);
let background_layout = pipeline_layout("tc_bg", &[Some(&globals_bind_group_layout)]);
let blit_layout = pipeline_layout("tc_blit", &[Some(&blit_bind_group_layout)]);
let effect_layout = pipeline_layout("tc_fx", &[Some(&effect_bind_group_layout)]);
let shadow_layout = pipeline_layout("tc_shadow", &[Some(&shadow_bind_group_layout), Some(&set_bind_group_layout)]);
let vertex_layout = [Some(wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<Vertex>() as u64,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &wgpu::vertex_attr_array![0 => Float32x3, 1 => Float32x3, 2 => Float32x4, 3 => Float32x2],
})];
// shortcut: the RGBA8 fallback clips highlights above 1 and bands in the darks; encode
// (e.g. RGBM) if WebGL2 without float color buffers turns out to matter.
// shortcut: the RGBA8 fallback clips highlights above 1 and bands in the darks; encode (e.g. RGBM) if WebGL2 without float color buffers turns out to matter
let hdr_format = if hdr_renderable(device) { HDR_FORMAT } else { COLOR_FORMAT };
let hdr = |fs, blend| Some((fs, hdr_format, blend));
let hdr_target = |fragment_entry_point, blend| Some((fragment_entry_point, hdr_format, blend));
let always = || Some(depth_state(false, wgpu::CompareFunction::Always));
let mesh_pipeline = pipeline(
device,
@@ -890,8 +920,8 @@ impl Renderer {
&mesh_layout,
&module,
"vs_mesh",
&vb,
hdr("fs_mesh", None),
&vertex_layout,
hdr_target("fs_mesh", None),
MSAA,
Some(depth_state(true, wgpu::CompareFunction::Less)),
);
@@ -901,32 +931,105 @@ impl Renderer {
&mesh_layout,
&module,
"vs_mesh",
&vb,
hdr("fs_opaque", None),
&vertex_layout,
hdr_target("fs_opaque", None),
MSAA,
Some(depth_state(true, wgpu::CompareFunction::Less)),
);
let uv_pipeline = pipeline(device, "tc_uv", &layout, &module, "vs_uv", &vb, hdr("fs_uv", None), MSAA, always());
let uv_pipeline = pipeline(device, "tc_uv", &layout, &module, "vs_uv", &vertex_layout, hdr_target("fs_uv", None), MSAA, always());
let blend = Some(wgpu::BlendState::ALPHA_BLENDING);
let wire_pipeline = pipeline(device, "tc_wire", &layout, &module, "vs_uv", &vb, hdr("fs_wire", blend), MSAA, always());
let wire_pipeline = pipeline(device, "tc_wire", &layout, &module, "vs_uv", &vertex_layout, hdr_target("fs_wire", blend), MSAA, always());
let wire3d_depth = Some(depth_state(false, wgpu::CompareFunction::LessEqual));
let wire3d_pipeline = pipeline(device, "tc_wire3d", &layout, &module, "vs_wire3d", &vb, hdr("fs_wire3d", blend), MSAA, wire3d_depth.clone());
let overlay_pipeline = pipeline(device, "tc_overlay", &bg_layout, &module, "vs_overlay", &vb, hdr("fs_overlay", blend), MSAA, wire3d_depth.clone());
let highlight_pipeline =
pipeline(device, "tc_highlight", &layout, &module, "vs_wire_highlight", &vb, hdr("fs_wire_highlight", blend), MSAA, wire3d_depth);
let bg_pipeline = pipeline(device, "tc_bg", &bg_layout, &module, "vs_bg", &[], hdr("fs_bg", None), MSAA, always());
let wire3d_pipeline =
pipeline(device, "tc_wire3d", &layout, &module, "vs_wire3d", &vertex_layout, hdr_target("fs_wire3d", blend), MSAA, wire3d_depth.clone());
let highlight_pipeline = pipeline(
device,
"tc_highlight",
&layout,
&module,
"vs_wire_highlight",
&vertex_layout,
hdr_target("fs_wire_highlight", blend),
MSAA,
wire3d_depth.clone(),
);
let [mesh_vertex_layout] = vertex_layout.clone();
let high_layout = [
mesh_vertex_layout,
Some(wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<f32>() as u64,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &wgpu::vertex_attr_array![4 => Float32],
}),
];
let bake_high_pipeline = pipeline(
device,
"tc_bake_high",
&background_layout,
&module,
"vs_bake_high",
&high_layout,
hdr_target("fs_bake_high", None),
MSAA,
Some(depth_state(true, wgpu::CompareFunction::Less)),
);
let corner_layout = [Some(wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<[f32; 3]>() as u64,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &wgpu::vertex_attr_array![0 => Float32x3],
})];
let cage_pipeline = pipeline(
device,
"tc_cage",
&background_layout,
&module,
"vs_bake_cage",
&corner_layout,
hdr_target("fs_bake_cage", blend),
MSAA,
wire3d_depth.clone(),
);
let cage_wire_pipeline = pipeline(
device,
"tc_cage_wire",
&background_layout,
&module,
"vs_bake_cage_wire",
&corner_layout,
hdr_target("fs_wire_bake_cage", blend),
MSAA,
wire3d_depth.clone(),
);
let uv_seam_pipeline =
pipeline(device, "tc_uv_seam", &layout, &module, "vs_wire3d", &vertex_layout, hdr_target("fs_wire_uv_seam", blend), MSAA, wire3d_depth.clone());
let bg_pipeline = pipeline(device, "tc_bg", &background_layout, &module, "vs_bg", &[], hdr_target("fs_bg", None), MSAA, always());
let overlay_pipeline =
pipeline(device, "tc_overlay", &background_layout, &module, "vs_overlay", &vertex_layout, hdr_target("fs_overlay", blend), MSAA, wire3d_depth);
let blit_pipeline = pipeline(device, "tc_blit", &blit_layout, &util_module, "vs_blit", &[], Some(("fs_blit", target_format, None)), 1, None);
let post_pipeline = pipeline(device, "tc_post", &fx_layout, &post_module, "vs_full", &[], Some(("fs_post", COLOR_FORMAT, None)), 1, None);
let bloom_pipelines = [("fs_bloom_pre", None), ("fs_bloom_down", None), ("fs_bloom_up", Some(ADDITIVE))]
.map(|(fs, blend)| pipeline(device, "tc_bloom", &fx_layout, &post_module, "vs_full", &[], hdr(fs, blend), 1, None));
let gbuf_pipeline =
pipeline(device, "tc_gbuf", &mesh_layout, &module, "vs_mesh", &vb, hdr("fs_gbuf", None), 1, Some(depth_state(true, wgpu::CompareFunction::Less)));
let sss_pipelines = ["fs_sss_h", "fs_sss_v"].map(|fs| pipeline(device, "tc_sss", &fx_layout, &post_module, "vs_full", &[], hdr(fs, None), 1, None));
let dof_pipeline = pipeline(device, "tc_dof", &fx_layout, &post_module, "vs_full", &[], hdr("fs_dof", None), 1, None);
let post_pipeline = pipeline(device, "tc_post", &effect_layout, &post_module, "vs_full", &[], Some(("fs_post", COLOR_FORMAT, None)), 1, None);
let bloom_pipelines = [("fs_bloom_pre", None), ("fs_bloom_down", None), ("fs_bloom_up", Some(ADDITIVE))].map(|(fragment_entry_point, blend)| {
pipeline(device, "tc_bloom", &effect_layout, &post_module, "vs_full", &[], hdr_target(fragment_entry_point, blend), 1, None)
});
let gbuffer_pipeline = pipeline(
device,
"tc_gbuf",
&mesh_layout,
&module,
"vs_mesh",
&vertex_layout,
hdr_target("fs_gbuf", None),
1,
Some(depth_state(true, wgpu::CompareFunction::Less)),
);
let sss_pipelines = ["fs_sss_h", "fs_sss_v"].map(|fragment_entry_point| {
pipeline(device, "tc_sss", &effect_layout, &post_module, "vs_full", &[], hdr_target(fragment_entry_point, None), 1, None)
});
let dof_pipeline = pipeline(device, "tc_dof", &effect_layout, &post_module, "vs_full", &[], hdr_target("fs_dof", None), 1, None);
let dof_target = Some(wgpu::ColorTargetState { format: hdr_format, blend: None, write_mask: wgpu::ColorWrites::ALL });
let dof_gather_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("tc_dof_gather"),
layout: Some(&fx_layout),
layout: Some(&effect_layout),
vertex: wgpu::VertexState { module: &post_module, entry_point: Some("vs_full"), buffers: &[], compilation_options: Default::default() },
primitive: wgpu::PrimitiveState::default(),
depth_stencil: None,
@@ -940,20 +1043,23 @@ impl Renderer {
multiview_mask: None,
cache: None,
});
let shadow_depth = wgpu::DepthStencilState {
bias: wgpu::DepthBiasState { constant: 0, slope_scale: 1.5, clamp: 0.0 },
bias: wgpu::DepthBiasState { constant: 0, slope_scale: SHADOW_SLOPE_BIAS, clamp: 0.0 },
..depth_state(true, wgpu::CompareFunction::Less)
};
let shadow_pipeline = pipeline(device, "tc_shadow", &shadow_layout, &shadow_module, "vs_shadow", &vb, None, 1, Some(shadow_depth));
// accum = hdr * k + accum * (1 - k) with k = 1 / (sample + 1): a running average.
let shadow_pipeline = pipeline(device, "tc_shadow", &shadow_layout, &shadow_module, "vs_shadow", &vertex_layout, None, 1, Some(shadow_depth));
// accum = hdr_target * k + accum * (1 - k) with k = 1 / (sample + 1): a running average
let average = wgpu::BlendComponent {
src_factor: wgpu::BlendFactor::Constant,
dst_factor: wgpu::BlendFactor::OneMinusConstant,
operation: wgpu::BlendOperation::Add,
};
let accum_layout = pl("tc_accum", &[Some(&mip_bgl)]);
let accum_layout = pipeline_layout("tc_accum", &[Some(&mip_bind_group_layout)]);
let accum_blend = Some(wgpu::BlendState { color: average, alpha: average });
let accum_pipeline = pipeline(device, "tc_accum", &accum_layout, &util_module, "vs_full", &[], hdr("fs_copy", accum_blend), 1, None);
let accum_pipeline = pipeline(device, "tc_accum", &accum_layout, &util_module, "vs_full", &[], hdr_target("fs_copy", accum_blend), 1, None);
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
label: Some("tc_tex"),
address_mode_u: wgpu::AddressMode::ClampToEdge,
@@ -961,7 +1067,7 @@ impl Renderer {
mag_filter: wgpu::FilterMode::Linear,
min_filter: wgpu::FilterMode::Linear,
mipmap_filter: wgpu::MipmapFilterMode::Linear,
anisotropy_clamp: 8,
anisotropy_clamp: MAX_ANISOTROPY,
..Default::default()
});
let env_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
@@ -980,7 +1086,8 @@ impl Renderer {
compare: Some(wgpu::CompareFunction::LessEqual),
..Default::default()
});
let shadow_buf = device.create_buffer(&wgpu::BufferDescriptor {
let shadow_buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("tc_shadow"),
size: std::mem::size_of::<[[f32; 4]; 5]>() as u64,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
@@ -994,28 +1101,30 @@ impl Renderer {
vp: Mat4::IDENTITY,
bg: device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("tc_shadow"),
layout: &shadow_bgl,
entries: &[wgpu::BindGroupEntry { binding: 0, resource: shadow_buf.as_entire_binding() }],
layout: &shadow_bind_group_layout,
entries: &[wgpu::BindGroupEntry { binding: 0, resource: shadow_buffer.as_entire_binding() }],
}),
buf: shadow_buf,
buf: shadow_buffer,
};
let black = Tex::new(device, "tc_black", 1, 1, COLOR_FORMAT, 1).view;
let refr_black = device.create_bind_group(&wgpu::BindGroupDescriptor {
let refraction_black = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("tc_refr_black"),
layout: &refr_bgl,
layout: &refraction_bind_group_layout,
entries: &[wgpu::BindGroupEntry { binding: 0, resource: wgpu::BindingResource::TextureView(&black) }],
});
let env = Tex::new(device, "tc_env", 512, 256, wgpu::TextureFormat::Rgba16Float, ENV_LEVELS);
let mut r = Renderer {
let env = Tex::new(device, "tc_env", PLACEHOLDER_ENV_SIZE[0], PLACEHOLDER_ENV_SIZE[1], wgpu::TextureFormat::Rgba16Float, ENV_LEVELS);
let mut renderer = Renderer {
target_format,
hdr_format,
globals_bgl,
set_bgl,
blit_bgl,
mip_bgl,
fx_bgl,
refr_bgl,
refr_black,
globals_bgl: globals_bind_group_layout,
set_bgl: set_bind_group_layout,
blit_bgl: blit_bind_group_layout,
mip_bgl: mip_bind_group_layout,
fx_bgl: effect_bind_group_layout,
refr_bgl: refraction_bind_group_layout,
refr_black: refraction_black,
mesh_pipeline,
opaque_pipeline,
uv_pipeline,
@@ -1023,11 +1132,15 @@ impl Renderer {
wire3d_pipeline,
overlay_pipeline,
highlight_pipeline,
bake_high_pipeline,
cage_pipeline,
cage_wire_pipeline,
uv_seam_pipeline,
bg_pipeline,
blit_pipeline,
post_pipeline,
bloom_pipelines,
gbuf_pipeline,
gbuf_pipeline: gbuffer_pipeline,
sss_pipelines,
dof_gather_pipeline,
dof_pipeline,
@@ -1047,13 +1160,16 @@ impl Renderer {
sh: [[0.0; 4]; 9],
content_rev: 0,
mesh: None,
bake_high: None,
cages: Vec::new(),
uv_seams: None,
sets: Vec::new(),
display_shift: 0,
material_sets: Vec::new(),
views: HashMap::new(),
};
r.set_environment(device, queue, "Studio Soft", None);
r
renderer.set_environment(device, queue, "Studio Soft", None);
renderer
}
pub fn environment_name(&self) -> &str {
@@ -1133,6 +1249,94 @@ impl Renderer {
self.content_rev += 1;
}
/// Upload the high poly when `id` changes; remove it on `None` or when its buffers exceed the device limit
pub fn set_bake_high(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, mesh: Option<&Mesh>, id: u64) -> Result<(), String> {
if self.bake_high.as_ref().map(|high| high.id) == mesh.map(|_| id) {
return Ok(());
}
self.bake_high = None;
self.content_rev += 1;
let Some(mesh) = mesh else { return Ok(()) };
// Imported high polys can outgrow a viewport buffer even when the CPU baker accepts them
let limit = device.limits().max_buffer_size;
let buffers = [
(mesh.positions.len(), std::mem::size_of::<Vertex>()),
(mesh.indices.len(), std::mem::size_of::<u32>()),
(mesh.positions.len(), std::mem::size_of::<f32>()),
];
if mesh.indices.len() > u32::MAX as usize || buffers.iter().any(|&(count, stride)| count.checked_mul(stride).is_none_or(|bytes| bytes as u64 > limit)) {
return Err(format!("High-poly visualization exceeds the GPU buffer limit ({limit} bytes). Use a smaller preview mesh or hide the high poly."));
}
self.bake_high = Some(HighPolyGpu {
id,
vertices: upload_buffer(device, queue, "tc_bake_high_vb", bytemuck::cast_slice(&subdivide::vertices(mesh)), wgpu::BufferUsages::VERTEX),
vertex_count: mesh.positions.len(),
indices: upload_buffer(device, queue, "tc_bake_high_ib", bytemuck::cast_slice(&mesh.indices), wgpu::BufferUsages::INDEX),
index_count: mesh.indices.len() as u32,
missed: upload_buffer(device, queue, "tc_bake_high_missed", bytemuck::cast_slice(&vec![0f32; mesh.positions.len()]), wgpu::BufferUsages::VERTEX),
});
Ok(())
}
/// Which vertices of high poly `id` the bake's rays miss (1) or reach (0); ignored for another mesh or vertex count
pub fn set_bake_high_missed(&mut self, queue: &wgpu::Queue, id: u64, missed: &[f32]) {
let Some(high) = self.bake_high.as_ref().filter(|high| high.id == id && high.vertex_count == missed.len()) else { return };
queue.write_buffer(&high.missed, 0, bytemuck::cast_slice(missed));
self.content_rev += 1;
}
/// Texture set `texture_set`'s cage: `corners` three per triangle and `edges` as corner pairs; `None` removes it. The same `topology` as before only rewrites the corners
pub fn set_bake_cage(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, texture_set: usize, cage: Option<(&[Vec3], &[[u32; 2]])>, topology: u64) {
if self.cages.len() <= texture_set {
self.cages.resize_with(texture_set + 1, || None);
}
let Some(slot) = self.cages.get_mut(texture_set) else { return };
match (cage, slot.as_mut()) {
(None, _) => *slot = None,
(Some((corners, _)), Some(gpu)) if gpu.topology == topology && gpu.corner_count as usize == corners.len() => {
queue.write_buffer(&gpu.corners, 0, bytemuck::cast_slice(corners));
}
(Some((corners, edges)), _) => {
*slot = Some(CageGpu {
topology,
corners: upload_buffer(device, queue, "tc_cage_vb", bytemuck::cast_slice(corners), wgpu::BufferUsages::VERTEX),
corner_count: corners.len() as u32,
edges: upload_buffer(device, queue, "tc_cage_ib", bytemuck::cast_slice(edges), wgpu::BufferUsages::INDEX),
edge_index_count: (edges.len() * 2) as u32,
});
}
}
self.content_rev += 1;
}
/// Forget the cages of texture sets from `count` on (sets that were removed)
pub fn truncate_bake_cages(&mut self, count: usize) {
if self.cages.len() > count {
self.cages.truncate(count);
self.content_rev += 1;
}
}
/// The low poly's UV seams as vertex pairs per material ([`Mesh::uv_seam_edges`]) for mesh `mesh_id`
pub fn set_uv_seams(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, mesh_id: u64, seams: &[Vec<(u32, u32)>]) {
let mut line_indices: Vec<u32> = Vec::new();
let mut line_draws = Vec::new();
for (material, edges) in seams.iter().enumerate() {
let start = line_indices.len() as u32;
line_indices.extend(edges.iter().flat_map(|&(from, to)| [from, to]));
line_draws.push((start, line_indices.len() as u32 - start, material));
}
let lines = upload_buffer(device, queue, "tc_uv_seams", bytemuck::cast_slice(&line_indices), wgpu::BufferUsages::INDEX);
self.uv_seams = Some(UvSeamGpu { mesh_id, lines, line_draws });
self.content_rev += 1;
}
/// Build the mesh's dense copy for displacement (once per mesh).
fn ensure_dense(&mut self, device: &wgpu::Device, queue: &wgpu::Queue) {
let Some(m) = self.mesh.as_mut() else { return };
@@ -1901,30 +2105,30 @@ impl Renderer {
self.draw_scene(encoder, v, resolve, globals_bg, frame, pipe, wire, highlight, refr);
}
/// One scene pass: background, then the mesh with `pipe`, the wireframe with `wire` and one set's highlighted edges with `highlight`
/// One scene pass: background, then the mesh with `mesh_pipeline`, the wireframe with `wire` and one set's highlighted edges with `highlight`
#[allow(clippy::too_many_arguments)]
fn draw_scene(
&self,
encoder: &mut wgpu::CommandEncoder,
v: &ViewTargets,
targets: &ViewTargets,
resolve: &wgpu::TextureView,
globals_bg: &wgpu::BindGroup,
globals_bind_group: &wgpu::BindGroup,
frame: &Frame,
pipe: &wgpu::RenderPipeline,
mesh_pipeline: &wgpu::RenderPipeline,
wire: Option<&wgpu::RenderPipeline>,
highlight: Option<(&wgpu::RenderPipeline, usize)>,
refr: &wgpu::BindGroup,
refraction_source: &wgpu::BindGroup,
) {
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("tc_view"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &v.msaa,
view: &targets.msaa,
resolve_target: Some(resolve),
depth_slice: None,
ops: wgpu::Operations { load: CLEAR, store: wgpu::StoreOp::Discard },
})],
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view: &v.depth,
view: &targets.depth,
depth_ops: Some(wgpu::Operations { load: wgpu::LoadOp::Clear(1.0), store: wgpu::StoreOp::Discard }),
stencil_ops: None,
}),
@@ -1932,41 +2136,95 @@ impl Renderer {
occlusion_query_set: None,
multiview_mask: None,
});
pass.set_bind_group(0, globals_bg, &[]);
pass.set_bind_group(0, globals_bind_group, &[]);
pass.set_pipeline(&self.bg_pipeline);
pass.draw(0..3, 0..1);
let Some(m) = &self.mesh else { return };
let uv_ts = match frame.kind {
let Some(mesh) = &self.mesh else { return };
let uv_set = match frame.kind {
ViewKind::Scene => None,
ViewKind::Uv { ts, .. } => Some(ts),
};
pass.set_bind_group(2, refr, &[]);
let geo = self.geometry(m, frame);
pass.set_bind_group(2, refraction_source, &[]);
let geometry = self.geometry(mesh, frame);
let bake = frame.shade.bake.filter(|_| uv_set.is_none());
let low_poly_shown = bake.is_none_or(|bake| bake.low_poly);
let low_poly_set = bake.and_then(|bake| bake.only_set).or(uv_set);
let (highlight, highlight_set) = highlight.filter(|(_, set)| low_poly_shown && low_poly_set.is_none_or(|only| only == *set)).unzip();
// The wireframe and the highlight draw the mesh's own edges (displaced in the vertex shader too)
let (highlight, highlight_set) = highlight.unzip();
let passes = [
(Some(pipe), &geo.vb, &geo.ib, &geo.draws, uv_ts, 1),
(wire, &m.geo.vb, &m.lines, &m.line_draws, uv_ts, 1),
(highlight, &m.geo.vb, &m.lines, &m.line_draws, highlight_set, HIGHLIGHT_SHIFTS),
(low_poly_shown.then_some(mesh_pipeline), &geometry.vb, &geometry.ib, &geometry.draws, low_poly_set, 1),
(wire.filter(|_| low_poly_shown), &mesh.geo.vb, &mesh.lines, &mesh.line_draws, low_poly_set, 1),
(highlight, &mesh.geo.vb, &mesh.lines, &mesh.line_draws, highlight_set, HIGHLIGHT_SHIFTS),
];
for (pipe, vb, ib, ranges, only, instances) in passes {
let Some(pipe) = pipe else { continue };
pass.set_pipeline(pipe);
pass.set_vertex_buffer(0, vb.slice(..));
pass.set_index_buffer(ib.slice(..), wgpu::IndexFormat::Uint32);
for (range, set) in self.drawable(ranges, &frame.visible, only) {
for (pipeline, vertices, indices, ranges, only_set, instances) in passes {
let Some(pipeline) = pipeline else { continue };
pass.set_pipeline(pipeline);
pass.set_vertex_buffer(0, vertices.slice(..));
pass.set_index_buffer(indices.slice(..), wgpu::IndexFormat::Uint32);
for (range, set) in self.drawable(ranges, &frame.visible, only_set) {
pass.set_bind_group(1, &set.bind_group, &[]);
pass.draw_indexed(range, 0, 0..instances);
}
}
if let Some(bake) = bake {
self.draw_bake(&mut pass, mesh, frame, bake);
}
// Overlay quads last, so the mesh's depth hides what is behind it
if uv_ts.is_none()
&& let Some(overlay) = v.overlay.as_ref().filter(|_| v.overlay_vertices > 0)
if uv_set.is_none()
&& let Some(overlay) = targets.overlay.as_ref().filter(|_| targets.overlay_vertices > 0)
{
pass.set_pipeline(&self.overlay_pipeline);
pass.set_vertex_buffer(0, overlay.slice(..));
pass.draw(0..v.overlay_vertices, 0..1);
pass.draw(0..targets.overlay_vertices, 0..1);
}
}
/// Bake mode's high poly, UV seams and cages over the low poly `low_poly`
fn draw_bake(&self, pass: &mut wgpu::RenderPass<'_>, low_poly: &MeshGpu, frame: &Frame, bake: BakeVisualization) {
if bake.high_poly
&& let Some(high) = &self.bake_high
{
pass.set_pipeline(&self.bake_high_pipeline);
pass.set_vertex_buffer(0, high.vertices.slice(..));
pass.set_vertex_buffer(1, high.missed.slice(..));
pass.set_index_buffer(high.indices.slice(..), wgpu::IndexFormat::Uint32);
pass.draw_indexed(0..high.index_count, 0, 0..1);
}
if bake.uv_seams
&& let Some(seams) = self.uv_seams.as_ref().filter(|seams| seams.mesh_id == low_poly.id)
{
pass.set_pipeline(&self.uv_seam_pipeline);
pass.set_vertex_buffer(0, low_poly.geo.vb.slice(..));
pass.set_index_buffer(seams.lines.slice(..), wgpu::IndexFormat::Uint32);
for (range, set) in self.drawable(&seams.line_draws, &frame.visible, bake.only_set) {
pass.set_bind_group(1, &set.bind_group, &[]);
pass.draw_indexed(range, 0, 0..1);
}
}
if !bake.cage {
return;
}
let shown = |texture_set: usize| bake.only_set.is_none_or(|only| only == texture_set) && frame.visible.get(texture_set).copied().unwrap_or(true);
let cages = || self.cages.iter().enumerate().filter(move |(texture_set, _)| shown(*texture_set)).filter_map(|(_, cage)| cage.as_ref());
// The translucent faces first, then the wireframe over them
pass.set_pipeline(&self.cage_pipeline);
for cage in cages() {
pass.set_vertex_buffer(0, cage.corners.slice(..));
pass.draw(0..cage.corner_count, 0..1);
}
pass.set_pipeline(&self.cage_wire_pipeline);
for cage in cages() {
pass.set_vertex_buffer(0, cage.corners.slice(..));
pass.set_index_buffer(cage.edges.slice(..), wgpu::IndexFormat::Uint32);
pass.draw_indexed(0..cage.edge_index_count, 0, 0..1);
}
}
@@ -2030,6 +2288,23 @@ const QUAD_TRIANGLES: [usize; 6] = [0, 1, 2, 0, 2, 3];
/// Write `quads` into the view's overlay buffer, colors divided by the exposure only (undoing ACES makes saturated colors overshoot)
fn upload_overlay(device: &wgpu::Device, queue: &wgpu::Queue, view: &mut ViewTargets, quads: &[OverlayQuad], exposure_ev: Option<f32>) {
view.overlay_vertices = 0;
let Some(vertex_count) = quads.len().checked_mul(QUAD_TRIANGLES.len()).and_then(|count| u32::try_from(count).ok()) else {
log::warn!("Symmetry overlay has too many vertices; hide the intersection or use a simpler mesh");
return;
};
// A valid imported mesh can produce more intersection vertices than the GPU accepts
let byte_count = u64::from(vertex_count) * std::mem::size_of::<Vertex>() as u64;
if byte_count > device.limits().max_buffer_size {
log::warn!("Symmetry overlay exceeds the GPU buffer limit; hide the intersection or use a simpler mesh");
return;
}
if vertex_count == 0 {
return;
}
let vertices: Vec<Vertex> = quads
.iter()
.flat_map(|quad| {
@@ -2038,11 +2313,6 @@ fn upload_overlay(device: &wgpu::Device, queue: &wgpu::Queue, view: &mut ViewTar
QUAD_TRIANGLES.map(|corner| vertex(quad.corners[corner]))
})
.collect();
view.overlay_vertices = u32::try_from(vertices.len()).unwrap_or(0);
if vertices.is_empty() {
return;
}
let bytes: &[u8] = bytemuck::cast_slice(&vertices);
if view.overlay.as_ref().is_none_or(|buffer| buffer.size() < bytes.len() as u64) {
view.overlay = Some(device.create_buffer(&wgpu::BufferDescriptor {
@@ -2054,6 +2324,7 @@ fn upload_overlay(device: &wgpu::Device, queue: &wgpu::Queue, view: &mut ViewTar
}
if let Some(buffer) = &view.overlay {
queue.write_buffer(buffer, 0, bytes);
view.overlay_vertices = vertex_count;
}
}
@@ -2078,6 +2349,23 @@ mod tests {
use super::*;
use crate::headless::Headless;
#[test]
fn oversized_overlays_leave_the_view_usable() {
let Ok(mut headless) = Headless::new() else { return };
let limit = headless.device.limits().max_buffer_size as usize;
let count = limit / (QUAD_TRIANGLES.len() * std::mem::size_of::<Vertex>()) + 1;
let quad = OverlayQuad { corners: [Vec3::ZERO; 4], color: [1.0, 0.0, 0.0], opacity: 0.5 };
let quads = vec![quad; count];
let view = headless.renderer.targets(&headless.device, 1, [32, 32], false, false, false).unwrap();
upload_overlay(&headless.device, &headless.queue, view, &[quad], None);
upload_overlay(&headless.device, &headless.queue, view, &quads, None);
assert_eq!(view.overlay_vertices, 0, "skip overlays beyond the device limit");
upload_overlay(&headless.device, &headless.queue, view, &[quad], None);
assert_eq!(view.overlay_vertices, 6, "the next frame can draw its overlay");
}
/// A headless renderer with one 256² texture set, and the base color slot's full-size
/// texture handed over as WebGPU would: allocated, with its verdict still to come.
fn awaiting() -> Option<(Headless, std::sync::mpsc::Sender<Option<String>>)> {
+88
View File
@@ -0,0 +1,88 @@
// Bake mode's 3D view: the high poly, the cage around the low poly and the UV seams
struct BakeViewOut {
@builtin(position) clip: vec4<f32>,
@location(0) world_position: vec3<f32>,
@location(1) normal: vec3<f32>,
// 1 where the bake's rays miss the high poly, 0 where they reach it
@location(2) missed: f32,
};
const HIGH_POLY_COLOR: vec3<f32> = vec3<f32>(0.10, 0.36, 0.56);
const MISSED_COLOR: vec3<f32> = vec3<f32>(0.80, 0.01, 0.01);
const CAGE_COLOR: vec3<f32> = vec3<f32>(0.55, 0.42, 0.26);
const CAGE_OPACITY: f32 = 0.16;
const CAGE_WIRE_COLOR: vec4<f32> = vec4<f32>(0.75, 0.62, 0.42, 0.7);
const UV_SEAM_COLOR: vec4<f32> = vec4<f32>(0.20, 0.85, 0.30, 1.0);
// Brightness of surfaces seen edge-on; facing the camera adds the rest
const HEADLIGHT_AMBIENT: f32 = 0.3;
// Fractions of the way to the camera each vertex moves, so near-coincident surfaces and lines draw in a fixed order
const HIGH_POLY_PULL_TOWARD_EYE: f32 = 0.001;
const CAGE_WIRE_PULL_TOWARD_EYE: f32 = 0.002;
// Lit from the camera: surfaces facing it are brightest
fn headlight(color: vec3<f32>, normal: vec3<f32>, world_position: vec3<f32>) -> vec3<f32> {
let facing = abs(dot(normal, normalize(g.eye.xyz - world_position)));
return color * (HEADLIGHT_AMBIENT + (1.0 - HEADLIGHT_AMBIENT) * facing);
}
fn pulled_toward_eye(world_position: vec3<f32>, fraction: f32) -> vec4<f32> {
return g.view_proj * vec4<f32>(world_position + (g.eye.xyz - world_position) * fraction, 1.0);
}
// Pulled slightly toward the camera: where the high poly nearly touches the low poly it shows instead of flickering with it
@vertex
fn vs_bake_high(@location(0) position: vec3<f32>, @location(1) normal: vec3<f32>, @location(4) missed: f32) -> BakeViewOut {
var out: BakeViewOut;
out.clip = pulled_toward_eye(position, HIGH_POLY_PULL_TOWARD_EYE);
out.world_position = position;
out.normal = normal;
out.missed = missed;
return out;
}
@fragment
fn fs_bake_high(surface: BakeViewOut) -> @location(0) vec4<f32> {
let color = mix(HIGH_POLY_COLOR, MISSED_COLOR, surface.missed);
return vec4<f32>(headlight(color, normalize(surface.normal), surface.world_position), 1.0);
}
@vertex
fn vs_bake_cage(@location(0) position: vec3<f32>) -> BakeViewOut {
var out: BakeViewOut;
out.clip = g.view_proj * vec4<f32>(position, 1.0);
out.world_position = position;
out.normal = vec3<f32>(0.0, 1.0, 0.0);
out.missed = 0.0;
return out;
}
// The cage's own faces: their normal comes from the screen-space change of the position
@fragment
fn fs_bake_cage(surface: BakeViewOut) -> @location(0) vec4<f32> {
let face_normal = normalize(cross(dpdx(surface.world_position), dpdy(surface.world_position)));
return vec4<f32>(headlight(CAGE_COLOR, face_normal, surface.world_position), CAGE_OPACITY);
}
// Pulled toward the camera like the 3D wireframe, so the lines win against the cage's faces
@vertex
fn vs_bake_cage_wire(@location(0) position: vec3<f32>) -> BakeViewOut {
var out: BakeViewOut;
out.clip = pulled_toward_eye(position, CAGE_WIRE_PULL_TOWARD_EYE);
out.world_position = position;
out.normal = vec3<f32>(0.0, 1.0, 0.0);
out.missed = 0.0;
return out;
}
@fragment
fn fs_wire_bake_cage(surface: BakeViewOut) -> @location(0) vec4<f32> {
return CAGE_WIRE_COLOR;
}
@fragment
fn fs_wire_uv_seam(surface: VOut) -> @location(0) vec4<f32> {
return UV_SEAM_COLOR;
}
+37 -32
View File
@@ -65,29 +65,31 @@ fn on_edge(verts: &[Vertex], (p, q): (u32, u32), k: u32, n: u32) -> Option<Verte
/// Split the triangles of `indices` (index ranges `draws`: first, count, material) `level`
/// times. Returns the vertices, indices and the draws over them.
pub(crate) fn subdivide(verts: &[Vertex], indices: &[u32], draws: &[Draw], level: u32) -> (Vec<Vertex>, Vec<u32>, Vec<Draw>) {
let n = 1u32 << level.min(MAX_LEVEL);
let (mut out_v, mut out_i, mut out_d) = (Vec::new(), Vec::new(), Vec::new());
pub(crate) fn subdivide(vertices: &[Vertex], indices: &[u32], draws: &[Draw], level: u32) -> (Vec<Vertex>, Vec<u32>, Vec<Draw>) {
let segments = 1u32 << level.min(MAX_LEVEL);
let (mut output_vertices, mut output_indices, mut output_draws) = (Vec::new(), Vec::new(), Vec::new());
for &(start, count, material) in draws {
let first = out_i.len() as u32;
let tris = indices.get(start as usize..(start as usize).saturating_add(count as usize)).unwrap_or_default();
for t in tris.chunks_exact(3) {
let (Some(a), Some(b), Some(c)) = (verts.get(t[0] as usize), verts.get(t[1] as usize), verts.get(t[2] as usize)) else { continue };
let base = out_v.len() as u32;
// Grid point (i, j), i + j <= n, row by row: row j holds n + 1 - j points. Points on
// the edges are computed the same way from both triangles sharing the edge (no cracks).
for j in 0..=n {
for i in 0..=n - j {
let first = output_indices.len() as u32;
let triangles = indices.get(start as usize..(start as usize).saturating_add(count as usize)).unwrap_or_default();
for t in triangles.as_chunks::<3>().0 {
let (Some(a), Some(b), Some(c)) = (vertices.get(t[0] as usize), vertices.get(t[1] as usize), vertices.get(t[2] as usize)) else { continue };
let base = output_vertices.len() as u32;
// Row j holds segments + 1 - j points inside the triangle
// Shared edges use identical interpolation in both triangles to prevent cracks
for j in 0..=segments {
for i in 0..=segments - j {
let edge = match (i, j) {
(_, 0) => Some(((t[0], t[1]), i)),
(0, _) => Some(((t[0], t[2]), j)),
_ if i + j == n => Some(((t[1], t[2]), j)),
_ if i + j == segments => Some(((t[1], t[2]), j)),
_ => None,
};
let p = match edge.and_then(|(ends, k)| on_edge(verts, ends, k, n)) {
let p = match edge.and_then(|(ends, k)| on_edge(vertices, ends, k, segments)) {
Some(p) => p,
None => {
let w = [1.0 - (i + j) as f32 / n as f32, i as f32 / n as f32, j as f32 / n as f32];
let w = [1.0 - (i + j) as f32 / segments as f32, i as f32 / segments as f32, j as f32 / segments as f32];
Vertex {
pos: mix(a.pos, b.pos, c.pos, w),
nrm: mix(a.nrm, b.nrm, c.nrm, w),
@@ -96,39 +98,42 @@ pub(crate) fn subdivide(verts: &[Vertex], indices: &[u32], draws: &[Draw], level
}
}
};
out_v.push(Vertex { tan: [p.tan[0], p.tan[1], p.tan[2], a.tan[3]], ..p });
output_vertices.push(Vertex { tan: [p.tan[0], p.tan[1], p.tan[2], a.tan[3]], ..p });
}
}
let at = |i: u32, j: u32| base + j * (n + 1) - j * j.saturating_sub(1) / 2 + i;
for j in 0..n {
for i in 0..n - j {
out_i.extend_from_slice(&[at(i, j), at(i + 1, j), at(i, j + 1)]);
if i + j + 1 < n {
out_i.extend_from_slice(&[at(i + 1, j), at(i + 1, j + 1), at(i, j + 1)]);
let at = |i: u32, j: u32| base + j * (segments + 1) - j * j.saturating_sub(1) / 2 + i;
for j in 0..segments {
for i in 0..segments - j {
output_indices.extend_from_slice(&[at(i, j), at(i + 1, j), at(i, j + 1)]);
if i + j + 1 < segments {
output_indices.extend_from_slice(&[at(i + 1, j), at(i + 1, j + 1), at(i, j + 1)]);
}
}
}
}
out_d.push((first, out_i.len() as u32 - first, material));
output_draws.push((first, output_indices.len() as u32 - first, material));
}
(out_v, out_i, out_d)
(output_vertices, output_indices, output_draws)
}
/// World length per unit of UV over the whole mesh (the square root of their area ratio): how
/// far a height change moves the surface for the slope height-to-normal shows.
pub(crate) fn world_per_uv(verts: &[Vertex], indices: &[u32]) -> Option<f32> {
let (mut world, mut uv) = (0.0f64, 0.0f64);
for t in indices.chunks_exact(3) {
let (Some(a), Some(b), Some(c)) = (verts.get(t[0] as usize), verts.get(t[1] as usize), verts.get(t[2] as usize)) else { continue };
pub(crate) fn world_per_uv(vertices: &[Vertex], indices: &[u32]) -> Option<f32> {
let (mut world_area, mut uv_area) = (0.0f64, 0.0f64);
for t in indices.as_chunks::<3>().0 {
let (Some(a), Some(b), Some(c)) = (vertices.get(t[0] as usize), vertices.get(t[1] as usize), vertices.get(t[2] as usize)) else { continue };
let d = |p: [f32; 3], q: [f32; 3]| [f64::from(q[0] - p[0]), f64::from(q[1] - p[1]), f64::from(q[2] - p[2])];
let (e1, e2) = (d(a.pos, b.pos), d(a.pos, c.pos));
let cross = [e1[1] * e2[2] - e1[2] * e2[1], e1[2] * e2[0] - e1[0] * e2[2], e1[0] * e2[1] - e1[1] * e2[0]];
world += (cross[0] * cross[0] + cross[1] * cross[1] + cross[2] * cross[2]).sqrt() * 0.5;
world_area += (cross[0] * cross[0] + cross[1] * cross[1] + cross[2] * cross[2]).sqrt() * 0.5;
let (u1, u2) = ([b.uv[0] - a.uv[0], b.uv[1] - a.uv[1]], [c.uv[0] - a.uv[0], c.uv[1] - a.uv[1]]);
uv += f64::from(u1[0] * u2[1] - u1[1] * u2[0]).abs() * 0.5;
uv_area += f64::from(u1[0] * u2[1] - u1[1] * u2[0]).abs() * 0.5;
}
let r = (world / uv).sqrt() as f32;
(uv > 1e-12 && r.is_finite() && r > 0.0).then_some(r)
let scale = (world_area / uv_area).sqrt() as f32;
(uv_area > 1e-12 && scale.is_finite() && scale > 0.0).then_some(scale)
}
#[cfg(test)]
+8
View File
@@ -0,0 +1,8 @@
/// The low poly's UV seams as a line list over its vertex buffer
pub(crate) struct UvSeamGpu {
/// The low-poly mesh the lines belong to
pub(crate) mesh_id: u64,
pub(crate) lines: wgpu::Buffer,
/// (first index, count, material) per material
pub(crate) line_draws: Vec<(u32, u32, usize)>,
}