From 77486da1c97c988205c8bb117b1168a1a0ef6a46 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Thu, 9 Jan 2025 22:45:13 +0100 Subject: occlusion --- doc/resources.md | 6 ++++-- shared/src/resources.rs | 3 +++ world/src/main.rs | 8 +++----- world/src/mesh.rs | 19 +++++++++++++++++-- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/doc/resources.md b/doc/resources.md index 6be292b..ca30d4c 100644 --- a/doc/resources.md +++ b/doc/resources.md @@ -63,7 +63,8 @@ white except normals are zero. | tex_alpha | Resource | Use alpha channel | | tex_transmission | Resource | Use red channel | | tex_emission | Resource | Use color channels | -| tex_thickness | Resource | Use green channels | +| tex_thickness | Resource | Use green channel | +| tex_occlusion | Resource | Use red channel | - **Attenuation**: Attenuation coefficient for each color channel due to scattering within the material volume expressed as e-folding distance (m^-1). @@ -76,7 +77,7 @@ white except normals are zero. - **Dispersion**: 20 / Abbe Number. Equivalent to `dispersion` of [KHR_materials_dispersion]. - **Unlit**: Directly transfers \*_albedo to the screen if set. No lighting - shaders are applied. + shaders are applied. See [KHR_materials_unlit] ## LightPart @@ -110,3 +111,4 @@ WebP [KHR_materials_volume]: https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_materials_volume [KHR_materials_dispersion]: https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_materials_dispersion [KHR_lights_punctual]: https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_lights_punctual +[KHR_lights_unlit]: https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_lights_unlit diff --git a/shared/src/resources.rs b/shared/src/resources.rs index 7a52e85..772a140 100644 --- a/shared/src/resources.rs +++ b/shared/src/resources.rs @@ -75,6 +75,7 @@ pub struct MeshPart { pub tex_alpha: Option>, pub tex_emission: Option>, pub tex_thickness: Option>, + pub tex_occlusion: Option>, } #[derive(Debug, Default, Clone)] @@ -201,6 +202,7 @@ impl ReadWrite for MeshPart { write_kv_opt(w, b"tex_transmission", &self.tex_transmission)?; write_kv_opt(w, b"tex_alpha", &self.tex_alpha)?; write_kv_opt(w, b"tex_emission", &self.tex_emission)?; + write_kv_opt(w, b"tex_occlusion", &self.tex_occlusion)?; Ok(()) } fn read(r: &mut dyn Read) -> Result { @@ -230,6 +232,7 @@ impl ReadWrite for MeshPart { b"tex_transmission" => Ok(s.tex_transmission = Some(read_slice(v)?)), b"tex_alpha" => Ok(s.tex_alpha = Some(read_slice(v)?)), b"tex_emission" => Ok(s.tex_emission = Some(read_slice(v)?)), + b"tex_occlusion" => Ok(s.tex_occlusion = Some(read_slice(v)?)), x => Ok(warn!( "unknown mesh part key: {:?}", String::from_utf8_lossy(x) diff --git a/world/src/main.rs b/world/src/main.rs index af32222..694d57b 100644 --- a/world/src/main.rs +++ b/world/src/main.rs @@ -78,12 +78,10 @@ fn main() -> Result<()> { Packet::Connect(random()).write(&mut sock)?; - // let (gltf, buffers, _) = gltf::import(&args.scene)?; let path_base = args.scene.parent().unwrap(); - - let gltf = Gltf::from_reader_without_validation(File::open(&args.scene)?)?; - - let buffers = import_buffers(&gltf, Some(path_base), None)?; + let mut gltf = Gltf::from_reader_without_validation(File::open(&args.scene)?)?; + let blob = gltf.blob.take(); + let buffers = import_buffers(&gltf, Some(path_base), blob)?; let mut prefab = Prefab::default(); diff --git a/world/src/mesh.rs b/world/src/mesh.rs index 1f19c28..7d17cc9 100644 --- a/world/src/mesh.rs +++ b/world/src/mesh.rs @@ -211,6 +211,17 @@ pub fn import_mesh( webp, )?); } + let mut tex_occlusion = None; + if let Some(tex) = p.material().occlusion_texture() { + tex_occlusion = Some(load_texture( + "occlusion", + &store, + path_base, + &buffers, + &tex.texture().source().source(), + webp, + )?); + } let mut tex_roughness = None; let mut tex_metallic = None; if let Some(tex) = p @@ -295,10 +306,12 @@ pub fn import_mesh( let g_attenuation = p.material().volume().map(|v| { let ref_dist = v.attenuation_distance(); - Vec3A::from_array(v.attenuation_color().map( + let att = Vec3A::from_array(v.attenuation_color().map( // manually derived from attenuation coefficient formula. i hope this is correct. |factor| -(factor.powf(1. / ref_dist)).ln(), - )) + )); + info!("attenuation is {att}"); + att }); let g_refractive_index = p.material().ior(); let g_thickness = p.material().volume().map(|v| v.thickness_factor()); @@ -314,6 +327,7 @@ pub fn import_mesh( .map(|e| e.contains_key("KHR_materials_unlit")) .unwrap_or(false) { + info!("unlit"); Some(()) } else { None @@ -346,6 +360,7 @@ pub fn import_mesh( tex_emission, tex_transmission, tex_thickness, + tex_occlusion, // not supported by gltf va_transmission: None, va_emission: None, -- cgit v1.2.3-70-g09d2