createVectorTilePolygons.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. define(['./defined-26bd4a03', './Check-da037458', './freezeObject-2d83f591', './defaultValue-f2e68450', './Math-fa6e45cb', './Cartesian2-2a723276', './defineProperties-6f7a50f2', './Transforms-65aba0a4', './RuntimeError-ad75c885', './WebGLConstants-497deb20', './when-ee12a2cb', './AttributeCompression-87682214', './IndexDatatype-3de60176', './IntersectionTests-c2360ffa', './Plane-a1a3fd52', './createTaskProcessorWorker', './EllipsoidTangentPlane-10c6053a', './OrientedBoundingBox-a786ab5d', './Color-63c0bcb4'], function (defined, Check, freezeObject, defaultValue, _Math, Cartesian2, defineProperties, Transforms, RuntimeError, WebGLConstants, when, 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 = 1 + 1 + numBVs * OrientedBoundingBox.OrientedBoundingBox.packedLength + 1 + packedBatchedIndicesLength(batchedIndices);
  34. var packedBuffer = new Float64Array(length);
  35. var offset = 0;
  36. packedBuffer[offset++] = indexDatatype;
  37. packedBuffer[offset++] = numBVs;
  38. for (var i = 0; i < numBVs; ++i) {
  39. OrientedBoundingBox.OrientedBoundingBox.pack(boundingVolumes[i], packedBuffer, offset);
  40. offset += OrientedBoundingBox.OrientedBoundingBox.packedLength;
  41. }
  42. var indicesLength = batchedIndices.length;
  43. packedBuffer[offset++] = indicesLength;
  44. for (var j = 0; j < indicesLength; ++j) {
  45. var batchedIndex = batchedIndices[j];
  46. Color.Color.pack(batchedIndex.color, packedBuffer, offset);
  47. offset += Color.Color.packedLength;
  48. packedBuffer[offset++] = batchedIndex.offset;
  49. packedBuffer[offset++] = batchedIndex.count;
  50. var batchIds = batchedIndex.batchIds;
  51. var batchIdsLength = batchIds.length;
  52. packedBuffer[offset++] = batchIdsLength;
  53. for (var k = 0; k < batchIdsLength; ++k) {
  54. packedBuffer[offset++] = batchIds[k];
  55. }
  56. }
  57. return packedBuffer;
  58. }
  59. var maxShort = 32767;
  60. var scratchEncodedPosition = new Cartesian2.Cartesian3();
  61. var scratchNormal = new Cartesian2.Cartesian3();
  62. var scratchScaledNormal = new Cartesian2.Cartesian3();
  63. var scratchMinHeightPosition = new Cartesian2.Cartesian3();
  64. var scratchMaxHeightPosition = new Cartesian2.Cartesian3();
  65. var scratchBVCartographic = new Cartesian2.Cartographic();
  66. var scratchBVRectangle = new Cartesian2.Rectangle();
  67. function createVectorTilePolygons(parameters, transferableObjects) {
  68. unpackBuffer(parameters.packedBuffer);
  69. var indices;
  70. var indexBytesPerElement = scratchScalars.indexBytesPerElement;
  71. if (indexBytesPerElement === 2) {
  72. indices = new Uint16Array(parameters.indices);
  73. } else {
  74. indices = new Uint32Array(parameters.indices);
  75. }
  76. var positions = new Uint16Array(parameters.positions);
  77. var counts = new Uint32Array(parameters.counts);
  78. var indexCounts = new Uint32Array(parameters.indexCounts);
  79. var batchIds = new Uint32Array(parameters.batchIds);
  80. var batchTableColors = new Uint32Array(parameters.batchTableColors);
  81. var boundingVolumes = new Array(counts.length);
  82. var center = scratchCenter;
  83. var ellipsoid = scratchEllipsoid;
  84. var rectangle = scratchRectangle;
  85. var minHeight = scratchScalars.min;
  86. var maxHeight = scratchScalars.max;
  87. var minimumHeights = parameters.minimumHeights;
  88. var maximumHeights = parameters.maximumHeights;
  89. if (defined.defined(minimumHeights) && defined.defined(maximumHeights)) {
  90. minimumHeights = new Float32Array(minimumHeights);
  91. maximumHeights = new Float32Array(maximumHeights);
  92. }
  93. var i;
  94. var j;
  95. var rgba;
  96. var positionsLength = positions.length / 2;
  97. var uBuffer = positions.subarray(0, positionsLength);
  98. var vBuffer = positions.subarray(positionsLength, 2 * positionsLength);
  99. AttributeCompression.AttributeCompression.zigZagDeltaDecode(uBuffer, vBuffer);
  100. var decodedPositions = new Float32Array(positionsLength * 3);
  101. for (i = 0; i < positionsLength; ++i) {
  102. var u = uBuffer[i];
  103. var v = vBuffer[i];
  104. var x = _Math.CesiumMath.lerp(rectangle.west, rectangle.east, u / maxShort);
  105. var y = _Math.CesiumMath.lerp(rectangle.south, rectangle.north, v / maxShort);
  106. var cart = Cartesian2.Cartographic.fromRadians(x, y, 0.0, scratchBVCartographic);
  107. var decodedPosition = ellipsoid.cartographicToCartesian(cart, scratchEncodedPosition);
  108. Cartesian2.Cartesian3.pack(decodedPosition, decodedPositions, i * 3);
  109. }
  110. var countsLength = counts.length;
  111. var offsets = new Array(countsLength);
  112. var indexOffsets = new Array(countsLength);
  113. var currentOffset = 0;
  114. var currentIndexOffset = 0;
  115. for (i = 0; i < countsLength; ++i) {
  116. offsets[i] = currentOffset;
  117. indexOffsets[i] = currentIndexOffset;
  118. currentOffset += counts[i];
  119. currentIndexOffset += indexCounts[i];
  120. }
  121. var batchedPositions = new Float32Array(positionsLength * 3 * 2);
  122. var batchedIds = new Uint16Array(positionsLength * 2);
  123. var batchedIndexOffsets = new Uint32Array(indexOffsets.length);
  124. var batchedIndexCounts = new Uint32Array(indexCounts.length);
  125. var batchedIndices = [];
  126. var colorToBuffers = {};
  127. for (i = 0; i < countsLength; ++i) {
  128. rgba = batchTableColors[i];
  129. if (!defined.defined(colorToBuffers[rgba])) {
  130. colorToBuffers[rgba] = {
  131. positionLength : counts[i],
  132. indexLength : indexCounts[i],
  133. offset : 0,
  134. indexOffset : 0,
  135. batchIds : [i]
  136. };
  137. } else {
  138. colorToBuffers[rgba].positionLength += counts[i];
  139. colorToBuffers[rgba].indexLength += indexCounts[i];
  140. colorToBuffers[rgba].batchIds.push(i);
  141. }
  142. }
  143. // get the offsets and counts for the positions and indices of each primitive
  144. var buffer;
  145. var byColorPositionOffset = 0;
  146. var byColorIndexOffset = 0;
  147. for (rgba in colorToBuffers) {
  148. if (colorToBuffers.hasOwnProperty(rgba)) {
  149. buffer = colorToBuffers[rgba];
  150. buffer.offset = byColorPositionOffset;
  151. buffer.indexOffset = byColorIndexOffset;
  152. var positionLength = buffer.positionLength * 2;
  153. var indexLength = buffer.indexLength * 2 + buffer.positionLength * 6;
  154. byColorPositionOffset += positionLength;
  155. byColorIndexOffset += indexLength;
  156. buffer.indexLength = indexLength;
  157. }
  158. }
  159. var batchedDrawCalls = [];
  160. for (rgba in colorToBuffers) {
  161. if (colorToBuffers.hasOwnProperty(rgba)) {
  162. buffer = colorToBuffers[rgba];
  163. batchedDrawCalls.push({
  164. color : Color.Color.fromRgba(parseInt(rgba)),
  165. offset : buffer.indexOffset,
  166. count : buffer.indexLength,
  167. batchIds : buffer.batchIds
  168. });
  169. }
  170. }
  171. for (i = 0; i < countsLength; ++i) {
  172. rgba = batchTableColors[i];
  173. buffer = colorToBuffers[rgba];
  174. var positionOffset = buffer.offset;
  175. var positionIndex = positionOffset * 3;
  176. var batchIdIndex = positionOffset;
  177. var polygonOffset = offsets[i];
  178. var polygonCount = counts[i];
  179. var batchId = batchIds[i];
  180. var polygonMinimumHeight = minHeight;
  181. var polygonMaximumHeight = maxHeight;
  182. if (defined.defined(minimumHeights) && defined.defined(maximumHeights)) {
  183. polygonMinimumHeight = minimumHeights[i];
  184. polygonMaximumHeight = maximumHeights[i];
  185. }
  186. var minLat = Number.POSITIVE_INFINITY;
  187. var maxLat = Number.NEGATIVE_INFINITY;
  188. var minLon = Number.POSITIVE_INFINITY;
  189. var maxLon = Number.NEGATIVE_INFINITY;
  190. for (j = 0; j < polygonCount; ++j) {
  191. var position = Cartesian2.Cartesian3.unpack(decodedPositions, polygonOffset * 3 + j * 3, scratchEncodedPosition);
  192. ellipsoid.scaleToGeodeticSurface(position, position);
  193. var carto = ellipsoid.cartesianToCartographic(position, scratchBVCartographic);
  194. var lat = carto.latitude;
  195. var lon = carto.longitude;
  196. minLat = Math.min(lat, minLat);
  197. maxLat = Math.max(lat, maxLat);
  198. minLon = Math.min(lon, minLon);
  199. maxLon = Math.max(lon, maxLon);
  200. var normal = ellipsoid.geodeticSurfaceNormal(position, scratchNormal);
  201. var scaledNormal = Cartesian2.Cartesian3.multiplyByScalar(normal, polygonMinimumHeight, scratchScaledNormal);
  202. var minHeightPosition = Cartesian2.Cartesian3.add(position, scaledNormal, scratchMinHeightPosition);
  203. scaledNormal = Cartesian2.Cartesian3.multiplyByScalar(normal, polygonMaximumHeight, scaledNormal);
  204. var maxHeightPosition = Cartesian2.Cartesian3.add(position, scaledNormal, scratchMaxHeightPosition);
  205. Cartesian2.Cartesian3.subtract(maxHeightPosition, center, maxHeightPosition);
  206. Cartesian2.Cartesian3.subtract(minHeightPosition, center, minHeightPosition);
  207. Cartesian2.Cartesian3.pack(maxHeightPosition, batchedPositions, positionIndex);
  208. Cartesian2.Cartesian3.pack(minHeightPosition, batchedPositions, positionIndex + 3);
  209. batchedIds[batchIdIndex] = batchId;
  210. batchedIds[batchIdIndex + 1] = batchId;
  211. positionIndex += 6;
  212. batchIdIndex += 2;
  213. }
  214. rectangle = scratchBVRectangle;
  215. rectangle.west = minLon;
  216. rectangle.east = maxLon;
  217. rectangle.south = minLat;
  218. rectangle.north = maxLat;
  219. boundingVolumes[i] = OrientedBoundingBox.OrientedBoundingBox.fromRectangle(rectangle, minHeight, maxHeight, ellipsoid);
  220. var indicesIndex = buffer.indexOffset;
  221. var indexOffset = indexOffsets[i];
  222. var indexCount = indexCounts[i];
  223. batchedIndexOffsets[i] = indicesIndex;
  224. for (j = 0; j < indexCount; j += 3) {
  225. var i0 = indices[indexOffset + j] - polygonOffset;
  226. var i1 = indices[indexOffset + j + 1] - polygonOffset;
  227. var i2 = indices[indexOffset + j + 2] - polygonOffset;
  228. // triangle on the top of the extruded polygon
  229. batchedIndices[indicesIndex++] = i0 * 2 + positionOffset;
  230. batchedIndices[indicesIndex++] = i1 * 2 + positionOffset;
  231. batchedIndices[indicesIndex++] = i2 * 2 + positionOffset;
  232. // triangle on the bottom of the extruded polygon
  233. batchedIndices[indicesIndex++] = i2 * 2 + 1 + positionOffset;
  234. batchedIndices[indicesIndex++] = i1 * 2 + 1 + positionOffset;
  235. batchedIndices[indicesIndex++] = i0 * 2 + 1 + positionOffset;
  236. }
  237. // indices for the walls of the extruded polygon
  238. for (j = 0; j < polygonCount; ++j) {
  239. var v0 = j;
  240. var v1 = (j + 1) % polygonCount;
  241. batchedIndices[indicesIndex++] = v0 * 2 + 1 + positionOffset;
  242. batchedIndices[indicesIndex++] = v1 * 2 + positionOffset;
  243. batchedIndices[indicesIndex++] = v0 * 2 + positionOffset;
  244. batchedIndices[indicesIndex++] = v0 * 2 + 1 + positionOffset;
  245. batchedIndices[indicesIndex++] = v1 * 2 + 1 + positionOffset;
  246. batchedIndices[indicesIndex++] = v1 * 2 + positionOffset;
  247. }
  248. buffer.offset += polygonCount * 2;
  249. buffer.indexOffset = indicesIndex;
  250. batchedIndexCounts[i] = indicesIndex - batchedIndexOffsets[i];
  251. }
  252. batchedIndices = IndexDatatype.IndexDatatype.createTypedArray(batchedPositions.length / 3, batchedIndices);
  253. var batchedIndicesLength = batchedDrawCalls.length;
  254. for (var m = 0; m < batchedIndicesLength; ++m) {
  255. var tempIds = batchedDrawCalls[m].batchIds;
  256. var count = 0;
  257. var tempIdsLength = tempIds.length;
  258. for (var n = 0; n < tempIdsLength; ++n) {
  259. count += batchedIndexCounts[tempIds[n]];
  260. }
  261. batchedDrawCalls[m].count = count;
  262. }
  263. var indexDatatype = (batchedIndices.BYTES_PER_ELEMENT === 2) ? IndexDatatype.IndexDatatype.UNSIGNED_SHORT : IndexDatatype.IndexDatatype.UNSIGNED_INT;
  264. var packedBuffer = packBuffer(indexDatatype, boundingVolumes, batchedDrawCalls);
  265. transferableObjects.push(batchedPositions.buffer, batchedIndices.buffer, batchedIndexOffsets.buffer, batchedIndexCounts.buffer, batchedIds.buffer, packedBuffer.buffer);
  266. return {
  267. positions : batchedPositions.buffer,
  268. indices : batchedIndices.buffer,
  269. indexOffsets : batchedIndexOffsets.buffer,
  270. indexCounts : batchedIndexCounts.buffer,
  271. batchIds : batchedIds.buffer,
  272. packedBuffer : packedBuffer.buffer
  273. };
  274. }
  275. var createVectorTilePolygons$1 = createTaskProcessorWorker(createVectorTilePolygons);
  276. return createVectorTilePolygons$1;
  277. });