mesh-tools 0.4.0

A Rust library for generating 3D meshes and exporting them to glTF/GLB files
Documentation
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//! # glTF Data Model Definitions
//!
//! This module contains the core data structures that represent a glTF 2.0 document.
//! These structures match the JSON schema defined in the [glTF 2.0 specification](https://www.khronos.org/registry/glTF/specs/2.0/glTF-2.0.html).
//!
//! The structures in this module are designed to be serialized to JSON using serde,
//! with optional fields that are skipped when None to produce valid glTF JSON.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Represents a complete glTF 2.0 document
///
/// A glTF document contains all the resources and metadata needed to represent a 3D scene or model.
/// It includes scenes, nodes, meshes, materials, textures, and binary data references.
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct Gltf {
    pub asset: Asset,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scene: Option<usize>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scenes: Option<Vec<Scene>>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub nodes: Option<Vec<Node>>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub meshes: Option<Vec<Mesh>>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub accessors: Option<Vec<Accessor>>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "bufferViews")]
    pub buffer_views: Option<Vec<BufferView>>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub buffers: Option<Vec<Buffer>>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub materials: Option<Vec<Material>>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub textures: Option<Vec<Texture>>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub images: Option<Vec<Image>>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub samplers: Option<Vec<Sampler>>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub animations: Option<Vec<Animation>>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub extensions: Option<serde_json::Value>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "extensionsUsed")]
    pub extensions_used: Option<Vec<String>>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "extensionsRequired")]
    pub extensions_required: Option<Vec<String>>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub extras: Option<HashMap<String, serde_json::Value>>,
}

/// Represents the glTF asset information
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct Asset {
    pub version: String,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub generator: Option<String>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub copyright: Option<String>,
}

/// Represents a glTF scene
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct Scene {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub nodes: Option<Vec<usize>>,
}

/// Represents a glTF node
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct Node {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mesh: Option<usize>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub translation: Option<[f32; 3]>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rotation: Option<[f32; 4]>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scale: Option<[f32; 3]>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub matrix: Option<[f32; 16]>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub children: Option<Vec<usize>>,
}

/// Represents a glTF mesh
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct Mesh {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    
    pub primitives: Vec<Primitive>,
}

/// Represents a glTF mesh primitive
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct Primitive {
    pub attributes: HashMap<String, usize>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub indices: Option<usize>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub material: Option<usize>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mode: Option<usize>,
}

/// Represents a glTF accessor
#[derive(Serialize, Deserialize, Debug)]
pub struct Accessor {
    #[serde(rename = "bufferView")]
    pub buffer_view: usize,
    #[serde(rename = "componentType")]
    pub component_type: usize,
    pub count: usize,
    #[serde(rename = "type")]
    pub type_: String,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "byteOffset")]
    pub byte_offset: Option<usize>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub min: Option<Vec<f32>>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max: Option<Vec<f32>>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub normalized: Option<bool>,
}

/// Represents a glTF buffer view
#[derive(Serialize, Deserialize, Debug)]
pub struct BufferView {
    pub buffer: usize,
    #[serde(rename = "byteOffset")]
    pub byte_offset: usize,
    #[serde(rename = "byteLength")]
    pub byte_length: usize,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "byteStride")]
    pub byte_stride: Option<usize>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub target: Option<usize>,
}

/// Represents a glTF buffer
#[derive(Serialize, Deserialize, Debug)]
pub struct Buffer {
    #[serde(rename = "byteLength")]
    pub byte_length: usize,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub uri: Option<String>,
}

/// Represents a glTF material
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct Material {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "pbrMetallicRoughness")]
    pub pbr_metallic_roughness: Option<PbrMetallicRoughness>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "normalTexture")]
    pub normal_texture: Option<NormalTextureInfo>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "occlusionTexture")]
    pub occlusion_texture: Option<OcclusionTextureInfo>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "emissiveTexture")]
    pub emissive_texture: Option<TextureInfo>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "emissiveFactor")]
    pub emissive_factor: Option<[f32; 3]>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "alphaMode")]
    pub alpha_mode: Option<String>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "alphaCutoff")]
    pub alpha_cutoff: Option<f32>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "doubleSided")]
    pub double_sided: Option<bool>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub extensions: Option<MaterialExtensions>,
}

/// Represents a glTF PBR material
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct PbrMetallicRoughness {
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "baseColorFactor")]
    pub base_color_factor: Option<[f32; 4]>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "baseColorTexture")]
    pub base_color_texture: Option<TextureInfo>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "metallicFactor")]
    pub metallic_factor: Option<f32>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "roughnessFactor")]
    pub roughness_factor: Option<f32>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "metallicRoughnessTexture")]
    pub metallic_roughness_texture: Option<TextureInfo>,
}

/// Represents basic texture reference information
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct TextureInfo {
    pub index: usize,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "texCoord")]
    pub tex_coord: Option<usize>,
}

/// Represents normal texture reference information
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct NormalTextureInfo {
    pub index: usize,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "texCoord")]
    pub tex_coord: Option<usize>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scale: Option<f32>,
}

/// Represents occlusion texture reference information
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct OcclusionTextureInfo {
    pub index: usize,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "texCoord")]
    pub tex_coord: Option<usize>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub strength: Option<f32>,
}

/// Represents a glTF texture
#[derive(Serialize, Deserialize, Debug)]
pub struct Texture {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    
    pub source: usize,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sampler: Option<usize>,
}

/// Represents a glTF image
#[derive(Serialize, Deserialize, Debug)]
pub struct Image {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub uri: Option<String>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "mimeType")]
    pub mime_type: Option<String>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "bufferView")]
    pub buffer_view: Option<usize>,
}

/// Represents a glTF sampler
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct Sampler {
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "magFilter")]
    pub mag_filter: Option<usize>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "minFilter")]
    pub min_filter: Option<usize>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "wrapS")]
    pub wrap_s: Option<usize>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "wrapT")]
    pub wrap_t: Option<usize>,
}

/// Represents a glTF animation
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct Animation {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub channels: Option<Vec<AnimationChannel>>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub samplers: Option<Vec<AnimationSampler>>
}

/// Represents a glTF animation channel
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct AnimationChannel {
    pub sampler: usize,
    
    pub target: AnimationChannelTarget
}

/// Represents a glTF animation channel target
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct AnimationChannelTarget {
    pub node: usize,
    
    pub path: String
}

/// Represents a glTF animation sampler
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct AnimationSampler {
    pub input: usize,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub interpolation: Option<String>,
    
    pub output: usize
}

/// Represents material extensions for glTF
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct MaterialExtensions {
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "KHR_materials_pbrSpecularGlossiness")]
    pub pbr_specular_glossiness: Option<PbrSpecularGlossiness>,
}

/// Represents a glTF specular-glossiness PBR material extension
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct PbrSpecularGlossiness {
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "diffuseFactor")]
    pub diffuse_factor: Option<[f32; 4]>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "diffuseTexture")]
    pub diffuse_texture: Option<TextureInfo>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "specularFactor")]
    pub specular_factor: Option<[f32; 3]>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "glossinessFactor")]
    pub glossiness_factor: Option<f32>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "specularGlossinessTexture")]
    pub specular_glossiness_texture: Option<TextureInfo>,
}