-
Notifications
You must be signed in to change notification settings - Fork 46
/
model.ts
61 lines (55 loc) · 1.76 KB
/
model.ts
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
import { glTFParser } from "./gltf/gltf-parser"
import { glTFAsset } from "./gltf/gltf-asset"
import { MaterialFactory } from "./material/material-factory"
import { Mesh3D } from "./mesh/mesh"
import { Animation } from "./animation"
import { Container3D } from "./container"
import { InstancedModel } from "./instanced-model"
import { AABB } from "./math/aabb"
/**
* Represents a model which has been loaded from a file. Contains a hierarchy of meshes and animations.
*/
export class Model extends Container3D {
/** The animations included in the model. */
animations: Animation[] = []
/**
* The meshes included in the model. Note that this array and the actual
* childen are not automatically synchronized after the model has been loaded.
*/
meshes: Mesh3D[] = []
/**
* Creates a new model from a source.
* @param source The source to create the model from.
* @param materialFactory The factory to use for creating materials.
*/
static from(source: glTFAsset, materialFactory?: MaterialFactory) {
return glTFParser.createModel(source, materialFactory)
}
/**
* Creates a new instance of this model.
*/
createInstance() {
return new InstancedModel(this)
}
/**
* Calculates and returns a axis-aligned bounding box of the model in world
* space. The bounding box will encapsulate the meshes included in the model.
*/
getBoundingBox() {
this.updateTransform()
let aabb = new AABB()
let mesh = this.meshes[0].getBoundingBox()
if (mesh) {
aabb.min = mesh.min
aabb.max = mesh.max
}
for (let i = 1; i < this.meshes.length; i++) {
let mesh = this.meshes[i].getBoundingBox()
if (mesh) {
aabb.encapsulate(mesh.min)
aabb.encapsulate(mesh.max)
}
}
return aabb
}
}