createVectorTilePolygons.js 14 KB

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