-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathEllipsoidGeometryUpdater.js
452 lines (395 loc) · 23.1 KB
/
EllipsoidGeometryUpdater.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
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
import Cartesian3 from '../Core/Cartesian3.js';
import Check from '../Core/Check.js';
import Color from '../Core/Color.js';
import ColorGeometryInstanceAttribute from '../Core/ColorGeometryInstanceAttribute.js';
import defaultValue from '../Core/defaultValue.js';
import defined from '../Core/defined.js';
import defineProperties from '../Core/defineProperties.js';
import DistanceDisplayCondition from '../Core/DistanceDisplayCondition.js';
import DistanceDisplayConditionGeometryInstanceAttribute from '../Core/DistanceDisplayConditionGeometryInstanceAttribute.js';
import EllipsoidGeometry from '../Core/EllipsoidGeometry.js';
import EllipsoidOutlineGeometry from '../Core/EllipsoidOutlineGeometry.js';
import GeometryInstance from '../Core/GeometryInstance.js';
import GeometryOffsetAttribute from '../Core/GeometryOffsetAttribute.js';
import Iso8601 from '../Core/Iso8601.js';
import Matrix4 from '../Core/Matrix4.js';
import OffsetGeometryInstanceAttribute from '../Core/OffsetGeometryInstanceAttribute.js';
import ShowGeometryInstanceAttribute from '../Core/ShowGeometryInstanceAttribute.js';
import HeightReference from '../Scene/HeightReference.js';
import MaterialAppearance from '../Scene/MaterialAppearance.js';
import PerInstanceColorAppearance from '../Scene/PerInstanceColorAppearance.js';
import Primitive from '../Scene/Primitive.js';
import SceneMode from '../Scene/SceneMode.js';
import ColorMaterialProperty from './ColorMaterialProperty.js';
import DynamicGeometryUpdater from './DynamicGeometryUpdater.js';
import GeometryUpdater from './GeometryUpdater.js';
import heightReferenceOnEntityPropertyChanged from './heightReferenceOnEntityPropertyChanged.js';
import MaterialProperty from './MaterialProperty.js';
import Property from './Property.js';
var defaultMaterial = new ColorMaterialProperty(Color.WHITE);
var defaultOffset = Cartesian3.ZERO;
var offsetScratch = new Cartesian3();
var radiiScratch = new Cartesian3();
var innerRadiiScratch = new Cartesian3();
var scratchColor = new Color();
var unitSphere = new Cartesian3(1, 1, 1);
function EllipsoidGeometryOptions(entity) {
this.id = entity;
this.vertexFormat = undefined;
this.radii = undefined;
this.innerRadii = undefined;
this.minimumClock = undefined;
this.maximumClock = undefined;
this.minimumCone = undefined;
this.maximumCone = undefined;
this.stackPartitions = undefined;
this.slicePartitions = undefined;
this.subdivisions = undefined;
this.offsetAttribute = undefined;
}
/**
* A {@link GeometryUpdater} for ellipsoids.
* Clients do not normally create this class directly, but instead rely on {@link DataSourceDisplay}.
* @alias EllipsoidGeometryUpdater
* @constructor
*
* @param {Entity} entity The entity containing the geometry to be visualized.
* @param {Scene} scene The scene where visualization is taking place.
*/
function EllipsoidGeometryUpdater(entity, scene) {
GeometryUpdater.call(this, {
entity : entity,
scene : scene,
geometryOptions : new EllipsoidGeometryOptions(entity),
geometryPropertyName : 'ellipsoid',
observedPropertyNames : ['availability', 'position', 'orientation', 'ellipsoid']
});
this._onEntityPropertyChanged(entity, 'ellipsoid', entity.ellipsoid, undefined);
}
if (defined(Object.create)) {
EllipsoidGeometryUpdater.prototype = Object.create(GeometryUpdater.prototype);
EllipsoidGeometryUpdater.prototype.constructor = EllipsoidGeometryUpdater;
}
defineProperties(EllipsoidGeometryUpdater.prototype, {
/**
* Gets the terrain offset property
* @type {TerrainOffsetProperty}
* @memberof EllipsoidGeometryUpdater.prototype
* @readonly
*/
terrainOffsetProperty: {
get: function() {
return this._terrainOffsetProperty;
}
}
});
/**
* Creates the geometry instance which represents the fill of the geometry.
*
* @param {JulianDate} time The time to use when retrieving initial attribute values.
* @param {Boolean} [skipModelMatrix=false] Whether to compute a model matrix for the geometry instance
* @param {Matrix4} [modelMatrixResult] Used to store the result of the model matrix calculation
* @returns {GeometryInstance} The geometry instance representing the filled portion of the geometry.
*
* @exception {DeveloperError} This instance does not represent a filled geometry.
*/
EllipsoidGeometryUpdater.prototype.createFillGeometryInstance = function(time, skipModelMatrix, modelMatrixResult) {
//>>includeStart('debug', pragmas.debug);
Check.defined('time', time);
//>>includeEnd('debug');
var entity = this._entity;
var isAvailable = entity.isAvailable(time);
var color;
var show = new ShowGeometryInstanceAttribute(isAvailable && entity.isShowing && this._showProperty.getValue(time) && this._fillProperty.getValue(time));
var distanceDisplayCondition = this._distanceDisplayConditionProperty.getValue(time);
var distanceDisplayConditionAttribute = DistanceDisplayConditionGeometryInstanceAttribute.fromDistanceDisplayCondition(distanceDisplayCondition);
var attributes = {
show : show,
distanceDisplayCondition : distanceDisplayConditionAttribute,
color : undefined,
offset: undefined
};
if (this._materialProperty instanceof ColorMaterialProperty) {
var currentColor;
if (defined(this._materialProperty.color) && (this._materialProperty.color.isConstant || isAvailable)) {
currentColor = this._materialProperty.color.getValue(time, scratchColor);
}
if (!defined(currentColor)) {
currentColor = Color.WHITE;
}
color = ColorGeometryInstanceAttribute.fromColor(currentColor);
attributes.color = color;
}
if (defined(this._options.offsetAttribute)) {
attributes.offset = OffsetGeometryInstanceAttribute.fromCartesian3(Property.getValueOrDefault(this._terrainOffsetProperty, time, defaultOffset, offsetScratch));
}
return new GeometryInstance({
id : entity,
geometry : new EllipsoidGeometry(this._options),
modelMatrix : skipModelMatrix ? undefined : entity.computeModelMatrixForHeightReference(time, entity.ellipsoid.heightReference, this._options.radii.z * 0.5, this._scene.mapProjection.ellipsoid, modelMatrixResult),
attributes : attributes
});
};
/**
* Creates the geometry instance which represents the outline of the geometry.
*
* @param {JulianDate} time The time to use when retrieving initial attribute values.
* @param {Boolean} [skipModelMatrix=false] Whether to compute a model matrix for the geometry instance
* @param {Matrix4} [modelMatrixResult] Used to store the result of the model matrix calculation
* @returns {GeometryInstance} The geometry instance representing the outline portion of the geometry.
*
* @exception {DeveloperError} This instance does not represent an outlined geometry.
*/
EllipsoidGeometryUpdater.prototype.createOutlineGeometryInstance = function(time, skipModelMatrix, modelMatrixResult) {
//>>includeStart('debug', pragmas.debug);
Check.defined('time', time);
//>>includeEnd('debug');
var entity = this._entity;
var isAvailable = entity.isAvailable(time);
var outlineColor = Property.getValueOrDefault(this._outlineColorProperty, time, Color.BLACK, scratchColor);
var distanceDisplayCondition = this._distanceDisplayConditionProperty.getValue(time);
var attributes = {
show : new ShowGeometryInstanceAttribute(isAvailable && entity.isShowing && this._showProperty.getValue(time) && this._showOutlineProperty.getValue(time)),
color : ColorGeometryInstanceAttribute.fromColor(outlineColor),
distanceDisplayCondition : DistanceDisplayConditionGeometryInstanceAttribute.fromDistanceDisplayCondition(distanceDisplayCondition),
offset : undefined
};
if (defined(this._options.offsetAttribute)) {
attributes.offset = OffsetGeometryInstanceAttribute.fromCartesian3(Property.getValueOrDefault(this._terrainOffsetProperty, time, defaultOffset, offsetScratch));
}
return new GeometryInstance({
id : entity,
geometry : new EllipsoidOutlineGeometry(this._options),
modelMatrix : skipModelMatrix ? undefined : entity.computeModelMatrixForHeightReference(time, entity.ellipsoid.heightReference, this._options.radii.z * 0.5, this._scene.mapProjection.ellipsoid, modelMatrixResult),
attributes : attributes
});
};
EllipsoidGeometryUpdater.prototype._computeCenter = function(time, result) {
return Property.getValueOrUndefined(this._entity.position, time, result);
};
EllipsoidGeometryUpdater.prototype._isHidden = function(entity, ellipsoid) {
return !defined(entity.position) || !defined(ellipsoid.radii) || GeometryUpdater.prototype._isHidden.call(this, entity, ellipsoid);
};
EllipsoidGeometryUpdater.prototype._isDynamic = function(entity, ellipsoid) {
return !entity.position.isConstant || //
!Property.isConstant(entity.orientation) || //
!ellipsoid.radii.isConstant || //
!Property.isConstant(ellipsoid.innerRadii) || //
!Property.isConstant(ellipsoid.stackPartitions) || //
!Property.isConstant(ellipsoid.slicePartitions) || //
!Property.isConstant(ellipsoid.outlineWidth) || //
!Property.isConstant(ellipsoid.minimumClock) || //
!Property.isConstant(ellipsoid.maximumClock) || //
!Property.isConstant(ellipsoid.minimumCone) || //
!Property.isConstant(ellipsoid.maximumCone) || //
!Property.isConstant(ellipsoid.subdivisions);
};
EllipsoidGeometryUpdater.prototype._setStaticOptions = function(entity, ellipsoid) {
var heightReference = Property.getValueOrDefault(ellipsoid.heightReference, Iso8601.MINIMUM_VALUE, HeightReference.NONE);
var options = this._options;
options.vertexFormat = this._materialProperty instanceof ColorMaterialProperty ? PerInstanceColorAppearance.VERTEX_FORMAT : MaterialAppearance.MaterialSupport.TEXTURED.vertexFormat;
options.radii = ellipsoid.radii.getValue(Iso8601.MINIMUM_VALUE, options.radii);
options.innerRadii = Property.getValueOrUndefined(ellipsoid.innerRadii, options.radii);
options.minimumClock = Property.getValueOrUndefined(ellipsoid.minimumClock, Iso8601.MINIMUM_VALUE);
options.maximumClock = Property.getValueOrUndefined(ellipsoid.maximumClock, Iso8601.MINIMUM_VALUE);
options.minimumCone = Property.getValueOrUndefined(ellipsoid.minimumCone, Iso8601.MINIMUM_VALUE);
options.maximumCone = Property.getValueOrUndefined(ellipsoid.maximumCone, Iso8601.MINIMUM_VALUE);
options.stackPartitions = Property.getValueOrUndefined(ellipsoid.stackPartitions, Iso8601.MINIMUM_VALUE);
options.slicePartitions = Property.getValueOrUndefined(ellipsoid.slicePartitions, Iso8601.MINIMUM_VALUE);
options.subdivisions = Property.getValueOrUndefined(ellipsoid.subdivisions, Iso8601.MINIMUM_VALUE);
options.offsetAttribute = heightReference !== HeightReference.NONE ? GeometryOffsetAttribute.ALL : undefined;
};
EllipsoidGeometryUpdater.prototype._onEntityPropertyChanged = heightReferenceOnEntityPropertyChanged;
EllipsoidGeometryUpdater.DynamicGeometryUpdater = DynamicEllipsoidGeometryUpdater;
/**
* @private
*/
function DynamicEllipsoidGeometryUpdater(geometryUpdater, primitives, groundPrimitives) {
DynamicGeometryUpdater.call(this, geometryUpdater, primitives, groundPrimitives);
this._scene = geometryUpdater._scene;
this._modelMatrix = new Matrix4();
this._attributes = undefined;
this._outlineAttributes = undefined;
this._lastSceneMode = undefined;
this._lastShow = undefined;
this._lastOutlineShow = undefined;
this._lastOutlineWidth = undefined;
this._lastOutlineColor = undefined;
this._lastOffset = new Cartesian3();
this._material = {};
}
if (defined(Object.create)) {
DynamicEllipsoidGeometryUpdater.prototype = Object.create(DynamicGeometryUpdater.prototype);
DynamicEllipsoidGeometryUpdater.prototype.constructor = DynamicEllipsoidGeometryUpdater;
}
DynamicEllipsoidGeometryUpdater.prototype.update = function(time) {
//>>includeStart('debug', pragmas.debug);
Check.defined('time', time);
//>>includeEnd('debug');
var entity = this._entity;
var ellipsoid = entity.ellipsoid;
if (!entity.isShowing || !entity.isAvailable(time) || !Property.getValueOrDefault(ellipsoid.show, time, true)) {
if (defined(this._primitive)) {
this._primitive.show = false;
}
if (defined(this._outlinePrimitive)) {
this._outlinePrimitive.show = false;
}
return;
}
var radii = Property.getValueOrUndefined(ellipsoid.radii, time, radiiScratch);
var modelMatrix = defined(radii) ? entity.computeModelMatrixForHeightReference(time, ellipsoid.heightReference, radii.z * 0.5, this._scene.mapProjection.ellipsoid, this._modelMatrix) : undefined;
if (!defined(modelMatrix) || !defined(radii)) {
if (defined(this._primitive)) {
this._primitive.show = false;
}
if (defined(this._outlinePrimitive)) {
this._outlinePrimitive.show = false;
}
return;
}
//Compute attributes and material.
var showFill = Property.getValueOrDefault(ellipsoid.fill, time, true);
var showOutline = Property.getValueOrDefault(ellipsoid.outline, time, false);
var outlineColor = Property.getValueOrClonedDefault(ellipsoid.outlineColor, time, Color.BLACK, scratchColor);
var material = MaterialProperty.getValue(time, defaultValue(ellipsoid.material, defaultMaterial), this._material);
// Check properties that could trigger a primitive rebuild.
var innerRadii = Property.getValueOrUndefined(ellipsoid.innerRadii, time, innerRadiiScratch);
var minimumClock = Property.getValueOrUndefined(ellipsoid.minimumClock, time);
var maximumClock = Property.getValueOrUndefined(ellipsoid.maximumClock, time);
var minimumCone = Property.getValueOrUndefined(ellipsoid.minimumCone, time);
var maximumCone = Property.getValueOrUndefined(ellipsoid.maximumCone, time);
var stackPartitions = Property.getValueOrUndefined(ellipsoid.stackPartitions, time);
var slicePartitions = Property.getValueOrUndefined(ellipsoid.slicePartitions, time);
var subdivisions = Property.getValueOrUndefined(ellipsoid.subdivisions, time);
var outlineWidth = Property.getValueOrDefault(ellipsoid.outlineWidth, time, 1.0);
var heightReference = Property.getValueOrDefault(ellipsoid.heightReference, time, HeightReference.NONE);
var offsetAttribute = heightReference !== HeightReference.NONE ? GeometryOffsetAttribute.ALL : undefined;
//In 3D we use a fast path by modifying Primitive.modelMatrix instead of regenerating the primitive every frame.
//Also check for height reference because this method doesn't work when the height is relative to terrain.
var sceneMode = this._scene.mode;
var in3D = sceneMode === SceneMode.SCENE3D && heightReference === HeightReference.NONE;
var options = this._options;
var shadows = this._geometryUpdater.shadowsProperty.getValue(time);
var distanceDisplayConditionProperty = this._geometryUpdater.distanceDisplayConditionProperty;
var distanceDisplayCondition = distanceDisplayConditionProperty.getValue(time);
var offset = Property.getValueOrDefault(this._geometryUpdater.terrainOffsetProperty, time, defaultOffset, offsetScratch);
//We only rebuild the primitive if something other than the radii has changed
//For the radii, we use unit sphere and then deform it with a scale matrix.
var rebuildPrimitives = !in3D || this._lastSceneMode !== sceneMode || !defined(this._primitive) || //
options.stackPartitions !== stackPartitions || options.slicePartitions !== slicePartitions || //
defined(innerRadii) && !Cartesian3.equals(options.innerRadii !== innerRadii) || options.minimumClock !== minimumClock || //
options.maximumClock !== maximumClock || options.minimumCone !== minimumCone || //
options.maximumCone !== maximumCone || options.subdivisions !== subdivisions || //
this._lastOutlineWidth !== outlineWidth || options.offsetAttribute !== offsetAttribute;
if (rebuildPrimitives) {
var primitives = this._primitives;
primitives.removeAndDestroy(this._primitive);
primitives.removeAndDestroy(this._outlinePrimitive);
this._primitive = undefined;
this._outlinePrimitive = undefined;
this._lastSceneMode = sceneMode;
this._lastOutlineWidth = outlineWidth;
options.stackPartitions = stackPartitions;
options.slicePartitions = slicePartitions;
options.subdivisions = subdivisions;
options.offsetAttribute = offsetAttribute;
options.radii = Cartesian3.clone(in3D ? unitSphere : radii, options.radii);
if (defined(innerRadii)) {
if (in3D) {
var mag = Cartesian3.magnitude(radii);
options.innerRadii = Cartesian3.fromElements(innerRadii.x/mag, innerRadii.y/mag, innerRadii.z/mag, options.innerRadii);
} else {
options.innerRadii = Cartesian3.clone(innerRadii, options.innerRadii);
}
} else {
options.innerRadii = undefined;
}
options.minimumClock = minimumClock;
options.maximumClock = maximumClock;
options.minimumCone = minimumCone;
options.maximumCone = maximumCone;
var appearance = new MaterialAppearance({
material : material,
translucent : material.isTranslucent(),
closed : true
});
options.vertexFormat = appearance.vertexFormat;
var fillInstance = this._geometryUpdater.createFillGeometryInstance(time, in3D, this._modelMatrix);
this._primitive = primitives.add(new Primitive({
geometryInstances : fillInstance,
appearance : appearance,
asynchronous : false,
shadows : shadows
}));
var outlineInstance = this._geometryUpdater.createOutlineGeometryInstance(time, in3D, this._modelMatrix);
this._outlinePrimitive = primitives.add(new Primitive({
geometryInstances : outlineInstance,
appearance : new PerInstanceColorAppearance({
flat : true,
translucent : outlineInstance.attributes.color.value[3] !== 255,
renderState : {
lineWidth : this._geometryUpdater._scene.clampLineWidth(outlineWidth)
}
}),
asynchronous : false,
shadows : shadows
}));
this._lastShow = showFill;
this._lastOutlineShow = showOutline;
this._lastOutlineColor = Color.clone(outlineColor, this._lastOutlineColor);
this._lastDistanceDisplayCondition = distanceDisplayCondition;
this._lastOffset = Cartesian3.clone(offset, this._lastOffset);
} else if (this._primitive.ready) {
//Update attributes only.
var primitive = this._primitive;
var outlinePrimitive = this._outlinePrimitive;
primitive.show = true;
outlinePrimitive.show = true;
primitive.appearance.material = material;
var attributes = this._attributes;
if (!defined(attributes)) {
attributes = primitive.getGeometryInstanceAttributes(entity);
this._attributes = attributes;
}
if (showFill !== this._lastShow) {
attributes.show = ShowGeometryInstanceAttribute.toValue(showFill, attributes.show);
this._lastShow = showFill;
}
var outlineAttributes = this._outlineAttributes;
if (!defined(outlineAttributes)) {
outlineAttributes = outlinePrimitive.getGeometryInstanceAttributes(entity);
this._outlineAttributes = outlineAttributes;
}
if (showOutline !== this._lastOutlineShow) {
outlineAttributes.show = ShowGeometryInstanceAttribute.toValue(showOutline, outlineAttributes.show);
this._lastOutlineShow = showOutline;
}
if (!Color.equals(outlineColor, this._lastOutlineColor)) {
outlineAttributes.color = ColorGeometryInstanceAttribute.toValue(outlineColor, outlineAttributes.color);
Color.clone(outlineColor, this._lastOutlineColor);
}
if (!DistanceDisplayCondition.equals(distanceDisplayCondition, this._lastDistanceDisplayCondition)) {
attributes.distanceDisplayCondition = DistanceDisplayConditionGeometryInstanceAttribute.toValue(distanceDisplayCondition, attributes.distanceDisplayCondition);
outlineAttributes.distanceDisplayCondition = DistanceDisplayConditionGeometryInstanceAttribute.toValue(distanceDisplayCondition, outlineAttributes.distanceDisplayCondition);
DistanceDisplayCondition.clone(distanceDisplayCondition, this._lastDistanceDisplayCondition);
}
if (!Cartesian3.equals(offset, this._lastOffset)) {
attributes.offset = OffsetGeometryInstanceAttribute.toValue(offset, attributes.offset);
outlineAttributes.offset = OffsetGeometryInstanceAttribute.toValue(offset, attributes.offset);
Cartesian3.clone(offset, this._lastOffset);
}
}
if (in3D) {
//Since we are scaling a unit sphere, we can't let any of the values go to zero.
//Instead we clamp them to a small value. To the naked eye, this produces the same results
//that you get passing EllipsoidGeometry a radii with a zero component.
radii.x = Math.max(radii.x, 0.001);
radii.y = Math.max(radii.y, 0.001);
radii.z = Math.max(radii.z, 0.001);
modelMatrix = Matrix4.multiplyByScale(modelMatrix, radii, modelMatrix);
this._primitive.modelMatrix = modelMatrix;
this._outlinePrimitive.modelMatrix = modelMatrix;
}
};
export default EllipsoidGeometryUpdater;