Vector3DTileGeometry.js 16 KB

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