unbanished_formats/types/mesh.rs
1//! Graphics meshes: parts, draw ranges, bones and the vertex strides.
2
3use super::*;
4
5/// A range of a mesh part that is drawn with one material.
6#[derive(Debug, Clone)]
7pub struct DrawRange {
8 /// Base vertex added to each range-local u16 index.
9 pub start_vertex: i32,
10 /// The number of vertices.
11 pub vertex_count: i32,
12 /// The first index.
13 pub start_index: i32,
14 /// The number of indices.
15 pub index_count: i32,
16 /// The material of the range, an index into the mesh's material set.
17 pub material_index: i32,
18 /// Rigid node transform, or -1 for weighted skinning.
19 pub bone: i32,
20}
21
22/// A part of a mesh, with its level of detail, its draw ranges and its buffers.
23#[derive(Debug, Clone)]
24pub struct MeshPart {
25 /// The distance up to which this level of detail is used.
26 pub lod_distance: f32,
27 /// The part's name.
28 pub name: String,
29 /// The ambient-occlusion texture.
30 pub ao_texture: Option<String>,
31 /// The draw ranges.
32 pub ranges: Vec<DrawRange>,
33 /// Both streams can be present in one detail level (for example, the trader boat).
34 pub static_indices: Vec<u8>,
35 /// The vertex buffer of the rigid geometry.
36 pub static_vertices: Vec<u8>,
37 /// The index buffer of the skinned geometry.
38 pub skinned_indices: Vec<u8>,
39 /// The vertex buffer of the skinned geometry.
40 pub skinned_vertices: Vec<u8>,
41}
42
43/// A bone of a skeleton.
44#[derive(Debug, Clone)]
45pub struct Bone {
46 /// The bone's name.
47 pub name: String,
48 /// The parent bone's index (-1 for the root).
49 pub parent: i32,
50 /// The rotation relative to the parent (a quaternion, stored conjugated).
51 pub rotation: [f32; 4],
52 /// The translation relative to the parent.
53 pub translation: [f32; 3],
54 /// The rotation of the inverse bind transform.
55 pub inv_rotation: [f32; 4],
56 /// The translation of the inverse bind transform.
57 pub inv_translation: [f32; 3],
58}
59
60/// A mesh: its parts, its material sets, its skeleton and its bounds.
61#[derive(Debug, Clone)]
62pub struct GraphicsMesh {
63 /// The original model file.
64 pub source: String,
65 /// The parts.
66 pub parts: Vec<MeshPart>,
67 /// Material instance references, one list per material set.
68 pub material_instances: Vec<Vec<Option<String>>>,
69 /// The skeleton's bones (empty for a rigid mesh).
70 pub bones: Vec<Bone>,
71 /// The smallest corner of the bounding box.
72 pub bounds_min: [f32; 3],
73 /// The largest corner of the bounding box.
74 pub bounds_max: [f32; 3],
75}
76
77/// `ModelLayout`: sint16x4 position, sint16x4 texcoords, unorm8x4 normal, 4 bytes padding.
78pub const STATIC_STRIDE: usize = 24;
79
80/// `ModelSkinLayout`: as static, plus unorm8x4 weights at 24 and uint8x4 bone indices at 28.
81pub const SKINNED_STRIDE: usize = 32;
82
83/// Positions are stored as int16 and scaled by this in the vertex shader.
84pub const POSITION_SCALE: f32 = 1.0 / 512.0;
85
86impl MeshPart {
87 /// Select the index and vertex stream used by a triangle list.
88 pub fn geometry(&self, range: &DrawRange) -> (&[u8], &[u8], usize) {
89 if range.bone < 0 {
90 (
91 &self.skinned_indices,
92 &self.skinned_vertices,
93 SKINNED_STRIDE,
94 )
95 } else {
96 (&self.static_indices, &self.static_vertices, STATIC_STRIDE)
97 }
98 }
99}
100
101impl GraphicsMesh {
102 /// The resource class name.
103 pub const CLASS: &'static str = "GraphicsMesh";
104
105 /// Read a mesh. Block version 0 (meshes from older toolkits, in mods) keeps each part's
106 /// geometry with the part (the DX9 block, then the DX11 block); later versions keep all of it
107 /// in the two platform blocks at the end, the DX11 one led by an integer.
108 ///
109 /// Original: `Graphics::GraphicsMesh::Serialize` and `PlatformSerialize`; the version 0 part
110 /// geometry, Runtime `0x1800a55c0`.
111 ///
112 /// Original: Runtime `0x180101c40`, Runtime `0x1800a5830`.
113 pub fn read(r: &mut Reader) -> Result<Self> {
114 /// One part's draw ranges and buffers (DX11).
115 fn geometry(r: &mut Reader, part: &mut MeshPart) -> Result<()> {
116 for _ in 0..r.count()? {
117 let v = [r.i32()?, r.i32()?, r.i32()?, r.i32()?, r.i32()?, r.i32()?];
118 part.ranges.push(DrawRange {
119 start_vertex: v[0],
120 vertex_count: v[1],
121 start_index: v[2],
122 index_count: v[3],
123 material_index: v[4],
124 bone: v[5],
125 });
126 }
127 let mut buffer = || -> Result<Vec<u8>> {
128 r.u8()?;
129 let n = r.u32()? as usize;
130 Ok(r.bytes(n)?.to_vec())
131 };
132 part.static_indices = buffer()?;
133 part.static_vertices = buffer()?;
134 part.skinned_indices = buffer()?;
135 part.skinned_vertices = buffer()?;
136 r.i32()?;
137 Ok(())
138 }
139 r.block(|r, version| {
140 let source = r.string()?;
141 r.f32s::<6>()?; // unused bounds, always (1,1,1)/(-1,-1,-1)
142 r.i32()?;
143 r.f32s::<2>()?;
144 let mut parts = Vec::new();
145 for _ in 0..r.count()? {
146 let mut part = MeshPart {
147 lod_distance: r.f32()?,
148 name: r.string()?,
149 ao_texture: r.external_ref()?,
150 ranges: Vec::new(),
151 static_indices: Vec::new(),
152 static_vertices: Vec::new(),
153 skinned_indices: Vec::new(),
154 skinned_vertices: Vec::new(),
155 };
156 if version == 0 {
157 r.skip_block()?; // DX9 geometry
158 r.block(|r, _| geometry(r, &mut part))?;
159 }
160 parts.push(part);
161 }
162 let mut material_instances = Vec::new();
163 for _ in 0..r.count()? {
164 let n = r.count()?;
165 material_instances.push((0..n).map(|_| r.external_ref()).collect::<Result<_>>()?);
166 }
167 let mut bones = Vec::new();
168 for _ in 0..r.count()? {
169 bones.push(Bone {
170 name: r.string()?,
171 parent: r.i32()?,
172 rotation: r.f32s()?,
173 translation: r.f32s()?,
174 inv_rotation: r.f32s()?,
175 inv_translation: r.f32s()?,
176 });
177 }
178 let bounds_min = r.f32s()?;
179 let bounds_max = r.f32s()?;
180 if version > 0 {
181 r.skip_block()?; // DX9 geometry
182 r.block(|r, _| {
183 r.i32()?;
184 for part in &mut parts {
185 geometry(r, part)?;
186 }
187 Ok(())
188 })?;
189 }
190 Ok(GraphicsMesh {
191 source,
192 parts,
193 material_instances,
194 bones,
195 bounds_min,
196 bounds_max,
197 })
198 })
199 }
200}