createVectorTileGeometries.js 15 KB

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