diff options
author | metamuffin <metamuffin@disroot.org> | 2025-01-28 15:25:50 +0100 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2025-01-28 15:25:50 +0100 |
commit | 58e3531cd2316e9c73b01221d68834592ad6a2ff (patch) | |
tree | 081b774c8d611d96d88b8589b92490dcc987f89a | |
parent | 3331150162e34471bf0294bdb61a1a748cb94ee5 (diff) | |
download | weareserver-58e3531cd2316e9c73b01221d68834592ad6a2ff.tar weareserver-58e3531cd2316e9c73b01221d68834592ad6a2ff.tar.bz2 weareserver-58e3531cd2316e9c73b01221d68834592ad6a2ff.tar.zst |
Little endian, tangent space handedness,
-rw-r--r-- | client/src/render/shaders/fragment_pbr.wgsl | 7 | ||||
-rw-r--r-- | client/src/render/shaders/vertex_world.wgsl | 10 | ||||
-rw-r--r-- | client/src/render/shaders/vertex_world_skin.wgsl | 10 | ||||
-rw-r--r-- | shared/src/helper.rs | 66 | ||||
-rw-r--r-- | shared/src/packets.rs | 30 | ||||
-rw-r--r-- | shared/src/resources.rs | 8 | ||||
-rw-r--r-- | shared/src/store.rs | 8 | ||||
-rw-r--r-- | world/src/mesh.rs | 6 |
8 files changed, 77 insertions, 68 deletions
diff --git a/client/src/render/shaders/fragment_pbr.wgsl b/client/src/render/shaders/fragment_pbr.wgsl index c8fb857..80d3802 100644 --- a/client/src/render/shaders/fragment_pbr.wgsl +++ b/client/src/render/shaders/fragment_pbr.wgsl @@ -17,8 +17,9 @@ struct VertexOut { @builtin(position) clip: vec4<f32>, @location(0) normal: vec3<f32>, @location(1) tangent: vec3<f32>, - @location(2) texcoord: vec2<f32>, - @location(3) position: vec3<f32>, + @location(2) tangent_binormal_sign: f32, + @location(3) texcoord: vec2<f32>, + @location(4) position: vec3<f32>, } struct Material { @@ -39,7 +40,7 @@ fn main(vo: VertexOut) -> @location(0) vec4<f32> { let t_albedo = textureSample(tex_albedo, tex_albedo_sampler, vo.texcoord); let t_normal = textureSample(tex_normal, tex_normal_sampler, vo.texcoord); - let tangent_basis = mat3x3(vo.tangent, cross(vo.tangent, vo.normal), vo.normal); + let tangent_basis = mat3x3(vo.tangent, vo.tangent_binormal_sign * cross(vo.tangent, vo.normal), vo.normal); let normal = tangent_basis * (t_normal.rgb * 2. - 1.); let light = vec3(0.64, 0.64, 0.64); diff --git a/client/src/render/shaders/vertex_world.wgsl b/client/src/render/shaders/vertex_world.wgsl index 4f342fd..3cce989 100644 --- a/client/src/render/shaders/vertex_world.wgsl +++ b/client/src/render/shaders/vertex_world.wgsl @@ -16,15 +16,16 @@ struct VertexIn { @location(0) position: vec3<f32>, @location(1) normal: vec3<f32>, - @location(2) tangent: vec3<f32>, // TODO maybe compress this + @location(2) tangent: vec4<f32>, // TODO maybe compress this @location(3) texcoord: vec2<f32>, } struct VertexOut { @builtin(position) clip: vec4<f32>, @location(0) normal: vec3<f32>, @location(1) tangent: vec3<f32>, - @location(2) texcoord: vec2<f32>, - @location(3) position: vec3<f32>, + @location(2) tangent_binormal_sign: f32, + @location(3) texcoord: vec2<f32>, + @location(4) position: vec3<f32>, } struct PushConst { @@ -40,7 +41,8 @@ fn main(vi: VertexIn) -> VertexOut { let vo = VertexOut( clip, normalize((pc.model * vec4(vi.normal, 0.)).xyz), - normalize((pc.model * vec4(vi.tangent, 0.)).xyz), + normalize((pc.model * vec4(vi.tangent.xyz, 0.)).xyz), + vi.tangent.w, vi.texcoord, (pc.model * vec4(vi.position, 1.)).xyz, ); diff --git a/client/src/render/shaders/vertex_world_skin.wgsl b/client/src/render/shaders/vertex_world_skin.wgsl index 6e2b308..2188cf4 100644 --- a/client/src/render/shaders/vertex_world_skin.wgsl +++ b/client/src/render/shaders/vertex_world_skin.wgsl @@ -16,7 +16,7 @@ struct VertexIn { @location(0) position: vec3<f32>, @location(1) normal: vec3<f32>, - @location(2) tangent: vec3<f32>, // TODO maybe compress this + @location(2) tangent: vec4<f32>, // TODO maybe compress this @location(3) texcoord: vec2<f32>, @location(4) joint_index: vec4<u32>, @location(5) joint_weight: vec4<f32>, @@ -25,8 +25,9 @@ struct VertexOut { @builtin(position) clip: vec4<f32>, @location(0) normal: vec3<f32>, @location(1) tangent: vec3<f32>, - @location(2) texcoord: vec2<f32>, - @location(3) position: vec3<f32>, + @location(2) tangent_binormal_sign: f32, + @location(3) texcoord: vec2<f32>, + @location(4) position: vec3<f32>, } struct PushConst { @@ -50,7 +51,8 @@ fn main(vi: VertexIn) -> VertexOut { let vo = VertexOut( clip, normalize((pc.model * vec4(vi.normal, 0.)).xyz), - normalize((pc.model * vec4(vi.tangent, 0.)).xyz), + normalize((pc.model * vec4(vi.tangent.xyz, 0.)).xyz), + vi.tangent.w, vi.texcoord, (pc.model * vec4(vi.position, 1.)).xyz, ); diff --git a/shared/src/helper.rs b/shared/src/helper.rs index be7d929..873c9d8 100644 --- a/shared/src/helper.rs +++ b/shared/src/helper.rs @@ -36,24 +36,24 @@ pub trait ReadWrite: Sized { impl ReadWrite for f32 { fn write(&self, w: &mut dyn Write) -> Result<()> { - w.write_all(&self.to_be_bytes())?; + w.write_all(&self.to_le_bytes())?; Ok(()) } fn read(r: &mut dyn Read) -> Result<Self> { let mut buf = [0; { size_of::<f32>() }]; r.read_exact(&mut buf)?; - Ok(f32::from_be_bytes(buf)) + Ok(f32::from_le_bytes(buf)) } } impl ReadWrite for u32 { fn write(&self, w: &mut dyn Write) -> Result<()> { - w.write_all(&self.to_be_bytes())?; + w.write_all(&self.to_le_bytes())?; Ok(()) } fn read(r: &mut dyn Read) -> Result<Self> { let mut buf = [0; { size_of::<u32>() }]; r.read_exact(&mut buf)?; - Ok(u32::from_be_bytes(buf)) + Ok(u32::from_le_bytes(buf)) } } impl ReadWrite for Vec<u8> { @@ -97,7 +97,7 @@ impl ReadWrite for Vec<Vec3A> { Ok(buf .into_iter() .array_chunks::<{ size_of::<f32>() }>() - .map(f32::from_be_bytes) + .map(f32::from_le_bytes) .array_chunks::<3>() .map(Vec3A::from_array) .collect()) @@ -116,7 +116,7 @@ impl ReadWrite for Vec<Vec3> { Ok(buf .into_iter() .array_chunks::<{ size_of::<f32>() }>() - .map(f32::from_be_bytes) + .map(f32::from_le_bytes) .array_chunks::<3>() .map(Vec3::from_array) .collect()) @@ -135,7 +135,7 @@ impl ReadWrite for Vec<Vec4> { Ok(buf .into_iter() .array_chunks::<{ size_of::<f32>() }>() - .map(f32::from_be_bytes) + .map(f32::from_le_bytes) .array_chunks::<4>() .map(Vec4::from_array) .collect()) @@ -154,7 +154,7 @@ impl ReadWrite for Vec<Vec2> { Ok(buf .into_iter() .array_chunks::<{ size_of::<f32>() }>() - .map(f32::from_be_bytes) + .map(f32::from_le_bytes) .array_chunks::<2>() .map(Vec2::from_array) .collect()) @@ -163,9 +163,9 @@ impl ReadWrite for Vec<Vec2> { impl ReadWrite for Vec<[u32; 3]> { fn write(&self, w: &mut dyn Write) -> Result<()> { for e in self { - w.write_all(&e[0].to_be_bytes())?; - w.write_all(&e[1].to_be_bytes())?; - w.write_all(&e[2].to_be_bytes())?; + w.write_all(&e[0].to_le_bytes())?; + w.write_all(&e[1].to_le_bytes())?; + w.write_all(&e[2].to_le_bytes())?; } Ok(()) } @@ -175,7 +175,7 @@ impl ReadWrite for Vec<[u32; 3]> { Ok(buf .into_iter() .array_chunks::<{ size_of::<u32>() }>() - .map(u32::from_be_bytes) + .map(u32::from_le_bytes) .array_chunks::<3>() .collect()) } @@ -183,10 +183,10 @@ impl ReadWrite for Vec<[u32; 3]> { impl ReadWrite for Vec<[u16; 4]> { fn write(&self, w: &mut dyn Write) -> Result<()> { for e in self { - w.write_all(&e[0].to_be_bytes())?; - w.write_all(&e[1].to_be_bytes())?; - w.write_all(&e[2].to_be_bytes())?; - w.write_all(&e[3].to_be_bytes())?; + w.write_all(&e[0].to_le_bytes())?; + w.write_all(&e[1].to_le_bytes())?; + w.write_all(&e[2].to_le_bytes())?; + w.write_all(&e[3].to_le_bytes())?; } Ok(()) } @@ -196,7 +196,7 @@ impl ReadWrite for Vec<[u16; 4]> { Ok(buf .into_iter() .array_chunks::<{ size_of::<u16>() }>() - .map(u16::from_be_bytes) + .map(u16::from_le_bytes) .array_chunks::<4>() .collect()) } @@ -204,10 +204,10 @@ impl ReadWrite for Vec<[u16; 4]> { impl ReadWrite for Vec<[f32; 4]> { fn write(&self, w: &mut dyn Write) -> Result<()> { for e in self { - w.write_all(&e[0].to_be_bytes())?; - w.write_all(&e[1].to_be_bytes())?; - w.write_all(&e[2].to_be_bytes())?; - w.write_all(&e[3].to_be_bytes())?; + w.write_all(&e[0].to_le_bytes())?; + w.write_all(&e[1].to_le_bytes())?; + w.write_all(&e[2].to_le_bytes())?; + w.write_all(&e[3].to_le_bytes())?; } Ok(()) } @@ -217,7 +217,7 @@ impl ReadWrite for Vec<[f32; 4]> { Ok(buf .into_iter() .array_chunks::<{ size_of::<f32>() }>() - .map(f32::from_be_bytes) + .map(f32::from_le_bytes) .array_chunks::<4>() .collect()) } @@ -235,7 +235,7 @@ impl ReadWrite for Vec<f32> { Ok(buf .into_iter() .array_chunks::<{ size_of::<f32>() }>() - .map(f32::from_be_bytes) + .map(f32::from_le_bytes) .collect()) } } @@ -252,7 +252,7 @@ impl ReadWrite for Vec<u16> { Ok(buf .into_iter() .array_chunks::<{ size_of::<u16>() }>() - .map(u16::from_be_bytes) + .map(u16::from_le_bytes) .collect()) } } @@ -269,7 +269,7 @@ impl ReadWrite for Vec<Affine3A> { Ok(buf .into_iter() .array_chunks::<{ size_of::<f32>() }>() - .map(f32::from_be_bytes) + .map(f32::from_le_bytes) .array_chunks::<12>() .map(|m| Affine3A::from_cols_array(&m)) .collect()) @@ -310,14 +310,14 @@ impl ReadWrite for String { impl ReadWrite for Data { fn write(&self, w: &mut dyn Write) -> Result<()> { - w.write_all(&(self.0.len() as u32).to_be_bytes())?; + w.write_all(&(self.0.len() as u32).to_le_bytes())?; w.write_all(&self.0)?; Ok(()) } fn read(r: &mut dyn Read) -> Result<Self> { let mut size = [0; { size_of::<u32>() }]; r.read_exact(&mut size)?; - let size = u32::from_be_bytes(size); + let size = u32::from_le_bytes(size); let mut buf = vec![0; size as usize]; r.read_exact(&mut buf)?; Ok(Self(buf)) @@ -325,14 +325,14 @@ impl ReadWrite for Data { } impl ReadWrite for Message { fn write(&self, w: &mut dyn Write) -> Result<()> { - w.write_all(&(self.0.len() as u32).to_be_bytes())?; + w.write_all(&(self.0.len() as u32).to_le_bytes())?; w.write_all(self.0.as_bytes())?; Ok(()) } fn read(r: &mut dyn Read) -> Result<Self> { let mut size = [0; { size_of::<u32>() }]; r.read_exact(&mut size)?; - let size = u32::from_be_bytes(size); + let size = u32::from_le_bytes(size); let mut buf = vec![0; size as usize]; r.read_exact(&mut buf)?; Ok(Self(String::from_utf8_lossy_owned(buf))) @@ -351,13 +351,13 @@ impl<T> ReadWrite for Resource<T> { } impl ReadWrite for Object { fn write(&self, w: &mut dyn Write) -> Result<()> { - w.write_all(&self.0.to_be_bytes())?; + w.write_all(&self.0.to_le_bytes())?; Ok(()) } fn read(r: &mut dyn Read) -> Result<Self> { let mut s = [0; 16]; r.read_exact(&mut s)?; - Ok(Object(u128::from_be_bytes(s))) + Ok(Object(u128::from_le_bytes(s))) } } impl<T: ReadWrite, const N: usize> ReadWrite for [T; N] { @@ -474,12 +474,12 @@ impl ReadWrite for u8 { } impl ReadWrite for u16 { fn write(&self, w: &mut dyn Write) -> Result<()> { - w.write_all(&self.to_be_bytes())?; + w.write_all(&self.to_le_bytes())?; Ok(()) } fn read(r: &mut dyn Read) -> Result<Self> { let mut buf = [0u8; 2]; r.read_exact(&mut buf)?; - Ok(u16::from_be_bytes(buf)) + Ok(u16::from_le_bytes(buf)) } } diff --git a/shared/src/packets.rs b/shared/src/packets.rs index dad09ce..b979460 100644 --- a/shared/src/packets.rs +++ b/shared/src/packets.rs @@ -82,7 +82,7 @@ impl Packet { match self { Packet::Connect(id) => { w.write_all(&[0x00])?; - w.write_all(&id.to_be_bytes())?; + w.write_all(&id.to_le_bytes())?; } Packet::Disconnect => { w.write_all(&[0xff])?; @@ -98,7 +98,7 @@ impl Packet { } Packet::Add(object, resource) => { w.write_all(&[0x03])?; - w.write_all(&object.0.to_be_bytes())?; + w.write_all(&object.0.to_le_bytes())?; w.write_all(&resource.0)?; } Packet::Remove(object) => { @@ -107,23 +107,23 @@ impl Packet { } Packet::Position(object, pos, rot) => { w.write_all(&[0x05])?; - w.write_all(&object.0.to_be_bytes())?; + w.write_all(&object.0.to_le_bytes())?; pos.write(w)?; rot.write(w)?; } Packet::Pose(object, vec) => { w.write_all(&[0x06])?; - w.write_all(&object.0.to_be_bytes())?; - w.write_all(&(vec.len() as u16).to_be_bytes())?; + w.write_all(&object.0.to_le_bytes())?; + w.write_all(&(vec.len() as u16).to_le_bytes())?; } Packet::Parent(parent, child) => { w.write_all(&[0x07])?; - w.write_all(&parent.0.to_be_bytes())?; - w.write_all(&child.0.to_be_bytes())?; + w.write_all(&parent.0.to_le_bytes())?; + w.write_all(&child.0.to_le_bytes())?; } Packet::Sound(object, data) => { w.write_all(&[0x08])?; - w.write_all(&object.0.to_be_bytes())?; + w.write_all(&object.0.to_le_bytes())?; data.write(w)?; } Packet::PrefabIndex(resource) => { @@ -147,7 +147,7 @@ impl ReadWrite for Packet { fn write(&self, w: &mut dyn Write) -> Result<()> { let mut buf = Vec::new(); self.serialize_inner(&mut buf)?; - w.write_all(&(buf.len() as u32).to_be_bytes())?; + w.write_all(&(buf.len() as u32).to_le_bytes())?; w.write_all(&buf)?; Ok(()) } @@ -183,13 +183,13 @@ impl ReadWrite for Packet { fn read_u128(r: &mut dyn Read) -> Result<u128> { let mut buf = [0; 16]; r.read_exact(&mut buf)?; - Ok(u128::from_be_bytes(buf)) + Ok(u128::from_le_bytes(buf)) } fn read_params(r: &mut dyn Read) -> Result<Vec<f32>> { let mut size = [0; 2]; r.read_exact(&mut size)?; - let size = u16::from_be_bytes(size); + let size = u16::from_le_bytes(size); let mut v = Vec::with_capacity(size as usize); for _ in 0..size { v.push(f32::read(r)?); @@ -202,10 +202,10 @@ impl<T> Display for Resource<T> { write!( f, "Res{{{:016x}{:016x}{:016x}{:016x}}}", - u64::from_be_bytes(self.0[0..8].try_into().unwrap()), - u64::from_be_bytes(self.0[8..16].try_into().unwrap()), - u64::from_be_bytes(self.0[16..24].try_into().unwrap()), - u64::from_be_bytes(self.0[24..32].try_into().unwrap()), + u64::from_le_bytes(self.0[0..8].try_into().unwrap()), + u64::from_le_bytes(self.0[8..16].try_into().unwrap()), + u64::from_le_bytes(self.0[16..24].try_into().unwrap()), + u64::from_le_bytes(self.0[24..32].try_into().unwrap()), ) } } diff --git a/shared/src/resources.rs b/shared/src/resources.rs index f2b7859..1dc326e 100644 --- a/shared/src/resources.rs +++ b/shared/src/resources.rs @@ -345,8 +345,8 @@ fn read_kv(r: &mut &[u8]) -> Result<(Vec<u8>, Vec<u8>)> { let mut value_size = [0; { size_of::<u16>() }]; r.read_exact(&mut key_size)?; r.read_exact(&mut value_size)?; - let key_size = u16::from_be_bytes(key_size); - let value_size = u16::from_be_bytes(value_size); + let key_size = u16::from_le_bytes(key_size); + let value_size = u16::from_le_bytes(value_size); let mut key = vec![0; key_size as usize]; let mut value = vec![0; value_size as usize]; r.read_exact(&mut key)?; @@ -375,8 +375,8 @@ fn write_kv_opt(w: &mut dyn Write, key: &[u8], value: &Option<impl ReadWrite>) - Ok(()) } fn write_kv(w: &mut dyn Write, key: &[u8], value: &[u8]) -> Result<()> { - w.write_all(&(key.len() as u16).to_be_bytes())?; - w.write_all(&(value.len() as u16).to_be_bytes())?; + w.write_all(&(key.len() as u16).to_le_bytes())?; + w.write_all(&(value.len() as u16).to_le_bytes())?; w.write_all(key)?; w.write_all(value)?; Ok(()) diff --git a/shared/src/store.rs b/shared/src/store.rs index b0dbe45..3db6bac 100644 --- a/shared/src/store.rs +++ b/shared/src/store.rs @@ -161,9 +161,9 @@ pub fn resource_hash(x: &[u8]) -> [u8; 32] { fn fs_cache_path(path: &Path, res: Resource) -> PathBuf { path.join(format!( "{:016x}{:016x}{:016x}{:016x}", - u64::from_be_bytes(res.0[0..8].try_into().unwrap()), - u64::from_be_bytes(res.0[8..16].try_into().unwrap()), - u64::from_be_bytes(res.0[16..24].try_into().unwrap()), - u64::from_be_bytes(res.0[24..32].try_into().unwrap()), + u64::from_le_bytes(res.0[0..8].try_into().unwrap()), + u64::from_le_bytes(res.0[8..16].try_into().unwrap()), + u64::from_le_bytes(res.0[16..24].try_into().unwrap()), + u64::from_le_bytes(res.0[24..32].try_into().unwrap()), )) } diff --git a/world/src/mesh.rs b/world/src/mesh.rs index 0d90b82..8afefb4 100644 --- a/world/src/mesh.rs +++ b/world/src/mesh.rs @@ -20,7 +20,10 @@ use gltf::{Mesh, Node, buffer::Data}; use log::{debug, info}; use std::path::Path; use weareshared::{ - resources::{Armature, MeshPart, Prefab}, store::ResourceStore, vec2, vec3a, vec4, Affine3A, Vec3A + Affine3A, Vec3A, + resources::{Armature, MeshPart, Prefab}, + store::ResourceStore, + vec2, vec3a, vec4, }; pub fn import_mesh( @@ -138,6 +141,7 @@ pub fn import_mesh( .into_u32() .array_chunks::<3>() .collect::<Vec<_>>(); + debug!("{} indecies", index.len() * 3); let index = Some(store.set(&index)?); |