Vector3DTilePolygons.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. import Cartesian3 from "../Core/Cartesian3.js";
  2. import Color from "../Core/Color.js";
  3. import defaultValue from "../Core/defaultValue.js";
  4. import defined from "../Core/defined.js";
  5. import destroyObject from "../Core/destroyObject.js";
  6. import Ellipsoid from "../Core/Ellipsoid.js";
  7. import IndexDatatype from "../Core/IndexDatatype.js";
  8. import OrientedBoundingBox from "../Core/OrientedBoundingBox.js";
  9. import Rectangle from "../Core/Rectangle.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 pre-triangulated polygons draped on terrain and/or 3D Tiles.
  16. *
  17. * @alias Vector3DTilePolygons
  18. * @constructor
  19. *
  20. * @param {object} options An object with following properties:
  21. * @param {Float32Array|Uint16Array} options.positions The positions of the polygons. The positions must be contiguous
  22. * so that the positions for polygon n are in [c, c + counts[n]] where c = sum{counts[0], counts[n - 1]} and they are the outer ring of
  23. * the polygon in counter-clockwise order.
  24. * @param {Uint32Array} options.counts The number of positions in the each polygon.
  25. * @param {Uint32Array} options.indices The indices of the triangulated polygons. The indices must be contiguous so that
  26. * the indices for polygon n are in [i, i + indexCounts[n]] where i = sum{indexCounts[0], indexCounts[n - 1]}.
  27. * @param {Uint32Array} options.indexCounts The number of indices for each polygon.
  28. * @param {number} options.minimumHeight The minimum height of the terrain covered by the tile.
  29. * @param {number} options.maximumHeight The maximum height of the terrain covered by the tile.
  30. * @param {Float32Array} [options.polygonMinimumHeights] An array containing the minimum heights for each polygon.
  31. * @param {Float32Array} [options.polygonMaximumHeights] An array containing the maximum heights for each polygon.
  32. * @param {Rectangle} options.rectangle The rectangle containing the tile.
  33. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid.
  34. * @param {Cartesian3} [options.center=Cartesian3.ZERO] The RTC center.
  35. * @param {Cesium3DTileBatchTable} options.batchTable The batch table for the tile containing the batched polygons.
  36. * @param {Uint16Array} options.batchIds The batch ids for each polygon.
  37. * @param {BoundingSphere} options.boundingVolume The bounding volume for the entire batch of polygons.
  38. *
  39. * @private
  40. */
  41. function Vector3DTilePolygons(options) {
  42. // All of the private properties will be released except _primitive after the Vector3DTilePrimitive is created.
  43. this._batchTable = options.batchTable;
  44. this._batchIds = options.batchIds;
  45. this._positions = options.positions;
  46. this._counts = options.counts;
  47. this._indices = options.indices;
  48. this._indexCounts = options.indexCounts;
  49. this._indexOffsets = undefined;
  50. this._batchTableColors = undefined;
  51. this._packedBuffer = undefined;
  52. this._batchedPositions = undefined;
  53. this._transferrableBatchIds = undefined;
  54. this._vertexBatchIds = undefined;
  55. this._ellipsoid = defaultValue(options.ellipsoid, Ellipsoid.WGS84);
  56. this._minimumHeight = options.minimumHeight;
  57. this._maximumHeight = options.maximumHeight;
  58. this._polygonMinimumHeights = options.polygonMinimumHeights;
  59. this._polygonMaximumHeights = options.polygonMaximumHeights;
  60. this._center = defaultValue(options.center, Cartesian3.ZERO);
  61. this._rectangle = options.rectangle;
  62. this._center = undefined;
  63. this._boundingVolume = options.boundingVolume;
  64. this._boundingVolumes = undefined;
  65. this._batchedIndices = undefined;
  66. this._ready = false;
  67. this._promise = undefined;
  68. this._error = undefined;
  69. this._primitive = undefined;
  70. /**
  71. * Draws the wireframe of the classification meshes.
  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(Vector3DTilePolygons.prototype, {
  90. /**
  91. * Gets the number of triangles.
  92. *
  93. * @memberof Vector3DTilePolygons.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 Vector3DTilePolygons.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. * Returns true when the primitive is ready to render.
  126. * @memberof Vector3DTilePolygons.prototype
  127. * @type {boolean}
  128. * @readonly
  129. * @private
  130. */
  131. ready: {
  132. get: function () {
  133. return this._ready;
  134. },
  135. },
  136. });
  137. function packBuffer(polygons) {
  138. const packedBuffer = new Float64Array(
  139. 3 +
  140. Cartesian3.packedLength +
  141. Ellipsoid.packedLength +
  142. Rectangle.packedLength
  143. );
  144. let offset = 0;
  145. packedBuffer[offset++] = polygons._indices.BYTES_PER_ELEMENT;
  146. packedBuffer[offset++] = polygons._minimumHeight;
  147. packedBuffer[offset++] = polygons._maximumHeight;
  148. Cartesian3.pack(polygons._center, packedBuffer, offset);
  149. offset += Cartesian3.packedLength;
  150. Ellipsoid.pack(polygons._ellipsoid, packedBuffer, offset);
  151. offset += Ellipsoid.packedLength;
  152. Rectangle.pack(polygons._rectangle, packedBuffer, offset);
  153. return packedBuffer;
  154. }
  155. function unpackBuffer(polygons, packedBuffer) {
  156. let offset = 1;
  157. const numBVS = packedBuffer[offset++];
  158. const bvs = (polygons._boundingVolumes = new Array(numBVS));
  159. for (let i = 0; i < numBVS; ++i) {
  160. bvs[i] = OrientedBoundingBox.unpack(packedBuffer, offset);
  161. offset += OrientedBoundingBox.packedLength;
  162. }
  163. const numBatchedIndices = packedBuffer[offset++];
  164. const bis = (polygons._batchedIndices = new Array(numBatchedIndices));
  165. for (let j = 0; j < numBatchedIndices; ++j) {
  166. const color = Color.unpack(packedBuffer, offset);
  167. offset += Color.packedLength;
  168. const indexOffset = packedBuffer[offset++];
  169. const count = packedBuffer[offset++];
  170. const length = packedBuffer[offset++];
  171. const batchIds = new Array(length);
  172. for (let k = 0; k < length; ++k) {
  173. batchIds[k] = packedBuffer[offset++];
  174. }
  175. bis[j] = new Vector3DTileBatch({
  176. color: color,
  177. offset: indexOffset,
  178. count: count,
  179. batchIds: batchIds,
  180. });
  181. }
  182. }
  183. const createVerticesTaskProcessor = new TaskProcessor(
  184. "createVectorTilePolygons",
  185. 5
  186. );
  187. const scratchColor = new Color();
  188. function createPrimitive(polygons) {
  189. if (defined(polygons._primitive)) {
  190. return;
  191. }
  192. let positions = polygons._positions;
  193. let counts = polygons._counts;
  194. let indexCounts = polygons._indexCounts;
  195. let indices = polygons._indices;
  196. let batchIds = polygons._transferrableBatchIds;
  197. let batchTableColors = polygons._batchTableColors;
  198. let packedBuffer = polygons._packedBuffer;
  199. if (!defined(batchTableColors)) {
  200. // Copy because they may be the views on the same buffer.
  201. positions = polygons._positions = polygons._positions.slice();
  202. counts = polygons._counts = polygons._counts.slice();
  203. indexCounts = polygons._indexCounts = polygons._indexCounts.slice();
  204. indices = polygons._indices = polygons._indices.slice();
  205. polygons._center = polygons._ellipsoid.cartographicToCartesian(
  206. Rectangle.center(polygons._rectangle)
  207. );
  208. batchIds = polygons._transferrableBatchIds = new Uint32Array(
  209. polygons._batchIds
  210. );
  211. batchTableColors = polygons._batchTableColors = new Uint32Array(
  212. batchIds.length
  213. );
  214. const batchTable = polygons._batchTable;
  215. const length = batchTableColors.length;
  216. for (let i = 0; i < length; ++i) {
  217. const color = batchTable.getColor(i, scratchColor);
  218. batchTableColors[i] = color.toRgba();
  219. }
  220. packedBuffer = polygons._packedBuffer = packBuffer(polygons);
  221. }
  222. const transferrableObjects = [
  223. positions.buffer,
  224. counts.buffer,
  225. indexCounts.buffer,
  226. indices.buffer,
  227. batchIds.buffer,
  228. batchTableColors.buffer,
  229. packedBuffer.buffer,
  230. ];
  231. const parameters = {
  232. packedBuffer: packedBuffer.buffer,
  233. positions: positions.buffer,
  234. counts: counts.buffer,
  235. indexCounts: indexCounts.buffer,
  236. indices: indices.buffer,
  237. batchIds: batchIds.buffer,
  238. batchTableColors: batchTableColors.buffer,
  239. };
  240. let minimumHeights = polygons._polygonMinimumHeights;
  241. let maximumHeights = polygons._polygonMaximumHeights;
  242. if (defined(minimumHeights) && defined(maximumHeights)) {
  243. minimumHeights = minimumHeights.slice();
  244. maximumHeights = maximumHeights.slice();
  245. transferrableObjects.push(minimumHeights.buffer, maximumHeights.buffer);
  246. parameters.minimumHeights = minimumHeights;
  247. parameters.maximumHeights = maximumHeights;
  248. }
  249. const verticesPromise = createVerticesTaskProcessor.scheduleTask(
  250. parameters,
  251. transferrableObjects
  252. );
  253. if (!defined(verticesPromise)) {
  254. // Postponed
  255. return;
  256. }
  257. return verticesPromise
  258. .then((result) => {
  259. if (polygons.isDestroyed()) {
  260. return;
  261. }
  262. polygons._positions = undefined;
  263. polygons._counts = undefined;
  264. polygons._polygonMinimumHeights = undefined;
  265. polygons._polygonMaximumHeights = undefined;
  266. const packedBuffer = new Float64Array(result.packedBuffer);
  267. const indexDatatype = packedBuffer[0];
  268. unpackBuffer(polygons, packedBuffer);
  269. polygons._indices =
  270. IndexDatatype.getSizeInBytes(indexDatatype) === 2
  271. ? new Uint16Array(result.indices)
  272. : new Uint32Array(result.indices);
  273. polygons._indexOffsets = new Uint32Array(result.indexOffsets);
  274. polygons._indexCounts = new Uint32Array(result.indexCounts);
  275. // will be released
  276. polygons._batchedPositions = new Float32Array(result.positions);
  277. polygons._vertexBatchIds = new Uint16Array(result.batchIds);
  278. finishPrimitive(polygons);
  279. polygons._ready = true;
  280. })
  281. .catch((error) => {
  282. if (polygons.isDestroyed()) {
  283. return;
  284. }
  285. // Throw the error next frame
  286. polygons._error = error;
  287. });
  288. }
  289. function finishPrimitive(polygons) {
  290. if (!defined(polygons._primitive)) {
  291. polygons._primitive = new Vector3DTilePrimitive({
  292. batchTable: polygons._batchTable,
  293. positions: polygons._batchedPositions,
  294. batchIds: polygons._batchIds,
  295. vertexBatchIds: polygons._vertexBatchIds,
  296. indices: polygons._indices,
  297. indexOffsets: polygons._indexOffsets,
  298. indexCounts: polygons._indexCounts,
  299. batchedIndices: polygons._batchedIndices,
  300. boundingVolume: polygons._boundingVolume,
  301. boundingVolumes: polygons._boundingVolumes,
  302. center: polygons._center,
  303. });
  304. polygons._batchTable = undefined;
  305. polygons._batchIds = undefined;
  306. polygons._positions = undefined;
  307. polygons._counts = undefined;
  308. polygons._indices = undefined;
  309. polygons._indexCounts = undefined;
  310. polygons._indexOffsets = undefined;
  311. polygons._batchTableColors = undefined;
  312. polygons._packedBuffer = undefined;
  313. polygons._batchedPositions = undefined;
  314. polygons._transferrableBatchIds = undefined;
  315. polygons._vertexBatchIds = undefined;
  316. polygons._ellipsoid = undefined;
  317. polygons._minimumHeight = undefined;
  318. polygons._maximumHeight = undefined;
  319. polygons._polygonMinimumHeights = undefined;
  320. polygons._polygonMaximumHeights = undefined;
  321. polygons._center = undefined;
  322. polygons._rectangle = undefined;
  323. polygons._boundingVolume = undefined;
  324. polygons._boundingVolumes = undefined;
  325. polygons._batchedIndices = undefined;
  326. }
  327. }
  328. /**
  329. * Creates features for each polygon and places it at the batch id index of features.
  330. *
  331. * @param {Vector3DTileContent} content The vector tile content.
  332. * @param {Cesium3DTileFeature[]} features An array of features where the polygon features will be placed.
  333. */
  334. Vector3DTilePolygons.prototype.createFeatures = function (content, features) {
  335. this._primitive.createFeatures(content, features);
  336. };
  337. /**
  338. * Colors the entire tile when enabled is true. The resulting color will be (polygon batch table color * color).
  339. *
  340. * @param {boolean} enabled Whether to enable debug coloring.
  341. * @param {Color} color The debug color.
  342. */
  343. Vector3DTilePolygons.prototype.applyDebugSettings = function (enabled, color) {
  344. this._primitive.applyDebugSettings(enabled, color);
  345. };
  346. /**
  347. * Apply a style to the content.
  348. *
  349. * @param {Cesium3DTileStyle} style The style.
  350. * @param {Cesium3DTileFeature[]} features The array of features.
  351. */
  352. Vector3DTilePolygons.prototype.applyStyle = function (style, features) {
  353. this._primitive.applyStyle(style, features);
  354. };
  355. /**
  356. * Call when updating the color of a polygon with batchId changes color. The polygons will need to be re-batched
  357. * on the next update.
  358. *
  359. * @param {number} batchId The batch id of the polygon whose color has changed.
  360. * @param {Color} color The new polygon color.
  361. */
  362. Vector3DTilePolygons.prototype.updateCommands = function (batchId, color) {
  363. this._primitive.updateCommands(batchId, color);
  364. };
  365. /**
  366. * Updates the batches and queues the commands for rendering.
  367. *
  368. * @param {FrameState} frameState The current frame state.
  369. */
  370. Vector3DTilePolygons.prototype.update = function (frameState) {
  371. if (!this._ready) {
  372. if (!defined(this._promise)) {
  373. this._promise = createPrimitive(this);
  374. }
  375. if (defined(this._error)) {
  376. const error = this._error;
  377. this._error = undefined;
  378. throw error;
  379. }
  380. return;
  381. }
  382. this._primitive.debugWireframe = this.debugWireframe;
  383. this._primitive.forceRebatch = this.forceRebatch;
  384. this._primitive.classificationType = this.classificationType;
  385. this._primitive.update(frameState);
  386. };
  387. /**
  388. * Returns true if this object was destroyed; otherwise, false.
  389. * <p>
  390. * If this object was destroyed, it should not be used; calling any function other than
  391. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
  392. * </p>
  393. *
  394. * @returns {boolean} <code>true</code> if this object was destroyed; otherwise, <code>false</code>.
  395. */
  396. Vector3DTilePolygons.prototype.isDestroyed = function () {
  397. return false;
  398. };
  399. /**
  400. * Destroys the WebGL resources held by this object. Destroying an object allows for deterministic
  401. * release of WebGL resources, instead of relying on the garbage collector to destroy this object.
  402. * <p>
  403. * Once an object is destroyed, it should not be used; calling any function other than
  404. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  405. * assign the return value (<code>undefined</code>) to the object as done in the example.
  406. * </p>
  407. *
  408. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  409. */
  410. Vector3DTilePolygons.prototype.destroy = function () {
  411. this._primitive = this._primitive && this._primitive.destroy();
  412. return destroyObject(this);
  413. };
  414. export default Vector3DTilePolygons;