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
use crate::builder::GltfBuilder;
use crate::models::{Animation, AnimationChannel, AnimationChannelTarget, AnimationSampler};
/// Animation path type
pub enum AnimationPath {
Translation,
Rotation,
Scale,
Weights,
}
impl AnimationPath {
/// Convert to string representation for glTF
pub fn to_string(&self) -> String {
match self {
AnimationPath::Translation => "translation".to_string(),
AnimationPath::Rotation => "rotation".to_string(),
AnimationPath::Scale => "scale".to_string(),
AnimationPath::Weights => "weights".to_string(),
}
}
}
/// Interpolation method for animation
pub enum InterpolationType {
Linear,
Step,
CubicSpline,
}
impl InterpolationType {
/// Convert to string representation for glTF
pub fn to_string(&self) -> String {
match self {
InterpolationType::Linear => "LINEAR".to_string(),
InterpolationType::Step => "STEP".to_string(),
InterpolationType::CubicSpline => "CUBICSPLINE".to_string(),
}
}
}
impl GltfBuilder {
/// Add an animation to the glTF
///
/// # Arguments
///
/// * `name` - Optional name for the animation
///
/// # Returns
///
/// The index of the created animation
pub fn add_animation(&mut self, name: Option<String>) -> usize {
let animation = Animation {
name,
channels: Some(Vec::new()),
samplers: Some(Vec::new()),
};
if let Some(animations) = &mut self.gltf.animations {
let index = animations.len();
animations.push(animation);
index
} else {
self.gltf.animations = Some(vec![animation]);
0
}
}
/// Add a sampler to an animation
///
/// # Arguments
///
/// * `animation_index` - The index of the animation to add the sampler to
/// * `input_accessor` - The accessor containing keyframe timestamps (in seconds)
/// * `output_accessor` - The accessor containing output values
/// * `interpolation` - The interpolation method
///
/// # Returns
///
/// The index of the created sampler within the animation
pub fn add_animation_sampler(
&mut self,
animation_index: usize,
input_accessor: usize,
output_accessor: usize,
interpolation: InterpolationType
) -> usize {
let sampler = AnimationSampler {
input: input_accessor,
interpolation: Some(interpolation.to_string()),
output: output_accessor,
};
let animations = self.gltf.animations.as_mut().expect("Animations array not initialized");
if animation_index >= animations.len() {
panic!("Animation index out of bounds");
}
let animation = &mut animations[animation_index];
let samplers = animation.samplers.get_or_insert_with(|| Vec::new());
let sampler_index = samplers.len();
samplers.push(sampler);
sampler_index
}
/// Add a channel to an animation
///
/// # Arguments
///
/// * `animation_index` - The index of the animation to add the channel to
/// * `sampler_index` - The index of the sampler within the animation
/// * `target_node` - The index of the node being animated
/// * `target_path` - The property being animated (translation, rotation, scale, weights)
///
/// # Returns
///
/// The index of the created channel within the animation
pub fn add_animation_channel(
&mut self,
animation_index: usize,
sampler_index: usize,
target_node: usize,
target_path: AnimationPath
) -> usize {
let channel = AnimationChannel {
sampler: sampler_index,
target: AnimationChannelTarget {
node: target_node,
path: target_path.to_string(),
}
};
let animations = self.gltf.animations.as_mut().expect("Animations array not initialized");
if animation_index >= animations.len() {
panic!("Animation index out of bounds");
}
let animation = &mut animations[animation_index];
let channels = animation.channels.get_or_insert_with(|| Vec::new());
let channel_index = channels.len();
channels.push(channel);
channel_index
}
/// Create translation keyframes for an animation
///
/// # Arguments
///
/// * `animation_index` - The index of the animation
/// * `node_index` - The index of the target node
/// * `timestamps` - Vector of keyframe timestamps (in seconds)
/// * `translations` - Vector of translation values ([x, y, z] for each keyframe)
/// * `interpolation` - The interpolation method
///
/// # Returns
///
/// The indices of the created channel and sampler
pub fn create_translation_animation(
&mut self,
animation_index: usize,
node_index: usize,
timestamps: Vec<f32>,
translations: Vec<[f32; 3]>,
interpolation: InterpolationType,
) -> (usize, usize) {
if timestamps.len() != translations.len() {
panic!("Timestamps and translations must have the same length");
}
// Create time input accessor
let timestamps_data: Vec<u8> = timestamps.iter().flat_map(|&t| t.to_le_bytes()).collect();
// Add buffer data and create buffer view
let (time_offset, time_length) = self.add_buffer_data(×tamps_data);
let time_buffer_view = self.add_buffer_view(
time_offset,
time_length,
None
);
// Create input accessor (timestamps)
let input_accessor = self.add_accessor(
time_buffer_view,
5126, // FLOAT component type
timestamps.len(),
"SCALAR".to_string(),
None,
None,
None
);
// Create translation output accessor
let translations_data: Vec<u8> = translations.iter().flat_map(|t| t.iter().flat_map(|&v| v.to_le_bytes())).collect();
// Add buffer data and create buffer view
let (trans_offset, trans_length) = self.add_buffer_data(&translations_data);
let trans_buffer_view = self.add_buffer_view(
trans_offset,
trans_length,
None
);
// Create output accessor (translations)
let output_accessor = self.add_accessor(
trans_buffer_view,
5126, // FLOAT component type
translations.len(),
"VEC3".to_string(),
None,
None,
None
);
// Create sampler and channel
let sampler_index = self.add_animation_sampler(
animation_index,
input_accessor,
output_accessor,
interpolation,
);
let channel_index = self.add_animation_channel(
animation_index,
sampler_index,
node_index,
AnimationPath::Translation,
);
(channel_index, sampler_index)
}
/// Create rotation keyframes for an animation
///
/// # Arguments
///
/// * `animation_index` - The index of the animation
/// * `node_index` - The index of the target node
/// * `timestamps` - Vector of keyframe timestamps (in seconds)
/// * `rotations` - Vector of rotation quaternions ([x, y, z, w] for each keyframe)
/// * `interpolation` - The interpolation method
///
/// # Returns
///
/// The indices of the created channel and sampler
pub fn create_rotation_animation(
&mut self,
animation_index: usize,
node_index: usize,
timestamps: Vec<f32>,
rotations: Vec<[f32; 4]>,
interpolation: InterpolationType,
) -> (usize, usize) {
if timestamps.len() != rotations.len() {
panic!("Timestamps and rotations must have the same length");
}
// Create time input accessor
let timestamps_data: Vec<u8> = timestamps.iter().flat_map(|&t| t.to_le_bytes()).collect();
// Add buffer data and create buffer view
let (time_offset, time_length) = self.add_buffer_data(×tamps_data);
let time_buffer_view = self.add_buffer_view(
time_offset,
time_length,
None
);
// Create input accessor (timestamps)
let input_accessor = self.add_accessor(
time_buffer_view,
5126, // FLOAT component type
timestamps.len(),
"SCALAR".to_string(),
None,
None,
None
);
// Create rotation output accessor
let rotations_data: Vec<u8> = rotations.iter().flat_map(|q| q.iter().flat_map(|&v| v.to_le_bytes())).collect();
// Add buffer data and create buffer view
let (rot_offset, rot_length) = self.add_buffer_data(&rotations_data);
let rot_buffer_view = self.add_buffer_view(
rot_offset,
rot_length,
None
);
// Create output accessor (rotations)
let output_accessor = self.add_accessor(
rot_buffer_view,
5126, // FLOAT component type
rotations.len(),
"VEC4".to_string(),
None,
None,
None
);
// Create sampler and channel
let sampler_index = self.add_animation_sampler(
animation_index,
input_accessor,
output_accessor,
interpolation,
);
let channel_index = self.add_animation_channel(
animation_index,
sampler_index,
node_index,
AnimationPath::Rotation,
);
(channel_index, sampler_index)
}
/// Create scale keyframes for an animation
///
/// # Arguments
///
/// * `animation_index` - The index of the animation
/// * `node_index` - The index of the target node
/// * `timestamps` - Vector of keyframe timestamps (in seconds)
/// * `scales` - Vector of scale values ([x, y, z] for each keyframe)
/// * `interpolation` - The interpolation method
///
/// # Returns
///
/// The indices of the created channel and sampler
pub fn create_scale_animation(
&mut self,
animation_index: usize,
node_index: usize,
timestamps: Vec<f32>,
scales: Vec<[f32; 3]>,
interpolation: InterpolationType,
) -> (usize, usize) {
if timestamps.len() != scales.len() {
panic!("Timestamps and scales must have the same length");
}
// Create time input accessor
let timestamps_data: Vec<u8> = timestamps.iter().flat_map(|&t| t.to_le_bytes()).collect();
// Add buffer data and create buffer view
let (time_offset, time_length) = self.add_buffer_data(×tamps_data);
let time_buffer_view = self.add_buffer_view(
time_offset,
time_length,
None
);
// Create input accessor (timestamps)
let input_accessor = self.add_accessor(
time_buffer_view,
5126, // FLOAT component type
timestamps.len(),
"SCALAR".to_string(),
None,
None,
None
);
// Create scale output accessor
let scales_data: Vec<u8> = scales.iter().flat_map(|s| s.iter().flat_map(|&v| v.to_le_bytes())).collect();
// Add buffer data and create buffer view
let (scale_offset, scale_length) = self.add_buffer_data(&scales_data);
let scale_buffer_view = self.add_buffer_view(
scale_offset,
scale_length,
None
);
// Create output accessor (scales)
let output_accessor = self.add_accessor(
scale_buffer_view,
5126, // FLOAT component type
scales.len(),
"VEC3".to_string(),
None,
None,
None
);
// Create sampler and channel
let sampler_index = self.add_animation_sampler(
animation_index,
input_accessor,
output_accessor,
interpolation,
);
let channel_index = self.add_animation_channel(
animation_index,
sampler_index,
node_index,
AnimationPath::Scale,
);
(channel_index, sampler_index)
}
}