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
|
/*
wearechat - generic multiplayer game with voip
Copyright (C) 2025 metamuffin
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, version 3 of the License only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use crate::load_texture;
use anyhow::Result;
use gltf::{Mesh, Node, buffer::Data};
use log::{debug, info};
use std::path::Path;
use weareshared::{
Affine3A, Vec3A,
resources::{AttributeArray, IndexArray, MeshPart, Prefab},
store::ResourceStore,
};
pub fn import_mesh(
mesh: Mesh,
buffers: &[Data],
store: &ResourceStore,
path_base: &Path,
node: &Node,
prefab: &mut Prefab,
webp: bool,
) -> Result<()> {
Ok(for p in mesh.primitives() {
let reader = p.reader(|buf| Some(&buffers[buf.index()]));
let va_position = reader
.read_positions()
.map(|iter| {
let mut pos_x = vec![];
let mut pos_y = vec![];
let mut pos_z = vec![];
for p in iter {
pos_x.push(p[0]);
pos_y.push(p[1]);
pos_z.push(p[2]);
}
info!("{} vertex positions", pos_x.len());
Ok::<_, anyhow::Error>([
store.set(&AttributeArray(pos_x))?,
store.set(&AttributeArray(pos_y))?,
store.set(&AttributeArray(pos_z))?,
])
})
.transpose()?;
let va_normal = reader
.read_normals()
.map(|iter| {
let mut normal_x = vec![];
let mut normal_y = vec![];
let mut normal_z = vec![];
for p in iter {
normal_x.push(p[0]);
normal_y.push(p[1]);
normal_z.push(p[2]);
}
info!("{} vertex normals", normal_x.len());
Ok::<_, anyhow::Error>([
store.set(&AttributeArray(normal_x))?,
store.set(&AttributeArray(normal_y))?,
store.set(&AttributeArray(normal_z))?,
])
})
.transpose()?;
let va_texcoord = reader
.read_tex_coords(0)
.map(|iter| {
let mut texcoord_u = vec![];
let mut texcoord_v = vec![];
for p in iter.into_f32() {
texcoord_u.push(p[0]);
texcoord_v.push(p[1]);
}
info!("{} vertex texture coordinates", texcoord_u.len());
Ok::<_, anyhow::Error>([
store.set(&AttributeArray(texcoord_u))?,
store.set(&AttributeArray(texcoord_v))?,
])
})
.transpose()?;
let va_albedo = reader
.read_colors(0)
.map(|iter| {
let mut color_r = vec![];
let mut color_g = vec![];
let mut color_b = vec![];
for p in iter.into_rgb_f32() {
color_r.push(p[0]);
color_g.push(p[1]);
color_b.push(p[2]);
}
info!("{} vertex colors", color_r.len());
Ok::<_, anyhow::Error>([
store.set(&AttributeArray(color_r))?,
store.set(&AttributeArray(color_g))?,
store.set(&AttributeArray(color_b))?,
])
})
.transpose()?;
let va_transmission = reader
.read_colors(0)
.map(|iter| {
let mut color_a = vec![];
for p in iter.into_rgba_f32() {
color_a.push(p[3]);
}
let o = if color_a.iter().any(|x| *x != 1.) {
info!("{} vertex transmissions", color_a.len());
Some(store.set(&AttributeArray(color_a))?)
} else {
debug!("vertex transmission pruned");
None
};
Ok::<_, anyhow::Error>(o)
})
.transpose()?
.flatten();
let index = reader
.read_indices()
.unwrap()
.into_u32()
.map(|e| e as u16)
.array_chunks::<3>()
.collect::<Vec<_>>();
info!("{} indecies", index.len() * 3);
let index = Some(store.set(&IndexArray(index))?);
let mut tex_albedo = None;
let mut tex_transmission = None;
if let Some(tex) = p.material().pbr_metallic_roughness().base_color_texture() {
let r = load_texture(
"albedo",
&store,
path_base,
&buffers,
&tex.texture().source().source(),
webp,
)?;
tex_albedo = Some(r.clone());
tex_transmission = Some(r.clone());
}
let mut tex_normal = None;
if let Some(tex) = p.material().normal_texture() {
tex_normal = Some(load_texture(
"normal",
&store,
path_base,
&buffers,
&tex.texture().source().source(),
webp,
)?);
}
let mut tex_emission = None;
if let Some(tex) = p.material().emissive_texture() {
tex_emission = Some(load_texture(
"emission",
&store,
path_base,
&buffers,
&tex.texture().source().source(),
webp,
)?);
}
let mut tex_roughness = None;
let mut tex_metallic = None;
if let Some(tex) = p
.material()
.pbr_metallic_roughness()
.metallic_roughness_texture()
{
let r = load_texture(
"metallic+roughness",
&store,
path_base,
&buffers,
&tex.texture().source().source(),
webp,
)?;
tex_roughness = Some(r.clone());
tex_metallic = Some(r.clone());
}
let g_metallic = Some(p.material().pbr_metallic_roughness().metallic_factor());
let g_roughness = Some(p.material().pbr_metallic_roughness().roughness_factor());
let base_color = p.material().pbr_metallic_roughness().base_color_factor();
let g_albedo = if base_color[0] != 1. || base_color[1] != 1. || base_color[2] != 1. {
info!(
"global albedo is r={},g={},b={}",
base_color[0], base_color[1], base_color[2]
);
Some(Vec3A::new(base_color[0], base_color[1], base_color[2]))
} else {
debug!("global albedo pruned");
None
};
let g_transmission = if base_color[3] != 1. {
info!("global transmission is {}", base_color[3]);
Some(base_color[3])
} else {
debug!("global transmission pruned");
None
};
let emission = p.material().emissive_factor();
let g_emission = if emission[0] != 0. || emission[1] != 0. || emission[2] != 0. {
info!(
"global emission is r={},g={},b={}",
base_color[0], base_color[1], base_color[2]
);
Some(Vec3A::new(emission[0], emission[1], emission[2]))
} else {
debug!("global emission pruned");
None
};
let mesh = store.set(&MeshPart {
g_albedo,
g_transmission,
g_metallic,
g_roughness,
g_emission,
va_position,
va_normal,
va_texcoord,
va_albedo,
va_transmission,
tex_albedo,
tex_normal,
tex_roughness,
tex_metallic,
tex_transmission,
tex_emission,
index,
va_emission: None, // not supported by gltf
va_metallic: None, // not supported by gltf
va_roughness: None, // not supported by gltf
})?;
let mat = node.transform().matrix();
let aff = Affine3A::from_cols_array_2d(&[
[mat[0][0], mat[0][1], mat[0][2]],
[mat[1][0], mat[1][1], mat[1][2]],
[mat[2][0], mat[2][1], mat[2][2]],
[mat[3][0], mat[3][1], mat[3][2]],
]);
prefab.mesh.push((aff, mesh))
})
}
|