1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
pub mod pmx_types {
    use std::fmt::{Display, Formatter};

    pub type Vec2 = [f32; 2];
    pub type Vec3 = [f32; 3];
    pub type Vec4 = [f32; 4];

    #[repr(u8)]
    #[derive(Debug, Clone, Copy)]
    pub enum Encode {
        UTF8 = 0x01,
        Utf16Le = 0x00,
    }
    /*Bone Flag*/
    pub const BONE_FLAG_TARGET_SHOW_MODE_MASK: u16 = 0x0001;
    pub const BONE_FLAG_ALLOW_ROTATE_MASK: u16 = 0x0002;
    //0b 0000 0000 0000 0010
    pub const BONE_FLAG_ALLOW_TRANSLATE_MASK: u16 = 0x0004;
    //0b 0000 0000 0000 0100
    pub const BONE_FLAG_VISIBLE_MASK: u16 = 0x0008;
    pub const BONE_FLAG_ALLOW_CONTROL_MASK: u16 = 0x0010;
    pub const BONE_FLAG_IK_MASK: u16 = 0x0020;
    pub const BONE_FLAG_APPEND_LOCAL_MASK: u16 = 0x0080;
    pub const BONE_FLAG_APPEND_ROTATE_MASK: u16 = 0x0100;
    //0b 0000 0001 0000 0000
    pub const BONE_FLAG_APPEND_TRANSLATE_MASK: u16 = 0x0200;
    //0b 0000 0010 0000 0000
    pub const BONE_FLAG_FIXED_AXIS_MASK: u16 = 0x0400;
    //0b 0000 0100 0000 0000
    pub const BONE_FLAG_LOCAL_AXIS_MASK: u16 = 0x0800;
    pub const BONE_FLAG_DEFORM_AFTER_PHYSICS_MASK: u16 = 0x1000;
    pub const BONE_FLAG_DEFORM_OUTER_PARENT_MASK: u16 = 0x2000;
    /*Material Flag*/
    const MATERIAL_DOUBLE_SIDE_MASK: u8 = 0x01;
    const MATERIAL_GROUND_SHADOW_MASK: u8 = 0x02;
    const MATERIAL_CAST_SELF_SHADOW_MASK: u8 = 0x04;
    const MATERIAL_RECEIVE_SELF_SHADOW_MASK: u8 = 0x08;
    const MATERIAL_EDGE_DRAW_MASK: u8 = 0x10;
    const MATERIAL_VERTEX_COLOR_MASK: u8 = 0x20;
    const MATERIAL_DRAW_POINT_MASK: u8 = 0x40;
    const MATERIAL_DRAW_LINE_MASK: u8 = 0x80;

    #[repr(packed)]
    pub struct PMXHeaderC {
        pub(crate) magic: [u8; 4],
        pub(crate) version: f32,
        pub(crate) length: u8,
        pub(crate) config: [u8; 8],
    }

    #[derive(Debug, Clone)]
    pub struct PMXHeaderRust {
        pub magic: String,
        pub version: f32,
        pub length: u8,
        pub encode: Encode,
        pub additional_uv: u8,
        pub s_vertex_index: u8,
        pub s_texture_index: u8,
        pub s_material_index: u8,
        pub s_bone_index: u8,
        pub s_morph_index: u8,
        pub s_rigid_body_index: u8,
    }

    pub enum IndexSize {
        Byte,
        Short,
        Int,
    }

    #[derive(Debug)]
    pub struct PMXModelInfo {
        pub  name: String,
        pub  name_en: String,
        pub  comment: String,
        pub  comment_en: String,
    }

    #[derive(Debug)]
    pub enum PMXVertexWeight {
        BDEF1 = 0x00,
        BDEF2 = 0x01,
        BDEF4 = 0x02,
        SDEF = 0x03,
        QDEF = 0x04,
    }

    #[derive(Debug)]
    pub struct PMXVertex {
        pub position: Vec3,
        pub norm: Vec3,
        pub uv: Vec2,
        pub add_uv: [Vec4; 4],
        pub weight_type: PMXVertexWeight,
        pub bone_indices: [i32; 4],
        pub bone_weights: [f32; 4],
        pub sdef_c: Vec3,
        pub sdef_r0: Vec3,
        pub sdef_r1: Vec3,
        pub edge_mag: f32,
    }

