createVectorTileGeometries.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. define(['./defined-26bd4a03', './Check-da037458', './freezeObject-2d83f591', './defaultValue-f2e68450', './Math-fa6e45cb', './Cartesian2-2a723276', './defineProperties-6f7a50f2', './Transforms-65aba0a4', './RuntimeError-ad75c885', './WebGLConstants-497deb20', './ComponentDatatype-69643096', './GeometryAttribute-ed359d71', './when-ee12a2cb', './GeometryAttributes-eecc9f43', './IndexDatatype-3de60176', './createTaskProcessorWorker', './GeometryOffsetAttribute-cb30cd97', './VertexFormat-fbb91dc7', './BoxGeometry-652222c8', './CylinderGeometryLibrary-1a22ba0e', './CylinderGeometry-3515f379', './EllipsoidGeometry-592b342c', './Color-63c0bcb4'], function (defined, Check, freezeObject, defaultValue, _Math, Cartesian2, defineProperties, Transforms, RuntimeError, WebGLConstants, ComponentDatatype, GeometryAttribute, when, GeometryAttributes, IndexDatatype, createTaskProcessorWorker, GeometryOffsetAttribute, VertexFormat, BoxGeometry, CylinderGeometryLibrary, CylinderGeometry, EllipsoidGeometry, Color) { '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. var scratchCartesian = new Cartesian2.Cartesian3();
  40. var packedBoxLength = Transforms.Matrix4.packedLength + Cartesian2.Cartesian3.packedLength;
  41. var packedCylinderLength = Transforms.Matrix4.packedLength + 2;
  42. var packedEllipsoidLength = Transforms.Matrix4.packedLength + Cartesian2.Cartesian3.packedLength;
  43. var packedSphereLength = Cartesian2.Cartesian3.packedLength + 1;
  44. var scratchModelMatrixAndBV = {
  45. modelMatrix : new Transforms.Matrix4(),
  46. boundingVolume : new Transforms.BoundingSphere()
  47. };
  48. function boxModelMatrixAndBoundingVolume(boxes, index) {
  49. var boxIndex = index * packedBoxLength;
  50. var dimensions = Cartesian2.Cartesian3.unpack(boxes, boxIndex, scratchCartesian);
  51. boxIndex += Cartesian2.Cartesian3.packedLength;
  52. var boxModelMatrix = Transforms.Matrix4.unpack(boxes, boxIndex, scratchModelMatrixAndBV.modelMatrix);
  53. Transforms.Matrix4.multiplyByScale(boxModelMatrix, dimensions, boxModelMatrix);
  54. var boundingVolume = scratchModelMatrixAndBV.boundingVolume;
  55. Cartesian2.Cartesian3.clone(Cartesian2.Cartesian3.ZERO, boundingVolume.center);
  56. boundingVolume.radius = Math.sqrt(3.0);
  57. return scratchModelMatrixAndBV;
  58. }
  59. function cylinderModelMatrixAndBoundingVolume(cylinders, index) {
  60. var cylinderIndex = index * packedCylinderLength;
  61. var cylinderRadius = cylinders[cylinderIndex++];
  62. var length = cylinders[cylinderIndex++];
  63. var scale = Cartesian2.Cartesian3.fromElements(cylinderRadius, cylinderRadius, length, scratchCartesian);
  64. var cylinderModelMatrix = Transforms.Matrix4.unpack(cylinders, cylinderIndex, scratchModelMatrixAndBV.modelMatrix);
  65. Transforms.Matrix4.multiplyByScale(cylinderModelMatrix, scale, cylinderModelMatrix);
  66. var boundingVolume = scratchModelMatrixAndBV.boundingVolume;
  67. Cartesian2.Cartesian3.clone(Cartesian2.Cartesian3.ZERO, boundingVolume.center);
  68. boundingVolume.radius = Math.sqrt(2.0);
  69. return scratchModelMatrixAndBV;
  70. }
  71. function ellipsoidModelMatrixAndBoundingVolume(ellipsoids, index) {
  72. var ellipsoidIndex = index * packedEllipsoidLength;
  73. var radii = Cartesian2.Cartesian3.unpack(ellipsoids, ellipsoidIndex, scratchCartesian);
  74. ellipsoidIndex += Cartesian2.Cartesian3.packedLength;
  75. var ellipsoidModelMatrix = Transforms.Matrix4.unpack(ellipsoids, ellipsoidIndex, scratchModelMatrixAndBV.modelMatrix);
  76. Transforms.Matrix4.multiplyByScale(ellipsoidModelMatrix, radii, ellipsoidModelMatrix);
  77. var boundingVolume = scratchModelMatrixAndBV.boundingVolume;
  78. Cartesian2.Cartesian3.clone(Cartesian2.Cartesian3.ZERO, boundingVolume.center);
  79. boundingVolume.radius = 1.0;
  80. return scratchModelMatrixAndBV;
  81. }
  82. function sphereModelMatrixAndBoundingVolume(spheres, index) {
  83. var sphereIndex = index * packedSphereLength;
  84. var sphereRadius = spheres[sphereIndex++];
  85. var sphereTranslation = Cartesian2.Cartesian3.unpack(spheres, sphereIndex, scratchCartesian);
  86. var sphereModelMatrix = Transforms.Matrix4.fromTranslation(sphereTranslation, scratchModelMatrixAndBV.modelMatrix);
  87. Transforms.Matrix4.multiplyByUniformScale(sphereModelMatrix, sphereRadius, sphereModelMatrix);
  88. var boundingVolume = scratchModelMatrixAndBV.boundingVolume;
  89. Cartesian2.Cartesian3.clone(Cartesian2.Cartesian3.ZERO, boundingVolume.center);
  90. boundingVolume.radius = 1.0;
  91. return scratchModelMatrixAndBV;
  92. }
  93. var scratchPosition = new Cartesian2.Cartesian3();
  94. function createPrimitive(options, primitive, primitiveBatchIds, geometry, getModelMatrixAndBoundingVolume) {
  95. if (!defined.defined(primitive)) {
  96. return;
  97. }
  98. var numberOfPrimitives = primitiveBatchIds.length;
  99. var geometryPositions = geometry.attributes.position.values;
  100. var geometryIndices = geometry.indices;
  101. var positions = options.positions;
  102. var vertexBatchIds = options.vertexBatchIds;
  103. var indices = options.indices;
  104. var batchIds = options.batchIds;
  105. var batchTableColors = options.batchTableColors;
  106. var batchedIndices = options.batchedIndices;
  107. var indexOffsets = options.indexOffsets;
  108. var indexCounts = options.indexCounts;
  109. var boundingVolumes = options.boundingVolumes;
  110. var modelMatrix = options.modelMatrix;
  111. var center = options.center;
  112. var positionOffset = options.positionOffset;
  113. var batchIdIndex = options.batchIdIndex;
  114. var indexOffset = options.indexOffset;
  115. var batchedIndicesOffset = options.batchedIndicesOffset;
  116. for (var i = 0; i < numberOfPrimitives; ++i) {
  117. var primitiveModelMatrixAndBV = getModelMatrixAndBoundingVolume(primitive, i);
  118. var primitiveModelMatrix = primitiveModelMatrixAndBV.modelMatrix;
  119. Transforms.Matrix4.multiply(modelMatrix, primitiveModelMatrix, primitiveModelMatrix);
  120. var batchId = primitiveBatchIds[i];
  121. var positionsLength = geometryPositions.length;
  122. for (var j = 0; j < positionsLength; j += 3) {
  123. var position = Cartesian2.Cartesian3.unpack(geometryPositions, j, scratchPosition);
  124. Transforms.Matrix4.multiplyByPoint(primitiveModelMatrix, position, position);
  125. Cartesian2.Cartesian3.subtract(position, center, position);
  126. Cartesian2.Cartesian3.pack(position, positions, positionOffset * 3 + j);
  127. vertexBatchIds[batchIdIndex++] = batchId;
  128. }
  129. var indicesLength = geometryIndices.length;
  130. for (var k = 0; k < indicesLength; ++k) {
  131. indices[indexOffset + k] = geometryIndices[k] + positionOffset;
  132. }
  133. var offset = i + batchedIndicesOffset;
  134. batchedIndices[offset] = new Vector3DTileBatch({
  135. offset : indexOffset,
  136. count : indicesLength,
  137. color : Color.Color.fromRgba(batchTableColors[batchId]),
  138. batchIds : [batchId]
  139. });
  140. batchIds[offset] = batchId;
  141. indexOffsets[offset] = indexOffset;
  142. indexCounts[offset] = indicesLength;
  143. boundingVolumes[offset] = Transforms.BoundingSphere.transform(primitiveModelMatrixAndBV.boundingVolume, primitiveModelMatrix);
  144. positionOffset += positionsLength / 3;
  145. indexOffset += indicesLength;
  146. }
  147. options.positionOffset = positionOffset;
  148. options.batchIdIndex = batchIdIndex;
  149. options.indexOffset = indexOffset;
  150. options.batchedIndicesOffset += numberOfPrimitives;
  151. }
  152. var scratchCenter = new Cartesian2.Cartesian3();
  153. var scratchMatrix4 = new Transforms.Matrix4();
  154. function unpackBuffer(buffer) {
  155. var packedBuffer = new Float64Array(buffer);
  156. var offset = 0;
  157. Cartesian2.Cartesian3.unpack(packedBuffer, offset, scratchCenter);
  158. offset += Cartesian2.Cartesian3.packedLength;
  159. Transforms.Matrix4.unpack(packedBuffer, offset, scratchMatrix4);
  160. }
  161. function packedBatchedIndicesLength(batchedIndices) {
  162. var length = batchedIndices.length;
  163. var count = 0;
  164. for (var i = 0; i < length; ++i) {
  165. count += Color.Color.packedLength + 3 + batchedIndices[i].batchIds.length;
  166. }
  167. return count;
  168. }
  169. function packBuffer(indicesBytesPerElement, batchedIndices, boundingVolumes) {
  170. var numBVs = boundingVolumes.length;
  171. var length = 1 + 1 + numBVs * Transforms.BoundingSphere.packedLength + 1 + packedBatchedIndicesLength(batchedIndices);
  172. var packedBuffer = new Float64Array(length);
  173. var offset = 0;
  174. packedBuffer[offset++] = indicesBytesPerElement;
  175. packedBuffer[offset++] = numBVs;
  176. for (var i = 0; i < numBVs; ++i) {
  177. Transforms.BoundingSphere.pack(boundingVolumes[i], packedBuffer, offset);
  178. offset += Transforms.BoundingSphere.packedLength;
  179. }
  180. var indicesLength = batchedIndices.length;
  181. packedBuffer[offset++] = indicesLength;
  182. for (var j = 0; j < indicesLength; ++j) {
  183. var batchedIndex = batchedIndices[j];
  184. Color.Color.pack(batchedIndex.color, packedBuffer, offset);
  185. offset += Color.Color.packedLength;
  186. packedBuffer[offset++] = batchedIndex.offset;
  187. packedBuffer[offset++] = batchedIndex.count;
  188. var batchIds = batchedIndex.batchIds;
  189. var batchIdsLength = batchIds.length;
  190. packedBuffer[offset++] = batchIdsLength;
  191. for (var k = 0; k < batchIdsLength; ++k) {
  192. packedBuffer[offset++] = batchIds[k];
  193. }
  194. }
  195. return packedBuffer;
  196. }
  197. function createVectorTileGeometries(parameters, transferableObjects) {
  198. var boxes = defined.defined(parameters.boxes) ? new Float32Array(parameters.boxes) : undefined;
  199. var boxBatchIds = defined.defined(parameters.boxBatchIds) ? new Uint16Array(parameters.boxBatchIds) : undefined;
  200. var cylinders = defined.defined(parameters.cylinders) ? new Float32Array(parameters.cylinders) : undefined;
  201. var cylinderBatchIds = defined.defined(parameters.cylinderBatchIds) ? new Uint16Array(parameters.cylinderBatchIds) : undefined;
  202. var ellipsoids = defined.defined(parameters.ellipsoids) ? new Float32Array(parameters.ellipsoids) : undefined;
  203. var ellipsoidBatchIds = defined.defined(parameters.ellipsoidBatchIds) ? new Uint16Array(parameters.ellipsoidBatchIds) : undefined;
  204. var spheres = defined.defined(parameters.spheres) ? new Float32Array(parameters.spheres) : undefined;
  205. var sphereBatchIds = defined.defined(parameters.sphereBatchIds) ? new Uint16Array(parameters.sphereBatchIds) : undefined;
  206. var numberOfBoxes = defined.defined(boxes) ? boxBatchIds.length : 0;
  207. var numberOfCylinders = defined.defined(cylinders) ? cylinderBatchIds.length : 0;
  208. var numberOfEllipsoids = defined.defined(ellipsoids) ? ellipsoidBatchIds.length : 0;
  209. var numberOfSpheres = defined.defined(spheres) ? sphereBatchIds.length : 0;
  210. var boxGeometry = BoxGeometry.BoxGeometry.getUnitBox();
  211. var cylinderGeometry = CylinderGeometry.CylinderGeometry.getUnitCylinder();
  212. var ellipsoidGeometry = EllipsoidGeometry.EllipsoidGeometry.getUnitEllipsoid();
  213. var boxPositions = boxGeometry.attributes.position.values;
  214. var cylinderPositions = cylinderGeometry.attributes.position.values;
  215. var ellipsoidPositions = ellipsoidGeometry.attributes.position.values;
  216. var numberOfPositions = boxPositions.length * numberOfBoxes;
  217. numberOfPositions += cylinderPositions.length * numberOfCylinders;
  218. numberOfPositions += ellipsoidPositions.length * (numberOfEllipsoids + numberOfSpheres);
  219. var boxIndices = boxGeometry.indices;
  220. var cylinderIndices = cylinderGeometry.indices;
  221. var ellipsoidIndices = ellipsoidGeometry.indices;
  222. var numberOfIndices = boxIndices.length * numberOfBoxes;
  223. numberOfIndices += cylinderIndices.length * numberOfCylinders;
  224. numberOfIndices += ellipsoidIndices.length * (numberOfEllipsoids + numberOfSpheres);
  225. var positions = new Float32Array(numberOfPositions);
  226. var vertexBatchIds = new Uint16Array(numberOfPositions / 3);
  227. var indices = IndexDatatype.IndexDatatype.createTypedArray(numberOfPositions / 3, numberOfIndices);
  228. var numberOfGeometries = numberOfBoxes + numberOfCylinders + numberOfEllipsoids + numberOfSpheres;
  229. var batchIds = new Uint16Array(numberOfGeometries);
  230. var batchedIndices = new Array(numberOfGeometries);
  231. var indexOffsets = new Uint32Array(numberOfGeometries);
  232. var indexCounts = new Uint32Array(numberOfGeometries);
  233. var boundingVolumes = new Array(numberOfGeometries);
  234. unpackBuffer(parameters.packedBuffer);
  235. var options = {
  236. batchTableColors : new Uint32Array(parameters.batchTableColors),
  237. positions : positions,
  238. vertexBatchIds : vertexBatchIds,
  239. indices : indices,
  240. batchIds : batchIds,
  241. batchedIndices : batchedIndices,
  242. indexOffsets : indexOffsets,
  243. indexCounts : indexCounts,
  244. boundingVolumes : boundingVolumes,
  245. positionOffset : 0,
  246. batchIdIndex : 0,
  247. indexOffset : 0,
  248. batchedIndicesOffset : 0,
  249. modelMatrix : scratchMatrix4,
  250. center : scratchCenter
  251. };
  252. createPrimitive(options, boxes, boxBatchIds, boxGeometry, boxModelMatrixAndBoundingVolume);
  253. createPrimitive(options, cylinders, cylinderBatchIds, cylinderGeometry, cylinderModelMatrixAndBoundingVolume);
  254. createPrimitive(options, ellipsoids, ellipsoidBatchIds, ellipsoidGeometry, ellipsoidModelMatrixAndBoundingVolume);
  255. createPrimitive(options, spheres, sphereBatchIds, ellipsoidGeometry, sphereModelMatrixAndBoundingVolume);
  256. var packedBuffer = packBuffer(indices.BYTES_PER_ELEMENT, batchedIndices, boundingVolumes);
  257. transferableObjects.push(positions.buffer, vertexBatchIds.buffer, indices.buffer);
  258. transferableObjects.push(batchIds.buffer, indexOffsets.buffer, indexCounts.buffer);
  259. transferableObjects.push(packedBuffer.buffer);
  260. return {
  261. positions : positions.buffer,
  262. vertexBatchIds : vertexBatchIds.buffer,
  263. indices : indices.buffer,
  264. indexOffsets : indexOffsets.buffer,
  265. indexCounts : indexCounts.buffer,
  266. batchIds : batchIds.buffer,
  267. packedBuffer : packedBuffer.buffer
  268. };
  269. }
  270. var createVectorTileGeometries$1 = createTaskProcessorWorker(createVectorTileGeometries);
  271. return createVectorTileGeometries$1;
  272. });