-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
ModelVisualizer.js
329 lines (291 loc) · 12.8 KB
/
ModelVisualizer.js
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
/*global define*/
define([
'../Core/AssociativeArray',
'../Core/BoundingSphere',
'../Core/Color',
'../Core/defined',
'../Core/destroyObject',
'../Core/DeveloperError',
'../Core/Matrix4',
'../Scene/ColorBlendMode',
'../Scene/HeightReference',
'../Scene/Model',
'../Scene/ModelAnimationLoop',
'../Scene/ShadowMode',
'./BoundingSphereState',
'./Property'
], function(
AssociativeArray,
BoundingSphere,
Color,
defined,
destroyObject,
DeveloperError,
Matrix4,
ColorBlendMode,
HeightReference,
Model,
ModelAnimationLoop,
ShadowMode,
BoundingSphereState,
Property) {
'use strict';
var defaultScale = 1.0;
var defaultMinimumPixelSize = 0.0;
var defaultIncrementallyLoadTextures = true;
var defaultShadows = ShadowMode.ENABLED;
var defaultHeightReference = HeightReference.NONE;
var defaultSilhouetteColor = Color.RED;
var defaultSilhouetteSize = 0.0;
var defaultColor = Color.WHITE;
var defaultColorBlendMode = ColorBlendMode.HIGHLIGHT;
var defaultColorBlendAmount = 0.5;
var color = new Color();
var modelMatrixScratch = new Matrix4();
var nodeMatrixScratch = new Matrix4();
/**
* A {@link Visualizer} which maps {@link Entity#model} to a {@link Model}.
* @alias ModelVisualizer
* @constructor
*
* @param {Scene} scene The scene the primitives will be rendered in.
* @param {EntityCollection} entityCollection The entityCollection to visualize.
*/
function ModelVisualizer(scene, entityCollection) {
//>>includeStart('debug', pragmas.debug);
if (!defined(scene)) {
throw new DeveloperError('scene is required.');
}
if (!defined(entityCollection)) {
throw new DeveloperError('entityCollection is required.');
}
//>>includeEnd('debug');
entityCollection.collectionChanged.addEventListener(ModelVisualizer.prototype._onCollectionChanged, this);
this._scene = scene;
this._primitives = scene.primitives;
this._entityCollection = entityCollection;
this._modelHash = {};
this._entitiesToVisualize = new AssociativeArray();
this._onCollectionChanged(entityCollection, entityCollection.values, [], []);
}
/**
* Updates models created this visualizer to match their
* Entity counterpart at the given time.
*
* @param {JulianDate} time The time to update to.
* @returns {Boolean} This function always returns true.
*/
ModelVisualizer.prototype.update = function(time) {
//>>includeStart('debug', pragmas.debug);
if (!defined(time)) {
throw new DeveloperError('time is required.');
}
//>>includeEnd('debug');
var entities = this._entitiesToVisualize.values;
var modelHash = this._modelHash;
var primitives = this._primitives;
for (var i = 0, len = entities.length; i < len; i++) {
var entity = entities[i];
var modelGraphics = entity._model;
var uri;
var modelData = modelHash[entity.id];
var show = entity.isShowing && entity.isAvailable(time) && Property.getValueOrDefault(modelGraphics._show, time, true);
var modelMatrix;
if (show) {
modelMatrix = entity._getModelMatrix(time, modelMatrixScratch);
uri = Property.getValueOrUndefined(modelGraphics._uri, time);
show = defined(modelMatrix) && defined(uri);
}
if (!show) {
if (defined(modelData)) {
modelData.modelPrimitive.show = false;
}
continue;
}
var model = defined(modelData) ? modelData.modelPrimitive : undefined;
if (!defined(model) || uri !== modelData.uri) {
if (defined(model)) {
primitives.removeAndDestroy(model);
delete modelHash[entity.id];
}
model = Model.fromGltf({
url : uri,
incrementallyLoadTextures : Property.getValueOrDefault(modelGraphics._incrementallyLoadTextures, time, defaultIncrementallyLoadTextures),
scene : this._scene
});
model.readyPromise.otherwise(onModelError);
model.id = entity;
primitives.add(model);
modelData = {
modelPrimitive : model,
uri : uri,
animationsRunning : false,
nodeTransformationsScratch : {},
originalNodeMatrixHash : {}
};
modelHash[entity.id] = modelData;
}
model.show = true;
model.scale = Property.getValueOrDefault(modelGraphics._scale, time, defaultScale);
model.minimumPixelSize = Property.getValueOrDefault(modelGraphics._minimumPixelSize, time, defaultMinimumPixelSize);
model.maximumScale = Property.getValueOrUndefined(modelGraphics._maximumScale, time);
model.modelMatrix = Matrix4.clone(modelMatrix, model.modelMatrix);
model.shadows = Property.getValueOrDefault(modelGraphics._shadows, time, defaultShadows);
model.heightReference = Property.getValueOrDefault(modelGraphics._heightReference, time, defaultHeightReference);
model.distanceDisplayCondition = Property.getValueOrUndefined(modelGraphics._distanceDisplayCondition, time);
model.silhouetteColor = Property.getValueOrDefault(modelGraphics.silhouetteColor, time, defaultSilhouetteColor);
model.silhouetteSize = Property.getValueOrDefault(modelGraphics.silhouetteSize, time, defaultSilhouetteSize);
model.color = Property.getValueOrDefault(modelGraphics._color, time, defaultColor, color);
model.colorBlendMode = Property.getValueOrDefault(modelGraphics._colorBlendMode, time, defaultColorBlendMode);
model.colorBlendAmount = Property.getValueOrDefault(modelGraphics._colorBlendAmount, time, defaultColorBlendAmount);
if (model.ready) {
var runAnimations = Property.getValueOrDefault(modelGraphics._runAnimations, time, true);
if (modelData.animationsRunning !== runAnimations) {
if (runAnimations) {
model.activeAnimations.addAll({
loop : ModelAnimationLoop.REPEAT
});
} else {
model.activeAnimations.removeAll();
}
modelData.animationsRunning = runAnimations;
}
// Apply node transformations
var nodeTransformations = Property.getValueOrUndefined(modelGraphics._nodeTransformations, time, modelData.nodeTransformationsScratch);
if (defined(nodeTransformations)) {
var originalNodeMatrixHash = modelData.originalNodeMatrixHash;
var nodeNames = Object.keys(nodeTransformations);
for (var nodeIndex = 0, nodeLength = nodeNames.length; nodeIndex < nodeLength; ++nodeIndex) {
var nodeName = nodeNames[nodeIndex];
var nodeTransformation = nodeTransformations[nodeName];
if (!defined(nodeTransformation)) {
continue;
}
var modelNode = model.getNode(nodeName);
if (!defined(modelNode)) {
continue;
}
var originalNodeMatrix = originalNodeMatrixHash[nodeName];
if (!defined(originalNodeMatrix)) {
originalNodeMatrix = modelNode.matrix.clone();
originalNodeMatrixHash[nodeName] = originalNodeMatrix;
}
var transformationMatrix = Matrix4.fromTranslationRotationScale(nodeTransformation, nodeMatrixScratch);
modelNode.matrix = Matrix4.multiply(originalNodeMatrix, transformationMatrix, transformationMatrix);
}
}
}
}
return true;
};
/**
* Returns true if this object was destroyed; otherwise, false.
*
* @returns {Boolean} True if this object was destroyed; otherwise, false.
*/
ModelVisualizer.prototype.isDestroyed = function() {
return false;
};
/**
* Removes and destroys all primitives created by this instance.
*/
ModelVisualizer.prototype.destroy = function() {
this._entityCollection.collectionChanged.removeEventListener(ModelVisualizer.prototype._onCollectionChanged, this);
var entities = this._entitiesToVisualize.values;
var modelHash = this._modelHash;
var primitives = this._primitives;
for (var i = entities.length - 1; i > -1; i--) {
removeModel(this, entities[i], modelHash, primitives);
}
return destroyObject(this);
};
/**
* Computes a bounding sphere which encloses the visualization produced for the specified entity.
* The bounding sphere is in the fixed frame of the scene's globe.
*
* @param {Entity} entity The entity whose bounding sphere to compute.
* @param {BoundingSphere} result The bounding sphere onto which to store the result.
* @returns {BoundingSphereState} BoundingSphereState.DONE if the result contains the bounding sphere,
* BoundingSphereState.PENDING if the result is still being computed, or
* BoundingSphereState.FAILED if the entity has no visualization in the current scene.
* @private
*/
ModelVisualizer.prototype.getBoundingSphere = function(entity, result) {
//>>includeStart('debug', pragmas.debug);
if (!defined(entity)) {
throw new DeveloperError('entity is required.');
}
if (!defined(result)) {
throw new DeveloperError('result is required.');
}
//>>includeEnd('debug');
var modelData = this._modelHash[entity.id];
if (!defined(modelData)) {
return BoundingSphereState.FAILED;
}
var model = modelData.modelPrimitive;
if (!defined(model) || !model.show) {
return BoundingSphereState.FAILED;
}
if (!model.ready) {
return BoundingSphereState.PENDING;
}
if (model.heightReference === HeightReference.NONE) {
BoundingSphere.transform(model.boundingSphere, model.modelMatrix, result);
} else {
if (!defined(model._clampedModelMatrix)) {
return BoundingSphereState.PENDING;
}
BoundingSphere.transform(model.boundingSphere, model._clampedModelMatrix, result);
}
return BoundingSphereState.DONE;
};
/**
* @private
*/
ModelVisualizer.prototype._onCollectionChanged = function(entityCollection, added, removed, changed) {
var i;
var entity;
var entities = this._entitiesToVisualize;
var modelHash = this._modelHash;
var primitives = this._primitives;
for (i = added.length - 1; i > -1; i--) {
entity = added[i];
if (defined(entity._model) && defined(entity._position)) {
entities.set(entity.id, entity);
}
}
for (i = changed.length - 1; i > -1; i--) {
entity = changed[i];
if (defined(entity._model) && defined(entity._position)) {
clearNodeTransformationsScratch(entity, modelHash);
entities.set(entity.id, entity);
} else {
removeModel(this, entity, modelHash, primitives);
entities.remove(entity.id);
}
}
for (i = removed.length - 1; i > -1; i--) {
entity = removed[i];
removeModel(this, entity, modelHash, primitives);
entities.remove(entity.id);
}
};
function removeModel(visualizer, entity, modelHash, primitives) {
var modelData = modelHash[entity.id];
if (defined(modelData)) {
primitives.removeAndDestroy(modelData.modelPrimitive);
delete modelHash[entity.id];
}
}
function clearNodeTransformationsScratch(entity, modelHash) {
var modelData = modelHash[entity.id];
if (defined(modelData)) {
modelData.nodeTransformationsScratch = {};
}
}
function onModelError(error) {
console.error(error);
}
return ModelVisualizer;
});