    /*Represent Triangle*/
    pub struct PMXFace {
        pub vertices: [u32; 3]
    }

    pub struct PMXFaces {
        pub faces: Vec<PMXFace>
    }

    pub struct PMXTextureList {
        pub textures: Vec<String>
    }

    pub enum PMXDrawModeFlags {
        BothFace = 0x01,
        GroundShadow = 0x02,
        CastSelfShadow = 0x04,
        RecieveSelfShadow = 0x08,
        DrawEdge = 0x10,
        VertexColor = 0x20,
        DrawPoint = 0x40,
        DrawLine = 0x80,
    }

    #[derive(Debug)]
    pub enum PMXSphereMode
    {
        None = 0x00,
        Mul = 0x01,
        Add = 0x02,
        SubTexture = 0x03,
    }

    #[derive(Debug)]
    pub enum PMXToonMode
    {
        Separate = 0x00,
        //< 0:個別Toon
        Common = 0x01,        //< 1:共有Toon[0-9] toon01.bmp~toon10.bmp
    }

    #[derive(Debug)]
    pub struct PMXMaterial {
        pub  name: String,
        pub  english_name: String,
        pub  diffuse: Vec4,
        pub  specular: Vec3,
        pub  specular_factor: f32,
        pub  ambient: Vec3,
        pub  drawmode: u8,
        pub  edge_color: Vec4,
        pub  edge_size: f32,
        pub  texture_index: i32,
        pub  sphere_mode_texture_index: i32,
        pub  spheremode: PMXSphereMode,
        pub  toon_mode: PMXToonMode,
        pub  toon_texture_index: i32,
        pub  memo: String,
        pub  num_face_vertices: i32,
    }

    #[derive(Debug)]
    pub struct PMXMaterials {
        pub  materials: Vec<PMXMaterial>
    }

    pub(crate) enum ReaderStage {
        Header,
        ModelInfo,
        VertexList,
        TextureList,
        SurfaceList,
        MaterialList,
        BoneList,
        MorphList,
        FrameList,
        RigidList,
        JointList,
    }

    #[derive(Debug)]
    pub struct PMXBone {
        pub(crate) name: String,
        pub(crate) english_name: String,
        pub(crate) position: Vec3,
        pub(crate) parent: i32,
        pub(crate) deform_depth: i32,
        pub(crate) boneflag: u16,
        pub(crate) offset: Vec3,
        pub(crate) child: i32,
        pub(crate) append_bone_index: i32,
        pub(crate) append_weight: f32,
        pub(crate) fixed_axis: Vec3,
        pub(crate) local_axis_x: Vec3,
        pub(crate) local_axis_z: Vec3,
        pub(crate) key_value: i32,
        pub(crate) ik_target_index: i32,
        pub(crate) ik_iter_count: i32,
        pub(crate) ik_limit: f32,
        pub(crate) ik_links: Vec<PMXIKLink>,
    }

    #[derive(Debug)]
    pub struct PMXIKLink {
        pub(crate) ik_bone_index: i32,
        pub(crate) enable_limit: u8,
        pub(crate) limit_min: Vec3,
        pub(crate) limit_max: Vec3,
    }

    #[derive(Debug)]
    pub struct PMXMorph {
        pub(crate) name: String,
        pub(crate) english_name: String,
        pub(crate) category: u8,
        pub(crate) morph_type: u8,
        pub(crate) offset: i32,
        pub(crate) morph_data: Vec<MorphTypes>,
    }

    #[derive(Debug)]
    pub enum MorphTypes {
        Vertex(VertexMorph),
        UV(UVMorph),
        UV1(UVMorph),
        UV2(UVMorph),
        UV3(UVMorph),
        UV4(UVMorph),
        Bone(BoneMorph),
        Material(MaterialMorph),
        Group(GroupMorph),
    }

    #[derive(Debug)]
    pub struct VertexMorph {
        pub(crate)  index: i32,
        pub(crate)  offset: Vec3,
    }

    #[derive(Debug)]
    pub struct UVMorph {
        pub(crate) index: i32,
        pub(crate) offset: Vec4,
    }

