createVectorTilePolygons.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. define(['./when-e6985d2a', './Check-24cae389', './Math-392d0035', './Cartesian2-a5d6dde9', './Transforms-81680c41', './RuntimeError-61701d3e', './WebGLConstants-34c08bc0', './AttributeCompression-114c6354', './IndexDatatype-1be7d1f8', './IntersectionTests-f17c84f0', './Plane-ac6a1d3e', './createTaskProcessorWorker', './EllipsoidTangentPlane-85280670', './OrientedBoundingBox-d8219c10', './Color-631eee82'], function (when, Check, _Math, Cartesian2, Transforms, RuntimeError, WebGLConstants, AttributeCompression, IndexDatatype, IntersectionTests, Plane, createTaskProcessorWorker, EllipsoidTangentPlane, OrientedBoundingBox, Color) { 'use strict';
  3. var scratchCenter = new Cartesian2.Cartesian3();
  4. var scratchEllipsoid = new Cartesian2.Ellipsoid();
  5. var scratchRectangle = new Cartesian2.Rectangle();
  6. var scratchScalars = {
  7. min: undefined,
  8. max: undefined,
  9. indexBytesPerElement: undefined,
  10. };
  11. function unpackBuffer(buffer) {
  12. var packedBuffer = new Float64Array(buffer);
  13. var offset = 0;
  14. scratchScalars.indexBytesPerElement = packedBuffer[offset++];
  15. scratchScalars.min = packedBuffer[offset++];
  16. scratchScalars.max = packedBuffer[offset++];
  17. Cartesian2.Cartesian3.unpack(packedBuffer, offset, scratchCenter);
  18. offset += Cartesian2.Cartesian3.packedLength;
  19. Cartesian2.Ellipsoid.unpack(packedBuffer, offset, scratchEllipsoid);
  20. offset += Cartesian2.Ellipsoid.packedLength;
  21. Cartesian2.Rectangle.unpack(packedBuffer, offset, scratchRectangle);
  22. }
  23. function packedBatchedIndicesLength(batchedIndices) {
  24. var length = batchedIndices.length;
  25. var count = 0;
  26. for (var i = 0; i < length; ++i) {
  27. count += Color.Color.packedLength + 3 + batchedIndices[i].batchIds.length;
  28. }
  29. return count;
  30. }
  31. function packBuffer(indexDatatype, boundingVolumes, batchedIndices) {
  32. var numBVs = boundingVolumes.length;
  33. var length =
  34. 1 +
  35. 1 +
  36. numBVs * OrientedBoundingBox.OrientedBoundingBox.packedLength +
  37. 1 +
  38. packedBatchedIndicesLength(batchedIndices);
  39. var packedBuffer = new Float64Array(length);
  40. var offset = 0;
  41. packedBuffer[offset++] = indexDatatype;
  42. packedBuffer[offset++] = numBVs;
  43. for (var i = 0; i < numBVs; ++i) {
  44. OrientedBoundingBox.OrientedBoundingBox.pack(boundingVolumes[i], packedBuffer, offset);
  45. offset += OrientedBoundingBox.OrientedBoundingBox.packedLength;
  46. }
  47. var indicesLength = batchedIndices.length;
  48. packedBuffer[offset++] = indicesLength;
  49. for (var j = 0; j < indicesLength; ++j) {
  50. var batchedIndex = batchedIndices[j];
  51. Color.Color.pack(batchedIndex.color, packedBuffer, offset);
  52. offset += Color.Color.packedLength;
  53. packedBuffer[offset++] = batchedIndex.offset;
  54. packedBuffer[offset++] = batchedIndex.count;
  55. var batchIds = batchedIndex.batchIds;
  56. var batchIdsLength = batchIds.length;
  57. packedBuffer[offset++] = batchIdsLength;
  58. for (var k = 0; k < batchIdsLength; ++k) {
  59. packedBuffer[offset++] = batchIds[k];
  60. }
  61. }
  62. return packedBuffer;
  63. }
  64. var maxShort = 32767;
  65. var scratchEncodedPosition = new Cartesian2.Cartesian3();
  66. var scratchNormal = new Cartesian2.Cartesian3();
  67. var scratchScaledNormal = new Cartesian2.Cartesian3();
  68. var scratchMinHeightPosition = new Cartesian2.Cartesian3();
  69. var scratchMaxHeightPosition = new Cartesian2.Cartesian3();
  70. var scratchBVCartographic = new Cartesian2.Cartographic();
  71. var scratchBVRectangle = new Cartesian2.Rectangle();
  72. function createVectorTilePolygons(parameters, transferableObjects) {
  73. unpackBuffer(parameters.packedBuffer);
  74. var indices;
  75. var indexBytesPerElement = scratchScalars.indexBytesPerElement;
  76. if (indexBytesPerElement === 2) {
  77. indices = new Uint16Array(parameters.indices);
  78. } else {
  79. indices = new Uint32Array(parameters.indices);
  80. }
  81. var positions = new Uint16Array(parameters.positions);
  82. var counts = new Uint32Array(parameters.counts);
  83. var indexCounts = new Uint32Array(parameters.indexCounts);
  84. var batchIds = new Uint32Array(parameters.batchIds);
  85. var batchTableColors = new Uint32Array(parameters.batchTableColors);
  86. var boundingVolumes = new Array(counts.length);
  87. var center = scratchCenter;
  88. var ellipsoid = scratchEllipsoid;
  89. var rectangle = scratchRectangle;
  90. var minHeight = scratchScalars.min;
  91. var maxHeight = scratchScalars.max;
  92. var minimumHeights = parameters.minimumHeights;
  93. var maximumHeights = parameters.maximumHeights;
  94. if (when.defined(minimumHeights) && when.defined(maximumHeights)) {
  95. minimumHeights = new Float32Array(minimumHeights);
  96. maximumHeights = new Float32Array(maximumHeights);
  97. }
  98. var i;
  99. var j;
  100. var rgba;
  101. var positionsLength = positions.length / 2;
  102. var uBuffer = positions.subarray(0, positionsLength);
  103. var vBuffer = positions.subarray(positionsLength, 2 * positionsLength);
  104. AttributeCompression.AttributeCompression.zigZagDeltaDecode(uBuffer, vBuffer);
  105. var decodedPositions = new Float64Array(positionsLength * 3);
  106. for (i = 0; i < positionsLength; ++i) {
  107. var u = uBuffer[i];
  108. var v = vBuffer[i];
  109. var x = _Math.CesiumMath.lerp(rectangle.west, rectangle.east, u / maxShort);
  110. var y = _Math.CesiumMath.lerp(rectangle.south, rectangle.north, v / maxShort);
  111. var cart = Cartesian2.Cartographic.fromRadians(x, y, 0.0, scratchBVCartographic);
  112. var decodedPosition = ellipsoid.cartographicToCartesian(
  113. cart,
  114. scratchEncodedPosition
  115. );
  116. Cartesian2.Cartesian3.pack(decodedPosition, decodedPositions, i * 3);
  117. }
  118. var countsLength = counts.length;
  119. var offsets = new Array(countsLength);
  120. var indexOffsets = new Array(countsLength);
  121. var currentOffset = 0;
  122. var currentIndexOffset = 0;
  123. for (i = 0; i < countsLength; ++i) {
  124. offsets[i] = currentOffset;
  125. indexOffsets[i] = currentIndexOffset;
  126. currentOffset += counts[i];
  127. currentIndexOffset += indexCounts[i];
  128. }
  129. var batchedPositions = new Float32Array(positionsLength * 3 * 2);
  130. var batchedIds = new Uint16Array(positionsLength * 2);
  131. var batchedIndexOffsets = new Uint32Array(indexOffsets.length);
  132. var batchedIndexCounts = new Uint32Array(indexCounts.length);
  133. var batchedIndices = [];
  134. var colorToBuffers = {};
  135. for (i = 0; i < countsLength; ++i) {
  136. rgba = batchTableColors[i];
  137. if (!when.defined(colorToBuffers[rgba])) {
  138. colorToBuffers[rgba] = {
  139. positionLength: counts[i],
  140. indexLength: indexCounts[i],
  141. offset: 0,
  142. indexOffset: 0,
  143. batchIds: [i],
  144. };
  145. } else {
  146. colorToBuffers[rgba].positionLength += counts[i];
  147. colorToBuffers[rgba].indexLength += indexCounts[i];
  148. colorToBuffers[rgba].batchIds.push(i);
  149. }
  150. }
  151. // get the offsets and counts for the positions and indices of each primitive
  152. var buffer;
  153. var byColorPositionOffset = 0;
  154. var byColorIndexOffset = 0;
  155. for (rgba in colorToBuffers) {
  156. if (colorToBuffers.hasOwnProperty(rgba)) {
  157. buffer = colorToBuffers[rgba];
  158. buffer.offset = byColorPositionOffset;
  159. buffer.indexOffset = byColorIndexOffset;
  160. var positionLength = buffer.positionLength * 2;
  161. var indexLength = buffer.indexLength * 2 + buffer.positionLength * 6;
  162. byColorPositionOffset += positionLength;
  163. byColorIndexOffset += indexLength;
  164. buffer.indexLength = indexLength;
  165. }
  166. }
  167. var batchedDrawCalls = [];
  168. for (rgba in colorToBuffers) {
  169. if (colorToBuffers.hasOwnProperty(rgba)) {
  170. buffer = colorToBuffers[rgba];
  171. batchedDrawCalls.push({
  172. color: Color.Color.fromRgba(parseInt(rgba)),
  173. offset: buffer.indexOffset,
  174. count: buffer.indexLength,
  175. batchIds: buffer.batchIds,
  176. });
  177. }
  178. }
  179. for (i = 0; i < countsLength; ++i) {
  180. rgba = batchTableColors[i];
  181. buffer = colorToBuffers[rgba];
  182. var positionOffset = buffer.offset;
  183. var positionIndex = positionOffset * 3;
  184. var batchIdIndex = positionOffset;
  185. var polygonOffset = offsets[i];
  186. var polygonCount = counts[i];
  187. var batchId = batchIds[i];
  188. var polygonMinimumHeight = minHeight;
  189. var polygonMaximumHeight = maxHeight;
  190. if (when.defined(minimumHeights) && when.defined(maximumHeights)) {
  191. polygonMinimumHeight = minimumHeights[i];
  192. polygonMaximumHeight = maximumHeights[i];
  193. }
  194. var minLat = Number.POSITIVE_INFINITY;
  195. var maxLat = Number.NEGATIVE_INFINITY;
  196. var minLon = Number.POSITIVE_INFINITY;
  197. var maxLon = Number.NEGATIVE_INFINITY;
  198. for (j = 0; j < polygonCount; ++j) {
  199. var position = Cartesian2.Cartesian3.unpack(
  200. decodedPositions,
  201. polygonOffset * 3 + j * 3,
  202. scratchEncodedPosition
  203. );
  204. ellipsoid.scaleToGeodeticSurface(position, position);
  205. var carto = ellipsoid.cartesianToCartographic(
  206. position,
  207. scratchBVCartographic
  208. );
  209. var lat = carto.latitude;
  210. var lon = carto.longitude;
  211. minLat = Math.min(lat, minLat);
  212. maxLat = Math.max(lat, maxLat);
  213. minLon = Math.min(lon, minLon);
  214. maxLon = Math.max(lon, maxLon);
  215. var normal = ellipsoid.geodeticSurfaceNormal(position, scratchNormal);
  216. var scaledNormal = Cartesian2.Cartesian3.multiplyByScalar(
  217. normal,
  218. polygonMinimumHeight,
  219. scratchScaledNormal
  220. );
  221. var minHeightPosition = Cartesian2.Cartesian3.add(
  222. position,
  223. scaledNormal,
  224. scratchMinHeightPosition
  225. );
  226. scaledNormal = Cartesian2.Cartesian3.multiplyByScalar(
  227. normal,
  228. polygonMaximumHeight,
  229. scaledNormal
  230. );
  231. var maxHeightPosition = Cartesian2.Cartesian3.add(
  232. position,
  233. scaledNormal,
  234. scratchMaxHeightPosition
  235. );
  236. Cartesian2.Cartesian3.subtract(maxHeightPosition, center, maxHeightPosition);
  237. Cartesian2.Cartesian3.subtract(minHeightPosition, center, minHeightPosition);
  238. Cartesian2.Cartesian3.pack(maxHeightPosition, batchedPositions, positionIndex);
  239. Cartesian2.Cartesian3.pack(minHeightPosition, batchedPositions, positionIndex + 3);
  240. batchedIds[batchIdIndex] = batchId;
  241. batchedIds[batchIdIndex + 1] = batchId;
  242. positionIndex += 6;
  243. batchIdIndex += 2;
  244. }
  245. rectangle = scratchBVRectangle;
  246. rectangle.west = minLon;
  247. rectangle.east = maxLon;
  248. rectangle.south = minLat;
  249. rectangle.north = maxLat;
  250. boundingVolumes[i] = OrientedBoundingBox.OrientedBoundingBox.fromRectangle(
  251. rectangle,
  252. minHeight,
  253. maxHeight,
  254. ellipsoid
  255. );
  256. var indicesIndex = buffer.indexOffset;
  257. var indexOffset = indexOffsets[i];
  258. var indexCount = indexCounts[i];
  259. batchedIndexOffsets[i] = indicesIndex;
  260. for (j = 0; j < indexCount; j += 3) {
  261. var i0 = indices[indexOffset + j] - polygonOffset;
  262. var i1 = indices[indexOffset + j + 1] - polygonOffset;
  263. var i2 = indices[indexOffset + j + 2] - polygonOffset;
  264. // triangle on the top of the extruded polygon
  265. batchedIndices[indicesIndex++] = i0 * 2 + positionOffset;
  266. batchedIndices[indicesIndex++] = i1 * 2 + positionOffset;
  267. batchedIndices[indicesIndex++] = i2 * 2 + positionOffset;
  268. // triangle on the bottom of the extruded polygon
  269. batchedIndices[indicesIndex++] = i2 * 2 + 1 + positionOffset;
  270. batchedIndices[indicesIndex++] = i1 * 2 + 1 + positionOffset;
  271. batchedIndices[indicesIndex++] = i0 * 2 + 1 + positionOffset;
  272. }
  273. // indices for the walls of the extruded polygon
  274. for (j = 0; j < polygonCount; ++j) {
  275. var v0 = j;
  276. var v1 = (j + 1) % polygonCount;
  277. batchedIndices[indicesIndex++] = v0 * 2 + 1 + positionOffset;
  278. batchedIndices[indicesIndex++] = v1 * 2 + positionOffset;
  279. batchedIndices[indicesIndex++] = v0 * 2 + positionOffset;
  280. batchedIndices[indicesIndex++] = v0 * 2 + 1 + positionOffset;
  281. batchedIndices[indicesIndex++] = v1 * 2 + 1 + positionOffset;
  282. batchedIndices[indicesIndex++] = v1 * 2 + positionOffset;
  283. }
  284. buffer.offset += polygonCount * 2;
  285. buffer.indexOffset = indicesIndex;
  286. batchedIndexCounts[i] = indicesIndex - batchedIndexOffsets[i];
  287. }
  288. batchedIndices = IndexDatatype.IndexDatatype.createTypedArray(
  289. batchedPositions.length / 3,
  290. batchedIndices
  291. );
  292. var batchedIndicesLength = batchedDrawCalls.length;
  293. for (var m = 0; m < batchedIndicesLength; ++m) {
  294. var tempIds = batchedDrawCalls[m].batchIds;
  295. var count = 0;
  296. var tempIdsLength = tempIds.length;
  297. for (var n = 0; n < tempIdsLength; ++n) {
  298. count += batchedIndexCounts[tempIds[n]];
  299. }
  300. batchedDrawCalls[m].count = count;
  301. }
  302. var indexDatatype =
  303. batchedIndices.BYTES_PER_ELEMENT === 2
  304. ? IndexDatatype.IndexDatatype.UNSIGNED_SHORT
  305. : IndexDatatype.IndexDatatype.UNSIGNED_INT;
  306. var packedBuffer = packBuffer(
  307. indexDatatype,
  308. boundingVolumes,
  309. batchedDrawCalls
  310. );
  311. transferableObjects.push(
  312. batchedPositions.buffer,
  313. batchedIndices.buffer,
  314. batchedIndexOffsets.buffer,
  315. batchedIndexCounts.buffer,
  316. batchedIds.buffer,
  317. packedBuffer.buffer
  318. );
  319. return {
  320. positions: batchedPositions.buffer,
  321. indices: batchedIndices.buffer,
  322. indexOffsets: batchedIndexOffsets.buffer,
  323. indexCounts: batchedIndexCounts.buffer,
  324. batchIds: batchedIds.buffer,
  325. packedBuffer: packedBuffer.buffer,
  326. };
  327. }
  328. var createVectorTilePolygons$1 = createTaskProcessorWorker(createVectorTilePolygons);
  329. return createVectorTilePolygons$1;
  330. });