1
Fork 0
mirror of https://github.com/redstrate/Physis.git synced 2025-04-23 05:07:46 +00:00

Simplify vertex loading and fix incorrect bone data

This commit is contained in:
Joshua Goins 2023-07-05 19:42:53 -04:00
parent b3debd7839
commit ef97cfd1ce

View file

@ -1,5 +1,5 @@
use crate::gamedata::MemoryBuffer; use crate::gamedata::MemoryBuffer;
use binrw::binrw; use binrw::{BinResult, binrw};
use binrw::BinRead; use binrw::BinRead;
use binrw::BinReaderExt; use binrw::BinReaderExt;
use half::f16; use half::f16;
@ -429,27 +429,56 @@ impl MDL {
match element.vertex_usage { match element.vertex_usage {
VertexUsage::Position => { VertexUsage::Position => {
vertices[k as usize].position = match element.vertex_type {
cursor.read_le::<[f32; 3]>().ok()?; VertexType::Half4 => {
vertices[k as usize].position.clone_from_slice(&MDL::read_half4(&mut cursor).unwrap()[0..3]);
}
VertexType::Single3 => {
vertices[k as usize].position = MDL::read_single3(&mut cursor).unwrap();
}
_ => {
panic!("Unexpected vertex type for position: {:#?}", element.vertex_type);
}
}
} }
VertexUsage::BlendWeights => { VertexUsage::BlendWeights => {
vertices[k as usize].bone_weight = match element.vertex_type {
cursor.read_le::<[f32; 4]>().ok()?; VertexType::ByteFloat4 => {
vertices[k as usize].bone_weight = MDL::read_byte_float4(&mut cursor).unwrap();
}
_ => {
panic!("Unexpected vertex type for blendweight: {:#?}", element.vertex_type);
}
}
} }
VertexUsage::BlendIndices => { VertexUsage::BlendIndices => {
vertices[k as usize].bone_id = cursor.read_le::<[u8; 4]>().ok()?; match element.vertex_type {
VertexType::UInt => {
vertices[k as usize].bone_id = MDL::read_uint(&mut cursor).unwrap();
}
_ => {
panic!("Unexpected vertex type for blendindice: {:#?}", element.vertex_type);
}
}
} }
VertexUsage::Normal => { VertexUsage::Normal => {
// TODO: normals are assumed to be half4 match element.vertex_type {
for i in 0..3 { VertexType::Half4 => {
vertices[k as usize].normal[i] = vertices[k as usize].normal.clone_from_slice(&MDL::read_half4(&mut cursor).unwrap()[0..3]);
f16::from_bits(cursor.read_le::<u16>().ok()?).to_f32(); }
_ => {
panic!("Unexpected vertex type for normal: {:#?}", element.vertex_type);
}
} }
} }
VertexUsage::UV => { VertexUsage::UV => {
for i in 0..2 { match element.vertex_type {
vertices[k as usize].uv[i] = VertexType::Half4 => {
f16::from_bits(cursor.read_le::<u16>().ok()?).to_f32(); vertices[k as usize].uv.clone_from_slice(&MDL::read_half4(&mut cursor).unwrap()[0..2]);
}
_ => {
panic!("Unexpected vertex type for uv: {:#?}", element.vertex_type);
}
} }
} }
VertexUsage::Tangent2 => {} VertexUsage::Tangent2 => {}
@ -486,4 +515,31 @@ impl MDL {
material_names material_names
}) })
} }
fn read_byte_float4(cursor: &mut Cursor<&MemoryBuffer>) -> Option<[f32; 4]> {
let mut arr: [f32; 4] = [0.0; 4];
for i in 0..4 {
arr[i] = f32::from(cursor.read_le::<u8>().ok()?) / 255.0;
}
Some(arr)
}
fn read_half4(cursor: &mut Cursor<&MemoryBuffer>) -> Option<[f32; 4]> {
let mut arr: [f32; 4] = [0.0; 4];
for i in 0..3 {
arr[i] = f16::from_bits(cursor.read_le::<u16>().ok()?).to_f32();
}
Some(arr)
}
fn read_uint(cursor: &mut Cursor<&MemoryBuffer>) -> BinResult<[u8; 4]> {
cursor.read_le::<[u8; 4]>()
}
fn read_single3(cursor: &mut Cursor<&MemoryBuffer>) -> BinResult<[f32; 3]> {
cursor.read_le::<[f32; 3]>()
}
} }