    #[derive(Debug)]
    pub struct GroupMorph {
        pub(crate) index: i32,
        pub(crate) morph_factor: f32,
    }

    #[derive(Debug)]
    pub struct BoneMorph {
        pub(crate) index: i32,
        pub(crate) translates: Vec3,
        pub(crate) rotates: Vec4,
    }

    #[derive(Debug)]
    pub struct MaterialMorph {
        pub(crate) index: i32,
        pub(crate) formula: u8,
        pub(crate) diffuse: Vec4,
        pub(crate) specular: Vec3,
        pub(crate) specular_factor: f32,
        pub(crate) ambient: Vec3,
        pub(crate) edge_color: Vec4,
        pub(crate) edge_size: f32,
        pub(crate) texture_factor: Vec4,
        pub(crate) sphere_texture_factor: Vec4,
        pub(crate) toon_texture_factor: Vec4,
    }

    #[derive(Debug)]
    pub struct PMXMorphs {
        pub(crate) morphs: Vec<PMXMorph>
    }

    pub struct PMXVertices {
        pub vertices: Vec<PMXVertex>
    }

    #[derive(Debug)]
    pub struct PMXBones {
        pub(crate) bones: Vec<PMXBone>
    }

    impl Display for PMXVertices {
        fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
            write!(f, "Vertices:{}", self.vertices.len());
            for vertex in self.vertices.iter() {
                writeln!(f, "{}", vertex);
            }
            Ok(())
        }
    }

    impl Display for PMXVertex {
        fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
            writeln!(f, "Vertex:[position:{:?} norm:{:?} uv:{:?}]", self.position, self.norm, self.uv);
            if [[0.0f32; 4]; 4] != self.add_uv {
                for add_uv in &self.add_uv {
                    write!(f, "{:?}", add_uv);
                }
            }
            match self.weight_type {
                PMXVertexWeight::BDEF1 => {
                    writeln!(f,
                             "BDEF1:[{}]", self.bone_indices[0]
                    );
                }
                PMXVertexWeight::BDEF2 => {
                    writeln!(f, "BDEF2:[index1:{} index2:{} weight1{}]", self.bone_indices[0], self.bone_indices[1], self.bone_weights[0]);
                }
                PMXVertexWeight::BDEF4 => {
                    writeln!(f, "BDEF4:[index1:{} index2:{} index3:{} index4:{}, weight1:{} weight2:{} weight3:{} weight4:{}]", self.bone_indices[0], self.bone_indices[1], self.bone_indices[2], self.bone_indices[3], self.bone_weights[0], self.bone_weights[1], self.bone_weights[2], self.bone_weights[3]);
                }
                PMXVertexWeight::SDEF => {
                    writeln!(f, "SDEF:[index1:{} index2:{} weight1{}]", self.bone_indices[0], self.bone_indices[1], self.bone_weights[0]);
                    writeln!(f, "SDEF Specific Params:[ C:[{:?}] R0:[{:?}] R1:[{:?}] ]", self.sdef_c, self.sdef_r0, self.sdef_r1);
                }
                PMXVertexWeight::QDEF => {
                    writeln!(f, "BDEF4:[index1:{} index2:{} index3:{} index4:{}, weight1:{} weight2:{} weight3:{} weight4:{}]", self.bone_indices[0], self.bone_indices[1], self.bone_indices[2], self.bone_indices[3], self.bone_weights[0], self.bone_weights[1], self.bone_weights[2], self.bone_weights[3]);
                }
            }
            writeln!(f, "edgeMagnifier:{}", self.edge_mag);
            Ok(())
        }
    }

    impl Display for PMXFace {
        fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
            writeln!(f, "Triangle:[{},{},{}]", self.vertices[0], self.vertices[1], self.vertices[2]);
            Ok(())
        }
    }


    impl Display for PMXFaces {
        fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
            writeln!(f, "Triangles:{}", self.faces.len());
            for triangle in self.faces.iter() {
                write!(f, "{}", triangle);
            }
            Ok(())
        }
    }

    impl Display for PMXTextureList {
        fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
            let _result=writeln!(f, "Textures:{}", self.textures.len());
            for name in self.textures.iter() {
                let _result=writeln!(f, "{}", name);
            }
            Ok(())
        }
    }
}