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
277 lines (236 loc) · 9.95 KB
/
Copy pathmodel.js
File metadata and controls
277 lines (236 loc) · 9.95 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
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
Object.assign(pc, function () {
/**
* @constructor
* @name pc.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 {pc.GraphNode} graph The root node of the model's graph node hierarchy.
* @property {pc.MeshInstance[]} meshInstances An array of meshInstances contained in this model.
*/
var Model = function Model() {
this.graph = null;
this.meshInstances = [];
this.skinInstances = [];
this.morphInstances = [];
this.cameras = [];
this.lights = [];
this._shadersVersion = 0;
};
Object.assign(Model.prototype, {
getGraph: function () {
return this.graph;
},
setGraph: function (graph) {
this.graph = graph;
},
getCameras: function () {
return this.cameras;
},
setCameras: function (cameras) {
this.cameras = cameras;
},
getLights: function () {
return this.lights;
},
setLights: function (lights) {
this.lights = lights;
},
getMaterials: function () {
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 pc.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 {pc.Model} A clone of the specified model.
* @example
* var clonedModel = model.clone();
*/
clone: function () {
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 pc.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 pc.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 pc.MeshInstance(cloneNodes[nodeIndex], meshInstance.mesh, meshInstance.material);
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 pc.Model();
clone.graph = cloneGraph;
clone.meshInstances = cloneMeshInstances;
clone.skinInstances = cloneSkinInstances;
clone.morphInstances = cloneMorphInstances;
clone.getGraph().syncHierarchy();
return clone;
},
/**
* @function
* @name pc.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: function () {
var meshInstances = this.meshInstances;
var meshInstance, mesh, skin, morph, ib, boneTex, j;
var device;
for (var i = 0; i < meshInstances.length; i++) {
meshInstance = meshInstances[i];
mesh = meshInstance.mesh;
if (mesh) {
mesh._refCount--;
if (mesh._refCount < 1) {
if (mesh.vertexBuffer) {
device = device || mesh.vertexBuffer.device;
mesh.vertexBuffer.destroy();
mesh.vertexBuffer = null;
}
for (j = 0; j < mesh.indexBuffer.length; j++) {
device = device || mesh.indexBuffer.device;
ib = mesh.indexBuffer[j];
if (!ib) continue;
ib.destroy();
}
mesh.indexBuffer.length = 0;
}
}
skin = meshInstance.skinInstance;
if (skin) {
boneTex = skin.boneTexture;
if (boneTex) {
boneTex.destroy();
}
}
meshInstance.skinInstance = null;
morph = meshInstance.morphInstance;
if (morph) {
morph.destroy();
}
meshInstance.morphInstance = null;
meshInstance.material = null; // make sure instance and material clear references
}
},
/**
* @function
* @name pc.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
* pc.RENDERSTYLE_WIREFRAME
* @example
* model.generateWireframe();
* for (var i = 0; i < model.meshInstances.length; i++) {
* model.meshInstances[i].renderStyle = pc.RENDERSTYLE_WIREFRAME;
* }
*/
generateWireframe: function () {
var i, j, k;
var i1, i2;
var mesh, base, count, indexBuffer, wireBuffer;
var srcIndices, dstIndices;
// Build an array of unique meshes in this model
var meshes = [];
for (i = 0; i < this.meshInstances.length; i++) {
mesh = this.meshInstances[i].mesh;
if (meshes.indexOf(mesh) === -1) {
meshes.push(mesh);
}
}
var offsets = [[0, 1], [1, 2], [2, 0]];
for (i = 0; i < meshes.length; i++) {
mesh = meshes[i];
base = mesh.primitive[pc.RENDERSTYLE_SOLID].base;
count = mesh.primitive[pc.RENDERSTYLE_SOLID].count;
indexBuffer = mesh.indexBuffer[pc.RENDERSTYLE_SOLID];
srcIndices = new Uint16Array(indexBuffer.lock());
var uniqueLineIndices = {};
var lines = [];
for (j = base; j < base + count; j += 3) {
for (k = 0; k < 3; k++) {
i1 = srcIndices[j + offsets[k][0]];
i2 = srcIndices[j + offsets[k][1]];
var line = (i1 > i2) ? ((i2 << 16) | i1) : ((i1 << 16) | i2);
if (uniqueLineIndices[line] === undefined) {
uniqueLineIndices[line] = 0;
lines.push(i1, i2);
}
}
}
indexBuffer.unlock();
wireBuffer = new pc.IndexBuffer(indexBuffer.device, pc.INDEXFORMAT_UINT16, lines.length);
dstIndices = new Uint16Array(wireBuffer.lock());
dstIndices.set(lines);
wireBuffer.unlock();
mesh.primitive[pc.RENDERSTYLE_WIREFRAME] = {
type: pc.PRIMITIVE_LINES,
base: 0,
count: lines.length,
indexed: true
};
mesh.indexBuffer[pc.RENDERSTYLE_WIREFRAME] = wireBuffer;
}
}
});
return {
Model: Model
};
}());