Skip to main content

rpfm_lib/files/rigidmodel/materials/
cloth.rs

1//---------------------------------------------------------------------------//
2// Copyright (c) 2017-2026 Ismael Gutiérrez González. All rights reserved.
3//
4// This file is part of the Rusted PackFile Manager (RPFM) project,
5// which can be found here: https://github.com/Frodo45127/rpfm.
6//
7// This file is licensed under the MIT license, which can be found here:
8// https://github.com/Frodo45127/rpfm/blob/master/LICENSE.
9//---------------------------------------------------------------------------//
10
11use super::*;
12
13//---------------------------------------------------------------------------//
14//                            Implementation
15//---------------------------------------------------------------------------//
16
17impl Material {
18    pub fn read_cloth<R: ReadBytes>(data: &mut R, version: u32) -> Result<Self> {
19        let mut mat = Self::read_default(data, version)?;
20
21        // These have a ton of extra data here.
22        let links_count = data.read_u32()?;
23        mat.cloth_links = Vec::with_capacity(links_count as usize);
24
25        let counts_count = data.read_u32()?;
26        mat.cloth_triangle_counts = Vec::with_capacity(counts_count as usize);
27
28        let triangles_count = data.read_u32()?;
29        mat.cloth_triangles = Vec::with_capacity(triangles_count as usize);
30
31        for _ in 0..mat.cloth_links.capacity() {
32            let mut link = ClothLink::default();
33
34            link.particle_a = data.read_i32()?;
35            link.particle_b = data.read_i32()?;
36            link.rest_length = data.read_f32()?;
37
38            mat.cloth_links.push(link);
39        }
40
41        for _ in 0..mat.cloth_triangle_counts.capacity() {
42            let mut count = ClothTriangleCount::default();
43
44            count.count = data.read_i32()?;
45
46            mat.cloth_triangle_counts.push(count);
47        }
48
49        for _ in 0..mat.cloth_triangles.capacity() {
50            let mut triangle = ClothTriangle::default();
51
52            triangle.index_0 = data.read_i32()?;
53            triangle.index_1 = data.read_i32()?;
54            triangle.index_2 = data.read_i32()?;
55
56            mat.cloth_triangles.push(triangle);
57        }
58
59        Ok(mat)
60    }
61    pub fn write_cloth<W: WriteBytes>(&self, buffer: &mut W, version: u32) -> Result<()> {
62        self.write_default(buffer, version)?;
63
64        buffer.write_i32(self.cloth_links.len() as i32)?;
65        buffer.write_i32(self.cloth_triangle_counts.len() as i32)?;
66        buffer.write_i32(self.cloth_triangles.len() as i32)?;
67
68        for link in self.cloth_links() {
69            buffer.write_i32(link.particle_a)?;
70            buffer.write_i32(link.particle_b)?;
71            buffer.write_f32(link.rest_length)?;
72        }
73
74        for count in self.cloth_triangle_counts() {
75            buffer.write_i32(count.count)?;
76        }
77
78        for triangle in self.cloth_triangles() {
79            buffer.write_i32(triangle.index_0)?;
80            buffer.write_i32(triangle.index_1)?;
81            buffer.write_i32(triangle.index_2)?;
82        }
83
84        Ok(())
85    }
86}