diff options
author | metamuffin <metamuffin@disroot.org> | 2025-04-07 13:12:47 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2025-04-07 13:12:47 +0200 |
commit | c6fb1f9c2a7fef8da6c90fe22c2a5d8e2decf598 (patch) | |
tree | 660339df4b65d98443e7d3c0054a81ec803f86fd | |
parent | 7317cfa33db2140300ea5f053ba6ff5a547fc371 (diff) | |
download | weareearth-c6fb1f9c2a7fef8da6c90fe22c2a5d8e2decf598.tar weareearth-c6fb1f9c2a7fef8da6c90fe22c2a5d8e2decf598.tar.bz2 weareearth-c6fb1f9c2a7fef8da6c90fe22c2a5d8e2decf598.tar.zst |
texture works
-rw-r--r-- | src/main.rs | 5 | ||||
-rw-r--r-- | src/mesh.rs | 52 |
2 files changed, 42 insertions, 15 deletions
diff --git a/src/main.rs b/src/main.rs index 6bebf4b..51c30d7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,7 @@ use reqwest::{ Client, header::{HeaderMap, HeaderName, HeaderValue}, }; -use std::path::PathBuf; +use std::{f32::consts::PI, path::PathBuf}; use tokio::{ fs::{File, create_dir_all}, io::{AsyncReadExt, AsyncWriteExt}, @@ -56,7 +56,8 @@ async fn main() -> Result<()> { for m in node_data.meshes { let mesh = convert_mesh(m, &store, &for_normals)?; meshes.push(( - Affine3A::from_mat4((transform / 3_000_000.).as_mat4()), + Affine3A::from_rotation_x(-PI / 2.) + * Affine3A::from_mat4((transform / 3_000_000.).as_mat4()), mesh, )) } diff --git a/src/mesh.rs b/src/mesh.rs index b387721..ff1ced9 100644 --- a/src/mesh.rs +++ b/src/mesh.rs @@ -1,7 +1,13 @@ -use crate::proto::Mesh; +use std::borrow::Cow; + +use crate::proto::{Mesh, Texture, texture::Format}; use anyhow::Result; use glam::{Vec2, Vec3A, vec2, vec3a}; -use weareshared::{packets::Resource, resources::MeshPart, store::ResourceStore}; +use weareshared::{ + packets::Resource, + resources::{Image, MeshPart}, + store::ResourceStore, +}; pub fn convert_mesh( m: Mesh, @@ -10,9 +16,9 @@ pub fn convert_mesh( ) -> Result<Resource<MeshPart>> { let index = decode_indices(&m.indices); let positions = decode_positions(m.vertices()); - let texcoords = decode_texcoords(m.texture_coords()); + let texcoords = decode_texcoords(m.texture_coordinates(), &m.uv_offset_and_scale); let normals = decode_normals(m.normals(), normal_table); - + let texture = decode_texture(&m.texture[0]); Ok(store.set(&MeshPart { index: Some(store.set(&index)?), @@ -20,27 +26,47 @@ pub fn convert_mesh( va_texcoord: texcoords.map(|x| store.set(&x)).transpose()?, va_normal: Some(store.set(&normals)?), g_double_sided: Some(()), + tex_albedo: Some(store.set(&texture)?), ..Default::default() })?) } -fn decode_texcoords(uv: &[u8]) -> Option<Vec<Vec2>> { +fn decode_texture(texture: &Texture) -> Image<'static> { + match texture.format() { + Format::Jpg => Image(Cow::Owned(texture.data[0].clone())), + Format::Dxt1 => todo!(), + Format::Etc1 => todo!(), + Format::Pvrtc2 => todo!(), + Format::Pvrtc4 => todo!(), + Format::CrnDxt1 => todo!(), + } +} + +fn decode_texcoords(uv: &[u8], offset_and_scale: &[f32]) -> Option<Vec<Vec2>> { if uv.is_empty() { return None; } let count = (uv.len() - 4) / 4; - let u_mod = 1 + u16::from_le_bytes([uv[0], uv[1]]); - let v_mod = 1 + u16::from_le_bytes([uv[2], uv[3]]); + let u_mod = u16::from_le_bytes([uv[0], uv[1]]); + let v_mod = u16::from_le_bytes([uv[2], uv[3]]); let uv = &uv[4..]; + let (offset, scale) = if offset_and_scale.len() == 4 { + ( + vec2(offset_and_scale[0], offset_and_scale[1]), + vec2(offset_and_scale[2], offset_and_scale[3]), + ) + } else { + (Vec2::ZERO, vec2(1. / u_mod as f32, -1. / v_mod as f32)) + }; let (mut u, mut v) = (0u16, 0u16); let mut texcoords = Vec::new(); for i in 0..count { - u = (u + (uv[count * 0 + i] as u16) + ((uv[count * 2 + i] as u16) << 8)) % u_mod; - v = (v + (uv[count * 1 + i] as u16) + ((uv[count * 3 + i] as u16) << 8)) % v_mod; - texcoords.push(vec2( - u as f32 / u_mod as f32 + 0.5, - v as f32 / v_mod as f32 + 0.5, - )); + u = u.wrapping_add((uv[count * 0 + i] as u16) + ((uv[count * 2 + i] as u16) << 8)); + v = v.wrapping_add((uv[count * 1 + i] as u16) + ((uv[count * 3 + i] as u16) << 8)); + u %= u_mod; + v %= v_mod; + let c = vec2(u as f32, v as f32); + texcoords.push(scale * (offset + c)) } Some(texcoords) } |