Vector3DTileGeometry.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. import arraySlice from '../Core/arraySlice.js';
  2. import BoundingSphere from '../Core/BoundingSphere.js';
  3. import Cartesian3 from '../Core/Cartesian3.js';
  4. import Color from '../Core/Color.js';
  5. import defaultValue from '../Core/defaultValue.js';
  6. import defined from '../Core/defined.js';
  7. import defineProperties from '../Core/defineProperties.js';
  8. import destroyObject from '../Core/destroyObject.js';
  9. import Matrix4 from '../Core/Matrix4.js';
  10. import TaskProcessor from '../Core/TaskProcessor.js';
  11. import when from '../ThirdParty/when.js';
  12. import ClassificationType from './ClassificationType.js';
  13. import Vector3DTileBatch from './Vector3DTileBatch.js';
  14. import Vector3DTilePrimitive from './Vector3DTilePrimitive.js';
  15. /**
  16. * Creates a batch of box, cylinder, ellipsoid and/or sphere geometries intersecting terrain or 3D Tiles.
  17. *
  18. * @alias Vector3DTileGeometry
  19. * @constructor
  20. *
  21. * @param {Object} options An object with following properties:
  22. * @param {Float32Array} [options.boxes] The boxes in the tile.
  23. * @param {Uint16Array} [options.boxBatchIds] The batch ids for each box.
  24. * @param {Float32Array} [options.cylinders] The cylinders in the tile.
  25. * @param {Uint16Array} [options.cylinderBatchIds] The batch ids for each cylinder.
  26. * @param {Float32Array} [options.ellipsoids] The ellipsoids in the tile.
  27. * @param {Uint16Array} [options.ellipsoidBatchIds] The batch ids for each ellipsoid.
  28. * @param {Float32Array} [options.spheres] The spheres in the tile.
  29. * @param {Uint16Array} [options.sphereBatchIds] The batch ids for each sphere.
  30. * @param {Cartesian3} options.center The RTC center of all geometries.
  31. * @param {Matrix4} options.modelMatrix The model matrix of all geometries. Applied after the individual geometry model matrices.
  32. * @param {Cesium3DTileBatchTable} options.batchTable The batch table.
  33. * @param {BoundingSphere} options.boundingVolume The bounding volume containing all of the geometry in the tile.
  34. *
  35. * @private
  36. */
  37. function Vector3DTileGeometry(options) {
  38. // these will all be released after the primitive is created
  39. this._boxes = options.boxes;
  40. this._boxBatchIds = options.boxBatchIds;
  41. this._cylinders = options.cylinders;
  42. this._cylinderBatchIds = options.cylinderBatchIds;
  43. this._ellipsoids = options.ellipsoids;
  44. this._ellipsoidBatchIds = options.ellipsoidBatchIds;
  45. this._spheres = options.spheres;
  46. this._sphereBatchIds = options.sphereBatchIds;
  47. this._modelMatrix = options.modelMatrix;
  48. this._batchTable = options.batchTable;
  49. this._boundingVolume = options.boundingVolume;
  50. this._center = options.center;
  51. if (!defined(this._center)) {
  52. if (defined(this._boundingVolume)) {
  53. this._center = Cartesian3.clone(this._boundingVolume.center);
  54. } else {
  55. this._center = Cartesian3.clone(Cartesian3.ZERO);
  56. }
  57. }
  58. this._boundingVolumes = undefined;
  59. this._batchedIndices = undefined;
  60. this._indices = undefined;
  61. this._indexOffsets = undefined;
  62. this._indexCounts = undefined;
  63. this._positions = undefined;
  64. this._vertexBatchIds = undefined;
  65. this._batchIds = undefined;
  66. this._batchTableColors = undefined;
  67. this._packedBuffer = undefined;
  68. this._ready = false;
  69. this._readyPromise = when.defer();
  70. this._verticesPromise = undefined;
  71. this._primitive = undefined;
  72. /**
  73. * Draws the wireframe of the classification geometries.
  74. * @type {Boolean}
  75. * @default false
  76. */
  77. this.debugWireframe = false;
  78. /**
  79. * Forces a re-batch instead of waiting after a number of frames have been rendered. For testing only.
  80. * @type {Boolean}
  81. * @default false
  82. */
  83. this.forceRebatch = false;
  84. /**
  85. * What this tile will classify.
  86. * @type {ClassificationType}
  87. * @default ClassificationType.BOTH
  88. */
  89. this.classificationType = ClassificationType.BOTH;
  90. }
  91. defineProperties(Vector3DTileGeometry.prototype, {
  92. /**
  93. * Gets the number of triangles.
  94. *
  95. * @memberof Vector3DTileGeometry.prototype
  96. *
  97. * @type {Number}
  98. * @readonly
  99. */
  100. trianglesLength : {
  101. get : function() {
  102. if (defined(this._primitive)) {
  103. return this._primitive.trianglesLength;
  104. }
  105. return 0;
  106. }
  107. },
  108. /**
  109. * Gets the geometry memory in bytes.
  110. *
  111. * @memberof Vector3DTileGeometry.prototype
  112. *
  113. * @type {Number}
  114. * @readonly
  115. */
  116. geometryByteLength : {
  117. get : function() {
  118. if (defined(this._primitive)) {
  119. return this._primitive.geometryByteLength;
  120. }
  121. return 0;
  122. }
  123. },
  124. /**
  125. * Gets a promise that resolves when the primitive is ready to render.
  126. * @memberof Vector3DTileGeometry.prototype
  127. * @type {Promise}
  128. * @readonly
  129. */
  130. readyPromise : {
  131. get : function() {
  132. return this._readyPromise.promise;
  133. }
  134. }
  135. });
  136. Vector3DTileGeometry.packedBoxLength = Matrix4.packedLength + Cartesian3.packedLength;
  137. Vector3DTileGeometry.packedCylinderLength = Matrix4.packedLength + 2;
  138. Vector3DTileGeometry.packedEllipsoidLength = Matrix4.packedLength + Cartesian3.packedLength;
  139. Vector3DTileGeometry.packedSphereLength = Cartesian3.packedLength + 1;
  140. function packBuffer(geometries) {
  141. var packedBuffer = new Float64Array(Matrix4.packedLength + Cartesian3.packedLength);
  142. var offset = 0;
  143. Cartesian3.pack(geometries._center, packedBuffer, offset);
  144. offset += Cartesian3.packedLength;
  145. Matrix4.pack(geometries._modelMatrix, packedBuffer, offset);
  146. return packedBuffer;
  147. }
  148. function unpackBuffer(geometries, packedBuffer) {
  149. var offset = 0;
  150. var indicesBytesPerElement = packedBuffer[offset++];
  151. var numBVS = packedBuffer[offset++];
  152. var bvs = geometries._boundingVolumes = new Array(numBVS);
  153. for (var i = 0; i < numBVS; ++i) {
  154. bvs[i] = BoundingSphere.unpack(packedBuffer, offset);
  155. offset += BoundingSphere.packedLength;
  156. }
  157. var numBatchedIndices = packedBuffer[offset++];
  158. var bis = geometries._batchedIndices = new Array(numBatchedIndices);
  159. for (var j = 0; j < numBatchedIndices; ++j) {
  160. var color = Color.unpack(packedBuffer, offset);
  161. offset += Color.packedLength;
  162. var indexOffset = packedBuffer[offset++];
  163. var count = packedBuffer[offset++];
  164. var length = packedBuffer[offset++];
  165. var batchIds = new Array(length);
  166. for (var k = 0; k < length; ++k) {
  167. batchIds[k] = packedBuffer[offset++];
  168. }
  169. bis[j] = new Vector3DTileBatch({
  170. color : color,
  171. offset : indexOffset,
  172. count : count,
  173. batchIds : batchIds
  174. });
  175. }
  176. return indicesBytesPerElement;
  177. }
  178. var createVerticesTaskProcessor = new TaskProcessor('createVectorTileGeometries');
  179. var scratchColor = new Color();
  180. function createPrimitive(geometries) {
  181. if (defined(geometries._primitive)) {
  182. return;
  183. }
  184. if (!defined(geometries._verticesPromise)) {
  185. var boxes = geometries._boxes;
  186. var boxBatchIds = geometries._boxBatchIds;
  187. var cylinders = geometries._cylinders;
  188. var cylinderBatchIds = geometries._cylinderBatchIds;
  189. var ellipsoids = geometries._ellipsoids;
  190. var ellipsoidBatchIds = geometries._ellipsoidBatchIds;
  191. var spheres = geometries._spheres;
  192. var sphereBatchIds = geometries._sphereBatchIds;
  193. var batchTableColors = geometries._batchTableColors;
  194. var packedBuffer = geometries._packedBuffer;
  195. if (!defined(batchTableColors)) {
  196. // Copy because they may be the views on the same buffer.
  197. var length = 0;
  198. if (defined(geometries._boxes)) {
  199. boxes = geometries._boxes = arraySlice(boxes);
  200. boxBatchIds = geometries._boxBatchIds = arraySlice(boxBatchIds);
  201. length += boxBatchIds.length;
  202. }
  203. if (defined(geometries._cylinders)) {
  204. cylinders = geometries._cylinders = arraySlice(cylinders);
  205. cylinderBatchIds = geometries._cylinderBatchIds = arraySlice(cylinderBatchIds);
  206. length += cylinderBatchIds.length;
  207. }
  208. if (defined(geometries._ellipsoids)) {
  209. ellipsoids = geometries._ellipsoids = arraySlice(ellipsoids);
  210. ellipsoidBatchIds = geometries._ellipsoidBatchIds = arraySlice(ellipsoidBatchIds);
  211. length += ellipsoidBatchIds.length;
  212. }
  213. if (defined(geometries._spheres)) {
  214. spheres = geometries._sphere = arraySlice(spheres);
  215. sphereBatchIds = geometries._sphereBatchIds = arraySlice(sphereBatchIds);
  216. length += sphereBatchIds.length;
  217. }
  218. batchTableColors = geometries._batchTableColors = new Uint32Array(length);
  219. var batchTable = geometries._batchTable;
  220. for (var i = 0; i < length; ++i) {
  221. var color = batchTable.getColor(i, scratchColor);
  222. batchTableColors[i] = color.toRgba();
  223. }
  224. packedBuffer = geometries._packedBuffer = packBuffer(geometries);
  225. }
  226. var transferrableObjects = [];
  227. if (defined(boxes)) {
  228. transferrableObjects.push(boxes.buffer, boxBatchIds.buffer);
  229. }
  230. if (defined(cylinders)) {
  231. transferrableObjects.push(cylinders.buffer, cylinderBatchIds.buffer);
  232. }
  233. if (defined(ellipsoids)) {
  234. transferrableObjects.push(ellipsoids.buffer, ellipsoidBatchIds.buffer);
  235. }
  236. if (defined(spheres)) {
  237. transferrableObjects.push(spheres.buffer, sphereBatchIds.buffer);
  238. }
  239. transferrableObjects.push(batchTableColors.buffer, packedBuffer.buffer);
  240. var parameters = {
  241. boxes : defined(boxes) ? boxes.buffer : undefined,
  242. boxBatchIds : defined(boxes) ? boxBatchIds.buffer : undefined,
  243. cylinders : defined(cylinders) ? cylinders.buffer : undefined,
  244. cylinderBatchIds : defined(cylinders) ? cylinderBatchIds.buffer : undefined,
  245. ellipsoids : defined(ellipsoids) ? ellipsoids.buffer : undefined,
  246. ellipsoidBatchIds : defined(ellipsoids) ? ellipsoidBatchIds.buffer : undefined,
  247. spheres : defined(spheres) ? spheres.buffer : undefined,
  248. sphereBatchIds : defined(spheres) ? sphereBatchIds.buffer : undefined,
  249. batchTableColors : batchTableColors.buffer,
  250. packedBuffer : packedBuffer.buffer
  251. };
  252. var verticesPromise = geometries._verticesPromise = createVerticesTaskProcessor.scheduleTask(parameters, transferrableObjects);
  253. if (!defined(verticesPromise)) {
  254. // Postponed
  255. return;
  256. }
  257. verticesPromise.then(function(result) {
  258. var packedBuffer = new Float64Array(result.packedBuffer);
  259. var indicesBytesPerElement = unpackBuffer(geometries, packedBuffer);
  260. if (indicesBytesPerElement === 2) {
  261. geometries._indices = new Uint16Array(result.indices);
  262. } else {
  263. geometries._indices = new Uint32Array(result.indices);
  264. }
  265. geometries._indexOffsets = new Uint32Array(result.indexOffsets);
  266. geometries._indexCounts = new Uint32Array(result.indexCounts);
  267. geometries._positions = new Float32Array(result.positions);
  268. geometries._vertexBatchIds = new Uint16Array(result.vertexBatchIds);
  269. geometries._batchIds = new Uint16Array(result.batchIds);
  270. geometries._ready = true;
  271. });
  272. }
  273. if (geometries._ready && !defined(geometries._primitive)) {
  274. geometries._primitive = new Vector3DTilePrimitive({
  275. batchTable : geometries._batchTable,
  276. positions : geometries._positions,
  277. batchIds : geometries._batchIds,
  278. vertexBatchIds : geometries._vertexBatchIds,
  279. indices : geometries._indices,
  280. indexOffsets : geometries._indexOffsets,
  281. indexCounts : geometries._indexCounts,
  282. batchedIndices : geometries._batchedIndices,
  283. boundingVolume : geometries._boundingVolume,
  284. boundingVolumes : geometries._boundingVolumes,
  285. center : geometries._center,
  286. pickObject : defaultValue(geometries._pickObject, geometries)
  287. });
  288. geometries._boxes = undefined;
  289. geometries._boxBatchIds = undefined;
  290. geometries._cylinders = undefined;
  291. geometries._cylinderBatchIds = undefined;
  292. geometries._ellipsoids = undefined;
  293. geometries._ellipsoidBatchIds = undefined;
  294. geometries._spheres = undefined;
  295. geometries._sphereBatchIds = undefined;
  296. geometries._center = undefined;
  297. geometries._modelMatrix = undefined;
  298. geometries._batchTable = undefined;
  299. geometries._boundingVolume = undefined;
  300. geometries._boundingVolumes = undefined;
  301. geometries._batchedIndices = undefined;
  302. geometries._indices = undefined;
  303. geometries._indexOffsets = undefined;
  304. geometries._indexCounts = undefined;
  305. geometries._positions = undefined;
  306. geometries._vertexBatchIds = undefined;
  307. geometries._batchIds = undefined;
  308. geometries._batchTableColors = undefined;
  309. geometries._packedBuffer = undefined;
  310. geometries._verticesPromise = undefined;
  311. geometries._readyPromise.resolve();
  312. }
  313. }
  314. /**
  315. * Creates features for each geometry and places it at the batch id index of features.
  316. *
  317. * @param {Vector3DTileContent} content The vector tile content.
  318. * @param {Cesium3DTileFeature[]} features An array of features where the polygon features will be placed.
  319. */
  320. Vector3DTileGeometry.prototype.createFeatures = function(content, features) {
  321. this._primitive.createFeatures(content, features);
  322. };
  323. /**
  324. * Colors the entire tile when enabled is true. The resulting color will be (geometry batch table color * color).
  325. *
  326. * @param {Boolean} enabled Whether to enable debug coloring.
  327. * @param {Color} color The debug color.
  328. */
  329. Vector3DTileGeometry.prototype.applyDebugSettings = function(enabled, color) {
  330. this._primitive.applyDebugSettings(enabled, color);
  331. };
  332. /**
  333. * Apply a style to the content.
  334. *
  335. * @param {Cesium3DTileStyle} style The style.
  336. * @param {Cesium3DTileFeature[]} features The array of features.
  337. */
  338. Vector3DTileGeometry.prototype.applyStyle = function(style, features) {
  339. this._primitive.applyStyle(style, features);
  340. };
  341. /**
  342. * Call when updating the color of a geometry with batchId changes color. The geometries will need to be re-batched
  343. * on the next update.
  344. *
  345. * @param {Number} batchId The batch id of the geometries whose color has changed.
  346. * @param {Color} color The new polygon color.
  347. */
  348. Vector3DTileGeometry.prototype.updateCommands = function(batchId, color) {
  349. this._primitive.updateCommands(batchId, color);
  350. };
  351. /**
  352. * Updates the batches and queues the commands for rendering.
  353. *
  354. * @param {FrameState} frameState The current frame state.
  355. */
  356. Vector3DTileGeometry.prototype.update = function(frameState) {
  357. createPrimitive(this);
  358. if (!this._ready) {
  359. return;
  360. }
  361. this._primitive.debugWireframe = this.debugWireframe;
  362. this._primitive.forceRebatch = this.forceRebatch;
  363. this._primitive.classificationType = this.classificationType;
  364. this._primitive.update(frameState);
  365. };
  366. /**
  367. * Returns true if this object was destroyed; otherwise, false.
  368. * <p>
  369. * If this object was destroyed, it should not be used; calling any function other than
  370. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
  371. * </p>
  372. *
  373. * @returns {Boolean} <code>true</code> if this object was destroyed; otherwise, <code>false</code>.
  374. */
  375. Vector3DTileGeometry.prototype.isDestroyed = function() {
  376. return false;
  377. };
  378. /**
  379. * Destroys the WebGL resources held by this object. Destroying an object allows for deterministic
  380. * release of WebGL resources, instead of relying on the garbage collector to destroy this object.
  381. * <p>
  382. * Once an object is destroyed, it should not be used; calling any function other than
  383. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  384. * assign the return value (<code>undefined</code>) to the object as done in the example.
  385. * </p>
  386. *
  387. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  388. */
  389. Vector3DTileGeometry.prototype.destroy = function() {
  390. this._primitive = this._primitive && this._primitive.destroy();
  391. return destroyObject(this);
  392. };
  393. export default Vector3DTileGeometry;