/
EllipsoidPrimitive.js
383 lines (339 loc) · 13.5 KB
/
EllipsoidPrimitive.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
/*global define*/
define([
'../Core/BoxTessellator',
'../Core/Cartesian3',
'../Core/combine',
'../Core/DeveloperError',
'../Core/destroyObject',
'../Core/Matrix4',
'../Core/BoundingSphere',
'../Core/PrimitiveType',
'../Renderer/CullFace',
'../Renderer/BlendingState',
'../Renderer/BufferUsage',
'../Renderer/CommandLists',
'../Renderer/DrawCommand',
'../Renderer/createPickFragmentShaderSource',
'./Material',
'./SceneMode',
'../Shaders/EllipsoidVS',
'../Shaders/EllipsoidFS'
], function(
BoxTessellator,
Cartesian3,
combine,
DeveloperError,
destroyObject,
Matrix4,
BoundingSphere,
PrimitiveType,
CullFace,
BlendingState,
BufferUsage,
CommandLists,
DrawCommand,
createPickFragmentShaderSource,
Material,
SceneMode,
EllipsoidVS,
EllipsoidFS) {
"use strict";
var attributeIndices = {
position : 0
};
/**
* A renderable ellipsoid. It can also draw spheres when the three {@link EllipsoidPrimitive#radii} components are equal.
* <p>
* This is only supported in 3D. The ellipsoid is not shown in 2D or Columbus view.
* </p>
*
* @alias EllipsoidPrimitive
* @constructor
*
* @example
* // 1. Create a sphere using the ellipsoid primitive
* var e = new EllipsoidPrimitive();
* e.center = ellipsoid.cartographicToCartesian(
* Cartographic.fromDegrees(-75.0, 40.0, 500000.0));
* e.radii = new Cartesian3(500000.0, 500000.0, 500000.0);
* primitives.add(e);
*
* @example
* // 2. Create a tall ellipsoid in an east-north-up reference frame
* var e = new EllipsoidPrimitive();
* e.modelMatrix = Transforms.eastNorthUpToFixedFrame(
* ellipsoid.cartographicToCartesian(
* Cartographic.fromDegrees(-95.0, 40.0, 200000.0)));
* e.radii = new Cartesian3(100000.0, 100000.0, 200000.0);
* primitives.add(e);
*
* @demo <a href="http://cesium.agi.com/Cesium/Apps/Sandcastle/index.html?src=Volumes.html">Cesium Sandcastle Volumes Demo</a>
*/
var EllipsoidPrimitive = function() {
/**
* The center of the ellipsoid in the ellipsoid's model coordinates.
* <p>
* The default is {@link Cartesian3.ZERO}.
* </p>
*
* @type {Cartesian3}
* @default {@link Cartesian3.ZERO}
*
* @see EllipsoidPrimitive#modelMatrix
*/
this.center = Cartesian3.ZERO.clone();
/**
* The radius of the ellipsoid along the <code>x</code>, <code>y</code>, and <code>z</code> axes in the ellipsoid's model coordinates.
* When these are the same, the ellipsoid is a sphere.
* <p>
* The default is <code>undefined</code>. The ellipsoid is not drawn until a radii is provided.
* </p>
*
* @type {Cartesian3}
* @default undefined
*
* @example
* // A sphere with a radius of 2.0
* e.radii = new Cartesian3(2.0, 2.0, 2.0);
*
* @see EllipsoidPrimitive#modelMatrix
*/
this.radii = undefined;
this._radii = new Cartesian3();
this._oneOverEllipsoidRadiiSquared = new Cartesian3();
this._boundingSphere = new BoundingSphere();
/**
* The 4x4 transformation matrix that transforms the ellipsoid from model to world coordinates.
* When this is the identity matrix, the ellipsoid is drawn in world coordinates, i.e., Earth's WGS84 coordinates.
* Local reference frames can be used by providing a different transformation matrix, like that returned
* by {@link Transforms.eastNorthUpToFixedFrame}. This matrix is available to GLSL vertex and fragment
* shaders via {@link czm_model} and derived uniforms.
* <p>
* The default is {@link Matrix4.IDENTITY}.
* </p>
*
* @type {Matrix4}
* @default {@link Matrix4.IDENTITY}
*
* @example
* var origin = ellipsoid.cartographicToCartesian(
* Cartographic.fromDegrees(-95.0, 40.0, 200000.0));
* e.modelMatrix = Transforms.eastNorthUpToFixedFrame(origin);
*
* @see Transforms.eastNorthUpToFixedFrame
* @see czm_model
*/
this.modelMatrix = Matrix4.IDENTITY.clone();
this._computedModelMatrix = Matrix4.IDENTITY.clone();
/**
* Determines if the ellipsoid primitive will be shown.
* <p>
* The default is <code>true</code>.
* </p>
*
* @type {Boolean}
* @default true
*/
this.show = true;
/**
* The surface appearance of the ellipsoid. This can be one of several built-in {@link Material} objects or a custom material, scripted with
* <a href='https://github.com/AnalyticalGraphicsInc/cesium/wiki/Fabric'>Fabric</a>.
* <p>
* The default material is <code>Material.ColorType</code>.
* </p>
*
* @type {Material}
* @default Material.fromType(undefined, Material.ColorType)
*
* @example
* // 1. Change the color of the default material to yellow
* e.material.uniforms.color = new Color(1.0, 1.0, 0.0, 1.0);
*
* // 2. Change material to horizontal stripes
* e.material = Material.fromType(scene.getContext(), Material.StripeType);
*
* @see <a href='https://github.com/AnalyticalGraphicsInc/cesium/wiki/Fabric'>Fabric</a>
*/
this.material = Material.fromType(undefined, Material.ColorType);
this._material = undefined;
this._sp = undefined;
this._rs = undefined;
this._va = undefined;
this._pickSP = undefined;
this._pickId = undefined;
this._colorCommand = new DrawCommand();
this._pickCommand = new DrawCommand();
this._commandLists = new CommandLists();
var that = this;
this._uniforms = {
u_radii : function() {
return that.radii;
},
u_oneOverEllipsoidRadiiSquared : function() {
return that._oneOverEllipsoidRadiiSquared;
}
};
this._pickUniforms = {
czm_pickColor : function() {
return that._pickId.color;
}
};
};
function getVertexArray(context) {
var vertexArray = context.cache.ellipsoidPrimitive_vertexArray;
if (typeof vertexArray !== 'undefined') {
return vertexArray;
}
var mesh = BoxTessellator.compute({
dimensions : new Cartesian3(2.0, 2.0, 2.0)
});
vertexArray = context.createVertexArrayFromMesh({
mesh: mesh,
attributeIndices: attributeIndices,
bufferUsage: BufferUsage.STATIC_DRAW
});
context.cache.ellipsoidPrimitive_vertexArray = vertexArray;
return vertexArray;
}
/**
* @private
*
* @exception {DeveloperError} this.material must be defined.
*/
EllipsoidPrimitive.prototype.update = function(context, frameState, commandList) {
if (!this.show ||
(frameState.mode !== SceneMode.SCENE3D) ||
(typeof this.center === 'undefined') ||
(typeof this.radii === 'undefined')) {
return;
}
if (typeof this.material === 'undefined') {
throw new DeveloperError('this.material must be defined.');
}
if (typeof this._rs === 'undefined') {
this._rs = context.createRenderState({
// Cull front faces - not back faces - so the ellipsoid doesn't
// disappear if the viewer enters the bounding box.
cull : {
enabled : true,
face : CullFace.FRONT
},
depthTest : {
enabled : true
},
// Do not write depth since the depth for the bounding box is
// wrong; it is not the true of the ray casted ellipsoid.
// Once WebGL has the extension for writing gl_FragDepth,
// we can write the correct depth. For now, most ellipsoids
// will be translucent so we don't want to write depth anyway.
depthMask : false,
blending : BlendingState.ALPHA_BLEND
});
}
if (typeof this._va === 'undefined') {
this._va = getVertexArray(context);
}
var radii = this.radii;
if (!Cartesian3.equals(this._radii, radii)) {
Cartesian3.clone(radii, this._radii);
var r = this._oneOverEllipsoidRadiiSquared;
r.x = 1.0 / (radii.x * radii.x);
r.y = 1.0 / (radii.y * radii.y);
r.z = 1.0 / (radii.z * radii.z);
this._boundingSphere.radius = Cartesian3.getMaximumComponent(radii);
}
// Translate model coordinates used for rendering such that the origin is the center of the ellipsoid.
Matrix4.multiplyByTranslation(this.modelMatrix, this.center, this._computedModelMatrix);
var ellipsoidCommandLists = this._commandLists;
ellipsoidCommandLists.removeAll();
var materialChanged = this._material !== this.material;
this._material = this.material;
if (frameState.passes.color) {
var colorCommand = this._colorCommand;
// Recompile shader when material changes
if (materialChanged) {
var colorFS =
'#line 0\n' +
this.material.shaderSource +
'#line 0\n' +
EllipsoidFS;
this._sp = context.getShaderCache().replaceShaderProgram(this._sp, EllipsoidVS, colorFS, attributeIndices);
colorCommand.primitiveType = PrimitiveType.TRIANGLES;
colorCommand.vertexArray = this._va;
colorCommand.renderState = this._rs;
colorCommand.shaderProgram = this._sp;
colorCommand.uniformMap = combine([this._uniforms, this.material._uniforms], false, false);
colorCommand.executeInClosestFrustum = true;
}
colorCommand.boundingVolume = this._boundingSphere;
colorCommand.modelMatrix = this._computedModelMatrix;
ellipsoidCommandLists.colorList.push(colorCommand);
}
if (frameState.passes.pick) {
var pickCommand = this._pickCommand;
if (typeof this._pickId === 'undefined') {
this._pickId = context.createPickId(this);
}
// Recompile shader when material changes
if (materialChanged || typeof this._pickSP === 'undefined') {
var pickFS = createPickFragmentShaderSource(
'#line 0\n' +
this.material.shaderSource +
'#line 0\n' +
EllipsoidFS, 'uniform');
this._pickSP = context.getShaderCache().replaceShaderProgram(this._pickSP, EllipsoidVS, pickFS, attributeIndices);
pickCommand.primitiveType = PrimitiveType.TRIANGLES;
pickCommand.vertexArray = this._va;
pickCommand.renderState = this._rs;
pickCommand.shaderProgram = this._pickSP;
pickCommand.uniformMap = combine([this._uniforms, this._pickUniforms, this.material._uniforms], false, false);
pickCommand.executeInClosestFrustum = true;
}
pickCommand.boundingVolume = this._boundingSphere;
pickCommand.modelMatrix = this._computedModelMatrix;
ellipsoidCommandLists.pickList.push(pickCommand);
}
commandList.push(ellipsoidCommandLists);
};
/**
* Returns true if this object was destroyed; otherwise, false.
* <br /><br />
* If this object was destroyed, it should not be used; calling any function other than
* <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
*
* @memberof EllipsoidPrimitive
*
* @return {Boolean} <code>true</code> if this object was destroyed; otherwise, <code>false</code>.
*
* @see EllipsoidPrimitive#destroy
*/
EllipsoidPrimitive.prototype.isDestroyed = function() {
return false;
};
/**
* Destroys the WebGL resources held by this object. Destroying an object allows for deterministic
* release of WebGL resources, instead of relying on the garbage collector to destroy this object.
* <br /><br />
* Once an object is destroyed, it should not be used; calling any function other than
* <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
* assign the return value (<code>undefined</code>) to the object as done in the example.
*
* @memberof EllipsoidPrimitive
*
* @return {undefined}
*
* @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
*
* @see EllipsoidPrimitive#isDestroyed
*
* @example
* e = e && e.destroy();
*/
EllipsoidPrimitive.prototype.destroy = function() {
this._sp = this._sp && this._sp.release();
this._pickSP = this._pickSP && this._pickSP.release();
this._pickId = this._pickId && this._pickId.destroy();
return destroyObject(this);
};
return EllipsoidPrimitive;
});