summaryrefslogtreecommitdiff
path: root/shared/src/resources.rs
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2025-01-07 12:25:37 +0100
committermetamuffin <metamuffin@disroot.org>2025-01-07 12:25:37 +0100
commitd81eebe423fd3e00df5ff035ec24fe7fb37f2c62 (patch)
treee96dddf8e179f47dc030729d6f725c30dfa372d9 /shared/src/resources.rs
parent31ae23b7eb8cd0b688be07ae6cb4b5a96ee02a68 (diff)
downloadweareserver-d81eebe423fd3e00df5ff035ec24fe7fb37f2c62.tar
weareserver-d81eebe423fd3e00df5ff035ec24fe7fb37f2c62.tar.bz2
weareserver-d81eebe423fd3e00df5ff035ec24fe7fb37f2c62.tar.zst
albedo texture works
Diffstat (limited to 'shared/src/resources.rs')
-rw-r--r--shared/src/resources.rs119
1 files changed, 54 insertions, 65 deletions
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])?;