live2d_parser/cubism_v1/moc/
meshes.rs

1use super::*;
2
3#[derive(Debug, Serialize, Deserialize, Default)]
4pub struct Mesh {
5    pub id: String,
6    pub target_id: String,
7    pub average_draw_order: i32,
8    pub pivot_draw_order: Vec<i32>,
9    pub pivot_opacity: Vec<f32>,
10    pub clip_id: Vec<String>,
11    pub values: ObjectData,
12    pub texture_id: i32,
13    pub point_count: i32,
14    pub polygon_count: i32,
15    pub index_array: ObjectData,
16    pub pivot_points: ObjectData,
17    pub uv_maps: ObjectData,
18    pub mesh_flags: i32,
19    pub color_composition_type: i32,
20    pub color_group_id: i32,
21    pub culling: bool,
22}
23
24impl MocObject for Mesh {
25    fn read_object(reader: &mut MocReader) -> Result<Self, L2Error>
26    where
27        Self: Sized,
28    {
29        let mut output = Mesh::default();
30        output.id = reader.read()?;
31        output.target_id = reader.read()?;
32        output.values = reader.read()?;
33        output.average_draw_order = reader.read()?;
34        output.pivot_draw_order = reader.read()?;
35        output.pivot_opacity = reader.read()?;
36        if reader.version() >= 11 {
37            let draw_id: String = reader.read()?;
38            if draw_id.is_empty() {} else if draw_id.contains(",") {
39                output.clip_id = draw_id.split(',').map(|s| s.to_string()).collect();
40            } else {
41                output.clip_id.push(draw_id)
42            }
43        }
44        output.texture_id = reader.read()?;
45        output.point_count = reader.read()?;
46        output.polygon_count = reader.read()?;
47        output.index_array = reader.read()?;
48        output.pivot_points = reader.read()?;
49        output.uv_maps = reader.read()?;
50        if reader.version() >= 8 {
51            output.mesh_flags = reader.read()?;
52            if output.mesh_flags != 0 {
53                if (output.mesh_flags & 1) != 0 {
54                    output.color_group_id = reader.read()?;
55                }
56                output.color_composition_type = if (output.mesh_flags & 30) != 0 { (output.mesh_flags & 30) >> 1 } else { 0 };
57                if (output.mesh_flags & 1 << 5) != 0 {
58                    output.culling = false;
59                }
60            }
61        }
62        Ok(output)
63    }
64}
65
66impl ObjectData {
67    pub fn as_texture(self) -> Vec<Mesh> {
68        match self {
69            ObjectData::Null => Vec::new(),
70            ObjectData::ObjectArray(o) => o.into_iter().map(|x| x.as_texture()).flatten().collect(),
71            // ObjectData::Pivot(v) => vec![v],
72            // ObjectData::PivotManager(v) => v.items,
73            s => {
74                warn!("ObjectData::as_texture() called on non-pivot object {s:?}");
75                vec![]
76            }
77        }
78    }
79}