createVectorTilePolygons.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /**
  2. * Cesium - https://github.com/AnalyticalGraphicsInc/cesium
  3. *
  4. * Copyright 2011-2017 Cesium Contributors
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * Columbus View (Pat. Pend.)
  19. *
  20. * Portions licensed separately.
  21. * See https://github.com/AnalyticalGraphicsInc/cesium/blob/master/LICENSE.md for full licensing details.
  22. */
  23. define(['./when-8d13db60', './Check-70bec281', './Math-61ede240', './Cartographic-fe4be337', './Cartesian2-85064f09', './BoundingSphere-775c5788', './Cartesian4-5af5bb24', './RuntimeError-ba10bc3e', './WebGLConstants-4c11ee5f', './FeatureDetection-7bd32c34', './Transforms-b2e71640', './buildModuleUrl-14bfe498', './AttributeCompression-84a90a13', './IndexDatatype-9435b55f', './IntersectionTests-397d9494', './Plane-8390418f', './createTaskProcessorWorker', './EllipsoidTangentPlane-a815c96f', './OrientedBoundingBox-635e6e10', './Color-69f1845f'], function (when, Check, _Math, Cartographic, Cartesian2, BoundingSphere, Cartesian4, RuntimeError, WebGLConstants, FeatureDetection, Transforms, buildModuleUrl, AttributeCompression, IndexDatatype, IntersectionTests, Plane, createTaskProcessorWorker, EllipsoidTangentPlane, OrientedBoundingBox, Color) { 'use strict';
  24. var scratchCenter = new Cartographic.Cartesian3();
  25. var scratchEllipsoid = new Cartesian2.Ellipsoid();
  26. var scratchRectangle = new Cartesian2.Rectangle();
  27. var scratchScalars = {
  28. min : undefined,
  29. max : undefined,
  30. indexBytesPerElement : undefined
  31. };
  32. function unpackBuffer(buffer) {
  33. var packedBuffer = new Float64Array(buffer);
  34. var offset = 0;
  35. scratchScalars.indexBytesPerElement = packedBuffer[offset++];
  36. scratchScalars.min = packedBuffer[offset++];
  37. scratchScalars.max = packedBuffer[offset++];
  38. Cartographic.Cartesian3.unpack(packedBuffer, offset, scratchCenter);
  39. offset += Cartographic.Cartesian3.packedLength;
  40. Cartesian2.Ellipsoid.unpack(packedBuffer, offset, scratchEllipsoid);
  41. offset += Cartesian2.Ellipsoid.packedLength;
  42. Cartesian2.Rectangle.unpack(packedBuffer, offset, scratchRectangle);
  43. }
  44. function packedBatchedIndicesLength(batchedIndices) {
  45. var length = batchedIndices.length;
  46. var count = 0;
  47. for (var i = 0; i < length; ++i) {
  48. count += Color.Color.packedLength + 3 + batchedIndices[i].batchIds.length;
  49. }
  50. return count;
  51. }
  52. function packBuffer(indexDatatype, boundingVolumes, batchedIndices) {
  53. var numBVs = boundingVolumes.length;
  54. var length = 1 + 1 + numBVs * OrientedBoundingBox.OrientedBoundingBox.packedLength + 1 + packedBatchedIndicesLength(batchedIndices);
  55. var packedBuffer = new Float64Array(length);
  56. var offset = 0;
  57. packedBuffer[offset++] = indexDatatype;
  58. packedBuffer[offset++] = numBVs;
  59. for (var i = 0; i < numBVs; ++i) {
  60. OrientedBoundingBox.OrientedBoundingBox.pack(boundingVolumes[i], packedBuffer, offset);
  61. offset += OrientedBoundingBox.OrientedBoundingBox.packedLength;
  62. }
  63. var indicesLength = batchedIndices.length;
  64. packedBuffer[offset++] = indicesLength;
  65. for (var j = 0; j < indicesLength; ++j) {
  66. var batchedIndex = batchedIndices[j];
  67. Color.Color.pack(batchedIndex.color, packedBuffer, offset);
  68. offset += Color.Color.packedLength;
  69. packedBuffer[offset++] = batchedIndex.offset;
  70. packedBuffer[offset++] = batchedIndex.count;
  71. var batchIds = batchedIndex.batchIds;
  72. var batchIdsLength = batchIds.length;
  73. packedBuffer[offset++] = batchIdsLength;
  74. for (var k = 0; k < batchIdsLength; ++k) {
  75. packedBuffer[offset++] = batchIds[k];
  76. }
  77. }
  78. return packedBuffer;
  79. }
  80. var maxShort = 32767;
  81. var scratchEncodedPosition = new Cartographic.Cartesian3();
  82. var scratchNormal = new Cartographic.Cartesian3();
  83. var scratchScaledNormal = new Cartographic.Cartesian3();
  84. var scratchMinHeightPosition = new Cartographic.Cartesian3();
  85. var scratchMaxHeightPosition = new Cartographic.Cartesian3();
  86. var scratchBVCartographic = new Cartographic.Cartographic();
  87. var scratchBVRectangle = new Cartesian2.Rectangle();
  88. function createVectorTilePolygons(parameters, transferableObjects) {
  89. unpackBuffer(parameters.packedBuffer);
  90. var indices;
  91. var indexBytesPerElement = scratchScalars.indexBytesPerElement;
  92. if (indexBytesPerElement === 2) {
  93. indices = new Uint16Array(parameters.indices);
  94. } else {
  95. indices = new Uint32Array(parameters.indices);
  96. }
  97. var positions = new Uint16Array(parameters.positions);
  98. var counts = new Uint32Array(parameters.counts);
  99. var indexCounts = new Uint32Array(parameters.indexCounts);
  100. var batchIds = new Uint32Array(parameters.batchIds);
  101. var batchTableColors = new Uint32Array(parameters.batchTableColors);
  102. var boundingVolumes = new Array(counts.length);
  103. var center = scratchCenter;
  104. var ellipsoid = scratchEllipsoid;
  105. var rectangle = scratchRectangle;
  106. var minHeight = scratchScalars.min;
  107. var maxHeight = scratchScalars.max;
  108. var minimumHeights = parameters.minimumHeights;
  109. var maximumHeights = parameters.maximumHeights;
  110. if (when.defined(minimumHeights) && when.defined(maximumHeights)) {
  111. minimumHeights = new Float32Array(minimumHeights);
  112. maximumHeights = new Float32Array(maximumHeights);
  113. }
  114. var i;
  115. var j;
  116. var rgba;
  117. var positionsLength = positions.length / 2;
  118. var uBuffer = positions.subarray(0, positionsLength);
  119. var vBuffer = positions.subarray(positionsLength, 2 * positionsLength);
  120. AttributeCompression.AttributeCompression.zigZagDeltaDecode(uBuffer, vBuffer);
  121. var decodedPositions = new Float32Array(positionsLength * 3);
  122. for (i = 0; i < positionsLength; ++i) {
  123. var u = uBuffer[i];
  124. var v = vBuffer[i];
  125. var x = _Math.CesiumMath.lerp(rectangle.west, rectangle.east, u / maxShort);
  126. var y = _Math.CesiumMath.lerp(rectangle.south, rectangle.north, v / maxShort);
  127. var cart = Cartographic.Cartographic.fromRadians(x, y, 0.0, scratchBVCartographic);
  128. var decodedPosition = ellipsoid.cartographicToCartesian(cart, scratchEncodedPosition);
  129. Cartographic.Cartesian3.pack(decodedPosition, decodedPositions, i * 3);
  130. }
  131. var countsLength = counts.length;
  132. var offsets = new Array(countsLength);
  133. var indexOffsets = new Array(countsLength);
  134. var currentOffset = 0;
  135. var currentIndexOffset = 0;
  136. for (i = 0; i < countsLength; ++i) {
  137. offsets[i] = currentOffset;
  138. indexOffsets[i] = currentIndexOffset;
  139. currentOffset += counts[i];
  140. currentIndexOffset += indexCounts[i];
  141. }
  142. var batchedPositions = new Float32Array(positionsLength * 3 * 2);
  143. var batchedIds = new Uint16Array(positionsLength * 2);
  144. var batchedIndexOffsets = new Uint32Array(indexOffsets.length);
  145. var batchedIndexCounts = new Uint32Array(indexCounts.length);
  146. var batchedIndices = [];
  147. var colorToBuffers = {};
  148. for (i = 0; i < countsLength; ++i) {
  149. rgba = batchTableColors[i];
  150. if (!when.defined(colorToBuffers[rgba])) {
  151. colorToBuffers[rgba] = {
  152. positionLength : counts[i],
  153. indexLength : indexCounts[i],
  154. offset : 0,
  155. indexOffset : 0,
  156. batchIds : [i]
  157. };
  158. } else {
  159. colorToBuffers[rgba].positionLength += counts[i];
  160. colorToBuffers[rgba].indexLength += indexCounts[i];
  161. colorToBuffers[rgba].batchIds.push(i);
  162. }
  163. }
  164. // get the offsets and counts for the positions and indices of each primitive
  165. var buffer;
  166. var byColorPositionOffset = 0;
  167. var byColorIndexOffset = 0;
  168. for (rgba in colorToBuffers) {
  169. if (colorToBuffers.hasOwnProperty(rgba)) {
  170. buffer = colorToBuffers[rgba];
  171. buffer.offset = byColorPositionOffset;
  172. buffer.indexOffset = byColorIndexOffset;
  173. var positionLength = buffer.positionLength * 2;
  174. var indexLength = buffer.indexLength * 2 + buffer.positionLength * 6;
  175. byColorPositionOffset += positionLength;
  176. byColorIndexOffset += indexLength;
  177. buffer.indexLength = indexLength;
  178. }
  179. }
  180. var batchedDrawCalls = [];
  181. for (rgba in colorToBuffers) {
  182. if (colorToBuffers.hasOwnProperty(rgba)) {
  183. buffer = colorToBuffers[rgba];
  184. batchedDrawCalls.push({
  185. color : Color.Color.fromRgba(parseInt(rgba)),
  186. offset : buffer.indexOffset,
  187. count : buffer.indexLength,
  188. batchIds : buffer.batchIds
  189. });
  190. }
  191. }
  192. for (i = 0; i < countsLength; ++i) {
  193. rgba = batchTableColors[i];
  194. buffer = colorToBuffers[rgba];
  195. var positionOffset = buffer.offset;
  196. var positionIndex = positionOffset * 3;
  197. var batchIdIndex = positionOffset;
  198. var polygonOffset = offsets[i];
  199. var polygonCount = counts[i];
  200. var batchId = batchIds[i];
  201. var polygonMinimumHeight = minHeight;
  202. var polygonMaximumHeight = maxHeight;
  203. if (when.defined(minimumHeights) && when.defined(maximumHeights)) {
  204. polygonMinimumHeight = minimumHeights[i];
  205. polygonMaximumHeight = maximumHeights[i];
  206. }
  207. var minLat = Number.POSITIVE_INFINITY;
  208. var maxLat = Number.NEGATIVE_INFINITY;
  209. var minLon = Number.POSITIVE_INFINITY;
  210. var maxLon = Number.NEGATIVE_INFINITY;
  211. for (j = 0; j < polygonCount; ++j) {
  212. var position = Cartographic.Cartesian3.unpack(decodedPositions, polygonOffset * 3 + j * 3, scratchEncodedPosition);
  213. ellipsoid.scaleToGeodeticSurface(position, position);
  214. var carto = ellipsoid.cartesianToCartographic(position, scratchBVCartographic);
  215. var lat = carto.latitude;
  216. var lon = carto.longitude;
  217. minLat = Math.min(lat, minLat);
  218. maxLat = Math.max(lat, maxLat);
  219. minLon = Math.min(lon, minLon);
  220. maxLon = Math.max(lon, maxLon);
  221. var normal = ellipsoid.geodeticSurfaceNormal(position, scratchNormal);
  222. var scaledNormal = Cartographic.Cartesian3.multiplyByScalar(normal, polygonMinimumHeight, scratchScaledNormal);
  223. var minHeightPosition = Cartographic.Cartesian3.add(position, scaledNormal, scratchMinHeightPosition);
  224. scaledNormal = Cartographic.Cartesian3.multiplyByScalar(normal, polygonMaximumHeight, scaledNormal);
  225. var maxHeightPosition = Cartographic.Cartesian3.add(position, scaledNormal, scratchMaxHeightPosition);
  226. Cartographic.Cartesian3.subtract(maxHeightPosition, center, maxHeightPosition);
  227. Cartographic.Cartesian3.subtract(minHeightPosition, center, minHeightPosition);
  228. Cartographic.Cartesian3.pack(maxHeightPosition, batchedPositions, positionIndex);
  229. Cartographic.Cartesian3.pack(minHeightPosition, batchedPositions, positionIndex + 3);
  230. batchedIds[batchIdIndex] = batchId;
  231. batchedIds[batchIdIndex + 1] = batchId;
  232. positionIndex += 6;
  233. batchIdIndex += 2;
  234. }
  235. rectangle = scratchBVRectangle;
  236. rectangle.west = minLon;
  237. rectangle.east = maxLon;
  238. rectangle.south = minLat;
  239. rectangle.north = maxLat;
  240. boundingVolumes[i] = OrientedBoundingBox.OrientedBoundingBox.fromRectangle(rectangle, minHeight, maxHeight, ellipsoid);
  241. var indicesIndex = buffer.indexOffset;
  242. var indexOffset = indexOffsets[i];
  243. var indexCount = indexCounts[i];
  244. batchedIndexOffsets[i] = indicesIndex;
  245. for (j = 0; j < indexCount; j += 3) {
  246. var i0 = indices[indexOffset + j] - polygonOffset;
  247. var i1 = indices[indexOffset + j + 1] - polygonOffset;
  248. var i2 = indices[indexOffset + j + 2] - polygonOffset;
  249. // triangle on the top of the extruded polygon
  250. batchedIndices[indicesIndex++] = i0 * 2 + positionOffset;
  251. batchedIndices[indicesIndex++] = i1 * 2 + positionOffset;
  252. batchedIndices[indicesIndex++] = i2 * 2 + positionOffset;
  253. // triangle on the bottom of the extruded polygon
  254. batchedIndices[indicesIndex++] = i2 * 2 + 1 + positionOffset;
  255. batchedIndices[indicesIndex++] = i1 * 2 + 1 + positionOffset;
  256. batchedIndices[indicesIndex++] = i0 * 2 + 1 + positionOffset;
  257. }
  258. // indices for the walls of the extruded polygon
  259. for (j = 0; j < polygonCount; ++j) {
  260. var v0 = j;
  261. var v1 = (j + 1) % polygonCount;
  262. batchedIndices[indicesIndex++] = v0 * 2 + 1 + positionOffset;
  263. batchedIndices[indicesIndex++] = v1 * 2 + positionOffset;
  264. batchedIndices[indicesIndex++] = v0 * 2 + positionOffset;
  265. batchedIndices[indicesIndex++] = v0 * 2 + 1 + positionOffset;
  266. batchedIndices[indicesIndex++] = v1 * 2 + 1 + positionOffset;
  267. batchedIndices[indicesIndex++] = v1 * 2 + positionOffset;
  268. }
  269. buffer.offset += polygonCount * 2;
  270. buffer.indexOffset = indicesIndex;
  271. batchedIndexCounts[i] = indicesIndex - batchedIndexOffsets[i];
  272. }
  273. batchedIndices = IndexDatatype.IndexDatatype.createTypedArray(batchedPositions.length / 3, batchedIndices);
  274. var batchedIndicesLength = batchedDrawCalls.length;
  275. for (var m = 0; m < batchedIndicesLength; ++m) {
  276. var tempIds = batchedDrawCalls[m].batchIds;
  277. var count = 0;
  278. var tempIdsLength = tempIds.length;
  279. for (var n = 0; n < tempIdsLength; ++n) {
  280. count += batchedIndexCounts[tempIds[n]];
  281. }
  282. batchedDrawCalls[m].count = count;
  283. }
  284. var indexDatatype = (batchedIndices.BYTES_PER_ELEMENT === 2) ? IndexDatatype.IndexDatatype.UNSIGNED_SHORT : IndexDatatype.IndexDatatype.UNSIGNED_INT;
  285. var packedBuffer = packBuffer(indexDatatype, boundingVolumes, batchedDrawCalls);
  286. transferableObjects.push(batchedPositions.buffer, batchedIndices.buffer, batchedIndexOffsets.buffer, batchedIndexCounts.buffer, batchedIds.buffer, packedBuffer.buffer);
  287. return {
  288. positions : batchedPositions.buffer,
  289. indices : batchedIndices.buffer,
  290. indexOffsets : batchedIndexOffsets.buffer,
  291. indexCounts : batchedIndexCounts.buffer,
  292. batchIds : batchedIds.buffer,
  293. packedBuffer : packedBuffer.buffer
  294. };
  295. }
  296. var createVectorTilePolygons$1 = createTaskProcessorWorker(createVectorTilePolygons);
  297. return createVectorTilePolygons$1;
  298. });