createVectorTilePolygons.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. define(['./AttributeCompression-3cfab808', './Matrix2-69c32d33', './Color-d6e135b0', './defaultValue-94c3e563', './IndexDatatype-c4099fe9', './ComponentDatatype-b1ea011a', './OrientedBoundingBox-a5e39a7d', './createTaskProcessorWorker', './RuntimeError-c581ca93', './Transforms-323408fe', './_commonjsHelpers-3aae1032-f55dc0c4', './combine-761d9c3f', './WebGLConstants-7dccdc96', './EllipsoidTangentPlane-1e8d1fc2', './AxisAlignedBoundingBox-df2331b2', './IntersectionTests-d5d945ac', './Plane-069b6800'], (function (AttributeCompression, Matrix2, Color, defaultValue, IndexDatatype, ComponentDatatype, OrientedBoundingBox, createTaskProcessorWorker, RuntimeError, Transforms, _commonjsHelpers3aae1032, combine, WebGLConstants, EllipsoidTangentPlane, AxisAlignedBoundingBox, IntersectionTests, Plane) { 'use strict';
  3. const scratchCenter = new Matrix2.Cartesian3();
  4. const scratchEllipsoid = new Matrix2.Ellipsoid();
  5. const scratchRectangle = new Matrix2.Rectangle();
  6. const scratchScalars = {
  7. min: undefined,
  8. max: undefined,
  9. indexBytesPerElement: undefined,
  10. };
  11. function unpackBuffer(buffer) {
  12. const packedBuffer = new Float64Array(buffer);
  13. let offset = 0;
  14. scratchScalars.indexBytesPerElement = packedBuffer[offset++];
  15. scratchScalars.min = packedBuffer[offset++];
  16. scratchScalars.max = packedBuffer[offset++];
  17. Matrix2.Cartesian3.unpack(packedBuffer, offset, scratchCenter);
  18. offset += Matrix2.Cartesian3.packedLength;
  19. Matrix2.Ellipsoid.unpack(packedBuffer, offset, scratchEllipsoid);
  20. offset += Matrix2.Ellipsoid.packedLength;
  21. Matrix2.Rectangle.unpack(packedBuffer, offset, scratchRectangle);
  22. }
  23. function packedBatchedIndicesLength(batchedIndices) {
  24. const length = batchedIndices.length;
  25. let count = 0;
  26. for (let 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. const numBVs = boundingVolumes.length;
  33. const length =
  34. 1 +
  35. 1 +
  36. numBVs * OrientedBoundingBox.OrientedBoundingBox.packedLength +
  37. 1 +
  38. packedBatchedIndicesLength(batchedIndices);
  39. const packedBuffer = new Float64Array(length);
  40. let offset = 0;
  41. packedBuffer[offset++] = indexDatatype;
  42. packedBuffer[offset++] = numBVs;
  43. for (let i = 0; i < numBVs; ++i) {
  44. OrientedBoundingBox.OrientedBoundingBox.pack(boundingVolumes[i], packedBuffer, offset);
  45. offset += OrientedBoundingBox.OrientedBoundingBox.packedLength;
  46. }
  47. const indicesLength = batchedIndices.length;
  48. packedBuffer[offset++] = indicesLength;
  49. for (let j = 0; j < indicesLength; ++j) {
  50. const 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. const batchIds = batchedIndex.batchIds;
  56. const batchIdsLength = batchIds.length;
  57. packedBuffer[offset++] = batchIdsLength;
  58. for (let k = 0; k < batchIdsLength; ++k) {
  59. packedBuffer[offset++] = batchIds[k];
  60. }
  61. }
  62. return packedBuffer;
  63. }
  64. const maxShort = 32767;
  65. const scratchEncodedPosition = new Matrix2.Cartesian3();
  66. const scratchNormal = new Matrix2.Cartesian3();
  67. const scratchScaledNormal = new Matrix2.Cartesian3();
  68. const scratchMinHeightPosition = new Matrix2.Cartesian3();
  69. const scratchMaxHeightPosition = new Matrix2.Cartesian3();
  70. const scratchBVCartographic = new Matrix2.Cartographic();
  71. const scratchBVRectangle = new Matrix2.Rectangle();
  72. function createVectorTilePolygons(parameters, transferableObjects) {
  73. unpackBuffer(parameters.packedBuffer);
  74. let indices;
  75. const indexBytesPerElement = scratchScalars.indexBytesPerElement;
  76. if (indexBytesPerElement === 2) {
  77. indices = new Uint16Array(parameters.indices);
  78. } else {
  79. indices = new Uint32Array(parameters.indices);
  80. }
  81. const positions = new Uint16Array(parameters.positions);
  82. const counts = new Uint32Array(parameters.counts);
  83. const indexCounts = new Uint32Array(parameters.indexCounts);
  84. const batchIds = new Uint32Array(parameters.batchIds);
  85. const batchTableColors = new Uint32Array(parameters.batchTableColors);
  86. const boundingVolumes = new Array(counts.length);
  87. const center = scratchCenter;
  88. const ellipsoid = scratchEllipsoid;
  89. let rectangle = scratchRectangle;
  90. const minHeight = scratchScalars.min;
  91. const maxHeight = scratchScalars.max;
  92. let minimumHeights = parameters.minimumHeights;
  93. let maximumHeights = parameters.maximumHeights;
  94. if (defaultValue.defined(minimumHeights) && defaultValue.defined(maximumHeights)) {
  95. minimumHeights = new Float32Array(minimumHeights);
  96. maximumHeights = new Float32Array(maximumHeights);
  97. }
  98. let i;
  99. let j;
  100. let rgba;
  101. const positionsLength = positions.length / 2;
  102. const uBuffer = positions.subarray(0, positionsLength);
  103. const vBuffer = positions.subarray(positionsLength, 2 * positionsLength);
  104. AttributeCompression.AttributeCompression.zigZagDeltaDecode(uBuffer, vBuffer);
  105. const decodedPositions = new Float64Array(positionsLength * 3);
  106. for (i = 0; i < positionsLength; ++i) {
  107. const u = uBuffer[i];
  108. const v = vBuffer[i];
  109. const x = ComponentDatatype.CesiumMath.lerp(rectangle.west, rectangle.east, u / maxShort);
  110. const y = ComponentDatatype.CesiumMath.lerp(rectangle.south, rectangle.north, v / maxShort);
  111. const cart = Matrix2.Cartographic.fromRadians(x, y, 0.0, scratchBVCartographic);
  112. const decodedPosition = ellipsoid.cartographicToCartesian(
  113. cart,
  114. scratchEncodedPosition
  115. );
  116. Matrix2.Cartesian3.pack(decodedPosition, decodedPositions, i * 3);
  117. }
  118. const countsLength = counts.length;
  119. const offsets = new Array(countsLength);
  120. const indexOffsets = new Array(countsLength);
  121. let currentOffset = 0;
  122. let 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. const batchedPositions = new Float32Array(positionsLength * 3 * 2);
  130. const batchedIds = new Uint16Array(positionsLength * 2);
  131. const batchedIndexOffsets = new Uint32Array(indexOffsets.length);
  132. const batchedIndexCounts = new Uint32Array(indexCounts.length);
  133. let batchedIndices = [];
  134. const colorToBuffers = {};
  135. for (i = 0; i < countsLength; ++i) {
  136. rgba = batchTableColors[i];
  137. if (!defaultValue.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. let buffer;
  153. let byColorPositionOffset = 0;
  154. let 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. const positionLength = buffer.positionLength * 2;
  161. const indexLength = buffer.indexLength * 2 + buffer.positionLength * 6;
  162. byColorPositionOffset += positionLength;
  163. byColorIndexOffset += indexLength;
  164. buffer.indexLength = indexLength;
  165. }
  166. }
  167. const 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. const positionOffset = buffer.offset;
  183. let positionIndex = positionOffset * 3;
  184. let batchIdIndex = positionOffset;
  185. const polygonOffset = offsets[i];
  186. const polygonCount = counts[i];
  187. const batchId = batchIds[i];
  188. let polygonMinimumHeight = minHeight;
  189. let polygonMaximumHeight = maxHeight;
  190. if (defaultValue.defined(minimumHeights) && defaultValue.defined(maximumHeights)) {
  191. polygonMinimumHeight = minimumHeights[i];
  192. polygonMaximumHeight = maximumHeights[i];
  193. }
  194. let minLat = Number.POSITIVE_INFINITY;
  195. let maxLat = Number.NEGATIVE_INFINITY;
  196. let minLon = Number.POSITIVE_INFINITY;
  197. let maxLon = Number.NEGATIVE_INFINITY;
  198. for (j = 0; j < polygonCount; ++j) {
  199. const position = Matrix2.Cartesian3.unpack(
  200. decodedPositions,
  201. polygonOffset * 3 + j * 3,
  202. scratchEncodedPosition
  203. );
  204. ellipsoid.scaleToGeodeticSurface(position, position);
  205. const carto = ellipsoid.cartesianToCartographic(
  206. position,
  207. scratchBVCartographic
  208. );
  209. const lat = carto.latitude;
  210. const 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. const normal = ellipsoid.geodeticSurfaceNormal(position, scratchNormal);
  216. let scaledNormal = Matrix2.Cartesian3.multiplyByScalar(
  217. normal,
  218. polygonMinimumHeight,
  219. scratchScaledNormal
  220. );
  221. const minHeightPosition = Matrix2.Cartesian3.add(
  222. position,
  223. scaledNormal,
  224. scratchMinHeightPosition
  225. );
  226. scaledNormal = Matrix2.Cartesian3.multiplyByScalar(
  227. normal,
  228. polygonMaximumHeight,
  229. scaledNormal
  230. );
  231. const maxHeightPosition = Matrix2.Cartesian3.add(
  232. position,
  233. scaledNormal,
  234. scratchMaxHeightPosition
  235. );
  236. Matrix2.Cartesian3.subtract(maxHeightPosition, center, maxHeightPosition);
  237. Matrix2.Cartesian3.subtract(minHeightPosition, center, minHeightPosition);
  238. Matrix2.Cartesian3.pack(maxHeightPosition, batchedPositions, positionIndex);
  239. Matrix2.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. let indicesIndex = buffer.indexOffset;
  257. const indexOffset = indexOffsets[i];
  258. const indexCount = indexCounts[i];
  259. batchedIndexOffsets[i] = indicesIndex;
  260. for (j = 0; j < indexCount; j += 3) {
  261. const i0 = indices[indexOffset + j] - polygonOffset;
  262. const i1 = indices[indexOffset + j + 1] - polygonOffset;
  263. const 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. const v0 = j;
  276. const 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. const batchedIndicesLength = batchedDrawCalls.length;
  293. for (let m = 0; m < batchedIndicesLength; ++m) {
  294. const tempIds = batchedDrawCalls[m].batchIds;
  295. let count = 0;
  296. const tempIdsLength = tempIds.length;
  297. for (let n = 0; n < tempIdsLength; ++n) {
  298. count += batchedIndexCounts[tempIds[n]];
  299. }
  300. batchedDrawCalls[m].count = count;
  301. }
  302. const indexDatatype =
  303. batchedIndices.BYTES_PER_ELEMENT === 2
  304. ? IndexDatatype.IndexDatatype.UNSIGNED_SHORT
  305. : IndexDatatype.IndexDatatype.UNSIGNED_INT;
  306. const 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. }));