summaryrefslogtreecommitdiff
path: root/shared
diff options
context:
space:
mode:
Diffstat (limited to 'shared')
-rw-r--r--shared/src/packets.rs27
-rw-r--r--shared/src/resources.rs119
2 files changed, 72 insertions, 74 deletions
diff --git a/shared/src/packets.rs b/shared/src/packets.rs
index 0f1a886..07c4345 100644
--- a/shared/src/packets.rs
+++ b/shared/src/packets.rs
@@ -103,14 +103,13 @@ 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 u16).to_be_bytes())?;
+ w.write_all(&(buf.len() as u32).to_be_bytes())?;
w.write_all(&buf)?;
Ok(())
}
fn read(r: &mut dyn Read) -> Result<Self> {
- let mut size_tag = [0u8; 3];
- r.read_exact(&mut size_tag)?;
- Ok(match size_tag[2] {
+ let packet_len = u32::read(r)?;
+ Ok(match u8::read(r)? {
0x00 => Packet::Connect(read_u128(r)?),
0x01 => Packet::RequestResource(Resource::read(r)?),
0x02 => Packet::RespondResource(Data::read(r)?),
@@ -125,8 +124,7 @@ impl ReadWrite for Packet {
0x07 => Packet::Parent(Object(read_u128(r)?), Object(read_u128(r)?)),
0x08 => Packet::Sound(Object(read_u128(r)?), Data::read(r)?),
_ => {
- let len = u16::from_be_bytes([size_tag[0], size_tag[1]]);
- for _ in 0..len.max(1) - 1 {
+ for _ in 0..packet_len.max(1) - 1 {
r.read_exact(&mut [0])?;
}
bail!("unknown packet tag");
@@ -153,14 +151,14 @@ impl ReadWrite for Resource {
}
impl ReadWrite for Data {
fn write(&self, w: &mut dyn Write) -> Result<()> {
- w.write_all(&(self.0.len() as u16).to_be_bytes())?;
+ w.write_all(&(self.0.len() as u32).to_be_bytes())?;
w.write_all(&self.0)?;
Ok(())
}
fn read(r: &mut dyn Read) -> Result<Self> {
- let mut size = [0; 2];
+ let mut size = [0; 4];
r.read_exact(&mut size)?;
- let size = u16::from_be_bytes(size);
+ let size = u32::from_be_bytes(size);
let mut buf = vec![0; size as usize];
r.read_exact(&mut buf)?;
Ok(Data(buf))
@@ -188,6 +186,17 @@ impl ReadWrite for f32 {
Ok(f32::from_be_bytes(buf))
}
}
+impl ReadWrite for u32 {
+ fn write(&self, w: &mut dyn Write) -> Result<()> {
+ w.write_all(&self.to_be_bytes())?;
+ Ok(())
+ }
+ fn read(r: &mut dyn Read) -> Result<Self> {
+ let mut buf = [0; 4];
+ r.read_exact(&mut buf)?;
+ Ok(u32::from_be_bytes(buf))
+ }
+}
impl Display for Resource {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
diff --git a/shared/src/resources.rs b/shared/src/resources.rs
index a071f1c..4c6321f 100644
--- a/shared/src/resources.rs
+++ b/shared/src/resources.rs
@@ -1,5 +1,5 @@
use crate::packets::{ReadWrite, Resource};
-use anyhow::{Result, bail};
+use anyhow::Result;
use glam::{Affine3A, Vec3A};
use log::warn;
use std::io::{Read, Write};
@@ -7,23 +7,27 @@ use std::io::{Read, Write};
#[derive(Debug, Default, Clone)]
pub struct Prefab(pub Vec<(Affine3A, Resource)>);
+/// Combinations of va_* and tex_* are multiplied except normal which is added.
+/// Defaults should be the identity for that operation, so default is 1 (or white) except normals are Vec3::ZERO.
#[derive(Debug, Default, Clone)]
pub struct Part {
pub index: Option<Resource>,
- pub va_position: Option<[Attribute; 3]>,
- pub va_normal: Option<[Attribute; 3]>,
- pub va_texcoord: Option<[Attribute; 2]>,
- pub va_pbr_roughness: Option<Attribute>,
- pub va_pbr_metallic: Option<Attribute>,
- pub va_pbr_albedo: Option<[Attribute; 3]>,
- pub va_pbr_transmission: Option<Attribute>,
-}
-
-#[derive(Debug, Clone)]
-pub enum Attribute {
- Constant(f32),
- Vertex(Resource),
- Texture(Resource, u8),
+ pub g_metallic: Option<f32>,
+ pub g_roughness: Option<f32>,
+ pub g_albedo: Option<Vec3A>,
+ pub g_transmission: Option<f32>,
+ pub va_position: Option<[Resource; 3]>,
+ pub va_normal: Option<[Resource; 3]>,
+ pub va_texcoord: Option<[Resource; 2]>,
+ pub va_pbr_roughness: Option<Resource>,
+ pub va_pbr_metallic: Option<Resource>,
+ pub va_pbr_albedo: Option<[Resource; 3]>,
+ pub va_pbr_transmission: Option<Resource>,
+ pub tex_normal: Option<Resource>,
+ pub tex_pbr_roughness: Option<Resource>,
+ pub tex_pbr_metallic: Option<Resource>,
+ pub tex_pbr_albedo: Option<Resource>,
+ pub tex_pbr_transmission: Option<Resource>,
}
#[derive(Debug, Default, Clone)]
@@ -86,27 +90,24 @@ impl ReadWrite for AttributeArray {
}
impl ReadWrite for Part {
fn write(&self, w: &mut dyn Write) -> Result<()> {
- if let Some(a) = &self.index {
- write_kv(w, b"index", &a.write_alloc())?;
- }
- if let Some(a) = &self.va_position {
- write_kv(w, b"va_position", &a.write_alloc())?;
- }
- if let Some(a) = &self.va_normal {
- write_kv(w, b"va_normal", &a.write_alloc())?;
- }
- if let Some(a) = &self.va_texcoord {
- write_kv(w, b"va_texcoord", &a.write_alloc())?;
- }
- if let Some(a) = &self.va_pbr_roughness {
- write_kv(w, b"va_pbr_roughness", &a.write_alloc())?;
- }
- if let Some(a) = &self.va_pbr_metallic {
- write_kv(w, b"va_pbr_metallic", &a.write_alloc())?;
- }
- if let Some(a) = &self.va_pbr_albedo {
- write_kv(w, b"va_pbr_albedo", &a.write_alloc())?;
- }
+ write_kv_opt(w, b"index", &self.index)?;
+ write_kv_opt(w, b"g_metallic", &self.g_metallic)?;
+ write_kv_opt(w, b"g_roughness", &self.g_roughness)?;
+ write_kv_opt(w, b"g_albedo", &self.g_albedo)?;
+ write_kv_opt(w, b"g_transmission", &self.g_transmission)?;
+ write_kv_opt(w, b"va_position", &self.va_position)?;
+ write_kv_opt(w, b"va_normal", &self.va_normal)?;
+ write_kv_opt(w, b"va_texcoord", &self.va_texcoord)?;
+ write_kv_opt(w, b"va_pbr_roughness", &self.va_pbr_roughness)?;
+ write_kv_opt(w, b"va_pbr_metallic", &self.va_pbr_metallic)?;
+ write_kv_opt(w, b"va_pbr_albedo", &self.va_pbr_albedo)?;
+ write_kv_opt(w, b"va_pbr_transmission", &self.va_pbr_transmission)?;
+ write_kv_opt(w, b"tex_normal", &self.tex_normal)?;
+ write_kv_opt(w, b"tex_pbr_roughness", &self.tex_pbr_roughness)?;
+ write_kv_opt(w, b"tex_pbr_metallic", &self.tex_pbr_metallic)?;
+ write_kv_opt(w, b"tex_pbr_albedo", &self.tex_pbr_albedo)?;
+ write_kv_opt(w, b"tex_pbr_transmission", &self.tex_pbr_transmission)?;
+
Ok(())
}
fn read(r: &mut dyn Read) -> Result<Self> {
@@ -119,6 +120,10 @@ impl ReadWrite for Part {
let mut v = v.as_slice();
match k.as_slice() {
b"index" => s.index = Some(<_ as ReadWrite>::read(&mut v)?),
+ b"g_metallic" => s.g_metallic = Some(<_ as ReadWrite>::read(&mut v)?),
+ b"g_roughness" => s.g_roughness = Some(<_ as ReadWrite>::read(&mut v)?),
+ b"g_albedo" => s.g_albedo = Some(<_ as ReadWrite>::read(&mut v)?),
+ b"g_transmission" => s.g_transmission = Some(<_ as ReadWrite>::read(&mut v)?),
b"va_position" => s.va_position = Some(<_ as ReadWrite>::read(&mut v)?),
b"va_normal" => s.va_normal = Some(<_ as ReadWrite>::read(&mut v)?),
b"va_texcoord" => s.va_texcoord = Some(<_ as ReadWrite>::read(&mut v)?),
@@ -128,6 +133,13 @@ impl ReadWrite for Part {
b"va_pbr_transmission" => {
s.va_pbr_transmission = Some(<_ as ReadWrite>::read(&mut v)?)
}
+ b"tex_normal" => s.tex_normal = Some(<_ as ReadWrite>::read(&mut v)?),
+ b"tex_pbr_roughness" => s.tex_pbr_roughness = Some(<_ as ReadWrite>::read(&mut v)?),
+ b"tex_pbr_metallic" => s.tex_pbr_metallic = Some(<_ as ReadWrite>::read(&mut v)?),
+ b"tex_pbr_albedo" => s.tex_pbr_albedo = Some(<_ as ReadWrite>::read(&mut v)?),
+ b"tex_pbr_transmission" => {
+ s.tex_pbr_transmission = Some(<_ as ReadWrite>::read(&mut v)?)
+ }
x => warn!("unknown part key: {:?}", String::from_utf8_lossy(x)),
}
}
@@ -149,6 +161,12 @@ fn read_kv(r: &mut &[u8]) -> Result<(Vec<u8>, Vec<u8>)> {
Ok((key, value))
}
+fn write_kv_opt(w: &mut dyn Write, key: &[u8], value: &Option<impl ReadWrite>) -> Result<()> {
+ if let Some(v) = value {
+ write_kv(w, key, &v.write_alloc())?;
+ }
+ 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())?;
@@ -157,35 +175,6 @@ fn write_kv(w: &mut dyn Write, key: &[u8], value: &[u8]) -> Result<()> {
Ok(())
}
-impl ReadWrite for Attribute {
- fn write(&self, w: &mut dyn Write) -> Result<()> {
- match self {
- Attribute::Constant(v) => {
- w.write_all(&[0x01])?;
- w.write_all(&v.to_be_bytes())?;
- }
- Attribute::Vertex(resource) => {
- w.write_all(&[0x02])?;
- resource.write(w)?;
- }
- Attribute::Texture(resource, channel) => {
- w.write_all(&[0x03])?;
- resource.write(w)?;
- channel.write(w)?;
- }
- }
- Ok(())
- }
- fn read(r: &mut dyn Read) -> Result<Self> {
- Ok(match u8::read(r)? {
- 0x01 => Self::Constant(f32::read(r)?),
- 0x02 => Self::Vertex(Resource::read(r)?),
- 0x03 => Self::Texture(Resource::read(r)?, u8::read(r)?),
- _ => bail!("unknown attribute tag"),
- })
- }
-}
-
impl ReadWrite for u8 {
fn write(&self, w: &mut dyn Write) -> Result<()> {
w.write_all(&[*self])?;