15832144755
2022-01-06 7b4c8991dca9cf2a809a95e239d144697d3afb56
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
import AttributeCompression from "../Core/AttributeCompression.js";
import Cartesian3 from "../Core/Cartesian3.js";
import Cartographic from "../Core/Cartographic.js";
import Color from "../Core/Color.js";
import defined from "../Core/defined.js";
import Ellipsoid from "../Core/Ellipsoid.js";
import IndexDatatype from "../Core/IndexDatatype.js";
import CesiumMath from "../Core/Math.js";
import OrientedBoundingBox from "../Core/OrientedBoundingBox.js";
import Rectangle from "../Core/Rectangle.js";
import createTaskProcessorWorker from "./createTaskProcessorWorker.js";
 
var scratchCenter = new Cartesian3();
var scratchEllipsoid = new Ellipsoid();
var scratchRectangle = new Rectangle();
var scratchScalars = {
  min: undefined,
  max: undefined,
  indexBytesPerElement: undefined,
};
 
function unpackBuffer(buffer) {
  var packedBuffer = new Float64Array(buffer);
 
  var offset = 0;
  scratchScalars.indexBytesPerElement = packedBuffer[offset++];
 
  scratchScalars.min = packedBuffer[offset++];
  scratchScalars.max = packedBuffer[offset++];
 
  Cartesian3.unpack(packedBuffer, offset, scratchCenter);
  offset += Cartesian3.packedLength;
 
  Ellipsoid.unpack(packedBuffer, offset, scratchEllipsoid);
  offset += Ellipsoid.packedLength;
 
  Rectangle.unpack(packedBuffer, offset, scratchRectangle);
}
 
function packedBatchedIndicesLength(batchedIndices) {
  var length = batchedIndices.length;
  var count = 0;
  for (var i = 0; i < length; ++i) {
    count += Color.packedLength + 3 + batchedIndices[i].batchIds.length;
  }
  return count;
}
 
function packBuffer(indexDatatype, boundingVolumes, batchedIndices) {
  var numBVs = boundingVolumes.length;
  var length =
    1 +
    1 +
    numBVs * OrientedBoundingBox.packedLength +
    1 +
    packedBatchedIndicesLength(batchedIndices);
 
  var packedBuffer = new Float64Array(length);
 
  var offset = 0;
  packedBuffer[offset++] = indexDatatype;
  packedBuffer[offset++] = numBVs;
 
  for (var i = 0; i < numBVs; ++i) {
    OrientedBoundingBox.pack(boundingVolumes[i], packedBuffer, offset);
    offset += OrientedBoundingBox.packedLength;
  }
 
  var indicesLength = batchedIndices.length;
  packedBuffer[offset++] = indicesLength;
 
  for (var j = 0; j < indicesLength; ++j) {
    var batchedIndex = batchedIndices[j];
 
    Color.pack(batchedIndex.color, packedBuffer, offset);
    offset += Color.packedLength;
 
    packedBuffer[offset++] = batchedIndex.offset;
    packedBuffer[offset++] = batchedIndex.count;
 
    var batchIds = batchedIndex.batchIds;
    var batchIdsLength = batchIds.length;
    packedBuffer[offset++] = batchIdsLength;
 
    for (var k = 0; k < batchIdsLength; ++k) {
      packedBuffer[offset++] = batchIds[k];
    }
  }
 
  return packedBuffer;
}
 
var maxShort = 32767;
 
var scratchEncodedPosition = new Cartesian3();
var scratchNormal = new Cartesian3();
var scratchScaledNormal = new Cartesian3();
var scratchMinHeightPosition = new Cartesian3();
var scratchMaxHeightPosition = new Cartesian3();
var scratchBVCartographic = new Cartographic();
var scratchBVRectangle = new Rectangle();
 
