summaryrefslogtreecommitdiff
path: root/shared/src/resources.rs
diff options
context:
space:
mode:
Diffstat (limited to 'shared/src/resources.rs')
-rw-r--r--shared/src/resources.rs154
1 files changed, 61 insertions, 93 deletions
diff --git a/shared/src/resources.rs b/shared/src/resources.rs
index 772a140..f422611 100644
--- a/shared/src/resources.rs
+++ b/shared/src/resources.rs
@@ -16,7 +16,7 @@
*/
use crate::{helper::ReadWrite, packets::Resource};
use anyhow::Result;
-use glam::{Affine3A, Vec3A};
+use glam::{Affine3A, Vec2, Vec3A};
use log::warn;
use std::{
collections::BTreeMap,
@@ -27,6 +27,7 @@ use std::{
pub struct Prefab {
pub name: Option<String>,
pub mesh: Vec<(Affine3A, Resource<MeshPart>)>,
+ pub collision: Vec<(Affine3A, Resource<CollisionPart>)>,
pub light: Vec<(Vec3A, Resource<LightPart>)>,
pub environment: Option<Resource<EnvironmentPart>>,
}
@@ -46,7 +47,7 @@ pub struct EnvironmentPart {
#[derive(Debug, Default, Clone)]
pub struct MeshPart {
pub name: Option<String>,
- pub index: Option<Resource<IndexArray>>,
+ pub index: Option<Resource<Vec<[u16; 3]>>>,
pub g_metallic: Option<f32>,
pub g_roughness: Option<f32>,
pub g_albedo: Option<Vec3A>,
@@ -58,15 +59,15 @@ pub struct MeshPart {
pub g_attenuation: Option<Vec3A>,
pub g_dispersion: Option<f32>,
pub g_unlit: Option<()>,
- pub va_position: Option<[Resource<AttributeArray>; 3]>,
- pub va_normal: Option<[Resource<AttributeArray>; 3]>,
- pub va_texcoord: Option<[Resource<AttributeArray>; 2]>,
- pub va_roughness: Option<Resource<AttributeArray>>,
- pub va_metallic: Option<Resource<AttributeArray>>,
- pub va_albedo: Option<[Resource<AttributeArray>; 3]>,
- pub va_transmission: Option<Resource<AttributeArray>>,
- pub va_alpha: Option<Resource<AttributeArray>>,
- pub va_emission: Option<[Resource<AttributeArray>; 3]>,
+ pub va_position: Option<Resource<Vec<Vec3A>>>,
+ pub va_normal: Option<Resource<Vec<Vec3A>>>,
+ pub va_texcoord: Option<Resource<Vec<Vec2>>>,
+ pub va_roughness: Option<Resource<Vec<f32>>>,
+ pub va_metallic: Option<Resource<Vec<f32>>>,
+ pub va_albedo: Option<Resource<Vec<Vec3A>>>,
+ pub va_transmission: Option<Resource<Vec<f32>>>,
+ pub va_alpha: Option<Resource<Vec<f32>>>,
+ pub va_emission: Option<Resource<Vec<Vec3A>>>,
pub tex_normal: Option<Resource<Image>>,
pub tex_roughness: Option<Resource<Image>>,
pub tex_metallic: Option<Resource<Image>>,
@@ -79,12 +80,20 @@ pub struct MeshPart {
}
#[derive(Debug, Default, Clone)]
-pub struct PrefabIndex(pub BTreeMap<String, Resource<Prefab>>);
+pub struct CollisionPart {
+ pub restitution_coeff: Option<f32>,
+ pub friction_kinetic: Option<f32>,
+ pub friction_static: Option<f32>,
+ pub sh_box: Option<Vec3A>,
+ pub sh_sphere: Option<f32>,
+ pub sh_cylinder: Option<(f32, f32, f32)>,
+ pub sh_capsule: Option<(f32, f32, f32)>,
+ pub sh_convex_hull: Option<Resource>,
+ pub sh_mesh: Option<(Resource<Vec<[u16; 3]>>, Resource<Vec<Vec3A>>)>,
+}
#[derive(Debug, Default, Clone)]
-pub struct AttributeArray(pub Vec<f32>);
-#[derive(Debug, Default, Clone)]
-pub struct IndexArray(pub Vec<[u16; 3]>);
+pub struct PrefabIndex(pub BTreeMap<String, Resource<Prefab>>);
#[derive(Debug, Clone)]
pub struct Image(pub Vec<u8>);
@@ -109,12 +118,48 @@ impl ReadWrite for PrefabIndex {
}
}
+impl ReadWrite for CollisionPart {
+ fn write(&self, w: &mut dyn Write) -> Result<()> {
+ write_kv_opt(w, b"restitution_coeff", &self.restitution_coeff)?;
+ write_kv_opt(w, b"friction_kinetic", &self.friction_kinetic)?;
+ write_kv_opt(w, b"friction_static", &self.friction_static)?;
+ write_kv_opt(w, b"sh_box", &self.sh_box)?;
+ write_kv_opt(w, b"sh_sphere", &self.sh_sphere)?;
+ write_kv_opt(w, b"sh_cylinder", &self.sh_cylinder)?;
+ write_kv_opt(w, b"sh_capsule", &self.sh_capsule)?;
+ write_kv_opt(w, b"sh_convex_hull", &self.sh_convex_hull)?;
+ write_kv_opt(w, b"sh_mesh", &self.sh_mesh)?;
+ Ok(())
+ }
+ fn read(r: &mut dyn Read) -> Result<Self> {
+ let mut s = Self::default();
+ read_kv_iter(r, |k, v| match k {
+ b"restitution_coeff" => Ok(s.restitution_coeff = Some(read_slice(v)?)),
+ b"friction_kinetic" => Ok(s.friction_kinetic = Some(read_slice(v)?)),
+ b"friction_static" => Ok(s.friction_static = Some(read_slice(v)?)),
+ b"sh_box" => Ok(s.sh_box = Some(read_slice(v)?)),
+ b"sh_sphere" => Ok(s.sh_sphere = Some(read_slice(v)?)),
+ b"sh_cylinder" => Ok(s.sh_cylinder = Some(read_slice(v)?)),
+ b"sh_capsule" => Ok(s.sh_capsule = Some(read_slice(v)?)),
+ b"sh_convex_hull" => Ok(s.sh_convex_hull = Some(read_slice(v)?)),
+ b"sh_mesh" => Ok(s.sh_mesh = Some(read_slice(v)?)),
+ x => Ok(warn!(
+ "unknown prefab key: {:?}",
+ String::from_utf8_lossy(x)
+ )),
+ })?;
+ Ok(s)
+ }
+}
impl ReadWrite for Prefab {
fn write(&self, w: &mut dyn Write) -> Result<()> {
write_kv_opt(w, b"name", &self.name)?;
for x in &self.mesh {
write_kv_opt(w, b"mesh", &Some(x.clone()))?;
}
+ for x in &self.collision {
+ write_kv_opt(w, b"collision", &Some(x.clone()))?;
+ }
for x in &self.light {
write_kv_opt(w, b"light", &Some(x.clone()))?;
}
@@ -126,6 +171,7 @@ impl ReadWrite for Prefab {
read_kv_iter(r, |k, v| match k {
b"name" => Ok(s.name = Some(read_slice(v)?)),
b"mesh" => Ok(s.mesh.push(read_slice(v)?)),
+ b"collision" => Ok(s.collision.push(read_slice(v)?)),
b"light" => Ok(s.light.push(read_slice(v)?)),
b"environment" => Ok(s.environment = Some(read_slice(v)?)),
x => Ok(warn!(
@@ -284,84 +330,6 @@ fn write_kv(w: &mut dyn Write, key: &[u8], value: &[u8]) -> Result<()> {
Ok(())
}
-impl ReadWrite for u8 {
- fn write(&self, w: &mut dyn Write) -> Result<()> {
- w.write_all(&[*self])?;
- Ok(())
- }
- fn read(r: &mut dyn Read) -> Result<Self> {
- let mut buf = [0u8; 1];
- r.read_exact(&mut buf)?;
- Ok(buf[0])
- }
-}
-
-impl ReadWrite for Vec3A {
- fn write(&self, w: &mut dyn Write) -> Result<()> {
- self.x.write(w)?;
- self.y.write(w)?;
- self.z.write(w)?;
- Ok(())
- }
- fn read(r: &mut dyn Read) -> Result<Self> {
- Ok(Self::new(f32::read(r)?, f32::read(r)?, f32::read(r)?))
- }
-}
-impl ReadWrite for Affine3A {
- fn write(&self, w: &mut dyn Write) -> Result<()> {
- for v in self.to_cols_array() {
- v.write(w)?
- }
- Ok(())
- }
- fn read(r: &mut dyn Read) -> Result<Self> {
- Ok(Self::from_cols_array(&[(); 12].try_map(|()| f32::read(r))?))
- }
-}
-impl ReadWrite for IndexArray {
- fn write(&self, w: &mut dyn Write) -> Result<()> {
- for x in self.0.clone() {
- w.write_all(x.map(|x| x.to_be_bytes()).as_flattened())?;
- }
- Ok(())
- }
- fn read(r: &mut dyn Read) -> Result<Self> {
- let mut s = Self(Vec::new());
- let mut g = Vec::new();
- r.read_to_end(&mut g)?;
- for x in g.iter().array_chunks::<2>().array_chunks::<3>() {
- s.0.push(x.map(|x| u16::from_be_bytes(x.map(|x| *x))))
- }
- Ok(s)
- }
-}
-impl ReadWrite for AttributeArray {
- fn write(&self, w: &mut dyn Write) -> Result<()> {
- for x in self.0.clone() {
- w.write_all(&x.to_be_bytes())?;
- }
- Ok(())
- }
- fn read(r: &mut dyn Read) -> Result<Self> {
- let mut s = Self(Vec::new());
- let mut g = Vec::new();
- r.read_to_end(&mut g)?;
- for x in g.iter().array_chunks::<4>() {
- s.0.push(f32::from_be_bytes(x.map(|x| *x)))
- }
- Ok(s)
- }
-}
-impl<A: ReadWrite, B: ReadWrite> ReadWrite for (A, B) {
- fn write(&self, w: &mut dyn Write) -> Result<()> {
- self.0.write(w)?;
- self.1.write(w)?;
- Ok(())
- }
- fn read(r: &mut dyn Read) -> Result<Self> {
- Ok((A::read(r)?, B::read(r)?))
- }
-}
impl ReadWrite for Image {
fn write(&self, w: &mut dyn Write) -> Result<()> {
self.0.write(w)