1
Fork 0
mirror of https://github.com/redstrate/Physis.git synced 2025-04-24 05:27:45 +00:00

Fix tangent reading/writing

This commit is contained in:
Joshua Goins 2023-12-17 18:58:48 -05:00
parent 3721644340
commit 6000720658
3 changed files with 53 additions and 22 deletions

View file

@ -272,8 +272,8 @@ pub struct Vertex {
pub uv0: [f32; 2], pub uv0: [f32; 2],
pub uv1: [f32; 2], pub uv1: [f32; 2],
pub normal: [f32; 3], pub normal: [f32; 3],
pub tangent1: [u8; 4], pub bitangent: [f32; 4],
pub tangent2: [u8; 4], //pub bitangent1: [f32; 4], // TODO: need to figure out what the heck this could be
pub color: [f32; 4], pub color: [f32; 4],
pub bone_weight: [f32; 4], pub bone_weight: [f32; 4],
@ -365,8 +365,7 @@ impl MDL {
uv0: [0.0; 2], uv0: [0.0; 2],
uv1: [0.0; 2], uv1: [0.0; 2],
normal: [0.0; 3], normal: [0.0; 3],
tangent1: [0u8; 4], bitangent: [0.0; 4],
tangent2: [0u8; 4],
color: [0.0; 4], color: [0.0; 4],
bone_weight: [0.0; 4], bone_weight: [0.0; 4],
bone_id: [0u8; 4], bone_id: [0u8; 4],
@ -455,23 +454,23 @@ impl MDL {
} }
} }
} }
VertexUsage::Tangent2 => { VertexUsage::BiTangent => {
match element.vertex_type { match element.vertex_type {
VertexType::ByteFloat4 => { VertexType::ByteFloat4 => {
vertices[k as usize].tangent2 = MDL::read_uint(&mut cursor).unwrap(); vertices[k as usize].bitangent = MDL::read_tangent(&mut cursor).unwrap();
} }
_ => { _ => {
panic!("Unexpected vertex type for tangent2: {:#?}", element.vertex_type); panic!("Unexpected vertex type for bitangent: {:#?}", element.vertex_type);
} }
} }
} }
VertexUsage::Tangent1 => { VertexUsage::Tangent => {
match element.vertex_type { match element.vertex_type {
VertexType::ByteFloat4 => { /*VertexType::ByteFloat4 => {
vertices[k as usize].tangent1 = MDL::read_uint(&mut cursor).unwrap(); vertices[k as usize].bitangent0 = MDL::read_tangent(&mut cursor).unwrap();
} }*/
_ => { _ => {
panic!("Unexpected vertex type for tangent1: {:#?}", element.vertex_type); panic!("Unexpected vertex type for tangent: {:#?}", element.vertex_type);
} }
} }
} }
@ -752,23 +751,23 @@ impl MDL {
} }
} }
} }
VertexUsage::Tangent2 => { VertexUsage::BiTangent => {
match element.vertex_type { match element.vertex_type {
VertexType::ByteFloat4 => { VertexType::ByteFloat4 => {
MDL::write_uint(&mut cursor, &vert.tangent2).ok()?; MDL::write_tangent(&mut cursor, &vert.bitangent).ok()?;
} }
_ => { _ => {
panic!("Unexpected vertex type for tangent2: {:#?}", element.vertex_type); panic!("Unexpected vertex type for bitangent: {:#?}", element.vertex_type);
} }
} }
} }
VertexUsage::Tangent1 => { VertexUsage::Tangent => {
match element.vertex_type { match element.vertex_type {
VertexType::ByteFloat4 => { /*VertexType::ByteFloat4 => {
MDL::write_uint(&mut cursor, &vert.tangent1).ok()?; MDL::write_tangent(&mut cursor, &vert.binormal).ok()?;
} }*/
_ => { _ => {
panic!("Unexpected vertex type for tangent1: {:#?}", element.vertex_type); panic!("Unexpected vertex type for tangent: {:#?}", element.vertex_type);
} }
} }
} }

View file

@ -28,6 +28,23 @@ impl MDL {
(vec[3] * MAX_BYTE_FLOAT).round() as u8]) (vec[3] * MAX_BYTE_FLOAT).round() as u8])
} }
pub(crate) fn read_tangent(cursor: &mut Cursor<ByteSpan>) -> Option<[f32; 4]> {
Some([
(f32::from(cursor.read_le::<u8>().ok()?) * 2.0 / MAX_BYTE_FLOAT - 1.0),
(f32::from(cursor.read_le::<u8>().ok()?) * 2.0 / MAX_BYTE_FLOAT - 1.0),
(f32::from(cursor.read_le::<u8>().ok()?) * 2.0 / MAX_BYTE_FLOAT - 1.0),
if (f32::from(cursor.read_le::<u8>().ok()?) * 2.0 / MAX_BYTE_FLOAT - 1.0) == 1.0 { 1.0 } else { -1.0 }
])
}
pub(crate) fn write_tangent<T: BinWriterExt>(cursor: &mut T, vec: &[f32; 4]) -> BinResult<()> {
cursor.write_le::<[u8; 4]>(&[
((vec[0] + 1.0) * (MAX_BYTE_FLOAT / 2.0)).round() as u8,
((vec[1] + 1.0) * (MAX_BYTE_FLOAT / 2.0)).round() as u8,
((vec[2] + 1.0) * (MAX_BYTE_FLOAT / 2.0)).round() as u8,
if vec[3] > 0.0 { 255 } else { 0 }]) // SqEx uses 0 as -1, not 1
}
pub(crate) fn read_half4(cursor: &mut Cursor<ByteSpan>) -> Option<[f32; 4]> { pub(crate) fn read_half4(cursor: &mut Cursor<ByteSpan>) -> Option<[f32; 4]> {
Some([ Some([
f16::from_bits(cursor.read_le::<u16>().ok()?).to_f32(), f16::from_bits(cursor.read_le::<u16>().ok()?).to_f32(),
@ -156,6 +173,21 @@ mod tests {
assert_eq!(MDL::read_single4(&mut read_cursor).unwrap(), a); assert_eq!(MDL::read_single4(&mut read_cursor).unwrap(), a);
} }
#[test]
fn tangent() {
let a = [1.0, 0.5, -0.5, 1.0];
let mut v = vec![];
let mut cursor = Cursor::new(&mut v);
MDL::write_tangent(&mut cursor, &a).unwrap();
let mut read_cursor = Cursor::new(v.as_slice());
let tangent = MDL::read_tangent(&mut read_cursor).unwrap();
assert_delta!(tangent, a, 0.001);
}
#[test] #[test]
fn pad_slice() { fn pad_slice() {
let a = [3.0, 0.0, -1.0]; let a = [3.0, 0.0, -1.0];

View file

@ -29,8 +29,8 @@ pub enum VertexUsage {
BlendIndices = 2, BlendIndices = 2,
Normal = 3, Normal = 3,
UV = 4, UV = 4,
Tangent2 = 5, Tangent = 5,
Tangent1 = 6, BiTangent = 6,
Color = 7, Color = 7,
} }