function createVectorTilePolygons(parameters, transferableObjects) {
  unpackBuffer(parameters.packedBuffer);
 
  var indices;
  var indexBytesPerElement = scratchScalars.indexBytesPerElement;
  if (indexBytesPerElement === 2) {
    indices = new Uint16Array(parameters.indices);
  } else {
    indices = new Uint32Array(parameters.indices);
  }
 
  var positions = new Uint16Array(parameters.positions);
  var counts = new Uint32Array(parameters.counts);
  var indexCounts = new Uint32Array(parameters.indexCounts);
  var batchIds = new Uint32Array(parameters.batchIds);
  var batchTableColors = new Uint32Array(parameters.batchTableColors);
 
  var boundingVolumes = new Array(counts.length);
 
  var center = scratchCenter;
  var ellipsoid = scratchEllipsoid;
  var rectangle = scratchRectangle;
  var minHeight = scratchScalars.min;
  var maxHeight = scratchScalars.max;
 
  var minimumHeights = parameters.minimumHeights;
  var maximumHeights = parameters.maximumHeights;
  if (defined(minimumHeights) && defined(maximumHeights)) {
    minimumHeights = new Float32Array(minimumHeights);
    maximumHeights = new Float32Array(maximumHeights);
  }
 
  var i;
  var j;
  var rgba;
 
  var positionsLength = positions.length / 2;
  var uBuffer = positions.subarray(0, positionsLength);
  var vBuffer = positions.subarray(positionsLength, 2 * positionsLength);
  AttributeCompression.zigZagDeltaDecode(uBuffer, vBuffer);
 
  var decodedPositions = new Float64Array(positionsLength * 3);
  for (i = 0; i < positionsLength; ++i) {
    var u = uBuffer[i];
    var v = vBuffer[i];
 
    var x = CesiumMath.lerp(rectangle.west, rectangle.east, u / maxShort);
    var y = CesiumMath.lerp(rectangle.south, rectangle.north, v / maxShort);
 
    var cart = Cartographic.fromRadians(x, y, 0.0, scratchBVCartographic);
    var decodedPosition = ellipsoid.cartographicToCartesian(
      cart,
      scratchEncodedPosition
    );
    Cartesian3.pack(decodedPosition, decodedPositions, i * 3);
  }
 
  var countsLength = counts.length;
  var offsets = new Array(countsLength);
  var indexOffsets = new Array(countsLength);
  var currentOffset = 0;
  var currentIndexOffset = 0;
  for (i = 0; i < countsLength; ++i) {
    offsets[i] = currentOffset;
    indexOffsets[i] = currentIndexOffset;
 
    currentOffset += counts[i];
    currentIndexOffset += indexCounts[i];
  }
 
  var batchedPositions = new Float32Array(positionsLength * 3 * 2);
  var batchedIds = new Uint16Array(positionsLength * 2);
  var batchedIndexOffsets = new Uint32Array(indexOffsets.length);
  var batchedIndexCounts = new Uint32Array(indexCounts.length);
  var batchedIndices = [];
 
  var colorToBuffers = {};
  for (i = 0; i < countsLength; ++i) {
    rgba = batchTableColors[i];
    if (!defined(colorToBuffers[rgba])) {
      colorToBuffers[rgba] = {
        positionLength: counts[i],
        indexLength: indexCounts[i],
        offset: 0,
        indexOffset: 0,
        batchIds: [i],
      };
    } else {
      colorToBuffers[rgba].positionLength += counts[i];
      colorToBuffers[rgba].indexLength += indexCounts[i];
      colorToBuffers[rgba].batchIds.push(i);
    }
  }
 
  // get the offsets and counts for the positions and indices of each primitive
  var buffer;
  var byColorPositionOffset = 0;
  var byColorIndexOffset = 0;
  for (rgba in colorToBuffers) {
    if (colorToBuffers.hasOwnProperty(rgba)) {
      buffer = colorToBuffers[rgba];
      buffer.offset = byColorPositionOffset;
      buffer.indexOffset = byColorIndexOffset;
 
      var positionLength = buffer.positionLength * 2;
      var indexLength = buffer.indexLength * 2 + buffer.positionLength * 6;
 
      byColorPositionOffset += positionLength;
      byColorIndexOffset += indexLength;
 
      buffer.indexLength = indexLength;
    }
  }
 
  var batchedDrawCalls = [];
 
  for (rgba in colorToBuffers) {
    if (colorToBuffers.hasOwnProperty(rgba)) {
      buffer = colorToBuffers[rgba];
 
      batchedDrawCalls.push({
        color: Color.fromRgba(parseInt(rgba)),
        offset: buffer.indexOffset,
        count: buffer.indexLength,
        batchIds: buffer.batchIds,
      });
    }
  }
 
  for (i = 0; i < countsLength; ++i) {
    rgba = batchTableColors[i];
 
    buffer = colorToBuffers[rgba];
    var positionOffset = buffer.offset;
    var positionIndex = positionOffset * 3;
    var batchIdIndex = positionOffset;
 
    var polygonOffset = offsets[i];
    var polygonCount = counts[i];
    var batchId = batchIds[i];
 
    var polygonMinimumHeight = minHeight;
    var polygonMaximumHeight = maxHeight;
    if (defined(minimumHeights) && defined(maximumHeights)) {
      polygonMinimumHeight = minimumHeights[i];
      polygonMaximumHeight = maximumHeights[i];
    }
 
    var minLat = Number.POSITIVE_INFINITY;
    var maxLat = Number.NEGATIVE_INFINITY;
    var minLon = Number.POSITIVE_INFINITY;
    var maxLon = Number.NEGATIVE_INFINITY;
 
    for (j = 0; j < polygonCount; ++j) {
      var position = Cartesian3.unpack(
        decodedPositions,
        polygonOffset * 3 + j * 3,
        scratchEncodedPosition
      );
      ellipsoid.scaleToGeodeticSurface(position, position);
 
      var carto = ellipsoid.cartesianToCartographic(
        position,
        scratchBVCartographic
      );
      var lat = carto.latitude;
      var lon = carto.longitude;
 
      minLat = Math.min(lat, minLat);
      maxLat = Math.max(lat, maxLat);
      minLon = Math.min(lon, minLon);
      maxLon = Math.max(lon, maxLon);
 
      var normal = ellipsoid.geodeticSurfaceNormal(position, scratchNormal);
      var scaledNormal = Cartesian3.multiplyByScalar(
        normal,
        polygonMinimumHeight,
        scratchScaledNormal
      );
      var minHeightPosition = Cartesian3.add(
        position,
        scaledNormal,
        scratchMinHeightPosition
      );
 
      scaledNormal = Cartesian3.multiplyByScalar(
        normal,
        polygonMaximumHeight,
        scaledNormal
      );
      var maxHeightPosition = Cartesian3.add(
        position,
        scaledNormal,
        scratchMaxHeightPosition
      );
 
      Cartesian3.subtract(maxHeightPosition, center, maxHeightPosition);
      Cartesian3.subtract(minHeightPosition, center, minHeightPosition);
 
      Cartesian3.pack(maxHeightPosition, batchedPositions, positionIndex);
      Cartesian3.pack(minHeightPosition, batchedPositions, positionIndex + 3);
 
      batchedIds[batchIdIndex] = batchId;
      batchedIds[batchIdIndex + 1] = batchId;
 
      positionIndex += 6;
      batchIdIndex += 2;
    }
 
    rectangle = scratchBVRectangle;
    rectangle.west = minLon;
    rectangle.east = maxLon;
    rectangle.south = minLat;
    rectangle.north = maxLat;
 
    boundingVolumes[i] = OrientedBoundingBox.fromRectangle(
      rectangle,
      minHeight,
      maxHeight,
      ellipsoid
    );
 
    var indicesIndex = buffer.indexOffset;
 
    var indexOffset = indexOffsets[i];
    var indexCount = indexCounts[i];
 
    batchedIndexOffsets[i] = indicesIndex;
 
    for (j = 0; j < indexCount; j += 3) {
      var i0 = indices[indexOffset + j] - polygonOffset;
      var i1 = indices[indexOffset + j + 1] - polygonOffset;
      var i2 = indices[indexOffset + j + 2] - polygonOffset;
 
      // triangle on the top of the extruded polygon
      batchedIndices[indicesIndex++] = i0 * 2 + positionOffset;
      batchedIndices[indicesIndex++] = i1 * 2 + positionOffset;
      batchedIndices[indicesIndex++] = i2 * 2 + positionOffset;
 
      // triangle on the bottom of the extruded polygon
      batchedIndices[indicesIndex++] = i2 * 2 + 1 + positionOffset;
      batchedIndices[indicesIndex++] = i1 * 2 + 1 + positionOffset;
      batchedIndices[indicesIndex++] = i0 * 2 + 1 + positionOffset;
    }
 
    // indices for the walls of the extruded polygon
    for (j = 0; j < polygonCount; ++j) {
      var v0 = j;
      var v1 = (j + 1) % polygonCount;
 
      batchedIndices[indicesIndex++] = v0 * 2 + 1 + positionOffset;
      batchedIndices[indicesIndex++] = v1 * 2 + positionOffset;
      batchedIndices[indicesIndex++] = v0 * 2 + positionOffset;
 
      batchedIndices[indicesIndex++] = v0 * 2 + 1 + positionOffset;
      batchedIndices[indicesIndex++] = v1 * 2 + 1 + positionOffset;
      batchedIndices[indicesIndex++] = v1 * 2 + positionOffset;
    }
 
    buffer.offset += polygonCount * 2;
    buffer.indexOffset = indicesIndex;
 
    batchedIndexCounts[i] = indicesIndex - batchedIndexOffsets[i];
  }
 
  batchedIndices = IndexDatatype.createTypedArray(
    batchedPositions.length / 3,
    batchedIndices
  );
 
  var batchedIndicesLength = batchedDrawCalls.length;
  for (var m = 0; m < batchedIndicesLength; ++m) {
    var tempIds = batchedDrawCalls[m].batchIds;
    var count = 0;
    var tempIdsLength = tempIds.length;
    for (var n = 0; n < tempIdsLength; ++n) {
      count += batchedIndexCounts[tempIds[n]];
    }
    batchedDrawCalls[m].count = count;
  }
 
  var indexDatatype =
    batchedIndices.BYTES_PER_ELEMENT === 2
      ? IndexDatatype.UNSIGNED_SHORT
      : IndexDatatype.UNSIGNED_INT;
  var packedBuffer = packBuffer(
    indexDatatype,
    boundingVolumes,
    batchedDrawCalls
  );
 
  transferableObjects.push(
    batchedPositions.buffer,
    batchedIndices.buffer,
    batchedIndexOffsets.buffer,
    batchedIndexCounts.buffer,
    batchedIds.buffer,
    packedBuffer.buffer
  );
 
  return {
    positions: batchedPositions.buffer,
    indices: batchedIndices.buffer,
    indexOffsets: batchedIndexOffsets.buffer,
    indexCounts: batchedIndexCounts.buffer,
    batchIds: batchedIds.buffer,
    packedBuffer: packedBuffer.buffer,
  };
}
export default createTaskProcessorWorker(createVectorTilePolygons);