SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Render preview: near-black colours match the exported image (#5761) (#13219)
Kevin Ravensberga2d42fdaec86
diff --git a/assets/shaders/viewport.wgsl b/assets/shaders/viewport.wgslindex ca7d8f376d..8031f4a080 100644--- a/assets/shaders/viewport.wgsl+++ b/assets/shaders/viewport.wgsl@@ -716,14 +716,20 @@ fn lit_color(i: TOut, visibility: f32, base: vec4<f32>) -> vec4<f32> {if (dot(n, v) < 0.0) { n = -n; }if u.product_lights_meta.x>0.0 {var radiance=vec3<f32>(0.0);+ let albedo=srgb_decode(base.rgb);for (var k=0u;k<min(u32(u.product_lights_meta.y),21u);k+=1u) {- if u.product_lights[k*4u].w == 4.0 {radiance+=to_linear(base.rgb)*u.product_lights[k*4u+1u].xyz/3.141592653589793;continue;}+ if u.product_lights[k*4u].w == 4.0 {radiance+=albedo*u.product_lights[k*4u+1u].xyz/3.141592653589793;continue;}let sample=product_light_sample(k,i.wp);- radiance+=to_linear(base.rgb)*sample[1]*(max(dot(n,sample[0]),0.0)/3.141592653589793);+ radiance+=albedo*sample[1]*(max(dot(n,sample[0]),0.0)/3.141592653589793);}let mapped=radiance/(vec3<f32>(1.0)+radiance);+ // A linear attachment takes linear light as it is; any other is encoded once, as the CPU+ // image is (decoding the encoded value again through `out_color` would flatten the dark end).+ if (u.back.w > 0.5) {+ return vec4<f32>(mapped,base.a);+ }let encoded=select(mapped*12.92,1.055*pow(mapped,vec3<f32>(1.0/2.4))-vec3<f32>(0.055),mapped>vec3<f32>(0.0031308));- return out_color(vec4<f32>(encoded,base.a));+ return vec4<f32>(encoded,base.a);}let key = normalize(v * 0.8 + vec3<f32>(-0.3, 0.2, 0.6));let diff = max(dot(n, key), 0.0);diff --git a/crates/ui-egui/src/gpu.rs b/crates/ui-egui/src/gpu.rsindex 180c62d697..a6e9b12e98 100644--- a/crates/ui-egui/src/gpu.rs+++ b/crates/ui-egui/src/gpu.rs@@ -570,6 +570,7 @@ pub(crate) fn render_offscreen(size,Offscreen {tess,+ product_lights: None,product: false,srgb: false,analysis,@@ -590,6 +591,7 @@ pub(crate) fn render_product_offscreen(eye: [f32; 4],size: u32,srgb: bool,+ product_lights: Option<Vec<[f32; 16]>>,) -> Option<Vec<u8>> {offscreen(scene,@@ -600,6 +602,7 @@ pub(crate) fn render_product_offscreen(size,Offscreen {tess: None,+ product_lights,product: true,srgb,analysis: [0.0; 8],@@ -614,6 +617,9 @@ pub(crate) fn render_product_offscreen(#[cfg(all(test, not(target_arch = "wasm32")))]struct Offscreen<'a> {tess: Option<&'a str>,+ /// The Render workspace's analytic preview lights (`Frame::product_lights`); `None` draws the+ /// product without them.+ product_lights: Option<Vec<[f32; 16]>>,product: bool,srgb: bool,analysis: [f32; 8],@@ -642,6 +648,7 @@ pub(crate) fn render_ao_offscreen(size,Offscreen {tess,+ product_lights: None,product: false,srgb: false,analysis: [0.0; 8],@@ -672,6 +679,7 @@ pub(crate) fn render_offscreen_ssaa(size,Offscreen {tess: None,+ product_lights: None,product: false,srgb: false,analysis: [0.0; 8],@@ -702,7 +710,17 @@ pub(crate) fn render_offscreen_ao(back,eye,size,- Offscreen { tess: None, product: false, srgb: false, analysis: [0.0; 8], analysis_colors: [0.0; 36], ssaa: 1.0, ao_radius, ao_samples },+ Offscreen {+ tess: None,+ product_lights: None,+ product: false,+ srgb: false,+ analysis: [0.0; 8],+ analysis_colors: [0.0; 36],+ ssaa: 1.0,+ ao_radius,+ ao_samples,+ },)}@@ -716,7 +734,7 @@ fn offscreen(size: u32,options: Offscreen<'_>,) -> Option<Vec<u8>> {- let Offscreen { tess, product, srgb, analysis, analysis_colors, ssaa, ao_radius, ao_samples } = options;+ let Offscreen { tess, product_lights, product, srgb, analysis, analysis_colors, ssaa, ao_radius, ao_samples } = options;let (adapter, device, queue) = offscreen_device()?;eprintln!("offscreen adapter: {:?}", adapter.get_info().backend);let format = if srgb { wgpu::TextureFormat::Rgba8UnormSrgb } else { wgpu::TextureFormat::Rgba8Unorm };@@ -727,7 +745,7 @@ fn offscreen(resources.insert(Renderer::new(&device, target, compute));let none = || Arc::new(Mutex::new(None));let cb = ViewportCallback(Frame {- product_lights: None,+ product_lights,product,upload_error: None,key: 1,diff --git a/crates/ui-egui/src/render_workspace_tests.rs b/crates/ui-egui/src/render_workspace_tests.rsindex a692343c37..f97a96f303 100644--- a/crates/ui-egui/src/render_workspace_tests.rs+++ b/crates/ui-egui/src/render_workspace_tests.rs@@ -1356,6 +1356,7 @@ fn opaque_pbr_product_gpu_and_cpu_share_color_and_material_semantics() {[eye[0], eye[1], eye[2], 0.0],SIZE,srgb,+ None,) else {eprintln!("skipping: no GPU adapter");return;@@ -1387,6 +1388,65 @@ fn opaque_pbr_product_gpu_and_cpu_share_color_and_material_semantics() {}}+#[cfg(not(target_arch = "wasm32"))]+#[test]+fn product_preview_lights_decode_simple_colours_as_the_cpu_image_does() {+ use solidcraft_engine::render::StandardView;+ // The CPU image's transfer (`job::image` decode/encode, `studio::tone_map`).+ let decode = |s: f64| if s <= 0.04045 { s / 12.92 } else { ((s + 0.055) / 1.055).powf(2.4) };+ let encode = |l: f64| if l <= 0.0031308 { l * 12.92 } else { 1.055 * l.powf(1.0 / 2.4) - 0.055 };+ // One uniform light of radiance π: every lit pixel is the albedo itself, whatever its normal.+ let mut light = [0.0_f32; 16];+ light[3] = 4.0;+ light[4..7].fill(std::f32::consts::PI);+ const SIZE: u32 = 64;+ let mut s = Session::default();+ s.execute("solid.sphere", &json!({"radius":10,"body_name":"Sphere"})).unwrap();+ // Black, both sides of the decoder's 0.04045 knee (#0a = 0.0392, #0b = 0.0431), and mid-tones.+ for hex in ["000000", "050505", "0a0a0a", "0b0b0b", "202020", "808080", "b47846"] {+ s.execute("appearance.assign", &json!({"bodies":["Sphere"],"color":format!("#{hex}")})).unwrap();+ let scene = solidcraft_engine::product_scene::extract(&s).unwrap();+ let (_, gpu) = gpu_scene(&scene).unwrap();+ let mut camera = Camera { fov: 0.0, ..Default::default() };+ camera.set_view(StandardView::Iso);+ camera.fit(&scene.bounds);+ let radius = scene.bounds.diagonal().max(10.0) + scene.bounds.center().dist(camera.target);+ let eye = camera.eye().to_f32();+ let expected: Vec<u8> = (0..3)+ .map(|c| {+ let l = decode(f64::from(u8::from_str_radix(&hex[2 * c..2 * c + 2], 16).unwrap()) / 255.0);+ (encode(l / (1.0 + l)) * 255.0).round() as u8+ })+ .collect();+ for srgb in [false, true] {+ let Some(pixels) = crate::gpu::render_product_offscreen(+ gpu.clone(),+ None,+ camera.view_proj(1.0, radius).to_f32(),+ camera.back().to_f32(),+ [eye[0], eye[1], eye[2], 0.0],+ SIZE,+ srgb,+ Some(vec![light]),+ ) else {+ eprintln!("skipping: no GPU adapter");+ return;+ };+ // The middle of the sphere, well inside its silhouette.+ for y in SIZE / 2 - 4..SIZE / 2 + 4 {+ for x in SIZE / 2 - 4..SIZE / 2 + 4 {+ let i = ((y * SIZE + x) * 4) as usize;+ let got = &pixels[i..i + 3];+ assert!(+ got.iter().zip(&expected).all(|(a, b)| a.abs_diff(*b) <= 1),+ "#{hex} srgb={srgb}: preview {got:?}, CPU image {expected:?}"+ );+ }+ }+ }+ }+}+#[test]fn control_render_export_respects_upstream_file_scope() {let mut h = Harness::new();@@ -5787,6 +5847,7 @@ fn render_workspace_product_pass_draws_only_requested_face_or_body_overlay() {[eye[0], eye[1], eye[2], 1.0],256,false,+ None,)};let Some(clean) = draw(None) else {