Newer
Older
XinYang_SanWei+RongYun / public / static / Cesium / Workers / createVectorTileGeometries.js
@raoxianxuan raoxianxuan on 21 Dec 2021 16 KB gis
  1. /**
  2. * Cesium - https://github.com/CesiumGS/cesium
  3. *
  4. * Copyright 2011-2020 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/CesiumGS/cesium/blob/master/LICENSE.md for full licensing details.
  22. */
  23. define(['./when-8d13db60', './Check-70bec281', './Math-61ede240', './Cartographic-f2a06374', './Cartesian2-16a61632', './BoundingSphere-d018a565', './Cartesian4-5af5bb24', './RuntimeError-ba10bc3e', './WebGLConstants-4c11ee5f', './ComponentDatatype-5862616f', './GeometryAttribute-1e248a71', './PrimitiveType-97893bc7', './FeatureDetection-7bd32c34', './Transforms-cd52cbaf', './buildModuleUrl-e7952659', './GeometryAttributes-aacecde6', './IndexDatatype-9435b55f', './createTaskProcessorWorker', './arrayFill-9766fb2e', './GeometryOffsetAttribute-999fc023', './VertexFormat-fe4db402', './BoxGeometry-be34f602', './CylinderGeometryLibrary-8c0fda9f', './CylinderGeometry-e26c252a', './EllipsoidGeometry-7b16faf8', './Color-69f1845f'], function (when, Check, _Math, Cartographic, Cartesian2, BoundingSphere, Cartesian4, RuntimeError, WebGLConstants, ComponentDatatype, GeometryAttribute, PrimitiveType, FeatureDetection, Transforms, buildModuleUrl, GeometryAttributes, IndexDatatype, createTaskProcessorWorker, arrayFill, GeometryOffsetAttribute, VertexFormat, BoxGeometry, CylinderGeometryLibrary, CylinderGeometry, EllipsoidGeometry, Color) { 'use strict';
  24.  
  25. /**
  26. * Describes a renderable batch of geometry.
  27. *
  28. * @alias Vector3DTileBatch
  29. * @constructor
  30. *
  31. * @param {Object} options An object with the following properties:
  32. * @param {Number} options.offset The offset of the batch into the indices buffer.
  33. * @param {Number} options.count The number of indices in the batch.
  34. * @param {Color} options.color The color of the geometry in the batch.
  35. * @param {Number[]} options.batchIds An array where each element is the batch id of the geometry in the batch.
  36. *
  37. * @private
  38. */
  39. function Vector3DTileBatch(options) {
  40. /**
  41. * The offset of the batch into the indices buffer.
  42. * @type {Number}
  43. */
  44. this.offset = options.offset;
  45. /**
  46. * The number of indices in the batch.
  47. * @type {Number}
  48. */
  49. this.count = options.count;
  50. /**
  51. * The color of the geometry in the batch.
  52. * @type {Color}
  53. */
  54. this.color = options.color;
  55. /**
  56. * An array where each element is the batch id of the geometry in the batch.
  57. * @type {Number[]}
  58. */
  59. this.batchIds = options.batchIds;
  60. }
  61.  
  62. var scratchCartesian = new Cartographic.Cartesian3();
  63.  
  64. var packedBoxLength = BoundingSphere.Matrix4.packedLength + Cartographic.Cartesian3.packedLength;
  65. var packedCylinderLength = BoundingSphere.Matrix4.packedLength + 2;
  66. var packedEllipsoidLength = BoundingSphere.Matrix4.packedLength + Cartographic.Cartesian3.packedLength;
  67. var packedSphereLength = Cartographic.Cartesian3.packedLength + 1;
  68.  
  69. var scratchModelMatrixAndBV = {
  70. modelMatrix : new BoundingSphere.Matrix4(),
  71. boundingVolume : new BoundingSphere.BoundingSphere()
  72. };
  73.  
  74. function boxModelMatrixAndBoundingVolume(boxes, index) {
  75. var boxIndex = index * packedBoxLength;
  76.  
  77. var dimensions = Cartographic.Cartesian3.unpack(boxes, boxIndex, scratchCartesian);
  78. boxIndex += Cartographic.Cartesian3.packedLength;
  79.  
  80. var boxModelMatrix = BoundingSphere.Matrix4.unpack(boxes, boxIndex, scratchModelMatrixAndBV.modelMatrix);
  81. BoundingSphere.Matrix4.multiplyByScale(boxModelMatrix, dimensions, boxModelMatrix);
  82.  
  83. var boundingVolume = scratchModelMatrixAndBV.boundingVolume;
  84. Cartographic.Cartesian3.clone(Cartographic.Cartesian3.ZERO, boundingVolume.center);
  85. boundingVolume.radius = Math.sqrt(3.0);
  86.  
  87. return scratchModelMatrixAndBV;
  88. }
  89.  
  90. function cylinderModelMatrixAndBoundingVolume(cylinders, index) {
  91. var cylinderIndex = index * packedCylinderLength;
  92.  
  93. var cylinderRadius = cylinders[cylinderIndex++];
  94. var length = cylinders[cylinderIndex++];
  95. var scale = Cartographic.Cartesian3.fromElements(cylinderRadius, cylinderRadius, length, scratchCartesian);
  96.  
  97. var cylinderModelMatrix = BoundingSphere.Matrix4.unpack(cylinders, cylinderIndex, scratchModelMatrixAndBV.modelMatrix);
  98. BoundingSphere.Matrix4.multiplyByScale(cylinderModelMatrix, scale, cylinderModelMatrix);
  99.  
  100. var boundingVolume = scratchModelMatrixAndBV.boundingVolume;
  101. Cartographic.Cartesian3.clone(Cartographic.Cartesian3.ZERO, boundingVolume.center);
  102. boundingVolume.radius = Math.sqrt(2.0);
  103.  
  104. return scratchModelMatrixAndBV;
  105. }
  106.  
  107. function ellipsoidModelMatrixAndBoundingVolume(ellipsoids, index) {
  108. var ellipsoidIndex = index * packedEllipsoidLength;
  109.  
  110. var radii = Cartographic.Cartesian3.unpack(ellipsoids, ellipsoidIndex, scratchCartesian);
  111. ellipsoidIndex += Cartographic.Cartesian3.packedLength;
  112.  
  113. var ellipsoidModelMatrix = BoundingSphere.Matrix4.unpack(ellipsoids, ellipsoidIndex, scratchModelMatrixAndBV.modelMatrix);
  114. BoundingSphere.Matrix4.multiplyByScale(ellipsoidModelMatrix, radii, ellipsoidModelMatrix);
  115.  
  116. var boundingVolume = scratchModelMatrixAndBV.boundingVolume;
  117. Cartographic.Cartesian3.clone(Cartographic.Cartesian3.ZERO, boundingVolume.center);
  118. boundingVolume.radius = 1.0;
  119.  
  120. return scratchModelMatrixAndBV;
  121. }
  122.  
  123. function sphereModelMatrixAndBoundingVolume(spheres, index) {
  124. var sphereIndex = index * packedSphereLength;
  125.  
  126. var sphereRadius = spheres[sphereIndex++];
  127.  
  128. var sphereTranslation = Cartographic.Cartesian3.unpack(spheres, sphereIndex, scratchCartesian);
  129. var sphereModelMatrix = BoundingSphere.Matrix4.fromTranslation(sphereTranslation, scratchModelMatrixAndBV.modelMatrix);
  130. BoundingSphere.Matrix4.multiplyByUniformScale(sphereModelMatrix, sphereRadius, sphereModelMatrix);
  131.  
  132. var boundingVolume = scratchModelMatrixAndBV.boundingVolume;
  133. Cartographic.Cartesian3.clone(Cartographic.Cartesian3.ZERO, boundingVolume.center);
  134. boundingVolume.radius = 1.0;
  135.  
  136. return scratchModelMatrixAndBV;
  137. }
  138.  
  139. var scratchPosition = new Cartographic.Cartesian3();
  140.  
  141. function createPrimitive(options, primitive, primitiveBatchIds, geometry, getModelMatrixAndBoundingVolume) {
  142. if (!when.defined(primitive)) {
  143. return;
  144. }
  145.  
  146. var numberOfPrimitives = primitiveBatchIds.length;
  147. var geometryPositions = geometry.attributes.position.values;
  148. var geometryIndices = geometry.indices;
  149.  
  150. var positions = options.positions;
  151. var vertexBatchIds = options.vertexBatchIds;
  152. var indices = options.indices;
  153.  
  154. var batchIds = options.batchIds;
  155. var batchTableColors = options.batchTableColors;
  156. var batchedIndices = options.batchedIndices;
  157. var indexOffsets = options.indexOffsets;
  158. var indexCounts = options.indexCounts;
  159. var boundingVolumes = options.boundingVolumes;
  160.  
  161. var modelMatrix = options.modelMatrix;
  162. var center = options.center;
  163.  
  164. var positionOffset = options.positionOffset;
  165. var batchIdIndex = options.batchIdIndex;
  166. var indexOffset = options.indexOffset;
  167. var batchedIndicesOffset = options.batchedIndicesOffset;
  168.  
  169. for (var i = 0; i < numberOfPrimitives; ++i) {
  170. var primitiveModelMatrixAndBV = getModelMatrixAndBoundingVolume(primitive, i);
  171. var primitiveModelMatrix = primitiveModelMatrixAndBV.modelMatrix;
  172. BoundingSphere.Matrix4.multiply(modelMatrix, primitiveModelMatrix, primitiveModelMatrix);
  173.  
  174. var batchId = primitiveBatchIds[i];
  175.  
  176. var positionsLength = geometryPositions.length;
  177. for (var j = 0; j < positionsLength; j += 3) {
  178. var position = Cartographic.Cartesian3.unpack(geometryPositions, j, scratchPosition);
  179. BoundingSphere.Matrix4.multiplyByPoint(primitiveModelMatrix, position, position);
  180. Cartographic.Cartesian3.subtract(position, center, position);
  181.  
  182. Cartographic.Cartesian3.pack(position, positions, positionOffset * 3 + j);
  183. vertexBatchIds[batchIdIndex++] = batchId;
  184. }
  185.  
  186. var indicesLength = geometryIndices.length;
  187. for (var k = 0; k < indicesLength; ++k) {
  188. indices[indexOffset + k] = geometryIndices[k] + positionOffset;
  189. }
  190.  
  191. var offset = i + batchedIndicesOffset;
  192. batchedIndices[offset] = new Vector3DTileBatch({
  193. offset : indexOffset,
  194. count : indicesLength,
  195. color : Color.Color.fromRgba(batchTableColors[batchId]),
  196. batchIds : [batchId]
  197. });
  198. batchIds[offset] = batchId;
  199. indexOffsets[offset] = indexOffset;
  200. indexCounts[offset] = indicesLength;
  201. boundingVolumes[offset] = BoundingSphere.BoundingSphere.transform(primitiveModelMatrixAndBV.boundingVolume, primitiveModelMatrix);
  202.  
  203. positionOffset += positionsLength / 3;
  204. indexOffset += indicesLength;
  205. }
  206.  
  207. options.positionOffset = positionOffset;
  208. options.batchIdIndex = batchIdIndex;
  209. options.indexOffset = indexOffset;
  210. options.batchedIndicesOffset += numberOfPrimitives;
  211. }
  212.  
  213. var scratchCenter = new Cartographic.Cartesian3();
  214. var scratchMatrix4 = new BoundingSphere.Matrix4();
  215.  
  216. function unpackBuffer(buffer) {
  217. var packedBuffer = new Float64Array(buffer);
  218.  
  219. var offset = 0;
  220. Cartographic.Cartesian3.unpack(packedBuffer, offset, scratchCenter);
  221. offset += Cartographic.Cartesian3.packedLength;
  222.  
  223. BoundingSphere.Matrix4.unpack(packedBuffer, offset, scratchMatrix4);
  224. }
  225.  
  226. function packedBatchedIndicesLength(batchedIndices) {
  227. var length = batchedIndices.length;
  228. var count = 0;
  229. for (var i = 0; i < length; ++i) {
  230. count += Color.Color.packedLength + 3 + batchedIndices[i].batchIds.length;
  231. }
  232. return count;
  233. }
  234.  
  235. function packBuffer(indicesBytesPerElement, batchedIndices, boundingVolumes) {
  236. var numBVs = boundingVolumes.length;
  237. var length = 1 + 1 + numBVs * BoundingSphere.BoundingSphere.packedLength + 1 + packedBatchedIndicesLength(batchedIndices);
  238.  
  239. var packedBuffer = new Float64Array(length);
  240.  
  241. var offset = 0;
  242. packedBuffer[offset++] = indicesBytesPerElement;
  243. packedBuffer[offset++] = numBVs;
  244.  
  245. for (var i = 0; i < numBVs; ++i) {
  246. BoundingSphere.BoundingSphere.pack(boundingVolumes[i], packedBuffer, offset);
  247. offset += BoundingSphere.BoundingSphere.packedLength;
  248. }
  249.  
  250. var indicesLength = batchedIndices.length;
  251. packedBuffer[offset++] = indicesLength;
  252.  
  253. for (var j = 0; j < indicesLength; ++j) {
  254. var batchedIndex = batchedIndices[j];
  255.  
  256. Color.Color.pack(batchedIndex.color, packedBuffer, offset);
  257. offset += Color.Color.packedLength;
  258.  
  259. packedBuffer[offset++] = batchedIndex.offset;
  260. packedBuffer[offset++] = batchedIndex.count;
  261.  
  262. var batchIds = batchedIndex.batchIds;
  263. var batchIdsLength = batchIds.length;
  264. packedBuffer[offset++] = batchIdsLength;
  265.  
  266. for (var k = 0; k < batchIdsLength; ++k) {
  267. packedBuffer[offset++] = batchIds[k];
  268. }
  269. }
  270.  
  271. return packedBuffer;
  272. }
  273.  
  274. function createVectorTileGeometries(parameters, transferableObjects) {
  275. var boxes = when.defined(parameters.boxes) ? new Float32Array(parameters.boxes) : undefined;
  276. var boxBatchIds = when.defined(parameters.boxBatchIds) ? new Uint16Array(parameters.boxBatchIds) : undefined;
  277. var cylinders = when.defined(parameters.cylinders) ? new Float32Array(parameters.cylinders) : undefined;
  278. var cylinderBatchIds = when.defined(parameters.cylinderBatchIds) ? new Uint16Array(parameters.cylinderBatchIds) : undefined;
  279. var ellipsoids = when.defined(parameters.ellipsoids) ? new Float32Array(parameters.ellipsoids) : undefined;
  280. var ellipsoidBatchIds = when.defined(parameters.ellipsoidBatchIds) ? new Uint16Array(parameters.ellipsoidBatchIds) : undefined;
  281. var spheres = when.defined(parameters.spheres) ? new Float32Array(parameters.spheres) : undefined;
  282. var sphereBatchIds = when.defined(parameters.sphereBatchIds) ? new Uint16Array(parameters.sphereBatchIds) : undefined;
  283.  
  284. var numberOfBoxes = when.defined(boxes) ? boxBatchIds.length : 0;
  285. var numberOfCylinders = when.defined(cylinders) ? cylinderBatchIds.length : 0;
  286. var numberOfEllipsoids = when.defined(ellipsoids) ? ellipsoidBatchIds.length : 0;
  287. var numberOfSpheres = when.defined(spheres) ? sphereBatchIds.length : 0;
  288.  
  289. var boxGeometry = BoxGeometry.BoxGeometry.getUnitBox();
  290. var cylinderGeometry = CylinderGeometry.CylinderGeometry.getUnitCylinder();
  291. var ellipsoidGeometry = EllipsoidGeometry.EllipsoidGeometry.getUnitEllipsoid();
  292.  
  293. var boxPositions = boxGeometry.attributes.position.values;
  294. var cylinderPositions = cylinderGeometry.attributes.position.values;
  295. var ellipsoidPositions = ellipsoidGeometry.attributes.position.values;
  296.  
  297. var numberOfPositions = boxPositions.length * numberOfBoxes;
  298. numberOfPositions += cylinderPositions.length * numberOfCylinders;
  299. numberOfPositions += ellipsoidPositions.length * (numberOfEllipsoids + numberOfSpheres);
  300.  
  301. var boxIndices = boxGeometry.indices;
  302. var cylinderIndices = cylinderGeometry.indices;
  303. var ellipsoidIndices = ellipsoidGeometry.indices;
  304.  
  305. var numberOfIndices = boxIndices.length * numberOfBoxes;
  306. numberOfIndices += cylinderIndices.length * numberOfCylinders;
  307. numberOfIndices += ellipsoidIndices.length * (numberOfEllipsoids + numberOfSpheres);
  308.  
  309. var positions = new Float32Array(numberOfPositions);
  310. var vertexBatchIds = new Uint16Array(numberOfPositions / 3);
  311. var indices = IndexDatatype.IndexDatatype.createTypedArray(numberOfPositions / 3, numberOfIndices);
  312.  
  313. var numberOfGeometries = numberOfBoxes + numberOfCylinders + numberOfEllipsoids + numberOfSpheres;
  314. var batchIds = new Uint16Array(numberOfGeometries);
  315. var batchedIndices = new Array(numberOfGeometries);
  316. var indexOffsets = new Uint32Array(numberOfGeometries);
  317. var indexCounts = new Uint32Array(numberOfGeometries);
  318. var boundingVolumes = new Array(numberOfGeometries);
  319.  
  320. unpackBuffer(parameters.packedBuffer);
  321.  
  322. var options = {
  323. batchTableColors : new Uint32Array(parameters.batchTableColors),
  324. positions : positions,
  325. vertexBatchIds : vertexBatchIds,
  326. indices : indices,
  327. batchIds : batchIds,
  328. batchedIndices : batchedIndices,
  329. indexOffsets : indexOffsets,
  330. indexCounts : indexCounts,
  331. boundingVolumes : boundingVolumes,
  332. positionOffset : 0,
  333. batchIdIndex : 0,
  334. indexOffset : 0,
  335. batchedIndicesOffset : 0,
  336. modelMatrix : scratchMatrix4,
  337. center : scratchCenter
  338. };
  339.  
  340. createPrimitive(options, boxes, boxBatchIds, boxGeometry, boxModelMatrixAndBoundingVolume);
  341. createPrimitive(options, cylinders, cylinderBatchIds, cylinderGeometry, cylinderModelMatrixAndBoundingVolume);
  342. createPrimitive(options, ellipsoids, ellipsoidBatchIds, ellipsoidGeometry, ellipsoidModelMatrixAndBoundingVolume);
  343. createPrimitive(options, spheres, sphereBatchIds, ellipsoidGeometry, sphereModelMatrixAndBoundingVolume);
  344.  
  345. var packedBuffer = packBuffer(indices.BYTES_PER_ELEMENT, batchedIndices, boundingVolumes);
  346. transferableObjects.push(positions.buffer, vertexBatchIds.buffer, indices.buffer);
  347. transferableObjects.push(batchIds.buffer, indexOffsets.buffer, indexCounts.buffer);
  348. transferableObjects.push(packedBuffer.buffer);
  349.  
  350. return {
  351. positions : positions.buffer,
  352. vertexBatchIds : vertexBatchIds.buffer,
  353. indices : indices.buffer,
  354. indexOffsets : indexOffsets.buffer,
  355. indexCounts : indexCounts.buffer,
  356. batchIds : batchIds.buffer,
  357. packedBuffer : packedBuffer.buffer
  358. };
  359. }
  360. var createVectorTileGeometries$1 = createTaskProcessorWorker(createVectorTileGeometries);
  361.  
  362. return createVectorTileGeometries$1;
  363.  
  364. });