createVectorTilePolygons.js 14 KB

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