forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.js
More file actions
195 lines (163 loc) · 6.26 KB
/
model.js
File metadata and controls
195 lines (163 loc) · 6.26 KB
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
import { RENDERSTYLE_WIREFRAME } from './constants.js';
import { MeshInstance } from './mesh-instance.js';
import { MorphInstance } from './morph-instance.js';
import { SkinInstance } from './skin-instance.js';
/**
* @class
* @name Model
* @classdesc A model is a graphical object that can be added to or removed from a scene.
* It contains a hierarchy and any number of mesh instances.
* @description Creates a new model.
* @example
* // Create a new model
* var model = new pc.Model();
* @property {GraphNode} graph The root node of the model's graph node hierarchy.
* @property {MeshInstance[]} meshInstances An array of MeshInstances contained in this model.
* @property {SkinInstance[]} skinInstances An array of SkinInstances contained in this model.
* @property {MorphInstance[]} morphInstances An array of MorphInstances contained in this model.
*/
class Model {
constructor() {
this.graph = null;
this.meshInstances = [];
this.skinInstances = [];
this.morphInstances = [];
this.cameras = [];
this.lights = [];
this._shadersVersion = 0;
// used by the model component to flag that this model has been assigned
this._immutable = false;
}
getGraph() {
return this.graph;
}
setGraph(graph) {
this.graph = graph;
}
getCameras() {
return this.cameras;
}
setCameras(cameras) {
this.cameras = cameras;
}
getLights() {
return this.lights;
}
setLights(lights) {
this.lights = lights;
}
getMaterials() {
var i;
var materials = [];
for (i = 0; i < this.meshInstances.length; i++) {
var meshInstance = this.meshInstances[i];
if (materials.indexOf(meshInstance.material) === -1) {
materials.push(meshInstance.material);
}
}
return materials;
}
/**
* @function
* @name Model#clone
* @description Clones a model. The returned model has a newly created hierarchy
* and mesh instances, but meshes are shared between the clone and the specified
* model.
* @returns {Model} A clone of the specified model.
* @example
* var clonedModel = model.clone();
*/
clone() {
var i, j;
// Duplicate the node hierarchy
var srcNodes = [];
var cloneNodes = [];
var _duplicate = function (node) {
var newNode = node.clone();
srcNodes.push(node);
cloneNodes.push(newNode);
for (var idx = 0; idx < node._children.length; idx++) {
newNode.addChild(_duplicate(node._children[idx]));
}
return newNode;
};
var cloneGraph = _duplicate(this.graph);
var cloneMeshInstances = [];
var cloneSkinInstances = [];
var cloneMorphInstances = [];
// Clone the skin instances
for (i = 0; i < this.skinInstances.length; i++) {
var skin = this.skinInstances[i].skin;
var cloneSkinInstance = new SkinInstance(skin);
// Resolve bone IDs to actual graph nodes
var bones = [];
for (j = 0; j < skin.boneNames.length; j++) {
var boneName = skin.boneNames[j];
var bone = cloneGraph.findByName(boneName);
bones.push(bone);
}
cloneSkinInstance.bones = bones;
cloneSkinInstances.push(cloneSkinInstance);
}
// Clone the morph instances
for (i = 0; i < this.morphInstances.length; i++) {
var morph = this.morphInstances[i].morph;
var cloneMorphInstance = new MorphInstance(morph);
cloneMorphInstances.push(cloneMorphInstance);
}
// Clone the mesh instances
for (i = 0; i < this.meshInstances.length; i++) {
var meshInstance = this.meshInstances[i];
var nodeIndex = srcNodes.indexOf(meshInstance.node);
var cloneMeshInstance = new MeshInstance(meshInstance.mesh, meshInstance.material, cloneNodes[nodeIndex]);
if (meshInstance.skinInstance) {
var skinInstanceIndex = this.skinInstances.indexOf(meshInstance.skinInstance);
cloneMeshInstance.skinInstance = cloneSkinInstances[skinInstanceIndex];
}
if (meshInstance.morphInstance) {
var morphInstanceIndex = this.morphInstances.indexOf(meshInstance.morphInstance);
cloneMeshInstance.morphInstance = cloneMorphInstances[morphInstanceIndex];
}
cloneMeshInstances.push(cloneMeshInstance);
}
var clone = new Model();
clone.graph = cloneGraph;
clone.meshInstances = cloneMeshInstances;
clone.skinInstances = cloneSkinInstances;
clone.morphInstances = cloneMorphInstances;
clone.getGraph().syncHierarchy();
return clone;
}
/**
* @function
* @name Model#destroy
* @description Destroys skinning texture and possibly deletes vertex/index buffers of a model.
* Mesh is reference-counted, so buffers are only deleted if all models with referencing mesh instances were deleted.
* That means all in-scene models + the "base" one (asset.resource) which is created when the model is parsed.
* It is recommended to use asset.unload() instead, which will also remove the model from the scene.
*/
destroy() {
var meshInstances = this.meshInstances;
for (var i = 0; i < meshInstances.length; i++) {
meshInstances[i].destroy();
}
this.meshInstances.length = 0;
}
/**
* @function
* @name Model#generateWireframe
* @description Generates the necessary internal data for a model to be
* renderable as wireframe. Once this function has been called, any mesh
* instance in the model can have its renderStyle property set to
* {@link RENDERSTYLE_WIREFRAME}.
* @example
* model.generateWireframe();
* for (var i = 0; i < model.meshInstances.length; i++) {
* model.meshInstances[i].renderStyle = pc.RENDERSTYLE_WIREFRAME;
* }
*/
generateWireframe() {
MeshInstance._prepareRenderStyleForArray(this.meshInstances, RENDERSTYLE_WIREFRAME);
}
}
export { Model };