-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathExtentTessellator.js
385 lines (337 loc) · 17.5 KB
/
ExtentTessellator.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
/*global define*/
define([
'./defaultValue',
'./DeveloperError',
'./Math',
'./Ellipsoid',
'./Extent',
'./Cartesian3',
'./ComponentDatatype',
'./PrimitiveType'
], function(
defaultValue,
DeveloperError,
CesiumMath,
Ellipsoid,
Extent,
Cartesian3,
ComponentDatatype,
PrimitiveType) {
"use strict";
/**
* Contains class functions to create a mesh or vertex array from a cartographic extent.
*
* @exports ExtentTessellator
*
* @see HeightmapTessellator
* @see CubeMapEllipsoidTessellator
* @see BoxTessellator
* @see PlaneTessellator
*/
var ExtentTessellator = {};
/**
* Compute vertices from a cartographic extent. This function is different from
* {@link ExtentTessellator#compute} and {@link ExtentTessellator#computeBuffers}
* in that it assumes that you have already allocated output arrays of the correct size.
*
* @param {Extent} description.extent A cartographic extent with north, south, east and west properties in radians.
* @param {Number} description.width The number of vertices in the longitude direction.
* @param {Number} description.height The number of vertices in the latitude direction.
* @param {Number} description.granularityX The distance, in radians, between each longitude.
* @param {Number} description.granularityY The distance, in radians, between each latitude.
* @param {Number} description.surfaceHeight The height from the surface of the ellipsoid.
* @param {Boolean} description.generateTextureCoordinates Whether to generate texture coordinates.
* @param {Boolean} description.interleaveTextureCoordinates Whether to interleave the texture coordinates into the vertex array.
* @param {Cartesian3} description.relativetoCenter The positions will be computed as <code>worldPosition.subtract(relativeToCenter)</code>.
* @param {Cartesian3} description.radiiSquared The radii squared of the ellipsoid to use.
* @param {Array|Float32Array} description.vertices The array to use to store computed vertices.
* @param {Array|Float32Array} description.textureCoordinates The array to use to store computed texture coordinates, unless interleaved.
* @param {Array|Float32Array} [description.indices] The array to use to store computed indices. If undefined, indices will be not computed.
*/
ExtentTessellator.computeVertices = function(description) {
description = defaultValue(description, {});
var extent = description.extent;
var surfaceHeight = description.surfaceHeight;
var width = description.width;
var height = description.height;
var granularityX = (extent.east - extent.west) / (width - 1);
var granularityY = (extent.north - extent.south) / (height - 1);
var generateTextureCoordinates = description.generateTextureCoordinates;
var interleaveTextureCoordinates = description.interleaveTextureCoordinates;
var relativeToCenter = description.relativeToCenter;
var vertices = description.vertices;
var textureCoordinates = description.textureCoordinates;
var indices = description.indices;
var radiiSquared = description.radiiSquared;
var radiiSquaredX = radiiSquared.x;
var radiiSquaredY = radiiSquared.y;
var radiiSquaredZ = radiiSquared.z;
var cos = Math.cos;
var sin = Math.sin;
var sqrt = Math.sqrt;
// for computing texture coordinates
var lonScalar = 1.0 / (extent.east - extent.west);
var latScalar = 1.0 / (extent.north - extent.south);
var vertexArrayIndex = 0;
var textureCoordinatesIndex = 0;
for ( var row = 0; row < height; ++row) {
var latitude = extent.north - granularityY * row;
var cosLatitude = cos(latitude);
var nZ = sin(latitude);
var kZ = radiiSquaredZ * nZ;
var geographicV = (latitude - extent.south) * latScalar;
var v = geographicV;
for ( var col = 0; col < width; ++col) {
var longitude = extent.west + granularityX * col;
var nX = cosLatitude * cos(longitude);
var nY = cosLatitude * sin(longitude);
var kX = radiiSquaredX * nX;
var kY = radiiSquaredY * nY;
var gamma = sqrt((kX * nX) + (kY * nY) + (kZ * nZ));
var rSurfaceX = kX / gamma;
var rSurfaceY = kY / gamma;
var rSurfaceZ = kZ / gamma;
vertices[vertexArrayIndex++] = rSurfaceX + nX * surfaceHeight - relativeToCenter.x;
vertices[vertexArrayIndex++] = rSurfaceY + nY * surfaceHeight - relativeToCenter.y;
vertices[vertexArrayIndex++] = rSurfaceZ + nZ * surfaceHeight - relativeToCenter.z;
if (generateTextureCoordinates) {
var geographicU = (longitude - extent.west) * lonScalar;
var u = geographicU;
if (interleaveTextureCoordinates) {
vertices[vertexArrayIndex++] = u;
vertices[vertexArrayIndex++] = v;
} else {
textureCoordinates[textureCoordinatesIndex++] = u;
textureCoordinates[textureCoordinatesIndex++] = v;
}
}
}
}
if (typeof indices !== 'undefined') {
var index = 0;
var indicesIndex = 0;
for ( var i = 0; i < height - 1; ++i) {
for ( var j = 0; j < width - 1; ++j) {
var upperLeft = index;
var lowerLeft = upperLeft + width;
var lowerRight = lowerLeft + 1;
var upperRight = upperLeft + 1;
indices[indicesIndex++] = upperLeft;
indices[indicesIndex++] = lowerLeft;
indices[indicesIndex++] = upperRight;
indices[indicesIndex++] = upperRight;
indices[indicesIndex++] = lowerLeft;
indices[indicesIndex++] = lowerRight;
++index;
}
++index;
}
}
};
/**
* Creates a mesh from a cartographic extent.
*
* @param {Extent} description.extent A cartographic extent with north, south, east and west properties in radians.
* @param {Ellipsoid} [description.ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the extent lies.
* @param {Number} [description.granularity=0.1] The distance, in radians, between each latitude and longitude. Determines the number of positions in the buffer.
* @param {Number} [description.surfaceHeight=0.0] The height from the surface of the ellipsoid.
* @param {Cartesian3} [description.relativetoCenter=Cartesian3.ZERO] The positions will be computed as <code>worldPosition.subtract(relativeToCenter)</code>.
* @param {Boolean} [description.generateTextureCoordinates=false] Whether to generate texture coordinates.
*
* @exception {DeveloperError} <code>description.extent</code> is required and must have north, south, east and west attributes.
* @exception {DeveloperError} <code>description.extent.north</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
* @exception {DeveloperError} <code>description.extent.south</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
* @exception {DeveloperError} <code>description.extent.east</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
* @exception {DeveloperError} <code>description.extent.west</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
* @exception {DeveloperError} <code>description.extent.north</code> must be greater than <code>extent.south</code>.
* @exception {DeveloperError} <code>description.extent.east</code> must be greater than <code>extent.west</code>.
* @exception {DeveloperError} <code>description.context</code> is required.
*
* @return {Object} A mesh containing attributes for positions, possibly texture coordinates and indices
* from the extent for creating a vertex array.
*
* @see Context#createVertexArrayFromMesh
* @see MeshFilters.createAttributeIndices
* @see MeshFilters.toWireframeInPlace
* @see Extent
*
* @example
* // Create a vertex array for rendering a wireframe extent.
* var mesh = ExtentTessellator.compute({
* ellipsoid : Ellipsoid.WGS84,
* extent : new Extent(
* CesiumMath.toRadians(-80.0),
* CesiumMath.toRadians(39.0),
* CesiumMath.toRadians(-74.0),
* CesiumMath.toRadians(42.0)
* ),
* granularity : 0.01,
* surfaceHeight : 10000.0
* });
* mesh = MeshFilters.toWireframeInPlace(mesh);
* var va = context.createVertexArrayFromMesh({
* mesh : mesh,
* attributeIndices : MeshFilters.createAttributeIndices(mesh)
* });
*/
ExtentTessellator.compute = function(description) {
description = defaultValue(description, {});
var extent = description.extent;
extent.validate();
var ellipsoid = defaultValue(description.ellipsoid, Ellipsoid.WGS84);
description.radiiSquared = ellipsoid.getRadiiSquared();
description.relativeToCenter = defaultValue(description.relativeToCenter, Cartesian3.ZERO);
var granularity = defaultValue(description.granularity, 0.1);
description.surfaceHeight = defaultValue(description.surfaceHeight, 0.0);
description.width = Math.ceil((extent.east - extent.west) / granularity) + 1;
description.height = Math.ceil((extent.north - extent.south) / granularity) + 1;
var vertices = [];
var indices = [];
var textureCoordinates = [];
description.generateTextureCoordinates = defaultValue(description.generateTextureCoordinates, false);
description.interleaveTextureCoordinates = false;
description.vertices = vertices;
description.textureCoordinates = textureCoordinates;
description.indices = indices;
ExtentTessellator.computeVertices(description);
var mesh = {
attributes : {},
indexLists : [{
primitiveType : PrimitiveType.TRIANGLES,
values : indices
}]
};
var positionName = defaultValue(description.positionName, 'position');
mesh.attributes[positionName] = {
componentDatatype : ComponentDatatype.FLOAT,
componentsPerAttribute : 3,
values : vertices
};
if (description.generateTextureCoordinates) {
var textureCoordinatesName = defaultValue(description.textureCoordinatesName, 'textureCoordinates');
mesh.attributes[textureCoordinatesName] = {
componentDatatype : ComponentDatatype.FLOAT,
componentsPerAttribute : 2,
values : textureCoordinates
};
}
return mesh;
};
/**
* Creates arrays of vertex attributes and indices from a cartographic extent.
*
* @param {Extent} description.extent A cartographic extent with north, south, east and west properties in radians.
* @param {Ellipsoid} [description.ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the extent lies.
* @param {Number} [description.granularity=0.1] The distance, in radians, between each latitude and longitude. Determines the number of positions in the buffer.
* @param {Number} [description.surfaceHeight=0.0] The height from the surface of the ellipsoid.
* @param {Cartesian3} [description.relativetoCenter=Cartesian3.ZERO] The positions will be computed as <code>worldPosition.subtract(relativeToCenter)</code>.
* @param {Boolean} [description.generateTextureCoordinates=false] Whether to generate texture coordinates.
* @param {Boolean} [description.interleaveTextureCoordinates=false] If texture coordinates are generated, whether to interleave the positions and texture coordinates in a single buffer.
*
* @exception {DeveloperError} <code>description.extent</code> is required and must have north, south, east and west attributes.
* @exception {DeveloperError} <code>description.extent.north</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
* @exception {DeveloperError} <code>description.extent.south</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
* @exception {DeveloperError} <code>description.extent.east</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
* @exception {DeveloperError} <code>description.extent.west</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
* @exception {DeveloperError} <code>description.extent.north</code> must be greater than <code>extent.south</code>. *
* @exception {DeveloperError} <code>description.extent.east</code> must be greater than <code>extent.west</code>.
*
* @return {Object} An object with flattened arrays for vertex attributes and indices.
*
* @example
* // Example 1:
* // Create a vertex array for a solid extent, with separate positions and texture coordinates.
* var buffers = ExtentTessellator.computeBuffers({
* ellipsoid : ellipsoid,
* extent : extent,
* generateTextureCoordinates : true
* });
*
* var datatype = ComponentDatatype.FLOAT;
* var usage = BufferUsage.STATIC_DRAW;
* var positionBuffer = context.createVertexBuffer(datatype.toTypedArray(buffers.positions), usage);
* var textureCoordinateBuffer = context.createVertexBuffer(datatype.toTypedArray(buffers.textureCoordinates), usage);
* attributes = [{
* index : attributeIndices.position,
* vertexBuffer : positionBuffer,
* componentDatatype : datatype,
* componentsPerAttribute : 3
* }, {
* index : attributeIndices.textureCoordinates,
* vertexBuffer : textureCoordinateBuffer,
* componentDatatype : datatype,
* componentsPerAttribute : 2
* }];
* var indexBuffer = context.createIndexBuffer(new Uint16Array(buffers.indices), usage, IndexDatatype.UNSIGNED_SHORT);
* var va = context.createVertexArray(attributes, indexBuffer);
*
* @example
* // Example 2:
* // Create a vertex array for a solid extent, with interleaved positions and texture coordinates.
* var buffers = ExtentTessellator.computeBuffers({
* ellipsoid : ellipsoid,
* extent : extent,
* generateTextureCoordinates : true,
* interleaveTextureCoordinates : true
* });
*
* var datatype = ComponentDatatype.FLOAT;
* var usage = BufferUsage.STATIC_DRAW;
* var typedArray = datatype.toTypedArray(buffers.vertices);
* var buffer = context.createVertexBuffer(typedArray, usage);
* var stride = 5 * datatype.sizeInBytes;
* var attributes = [{
* index : attributeIndices.position3D,
* vertexBuffer : buffer,
* componentDatatype : datatype,
* componentsPerAttribute : 3,
* normalize : false,
* offsetInBytes : 0,
* strideInBytes : stride
* }, {
* index : attributeIndices.textureCoordinates,
* vertexBuffer : buffer,
* componentDatatype : datatype,
* componentsPerAttribute : 2,
* normalize : false,
* offsetInBytes : 3 * datatype.sizeInBytes,
* strideInBytes : stride
* }];
* var indexBuffer = context.createIndexBuffer(new Uint16Array(buffers.indices), usage, IndexDatatype.UNSIGNED_SHORT);
* var vacontext.createVertexArray(attributes, indexBuffer);
*/
ExtentTessellator.computeBuffers = function(description) {
description = defaultValue(description, {});
var extent = description.extent;
extent.validate();
var ellipsoid = defaultValue(description.ellipsoid, Ellipsoid.WGS84);
description.radiiSquared = ellipsoid.getRadiiSquared();
description.relativeToCenter = defaultValue(description.relativeToCenter, Cartesian3.ZERO);
var granularity = defaultValue(description.granularity, 0.1);
description.surfaceHeight = defaultValue(description.surfaceHeight, 0.0);
description.width = Math.ceil((extent.east - extent.west) / granularity) + 1;
description.height = Math.ceil((extent.north - extent.south) / granularity) + 1;
var vertices = [];
var indices = [];
var textureCoordinates = [];
description.generateTextureCoordinates = defaultValue(description.generateTextureCoordinates, false);
description.interleaveTextureCoordinates = defaultValue(description.interleaveTextureCoordinates, false);
description.vertices = vertices;
description.textureCoordinates = textureCoordinates;
description.indices = indices;
ExtentTessellator.computeVertices(description);
var result = {
indices : indices
};
if (description.interleaveTextureCoordinates) {
result.vertices = vertices;
} else {
result.positions = vertices;
if (description.generateTextureCoordinates) {
result.textureCoordinates = textureCoordinates;
}
}
return result;
};
return ExtentTessellator;
});