2023-08-06 08:25:04 -04:00
|
|
|
// SPDX-FileCopyrightText: 2023 Joshua Goins <josh@redstrate.com>
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2023-12-13 17:23:53 -05:00
|
|
|
use std::io::{Cursor, Seek, SeekFrom};
|
2023-12-12 22:04:30 -05:00
|
|
|
use std::mem::size_of;
|
2023-08-06 08:25:04 -04:00
|
|
|
|
2023-12-13 17:23:53 -05:00
|
|
|
use binrw::{binrw, BinWrite, BinWriterExt};
|
2022-08-16 11:52:07 -04:00
|
|
|
use binrw::BinRead;
|
2022-10-20 11:45:55 -04:00
|
|
|
use binrw::BinReaderExt;
|
2023-10-13 16:16:04 -04:00
|
|
|
use crate::{ByteBuffer, ByteSpan};
|
2023-12-13 17:23:53 -05:00
|
|
|
use crate::model_vertex_declarations::{vertex_element_parser, vertex_element_writer, VertexDeclaration, VertexType, VertexUsage};
|
2023-11-24 08:25:45 -05:00
|
|
|
|
2022-07-19 19:29:41 -04:00
|
|
|
#[binrw]
|
2022-07-28 14:11:02 -04:00
|
|
|
#[derive(Debug)]
|
2022-10-13 16:03:46 -04:00
|
|
|
#[brw(little)]
|
2022-07-28 14:11:02 -04:00
|
|
|
pub struct ModelFileHeader {
|
2023-12-13 17:19:02 -05:00
|
|
|
pub version: u32,
|
2022-07-19 19:29:41 -04:00
|
|
|
|
|
|
|
pub stack_size: u32,
|
|
|
|
pub runtime_size: u32,
|
|
|
|
|
|
|
|
pub vertex_declaration_count: u16,
|
|
|
|
pub material_count: u16,
|
|
|
|
|
|
|
|
pub vertex_offsets: [u32; 3],
|
|
|
|
pub index_offsets: [u32; 3],
|
|
|
|
pub vertex_buffer_size: [u32; 3],
|
|
|
|
pub index_buffer_size: [u32; 3],
|
|
|
|
|
|
|
|
pub lod_count: u8,
|
|
|
|
|
|
|
|
#[br(map = | x: u8 | x != 0)]
|
|
|
|
#[bw(map = | x: & bool | -> u8 { if * x { 1 } else { 0 } })]
|
|
|
|
pub index_buffer_streaming_enabled: bool,
|
|
|
|
#[br(map = | x: u8 | x != 0)]
|
|
|
|
#[bw(map = | x: & bool | -> u8 { if * x { 1 } else { 0 } })]
|
|
|
|
#[brw(pad_after = 1)]
|
|
|
|
pub has_edge_geometry: bool,
|
2023-12-13 17:19:02 -05:00
|
|
|
|
|
|
|
#[br(args(vertex_declaration_count), parse_with = vertex_element_parser)]
|
|
|
|
#[bw(write_with = vertex_element_writer)]
|
|
|
|
pub vertex_declarations: Vec<VertexDeclaration>
|
2022-07-19 19:29:41 -04:00
|
|
|
}
|
|
|
|
|
2022-10-13 17:11:03 -04:00
|
|
|
#[binrw]
|
|
|
|
#[brw(repr = u8)]
|
2023-09-22 16:44:19 -04:00
|
|
|
#[derive(Debug, Clone)]
|
2022-07-28 14:11:02 -04:00
|
|
|
enum ModelFlags1 {
|
|
|
|
DustOcclusionEnabled = 0x80,
|
|
|
|
SnowOcclusionEnabled = 0x40,
|
|
|
|
RainOcclusionEnabled = 0x20,
|
|
|
|
Unknown1 = 0x10,
|
|
|
|
LightingReflectionEnabled = 0x08,
|
|
|
|
WavingAnimationDisabled = 0x04,
|
|
|
|
LightShadowDisabled = 0x02,
|
2022-08-16 11:52:07 -04:00
|
|
|
ShadowDisabled = 0x01,
|
2022-07-28 14:11:02 -04:00
|
|
|
}
|
|
|
|
|
2022-10-13 17:11:03 -04:00
|
|
|
#[binrw]
|
|
|
|
#[brw(repr = u8)]
|
2023-09-22 16:44:19 -04:00
|
|
|
#[derive(Debug, Clone)]
|
2022-07-28 14:11:02 -04:00
|
|
|
enum ModelFlags2 {
|
|
|
|
None = 0x0,
|
|
|
|
Unknown2 = 0x80,
|
|
|
|
BgUvScrollEnabled = 0x40,
|
|
|
|
EnableForceNonResident = 0x20,
|
|
|
|
ExtraLodEnabled = 0x10,
|
|
|
|
ShadowMaskEnabled = 0x08,
|
|
|
|
ForceLodRangeEnabled = 0x04,
|
|
|
|
EdgeGeometryEnabled = 0x02,
|
2022-08-16 11:52:07 -04:00
|
|
|
Unknown3 = 0x01,
|
2022-07-28 14:11:02 -04:00
|
|
|
}
|
|
|
|
|
2022-10-13 17:11:03 -04:00
|
|
|
#[binrw]
|
2023-09-22 16:44:19 -04:00
|
|
|
#[derive(Debug, Clone)]
|
2022-09-15 16:26:31 -04:00
|
|
|
#[allow(dead_code)]
|
2022-07-28 14:11:02 -04:00
|
|
|
pub struct ModelHeader {
|
2023-11-22 21:21:01 -05:00
|
|
|
#[brw(pad_after = 2)]
|
2022-08-16 11:52:07 -04:00
|
|
|
string_count: u16,
|
|
|
|
string_size: u32,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
|
|
|
#[br(count = string_size)]
|
2022-08-16 11:52:07 -04:00
|
|
|
strings: Vec<u8>,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
2022-08-16 11:52:07 -04:00
|
|
|
radius: f32,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
2022-08-16 11:52:07 -04:00
|
|
|
mesh_count: u16,
|
|
|
|
attribute_count: u16,
|
|
|
|
submesh_count: u16,
|
|
|
|
material_count: u16,
|
|
|
|
bone_count: u16,
|
|
|
|
bone_table_count: u16,
|
|
|
|
shape_count: u16,
|
|
|
|
shape_mesh_count: u16,
|
|
|
|
shape_value_count: u16,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
2022-08-16 11:52:07 -04:00
|
|
|
lod_count: u8,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
2022-08-16 11:52:07 -04:00
|
|
|
flags1: ModelFlags1,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
2022-08-16 11:52:07 -04:00
|
|
|
element_id_count: u16,
|
|
|
|
terrain_shadow_mesh_count: u8,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
2022-08-16 11:52:07 -04:00
|
|
|
flags2: ModelFlags2,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
2022-08-16 11:52:07 -04:00
|
|
|
model_clip_out_of_distance: f32,
|
|
|
|
shadow_clip_out_of_distance: f32,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
2023-11-22 21:21:01 -05:00
|
|
|
#[brw(pad_before = 2)]
|
|
|
|
#[brw(pad_after = 2)]
|
2022-08-16 11:52:07 -04:00
|
|
|
terrain_shadow_submesh_count: u16,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
2022-08-16 11:52:07 -04:00
|
|
|
bg_change_material_index: u8,
|
2023-11-22 21:21:01 -05:00
|
|
|
#[brw(pad_after = 12)]
|
2022-08-16 11:52:07 -04:00
|
|
|
bg_crest_change_material_index: u8,
|
2022-07-28 14:11:02 -04:00
|
|
|
}
|
|
|
|
|
2022-10-13 17:11:03 -04:00
|
|
|
#[binrw]
|
2023-09-22 16:44:19 -04:00
|
|
|
#[derive(Debug, Clone)]
|
2022-09-15 16:26:31 -04:00
|
|
|
#[allow(dead_code)]
|
2022-07-28 14:11:02 -04:00
|
|
|
struct MeshLod {
|
2022-08-16 11:52:07 -04:00
|
|
|
mesh_index: u16,
|
|
|
|
mesh_count: u16,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
2022-08-16 11:52:07 -04:00
|
|
|
model_lod_range: f32,
|
|
|
|
texture_lod_range: f32,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
2022-08-16 11:52:07 -04:00
|
|
|
water_mesh_index: u16,
|
|
|
|
water_mesh_count: u16,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
2022-08-16 11:52:07 -04:00
|
|
|
shadow_mesh_index: u16,
|
|
|
|
shadow_mesh_count: u16,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
2022-08-16 11:52:07 -04:00
|
|
|
terrain_shadow_mesh_count: u16,
|
|
|
|
terrain_shadow_mesh_index: u16,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
2022-08-16 11:52:07 -04:00
|
|
|
vertical_fog_mesh_index: u16,
|
|
|
|
vertical_fog_mesh_count: u16,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
|
|
|
// unused on win32 according to lumina devs
|
2022-08-16 11:52:07 -04:00
|
|
|
edge_geometry_size: u32,
|
|
|
|
edge_geometry_data_offset: u32,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
2023-11-22 21:21:01 -05:00
|
|
|
#[brw(pad_after = 4)]
|
2022-08-16 11:52:07 -04:00
|
|
|
polygon_count: u32,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
2022-08-16 11:52:07 -04:00
|
|
|
vertex_buffer_size: u32,
|
|
|
|
index_buffer_size: u32,
|
|
|
|
vertex_data_offset: u32,
|
|
|
|
index_data_offset: u32,
|
2022-07-28 14:11:02 -04:00
|
|
|
}
|
|
|
|
|
2022-10-13 17:11:03 -04:00
|
|
|
#[binrw]
|
2023-09-22 16:44:19 -04:00
|
|
|
#[derive(Debug, Clone)]
|
2022-09-15 16:26:31 -04:00
|
|
|
#[allow(dead_code)]
|
2022-07-28 14:11:02 -04:00
|
|
|
struct Mesh {
|
2023-11-22 21:21:01 -05:00
|
|
|
#[brw(pad_after = 2)]
|
2022-08-16 11:52:07 -04:00
|
|
|
vertex_count: u16,
|
|
|
|
index_count: u32,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
2022-08-16 11:52:07 -04:00
|
|
|
material_index: u16,
|
|
|
|
submesh_index: u16,
|
|
|
|
submesh_count: u16,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
2022-08-16 11:52:07 -04:00
|
|
|
bone_table_index: u16,
|
|
|
|
start_index: u32,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
2022-08-16 11:52:07 -04:00
|
|
|
vertex_buffer_offsets: [u32; 3],
|
|
|
|
vertex_buffer_strides: [u8; 3],
|
2022-07-28 14:11:02 -04:00
|
|
|
|
2022-08-16 11:52:07 -04:00
|
|
|
vertex_stream_count: u8,
|
2022-07-28 14:11:02 -04:00
|
|
|
}
|
|
|
|
|
2022-10-13 17:11:03 -04:00
|
|
|
#[binrw]
|
2023-09-22 16:44:19 -04:00
|
|
|
#[derive(Debug, Clone)]
|
2022-09-15 16:26:31 -04:00
|
|
|
#[allow(dead_code)]
|
2022-07-28 14:11:02 -04:00
|
|
|
struct Submesh {
|
2023-12-12 21:43:04 -05:00
|
|
|
index_offset: u32,
|
|
|
|
index_count: u32,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
2022-08-16 11:52:07 -04:00
|
|
|
attribute_index_mask: u32,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
2022-08-16 11:52:07 -04:00
|
|
|
bone_start_index: u16,
|
|
|
|
bone_count: u16,
|
2022-07-28 14:11:02 -04:00
|
|
|
}
|
|
|
|
|
2022-10-13 17:11:03 -04:00
|
|
|
#[binrw]
|
2023-09-22 16:44:19 -04:00
|
|
|
#[derive(Debug, Clone)]
|
2022-09-15 16:26:31 -04:00
|
|
|
#[allow(dead_code)]
|
2022-07-28 14:11:02 -04:00
|
|
|
struct BoneTable {
|
2022-08-16 11:52:07 -04:00
|
|
|
bone_indices: [u16; 64],
|
2022-07-28 14:11:02 -04:00
|
|
|
|
2023-11-22 21:21:01 -05:00
|
|
|
#[brw(pad_after = 3)]
|
2022-08-16 11:52:07 -04:00
|
|
|
bone_count: u8,
|
2022-07-28 14:11:02 -04:00
|
|
|
}
|
|
|
|
|
2022-10-13 17:11:03 -04:00
|
|
|
#[binrw]
|
2023-09-22 16:44:19 -04:00
|
|
|
#[derive(Debug, Clone)]
|
2022-09-15 16:26:31 -04:00
|
|
|
#[allow(dead_code)]
|
2022-07-28 14:11:02 -04:00
|
|
|
struct BoundingBox {
|
2022-08-16 11:52:07 -04:00
|
|
|
min: [f32; 4],
|
|
|
|
max: [f32; 4],
|
2022-07-28 14:11:02 -04:00
|
|
|
}
|
|
|
|
|
2022-10-13 17:11:03 -04:00
|
|
|
#[binrw]
|
2023-09-22 16:44:19 -04:00
|
|
|
#[derive(Debug, Clone)]
|
2022-09-15 16:26:31 -04:00
|
|
|
#[allow(dead_code)]
|
2022-10-13 17:11:03 -04:00
|
|
|
#[brw(little)]
|
2022-07-28 14:11:02 -04:00
|
|
|
struct ModelData {
|
2022-08-16 11:52:07 -04:00
|
|
|
header: ModelHeader,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
|
|
|
#[br(count = header.element_id_count)]
|
2022-08-16 11:52:07 -04:00
|
|
|
element_ids: Vec<ElementId>,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
|
|
|
#[br(count = 3)]
|
2022-08-16 11:52:07 -04:00
|
|
|
lods: Vec<MeshLod>,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
|
|
|
#[br(count = header.mesh_count)]
|
2022-08-16 11:52:07 -04:00
|
|
|
meshes: Vec<Mesh>,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
|
|
|
#[br(count = header.attribute_count)]
|
2022-08-16 11:52:07 -04:00
|
|
|
attribute_name_offsets: Vec<u32>,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
|
|
|
// TODO: implement terrain shadow meshes
|
|
|
|
#[br(count = header.submesh_count)]
|
2022-08-16 11:52:07 -04:00
|
|
|
submeshes: Vec<Submesh>,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
|
|
|
// TODO: implement terrain shadow submeshes
|
|
|
|
#[br(count = header.material_count)]
|
2022-08-16 11:52:07 -04:00
|
|
|
material_name_offsets: Vec<u32>,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
|
|
|
#[br(count = header.bone_count)]
|
2022-08-16 11:52:07 -04:00
|
|
|
bone_name_offsets: Vec<u32>,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
|
|
|
#[br(count = header.bone_table_count)]
|
2022-08-16 11:52:07 -04:00
|
|
|
bone_tables: Vec<BoneTable>,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
|
|
|
// TODO: implement shapes
|
2022-08-16 11:52:07 -04:00
|
|
|
submesh_bone_map_size: u32,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
|
|
|
#[br(count = submesh_bone_map_size / 2, err_context("lods = {:#?}", lods))]
|
2022-08-16 11:52:07 -04:00
|
|
|
submesh_bone_map: Vec<u16>,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
2023-11-22 21:21:01 -05:00
|
|
|
// TODO: what actually is this?
|
2022-08-16 11:52:07 -04:00
|
|
|
padding_amount: u8,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
|
|
|
#[br(pad_before = padding_amount)]
|
2023-11-22 21:21:01 -05:00
|
|
|
#[bw(pad_before = *padding_amount)]
|
2022-08-16 11:52:07 -04:00
|
|
|
bounding_box: BoundingBox,
|
|
|
|
model_bounding_box: BoundingBox,
|
|
|
|
water_bounding_box: BoundingBox,
|
|
|
|
vertical_fog_bounding_box: BoundingBox,
|
2022-07-28 14:11:02 -04:00
|
|
|
|
|
|
|
#[br(count = header.bone_count)]
|
2022-08-16 11:52:07 -04:00
|
|
|
bone_bounding_boxes: Vec<BoundingBox>,
|
2022-07-28 14:11:02 -04:00
|
|
|
}
|
|
|
|
|
2022-10-13 17:11:03 -04:00
|
|
|
#[binrw]
|
2023-09-22 16:44:19 -04:00
|
|
|
#[derive(Debug, Clone)]
|
2022-09-15 16:26:31 -04:00
|
|
|
#[allow(dead_code)]
|
2022-07-28 14:11:02 -04:00
|
|
|
struct ElementId {
|
2022-08-16 11:52:07 -04:00
|
|
|
element_id: u32,
|
|
|
|
parent_bone_name: u32,
|
|
|
|
translate: [f32; 3],
|
|
|
|
rotate: [f32; 3],
|
2022-07-28 14:11:02 -04:00
|
|
|
}
|
|
|
|
|
2023-12-09 14:45:00 -05:00
|
|
|
#[derive(Clone, Copy)]
|
2022-07-28 15:20:47 -04:00
|
|
|
#[repr(C)]
|
2022-07-28 14:11:02 -04:00
|
|
|
pub struct Vertex {
|
2022-08-16 11:52:07 -04:00
|
|
|
pub position: [f32; 3],
|
2023-11-24 08:07:30 -05:00
|
|
|
pub uv0: [f32; 2],
|
|
|
|
pub uv1: [f32; 2],
|
2022-07-28 15:20:47 -04:00
|
|
|
pub normal: [f32; 3],
|
2023-12-17 18:58:48 -05:00
|
|
|
pub bitangent: [f32; 4],
|
|
|
|
//pub bitangent1: [f32; 4], // TODO: need to figure out what the heck this could be
|
2023-11-24 08:07:30 -05:00
|
|
|
pub color: [f32; 4],
|
2022-07-28 14:11:02 -04:00
|
|
|
|
2022-07-28 15:20:47 -04:00
|
|
|
pub bone_weight: [f32; 4],
|
2022-08-16 11:52:07 -04:00
|
|
|
pub bone_id: [u8; 4],
|
2022-07-28 14:11:02 -04:00
|
|
|
}
|
|
|
|
|
2024-01-29 16:01:58 -05:00
|
|
|
impl Default for Vertex {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
position: [0.0; 3],
|
|
|
|
uv0: [0.0; 2],
|
|
|
|
uv1: [0.0; 2],
|
|
|
|
normal: [0.0; 3],
|
|
|
|
bitangent: [0.0; 4],
|
|
|
|
color: [0.0; 4],
|
|
|
|
bone_weight: [0.0; 4],
|
|
|
|
bone_id: [0u8; 4],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-12 21:43:04 -05:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct SubMesh {
|
|
|
|
submesh_index: usize,
|
|
|
|
pub index_count: u32,
|
|
|
|
pub index_offset: u32
|
|
|
|
}
|
|
|
|
|
2022-07-28 14:11:02 -04:00
|
|
|
pub struct Part {
|
2023-09-22 17:42:04 -04:00
|
|
|
mesh_index: u16,
|
2022-08-16 11:52:07 -04:00
|
|
|
pub vertices: Vec<Vertex>,
|
|
|
|
pub indices: Vec<u16>,
|
2023-12-12 21:43:04 -05:00
|
|
|
pub material_index: u16,
|
|
|
|
pub submeshes: Vec<SubMesh>
|
2022-07-28 14:11:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Lod {
|
2022-08-16 11:52:07 -04:00
|
|
|
pub parts: Vec<Part>,
|
2022-07-28 14:11:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct MDL {
|
2023-09-22 16:44:19 -04:00
|
|
|
file_header: ModelFileHeader,
|
2023-09-22 17:04:48 -04:00
|
|
|
model_data: ModelData,
|
2023-09-22 16:44:19 -04:00
|
|
|
|
2022-08-16 11:52:07 -04:00
|
|
|
pub lods: Vec<Lod>,
|
|
|
|
pub affected_bone_names: Vec<String>,
|
2023-03-31 21:31:03 -04:00
|
|
|
pub material_names: Vec<String>
|
2022-07-28 14:11:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MDL {
|
2023-10-13 16:16:04 -04:00
|
|
|
pub fn from_existing(buffer: ByteSpan) -> Option<MDL> {
|
2022-07-28 14:11:02 -04:00
|
|
|
let mut cursor = Cursor::new(buffer);
|
|
|
|
let model_file_header = ModelFileHeader::read(&mut cursor).unwrap();
|
|
|
|
|
|
|
|
let model = ModelData::read(&mut cursor).unwrap();
|
|
|
|
|
2022-08-10 14:51:50 -04:00
|
|
|
let mut affected_bone_names = vec![];
|
|
|
|
|
2023-09-22 17:04:48 -04:00
|
|
|
for offset in &model.bone_name_offsets {
|
|
|
|
let mut offset = *offset;
|
2022-08-10 14:51:50 -04:00
|
|
|
let mut string = String::new();
|
|
|
|
|
|
|
|
let mut next_char = model.header.strings[offset as usize] as char;
|
|
|
|
while next_char != '\0' {
|
|
|
|
string.push(next_char);
|
|
|
|
offset += 1;
|
|
|
|
next_char = model.header.strings[offset as usize] as char;
|
|
|
|
}
|
|
|
|
|
|
|
|
affected_bone_names.push(string);
|
|
|
|
}
|
|
|
|
|
2023-03-31 21:31:03 -04:00
|
|
|
let mut material_names = vec![];
|
|
|
|
|
2023-09-22 17:04:48 -04:00
|
|
|
for offset in &model.material_name_offsets {
|
|
|
|
let mut offset = *offset;
|
2023-03-31 21:31:03 -04:00
|
|
|
let mut string = String::new();
|
|
|
|
|
|
|
|
let mut next_char = model.header.strings[offset as usize] as char;
|
|
|
|
while next_char != '\0' {
|
|
|
|
string.push(next_char);
|
|
|
|
offset += 1;
|
|
|
|
next_char = model.header.strings[offset as usize] as char;
|
|
|
|
}
|
|
|
|
|
|
|
|
material_names.push(string);
|
|
|
|
}
|
|
|
|
|
2022-07-28 14:11:02 -04:00
|
|
|
let mut lods = vec![];
|
|
|
|
|
|
|
|
for i in 0..model.header.lod_count {
|
|
|
|
let mut parts = vec![];
|
|
|
|
|
2022-08-16 11:52:07 -04:00
|
|
|
for j in model.lods[i as usize].mesh_index
|
|
|
|
..model.lods[i as usize].mesh_index + model.lods[i as usize].mesh_count
|
|
|
|
{
|
2023-12-13 17:19:02 -05:00
|
|
|
let declaration = &model_file_header.vertex_declarations[j as usize];
|
2022-07-28 14:11:02 -04:00
|
|
|
let vertex_count = model.meshes[j as usize].vertex_count;
|
2023-03-31 21:31:15 -04:00
|
|
|
let material_index = model.meshes[j as usize].material_index;
|
2022-07-28 14:11:02 -04:00
|
|
|
|
2024-01-29 16:01:58 -05:00
|
|
|
let mut vertices: Vec<Vertex> = vec![Vertex::default(); vertex_count as usize];
|
2022-07-28 14:11:02 -04:00
|
|
|
|
|
|
|
for k in 0..vertex_count {
|
|
|
|
for element in &declaration.elements {
|
2022-08-16 11:52:07 -04:00
|
|
|
cursor
|
|
|
|
.seek(SeekFrom::Start(
|
|
|
|
(model.lods[i as usize].vertex_data_offset
|
|
|
|
+ model.meshes[j as usize].vertex_buffer_offsets
|
2023-12-12 22:13:03 -05:00
|
|
|
[element.stream as usize]
|
2022-08-16 11:52:07 -04:00
|
|
|
+ element.offset as u32
|
2023-12-12 21:43:04 -05:00
|
|
|
+ model.meshes[j as usize].vertex_buffer_strides
|
2023-12-12 22:13:03 -05:00
|
|
|
[element.stream as usize]
|
|
|
|
as u32
|
|
|
|
* k as u32) as u64,
|
2022-08-16 11:52:07 -04:00
|
|
|
))
|
|
|
|
.ok()?;
|
2022-07-28 14:11:02 -04:00
|
|
|
|
|
|
|
match element.vertex_usage {
|
|
|
|
VertexUsage::Position => {
|
2023-07-05 19:42:53 -04:00
|
|
|
match element.vertex_type {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2022-07-28 14:11:02 -04:00
|
|
|
}
|
|
|
|
VertexUsage::BlendWeights => {
|
2023-07-05 19:42:53 -04:00
|
|
|
match element.vertex_type {
|
|
|
|
VertexType::ByteFloat4 => {
|
|
|
|
vertices[k as usize].bone_weight = MDL::read_byte_float4(&mut cursor).unwrap();
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
panic!("Unexpected vertex type for blendweight: {:#?}", element.vertex_type);
|
|
|
|
}
|
|
|
|
}
|
2022-07-28 14:11:02 -04:00
|
|
|
}
|
|
|
|
VertexUsage::BlendIndices => {
|
2023-07-05 19:42:53 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2022-07-28 14:11:02 -04:00
|
|
|
}
|
|
|
|
VertexUsage::Normal => {
|
2023-07-05 19:42:53 -04:00
|
|
|
match element.vertex_type {
|
|
|
|
VertexType::Half4 => {
|
|
|
|
vertices[k as usize].normal.clone_from_slice(&MDL::read_half4(&mut cursor).unwrap()[0..3]);
|
|
|
|
}
|
2023-07-09 11:56:45 -04:00
|
|
|
VertexType::Single3 => {
|
|
|
|
vertices[k as usize].normal = MDL::read_single3(&mut cursor).unwrap();
|
|
|
|
}
|
2023-07-05 19:42:53 -04:00
|
|
|
_ => {
|
|
|
|
panic!("Unexpected vertex type for normal: {:#?}", element.vertex_type);
|
|
|
|
}
|
2022-10-17 19:24:02 -04:00
|
|
|
}
|
2022-07-28 14:11:02 -04:00
|
|
|
}
|
|
|
|
VertexUsage::UV => {
|
2023-07-05 19:42:53 -04:00
|
|
|
match element.vertex_type {
|
|
|
|
VertexType::Half4 => {
|
2023-11-24 08:07:30 -05:00
|
|
|
let combined = MDL::read_half4(&mut cursor).unwrap();
|
|
|
|
|
|
|
|
vertices[k as usize].uv0.clone_from_slice(&combined[0..2]);
|
|
|
|
vertices[k as usize].uv1.clone_from_slice(&combined[2..4]);
|
2023-07-05 19:42:53 -04:00
|
|
|
}
|
2023-07-09 11:56:45 -04:00
|
|
|
VertexType::Single4 => {
|
2023-11-24 08:07:30 -05:00
|
|
|
let combined = MDL::read_single4(&mut cursor).unwrap();
|
|
|
|
|
|
|
|
vertices[k as usize].uv0.clone_from_slice(&combined[0..2]);
|
|
|
|
vertices[k as usize].uv1.clone_from_slice(&combined[2..4]);
|
2023-07-09 11:56:45 -04:00
|
|
|
}
|
2023-07-05 19:42:53 -04:00
|
|
|
_ => {
|
|
|
|
panic!("Unexpected vertex type for uv: {:#?}", element.vertex_type);
|
|
|
|
}
|
2022-10-17 19:24:02 -04:00
|
|
|
}
|
2022-07-28 14:11:02 -04:00
|
|
|
}
|
2023-12-17 18:58:48 -05:00
|
|
|
VertexUsage::BiTangent => {
|
2023-11-24 08:07:30 -05:00
|
|
|
match element.vertex_type {
|
|
|
|
VertexType::ByteFloat4 => {
|
2023-12-17 18:58:48 -05:00
|
|
|
vertices[k as usize].bitangent = MDL::read_tangent(&mut cursor).unwrap();
|
2023-11-24 08:07:30 -05:00
|
|
|
}
|
|
|
|
_ => {
|
2023-12-17 18:58:48 -05:00
|
|
|
panic!("Unexpected vertex type for bitangent: {:#?}", element.vertex_type);
|
2023-11-24 08:07:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-17 18:58:48 -05:00
|
|
|
VertexUsage::Tangent => {
|
2023-11-24 08:07:30 -05:00
|
|
|
match element.vertex_type {
|
2023-12-17 18:58:48 -05:00
|
|
|
/*VertexType::ByteFloat4 => {
|
|
|
|
vertices[k as usize].bitangent0 = MDL::read_tangent(&mut cursor).unwrap();
|
|
|
|
}*/
|
2023-11-24 08:07:30 -05:00
|
|
|
_ => {
|
2023-12-17 18:58:48 -05:00
|
|
|
panic!("Unexpected vertex type for tangent: {:#?}", element.vertex_type);
|
2023-11-24 08:07:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
VertexUsage::Color => {
|
|
|
|
match element.vertex_type {
|
|
|
|
VertexType::ByteFloat4 => {
|
|
|
|
vertices[k as usize].color = MDL::read_byte_float4(&mut cursor).unwrap();
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
panic!("Unexpected vertex type for color: {:#?}", element.vertex_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-28 14:11:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-16 11:52:07 -04:00
|
|
|
cursor
|
|
|
|
.seek(SeekFrom::Start(
|
|
|
|
(model_file_header.index_offsets[i as usize]
|
2023-12-12 22:04:30 -05:00
|
|
|
+ (model.meshes[j as usize].start_index * size_of::<u16>() as u32))
|
2022-08-16 11:52:07 -04:00
|
|
|
as u64,
|
|
|
|
))
|
|
|
|
.ok()?;
|
2022-07-28 14:11:02 -04:00
|
|
|
|
|
|
|
// TODO: optimize!
|
2022-08-16 11:52:07 -04:00
|
|
|
let mut indices: Vec<u16> =
|
|
|
|
Vec::with_capacity(model.meshes[j as usize].index_count as usize);
|
2022-08-06 18:07:42 -04:00
|
|
|
for _ in 0..model.meshes[j as usize].index_count {
|
2022-10-17 19:24:02 -04:00
|
|
|
indices.push(cursor.read_le::<u16>().ok()?);
|
2022-07-28 14:11:02 -04:00
|
|
|
}
|
|
|
|
|
2023-12-12 21:43:04 -05:00
|
|
|
let mut submeshes: Vec<SubMesh> = Vec::with_capacity(model.meshes[j as usize].submesh_count as usize);
|
|
|
|
for i in 0..model.meshes[j as usize].submesh_count {
|
|
|
|
submeshes.push(SubMesh {
|
|
|
|
submesh_index: model.meshes[j as usize].submesh_index as usize + i as usize,
|
|
|
|
index_count: model.submeshes[model.meshes[j as usize].submesh_index as usize + i as usize].index_count,
|
|
|
|
index_offset: model.submeshes[model.meshes[j as usize].submesh_index as usize + i as usize].index_offset,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
parts.push(Part { mesh_index: j, vertices, indices, material_index, submeshes });
|
2022-07-28 14:11:02 -04:00
|
|
|
}
|
|
|
|
|
2022-08-16 11:52:07 -04:00
|
|
|
lods.push(Lod { parts });
|
2022-07-28 14:11:02 -04:00
|
|
|
}
|
2022-07-19 19:29:41 -04:00
|
|
|
|
2022-07-28 14:11:02 -04:00
|
|
|
Some(MDL {
|
2023-09-22 16:44:19 -04:00
|
|
|
file_header: model_file_header,
|
2023-09-22 17:04:48 -04:00
|
|
|
model_data: model,
|
2022-08-10 14:51:50 -04:00
|
|
|
lods,
|
2022-08-16 11:52:07 -04:00
|
|
|
affected_bone_names,
|
2023-03-31 21:31:03 -04:00
|
|
|
material_names
|
2022-07-28 14:11:02 -04:00
|
|
|
})
|
|
|
|
}
|
2023-07-05 19:42:53 -04:00
|
|
|
|
2023-12-12 21:43:04 -05:00
|
|
|
pub fn replace_vertices(&mut self, lod_index: usize, part_index: usize, vertices: &[Vertex], indices: &[u16], submeshes: &[SubMesh]) {
|
2023-12-09 14:45:00 -05:00
|
|
|
let part = &mut self.lods[lod_index].parts[part_index];
|
|
|
|
|
2023-12-09 17:18:02 -05:00
|
|
|
part.vertices = Vec::from(vertices);
|
|
|
|
part.indices = Vec::from(indices);
|
|
|
|
|
2023-12-12 21:43:04 -05:00
|
|
|
for (i, submesh) in part.submeshes.iter().enumerate() {
|
|
|
|
if i < submeshes.len() {
|
|
|
|
self.model_data.submeshes[submesh.submesh_index].index_offset = submeshes[i].index_offset;
|
|
|
|
self.model_data.submeshes[submesh.submesh_index].index_count = submeshes[i].index_count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-09 17:18:02 -05:00
|
|
|
// Update vertex count in header
|
|
|
|
self.model_data.meshes[part.mesh_index as usize].vertex_count = part.vertices.len() as u16;
|
2023-12-12 21:43:04 -05:00
|
|
|
self.model_data.meshes[part.mesh_index as usize].index_count = part.indices.len() as u32;
|
2023-12-10 14:52:49 -05:00
|
|
|
|
|
|
|
// update values
|
|
|
|
for i in 0..self.file_header.lod_count {
|
|
|
|
let mut vertex_offset = 0;
|
2023-12-12 21:43:04 -05:00
|
|
|
let mut index_count = 0;
|
2023-12-10 14:52:49 -05:00
|
|
|
|
|
|
|
for j in self.model_data.lods[i as usize].mesh_index
|
|
|
|
..self.model_data.lods[i as usize].mesh_index + self.model_data.lods[i as usize].mesh_count
|
|
|
|
{
|
|
|
|
let mesh = &mut self.model_data.meshes[j as usize];
|
|
|
|
|
2023-12-12 21:43:04 -05:00
|
|
|
mesh.start_index = index_count;
|
|
|
|
index_count += mesh.index_count;
|
|
|
|
|
2023-12-10 14:52:49 -05:00
|
|
|
for i in 0..mesh.vertex_stream_count as usize {
|
|
|
|
mesh.vertex_buffer_offsets[i] = vertex_offset;
|
|
|
|
vertex_offset += mesh.vertex_count as u32 * mesh.vertex_buffer_strides[i] as u32;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for lod in &mut self.model_data.lods {
|
|
|
|
let mut total_vertex_buffer_size = 0;
|
2023-12-12 21:43:04 -05:00
|
|
|
let mut total_index_buffer_size = 0;
|
2023-12-10 14:52:49 -05:00
|
|
|
|
|
|
|
// still slightly off?
|
|
|
|
for j in lod.mesh_index
|
|
|
|
..lod.mesh_index + lod.mesh_count
|
|
|
|
{
|
|
|
|
let vertex_count = self.model_data.meshes[j as usize].vertex_count;
|
2023-12-12 21:43:04 -05:00
|
|
|
let index_count = self.model_data.meshes[j as usize].index_count;
|
2023-12-10 14:52:49 -05:00
|
|
|
|
|
|
|
let mut total_vertex_stride: u32 = 0;
|
|
|
|
for i in 0..self.model_data.meshes[j as usize].vertex_stream_count as usize {
|
|
|
|
total_vertex_stride += self.model_data.meshes[j as usize].vertex_buffer_strides[i] as u32;
|
|
|
|
}
|
|
|
|
|
|
|
|
total_vertex_buffer_size += vertex_count as u32 * total_vertex_stride;
|
2023-12-12 22:04:30 -05:00
|
|
|
total_index_buffer_size += index_count * size_of::<u16>() as u32;
|
2023-12-12 21:43:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unknown padding?
|
|
|
|
let mut index_padding = 16 - total_index_buffer_size % 16;
|
|
|
|
if index_padding == 16 {
|
|
|
|
index_padding = 0;
|
2023-12-10 14:52:49 -05:00
|
|
|
}
|
|
|
|
|
2023-12-12 21:43:04 -05:00
|
|
|
lod.vertex_buffer_size = total_vertex_buffer_size;
|
|
|
|
lod.index_buffer_size = total_index_buffer_size + index_padding;
|
2023-12-10 14:52:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// update lod values
|
2023-12-12 21:43:04 -05:00
|
|
|
// TODO: From Xande, need to be cleaned up :)
|
2023-12-13 16:51:40 -05:00
|
|
|
self.file_header.stack_size = self.file_header.calculate_stack_size();
|
2024-01-29 15:57:00 -05:00
|
|
|
self.file_header.runtime_size = self.model_data.calculate_runtime_size();
|
2023-12-12 21:43:04 -05:00
|
|
|
|
|
|
|
let mut vertex_offset = self.file_header.runtime_size
|
2023-12-12 22:04:30 -05:00
|
|
|
+ size_of::<ModelFileHeader>() as u32
|
2023-12-12 21:43:04 -05:00
|
|
|
+ self.file_header.stack_size;
|
2023-12-10 14:52:49 -05:00
|
|
|
|
|
|
|
for lod in &mut self.model_data.lods {
|
2023-12-12 21:43:04 -05:00
|
|
|
lod.vertex_data_offset = vertex_offset;
|
|
|
|
|
|
|
|
vertex_offset = lod.vertex_data_offset + lod.vertex_buffer_size;
|
2023-12-10 14:52:49 -05:00
|
|
|
|
2023-12-12 21:43:04 -05:00
|
|
|
lod.index_data_offset = vertex_offset;
|
2023-12-10 14:52:49 -05:00
|
|
|
|
2023-12-12 21:43:04 -05:00
|
|
|
// dummy
|
|
|
|
lod.edge_geometry_data_offset = vertex_offset;
|
2023-12-10 14:52:49 -05:00
|
|
|
|
2023-12-12 21:43:04 -05:00
|
|
|
vertex_offset = lod.index_data_offset + lod.index_buffer_size;
|
2023-12-10 14:52:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for i in 0..self.lods.len() {
|
|
|
|
self.file_header.vertex_buffer_size[i] = self.model_data.lods[i].vertex_buffer_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
for i in 0..self.lods.len() {
|
|
|
|
self.file_header.vertex_offsets[i] = self.model_data.lods[i].vertex_data_offset;
|
|
|
|
}
|
2023-12-12 21:43:04 -05:00
|
|
|
|
|
|
|
for i in 0..self.lods.len() {
|
|
|
|
self.file_header.index_buffer_size[i] = self.model_data.lods[i].index_buffer_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
for i in 0..self.lods.len() {
|
|
|
|
self.file_header.index_offsets[i] = self.model_data.lods[i].index_data_offset;
|
|
|
|
}
|
2023-12-09 14:45:00 -05:00
|
|
|
}
|
|
|
|
|
2023-10-13 16:16:04 -04:00
|
|
|
pub fn write_to_buffer(&self) -> Option<ByteBuffer> {
|
|
|
|
let mut buffer = ByteBuffer::new();
|
2023-09-22 16:44:19 -04:00
|
|
|
|
|
|
|
{
|
|
|
|
let mut cursor = Cursor::new(&mut buffer);
|
|
|
|
|
2023-09-22 17:04:48 -04:00
|
|
|
// write file header
|
|
|
|
self.file_header.write(&mut cursor).ok()?;
|
|
|
|
|
|
|
|
self.model_data.write(&mut cursor).ok()?;
|
2023-09-22 17:42:04 -04:00
|
|
|
|
|
|
|
for (l, lod) in self.lods.iter().enumerate() {
|
2023-11-24 08:25:45 -05:00
|
|
|
for part in lod.parts.iter() {
|
2023-12-13 17:19:02 -05:00
|
|
|
let declaration = &self.file_header.vertex_declarations[part.mesh_index as usize];
|
2023-09-22 17:42:04 -04:00
|
|
|
|
2023-11-22 21:21:01 -05:00
|
|
|
for (k, vert) in part.vertices.iter().enumerate() {
|
2023-09-22 17:42:04 -04:00
|
|
|
for element in &declaration.elements {
|
|
|
|
cursor
|
|
|
|
.seek(SeekFrom::Start(
|
2023-09-22 19:17:24 -04:00
|
|
|
(self.model_data.lods[l].vertex_data_offset
|
2023-09-22 17:42:04 -04:00
|
|
|
+ self.model_data.meshes[part.mesh_index as usize].vertex_buffer_offsets
|
2023-12-12 22:13:03 -05:00
|
|
|
[element.stream as usize]
|
2023-09-22 17:42:04 -04:00
|
|
|
+ element.offset as u32
|
|
|
|
+ self.model_data.meshes[part.mesh_index as usize].vertex_buffer_strides
|
2023-12-12 22:13:03 -05:00
|
|
|
[element.stream as usize]
|
|
|
|
as u32
|
|
|
|
* k as u32) as u64,
|
2023-09-22 17:42:04 -04:00
|
|
|
))
|
|
|
|
.ok()?;
|
|
|
|
|
|
|
|
match element.vertex_usage {
|
|
|
|
VertexUsage::Position => {
|
|
|
|
match element.vertex_type {
|
|
|
|
VertexType::Half4 => {
|
2023-11-24 08:07:30 -05:00
|
|
|
MDL::write_single4(&mut cursor, &MDL::pad_slice(&vert.position, 1.0)).ok()?;
|
2023-09-22 17:42:04 -04:00
|
|
|
}
|
|
|
|
VertexType::Single3 => {
|
|
|
|
MDL::write_single3(&mut cursor, &vert.position).ok()?;
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
panic!("Unexpected vertex type for position: {:#?}", element.vertex_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
VertexUsage::BlendWeights => {
|
|
|
|
match element.vertex_type {
|
|
|
|
VertexType::ByteFloat4 => {
|
2023-11-22 21:21:01 -05:00
|
|
|
MDL::write_byte_float4(&mut cursor, &vert.bone_weight).ok()?;
|
2023-09-22 17:42:04 -04:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
panic!("Unexpected vertex type for blendweight: {:#?}", element.vertex_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
VertexUsage::BlendIndices => {
|
|
|
|
match element.vertex_type {
|
|
|
|
VertexType::UInt => {
|
|
|
|
MDL::write_uint(&mut cursor, &vert.bone_id).ok()?;
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
panic!("Unexpected vertex type for blendindice: {:#?}", element.vertex_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
VertexUsage::Normal => {
|
|
|
|
match element.vertex_type {
|
|
|
|
VertexType::Half4 => {
|
2023-11-24 08:07:30 -05:00
|
|
|
MDL::write_half4(&mut cursor, &MDL::pad_slice(&vert.normal, 1.0)).ok()?;
|
2023-09-22 17:42:04 -04:00
|
|
|
}
|
|
|
|
VertexType::Single3 => {
|
|
|
|
MDL::write_single3(&mut cursor, &vert.normal).ok()?;
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
panic!("Unexpected vertex type for normal: {:#?}", element.vertex_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
VertexUsage::UV => {
|
|
|
|
match element.vertex_type {
|
|
|
|
VertexType::Half4 => {
|
2023-11-24 08:07:30 -05:00
|
|
|
let combined = [vert.uv0[0], vert.uv0[1], vert.uv1[0], vert.uv1[1]];
|
|
|
|
|
|
|
|
MDL::write_half4(&mut cursor, &combined).ok()?;
|
2023-09-22 17:42:04 -04:00
|
|
|
}
|
|
|
|
VertexType::Single4 => {
|
2023-11-24 08:07:30 -05:00
|
|
|
let combined = [vert.uv0[0], vert.uv0[1], vert.uv1[0], vert.uv1[1]];
|
|
|
|
|
|
|
|
MDL::write_single4(&mut cursor, &combined).ok()?;
|
2023-09-22 17:42:04 -04:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
panic!("Unexpected vertex type for uv: {:#?}", element.vertex_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-17 18:58:48 -05:00
|
|
|
VertexUsage::BiTangent => {
|
2023-11-24 08:07:30 -05:00
|
|
|
match element.vertex_type {
|
|
|
|
VertexType::ByteFloat4 => {
|
2023-12-17 18:58:48 -05:00
|
|
|
MDL::write_tangent(&mut cursor, &vert.bitangent).ok()?;
|
2023-11-24 08:07:30 -05:00
|
|
|
}
|
|
|
|
_ => {
|
2023-12-17 18:58:48 -05:00
|
|
|
panic!("Unexpected vertex type for bitangent: {:#?}", element.vertex_type);
|
2023-11-24 08:07:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-17 18:58:48 -05:00
|
|
|
VertexUsage::Tangent => {
|
2023-11-24 08:07:30 -05:00
|
|
|
match element.vertex_type {
|
2023-12-17 18:58:48 -05:00
|
|
|
/*VertexType::ByteFloat4 => {
|
|
|
|
MDL::write_tangent(&mut cursor, &vert.binormal).ok()?;
|
|
|
|
}*/
|
2023-11-24 08:07:30 -05:00
|
|
|
_ => {
|
2023-12-17 18:58:48 -05:00
|
|
|
panic!("Unexpected vertex type for tangent: {:#?}", element.vertex_type);
|
2023-11-24 08:07:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
VertexUsage::Color => {
|
|
|
|
match element.vertex_type {
|
|
|
|
VertexType::ByteFloat4 => {
|
|
|
|
MDL::write_byte_float4(&mut cursor, &vert.color).ok()?;
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
panic!("Unexpected vertex type for color: {:#?}", element.vertex_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-22 17:42:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor
|
|
|
|
.seek(SeekFrom::Start(
|
2023-11-22 21:21:01 -05:00
|
|
|
(self.file_header.index_offsets[l]
|
2023-12-12 22:04:30 -05:00
|
|
|
+ (self.model_data.meshes[part.mesh_index as usize].start_index * size_of::<u16>() as u32))
|
2023-09-22 17:42:04 -04:00
|
|
|
as u64,
|
|
|
|
))
|
|
|
|
.ok()?;
|
|
|
|
|
2023-11-24 08:29:32 -05:00
|
|
|
cursor.write_le(&part.indices).ok()?;
|
2023-09-22 17:42:04 -04:00
|
|
|
}
|
|
|
|
}
|
2023-09-22 16:44:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Some(buffer)
|
|
|
|
}
|
2023-12-13 16:51:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ModelFileHeader {
|
|
|
|
pub fn calculate_stack_size(&self) -> u32 {
|
|
|
|
// TODO: where does this magical 136 constant come from?
|
|
|
|
self.vertex_declaration_count as u32 * 136
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-29 15:57:00 -05:00
|
|
|
impl ModelData {
|
|
|
|
pub fn calculate_runtime_size(&self) -> u32 {
|
|
|
|
2 //StringCount
|
|
|
|
+ 2 // Unknown
|
|
|
|
+ 4 //StringSize
|
|
|
|
+ self.header.string_size
|
|
|
|
+ 56 //ModelHeader
|
|
|
|
+ (self.element_ids.len() as u32 * 32)
|
|
|
|
+ (3 * 60) // 3 Lods
|
|
|
|
//+ ( /*file.ModelHeader.ExtraLodEnabled ? 40*/ 0 )
|
|
|
|
+ self.meshes.len() as u32 * 36
|
|
|
|
+ self.attribute_name_offsets.len() as u32 * size_of::<u32>() as u32
|
|
|
|
+ self.header.terrain_shadow_mesh_count as u32 * 20
|
|
|
|
+ self.header.submesh_count as u32 * 16
|
|
|
|
+ self.header.terrain_shadow_submesh_count as u32 * 10
|
|
|
|
+ self.material_name_offsets.len() as u32 * size_of::<u32>() as u32
|
|
|
|
+ self.bone_name_offsets.len() as u32 * size_of::<u32>() as u32
|
|
|
|
+ self.bone_tables.len() as u32 * 132
|
|
|
|
+ self.header.shape_count as u32 * 16
|
|
|
|
+ self.header.shape_mesh_count as u32 * 12
|
|
|
|
+ self.header.shape_value_count as u32 * 4
|
|
|
|
+ 4 // SubmeshBoneMapSize
|
|
|
|
+ self.submesh_bone_map.len() as u32 * 2
|
|
|
|
+ 8 // PaddingAmount and Padding
|
|
|
|
+ (4 * 32) // 4 BoundingBoxes
|
|
|
|
+ (self.header.bone_count as u32 * 32)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-13 16:51:40 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use std::io::Cursor;
|
|
|
|
use crate::model::{MDL, ModelFileHeader};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_stack_size() {
|
|
|
|
let example_header = ModelFileHeader {
|
|
|
|
version: 0,
|
|
|
|
stack_size: 0,
|
|
|
|
runtime_size: 0,
|
|
|
|
vertex_declaration_count: 6,
|
|
|
|
material_count: 0,
|
|
|
|
vertex_offsets: [0; 3],
|
|
|
|
index_offsets: [0; 3],
|
|
|
|
vertex_buffer_size: [0; 3],
|
|
|
|
index_buffer_size: [0; 3],
|
|
|
|
lod_count: 0,
|
|
|
|
index_buffer_streaming_enabled: false,
|
|
|
|
has_edge_geometry: false,
|
2023-12-13 17:19:02 -05:00
|
|
|
vertex_declarations: Vec::new(),
|
2023-12-13 16:51:40 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(816, example_header.calculate_stack_size());
|
|
|
|
|
|
|
|
let example_header2 = ModelFileHeader {
|
|
|
|
version: 0,
|
|
|
|
stack_size: 0,
|
|
|
|
runtime_size: 0,
|
|
|
|
vertex_declaration_count: 2,
|
|
|
|
material_count: 0,
|
|
|
|
vertex_offsets: [0; 3],
|
|
|
|
index_offsets: [0; 3],
|
|
|
|
vertex_buffer_size: [0; 3],
|
|
|
|
index_buffer_size: [0; 3],
|
|
|
|
lod_count: 0,
|
|
|
|
index_buffer_streaming_enabled: false,
|
|
|
|
has_edge_geometry: false,
|
2023-12-13 17:19:02 -05:00
|
|
|
vertex_declarations: Vec::new()
|
2023-12-13 16:51:40 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(272, example_header2.calculate_stack_size());
|
|
|
|
}
|
2023-12-12 22:13:03 -05:00
|
|
|
}
|