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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
//! Utilites for creating reusable templates for scene objects.
//!
//! It is often the case that you will want to have multiple instances of the same model or
//! hierarchy of objects in your scene. While you could manually construct each instance yourself,
//! three-rs provides a templating system to allow you to describe your model's hierarchy ahead
//! of time, and then quickly create instances that three can efficiently batch render.
//! [`Template`] describes the objects for a single model, and can be instantiated with
//! [`Factory::instantiate_template`].
//!
//! The easiest way to create a template is to load one from a glTF file using
//! [`Factory::load_gltf`].
//!
//! # Object Relations
//!
//! Often, one object needs to reference another object in the template, e.g. a bone needs
//! to specify which skeleton it belongs to, and any object can specify that it belongs to
//! a group in the template. When doing so, objects reference each other by their index in
//! their respective arrays in [`Template`]. When such indices are used, the documentation
//! will specify which array the index refers to.
//!
//! # Object Templates
//!
//! The [`objects`] field of [`Template`] provides a flattened, type-erased list of all objects
//! defined in the template. Each type of object provides its type-specific data in that type's
//! array, and then specifies the index of an [`ObjectTemplate`] in [`objects`]. Every object
//! in the template must be represented in [`objects`] exactly once.
//!
//! The full, flattened list of objects is primarily used by [`AnimationTemplate`] to allow
//! tracks in the animation to reference the object targeted by the track regardless of the
//! target object's concrete type.
//!
//! # Animations
//!
//! Templates can also describe animations that apply to the objects in a template.
//! When instantiated, the resulting animation clips will be unique to that instance of of the
//! template. This allows for instances of the template to be animated independently of each
//! other, without requiring you to manually setup animations for each instance.
//!
//! An animation in a template can target any of the objects described in the template. It does
//! this by specifying the index of the objects in [`objects`]. See
//! [`AnimationTemplate::tracks`] for more information.
//!
//! # Mesh Instancing
//!
//! When setting up a mesh in a template, you must first upload your [`Geometry`] to the GPU
//! using [`Factory::upload_geometry`]. This will give you an [`InstancedGeometry`] object
//! that acts as a shared handle to the GPU resources for that geometry. By uploading the
//! data to the GPU ahead of time, we can ensure that all mesh nodes that reference that
//! geometry, and all [`Mesh`] instances created from the template, will share a single copy
//! of the data on the GPU. This reduces GPU resource usage and, for any meshes that also share
//! a material, allows three to render many objects at once.
//!
//! [`Factory::instantiate_template`]: ../struct.Factory.html#method.instantiate_template
//! [`Factory::load_gltf`]: ../struct.Factory.html#method.load_gltf
//! [`Factory::upload_geometry`]: ../struct.Factory.html#method.upload_geometry
//! [`Object`]: ../trait.Object.html
//! [`Group`]: ../struct.Group.html
//! [`Geometry`]: ../struct.Geometry.html
//! [`Mesh`]: ../struct.Mesh.html
//! [`Template`]: ./struct.Template.html
//! [`ObjectTemplate`]: ./struct.ObjectTemplate.html
//! [`AnimationTemplate`]: ./struct.AnimationTemplate.html
//! [`AnimationTemplate::tracks`]: ./struct.AnimationTemplate.html#structfield.tracks
//! [`nodes`]: ./struct.Template.html#structfield.nodes
//! [`cameras`]: ./struct.Template.html#structfield.cameras
//! [`meshes`]: ./struct.Template.html#structfield.meshes
//! [`roots`]: ./struct.Template.html#structfield.roots
//! [`objects`]: ./struct.Template.html#structfield.objects
//! [`InstancedGeometry`]: ./struct.InstancedGeometry.html
use Track;
use Projection;
use Color;
use Material;
use Transform;
use GpuData;
use InverseBindMatrix;
/// A template representing a hierarchy of objects.
///
/// To create an instance of the template that can be added to your scene, use
/// [`Factory::instantiate_template`]. For more information about the templating system and how
/// to use it, see the [module documentation].
///
/// [`Factory::instantiate_template`]: ../struct.Factory.html#method.instantiate_template
/// [module documentation]: ./index.html
/// Common data used by all object types.
///
/// All objects (i.e. three-rs types that implement the [`Object`] trait) have common data
/// that the user can set at runtime. `ObjectTemplate` encapsultes these fields, and the
/// various template types have a way to reference an `ObjectTemplate` to specify the object
/// data for that template.
///
/// See the [module documentation] for more information on how object data is defined in
/// templates.
///
/// [`Object`]: ../trait.Object.html
/// [module documentation]: ./index.html#object-templates
/// Information for instantiating a [`Mesh`].
///
/// See the [module documentation] for more information on mesh instancing and how mesh
/// data is setup for templates.
///
/// [`Mesh`]: ../struct.Mesh.html
/// [module documentation]: ./index.html#mesh-instancing
/// A template for a [`Camera`] object.
///
/// [`Camera`]: ../struct.Camera.html
/// A template for a [`Bone`] object.
///
/// For more information about creating a [`Bone`], see [`Factory::bone`].
///
/// [`Bone`]: ../skeleton/struct.Bone.html
/// [`Factory::bone`]: ../struct.Factory.html#method.bone
/// The definition for an animation targeting objects in a [`Template`].
///
/// See the [module documentation] for more information on template animations and how they
/// are used.
///
/// [`Template`]: ./struct.Template.html
/// [module documentation]: ./index.html#animations
/// Common information for instantiating the various types of lights.
///
/// See the [module documentation] for information on how templates are setup and how objects
/// are added to the template.
///
/// [module documentation]: ./index.html
/// Template information about the different sub-types for light.
///
/// See [`LightTemplate`] for more more information on settings up light templates, and
/// utilities for doing so.
///
/// [`LightTemplate`]: ./struct.LightTemplate.html
/// Geometry data that has been loaded to the GPU.
///
/// [`Mesh`] objects instantiated with this data will share GPU resources, allowing for more
/// efficient instanced rendering. Use [`Factory::upload_geometry`] to upload [`Geometry`]
/// to the GPU and get an `InstancedGeometry`. You can use an `InstancedGeometry` to create
/// a [`MeshTemplate`] for use in a [`Template`], or you can use [`Factory::create_instanced_mesh`]
/// to create a [`Mesh`] directly.
///
/// [`Factory::upload_geometry`]: ../struct.Factory.html#method.upload_geometry
/// [`Factory::create_instanced_mesh`]: ../struct.Factory.html#method.create_instanced_mesh
/// [`Mesh`]: ../struct.Mesh.html
/// [`Geometry`]: ../struct.Geometry.html
/// [`Template`]: ./struct.Template.html
/// [`MeshTemplate`]: ./struct.MeshTemplate.html