Vector3DTileGeometry.js 15 KB

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