Vector3DTilePolygons.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. import arraySlice from "../Core/arraySlice.js";
  2. import Cartesian3 from "../Core/Cartesian3.js";
  3. import Color from "../Core/Color.js";
  4. import defaultValue from "../Core/defaultValue.js";
  5. import defer from "../Core/defer.js";
  6. import defined from "../Core/defined.js";
  7. import destroyObject from "../Core/destroyObject.js";
  8. import Ellipsoid from "../Core/Ellipsoid.js";
  9. import IndexDatatype from "../Core/IndexDatatype.js";
  10. import OrientedBoundingBox from "../Core/OrientedBoundingBox.js";
  11. import Rectangle from "../Core/Rectangle.js";
  12. import TaskProcessor from "../Core/TaskProcessor.js";
  13. import ClassificationType from "./ClassificationType.js";
  14. import Vector3DTileBatch from "./Vector3DTileBatch.js";
  15. import Vector3DTilePrimitive from "./Vector3DTilePrimitive.js";
  16. /**
  17. * Creates a batch of pre-triangulated polygons draped on terrain and/or 3D Tiles.
  18. *
  19. * @alias Vector3DTilePolygons
  20. * @constructor
  21. *
  22. * @param {Object} options An object with following properties:
  23. * @param {Float32Array|Uint16Array} options.positions The positions of the polygons. The positions must be contiguous
  24. * 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
  25. * the polygon in counter-clockwise order.
  26. * @param {Uint32Array} options.counts The number of positions in the each polygon.
  27. * @param {Uint32Array} options.indices The indices of the triangulated polygons. The indices must be contiguous so that
  28. * the indices for polygon n are in [i, i + indexCounts[n]] where i = sum{indexCounts[0], indexCounts[n - 1]}.
  29. * @param {Uint32Array} options.indexCounts The number of indices for each polygon.
  30. * @param {Number} options.minimumHeight The minimum height of the terrain covered by the tile.
  31. * @param {Number} options.maximumHeight The maximum height of the terrain covered by the tile.
  32. * @param {Float32Array} [options.polygonMinimumHeights] An array containing the minimum heights for each polygon.
  33. * @param {Float32Array} [options.polygonMaximumHeights] An array containing the maximum heights for each polygon.
  34. * @param {Rectangle} options.rectangle The rectangle containing the tile.
  35. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid.
  36. * @param {Cartesian3} [options.center=Cartesian3.ZERO] The RTC center.
  37. * @param {Cesium3DTileBatchTable} options.batchTable The batch table for the tile containing the batched polygons.
  38. * @param {Uint16Array} options.batchIds The batch ids for each polygon.
  39. * @param {BoundingSphere} options.boundingVolume The bounding volume for the entire batch of polygons.
  40. *
  41. * @private
  42. */
  43. function Vector3DTilePolygons(options) {
  44. // All of the private properties will be released except _readyPromise
  45. // and _primitive after the Vector3DTilePrimitive is created.
  46. this._batchTable = options.batchTable;
  47. this._batchIds = options.batchIds;
  48. this._positions = options.positions;
  49. this._counts = options.counts;
  50. this._indices = options.indices;
  51. this._indexCounts = options.indexCounts;
  52. this._indexOffsets = undefined;
  53. this._batchTableColors = undefined;
  54. this._packedBuffer = undefined;
  55. this._batchedPositions = undefined;
  56. this._transferrableBatchIds = undefined;
  57. this._vertexBatchIds = undefined;
  58. this._ellipsoid = defaultValue(options.ellipsoid, Ellipsoid.WGS84);
  59. this._minimumHeight = options.minimumHeight;
  60. this._maximumHeight = options.maximumHeight;
  61. this._polygonMinimumHeights = options.polygonMinimumHeights;
  62. this._polygonMaximumHeights = options.polygonMaximumHeights;
  63. this._center = defaultValue(options.center, Cartesian3.ZERO);
  64. this._rectangle = options.rectangle;
  65. this._center = undefined;
  66. this._boundingVolume = options.boundingVolume;
  67. this._boundingVolumes = undefined;
  68. this._batchedIndices = undefined;
  69. this._ready = false;
  70. this._readyPromise = defer();
  71. this._verticesPromise = undefined;
  72. this._primitive = undefined;
  73. /**
  74. * Draws the wireframe of the classification meshes.
  75. * @type {Boolean}
  76. * @default false
  77. */
  78. this.debugWireframe = false;
  79. /**
  80. * Forces a re-batch instead of waiting after a number of frames have been rendered. For testing only.
  81. * @type {Boolean}
  82. * @default false
  83. */
  84. this.forceRebatch = false;
  85. /**
  86. * What this tile will classify.
  87. * @type {ClassificationType}
  88. * @default ClassificationType.BOTH
  89. */
  90. this.classificationType = ClassificationType.BOTH;
  91. }
  92. Object.defineProperties(Vector3DTilePolygons.prototype, {
  93. /**
  94. * Gets the number of triangles.
  95. *
  96. * @memberof Vector3DTilePolygons.prototype
  97. *
  98. * @type {Number}
  99. * @readonly
  100. */
  101. trianglesLength: {
  102. get: function () {
  103. if (defined(this._primitive)) {
  104. return this._primitive.trianglesLength;
  105. }
  106. return 0;
  107. },
  108. },
  109. /**
  110. * Gets the geometry memory in bytes.
  111. *
  112. * @memberof Vector3DTilePolygons.prototype
  113. *
  114. * @type {Number}
  115. * @readonly
  116. */
  117. geometryByteLength: {
  118. get: function () {
  119. if (defined(this._primitive)) {
  120. return this._primitive.geometryByteLength;
  121. }
  122. return 0;
  123. },
  124. },
  125. /**
  126. * Gets a promise that resolves when the primitive is ready to render.
  127. * @memberof Vector3DTilePolygons.prototype
  128. * @type {Promise<void>}
  129. * @readonly
  130. */
  131. readyPromise: {
  132. get: function () {
  133. return this._readyPromise.promise;
  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. if (!defined(polygons._verticesPromise)) {
  193. let positions = polygons._positions;
  194. let counts = polygons._counts;
  195. let indexCounts = polygons._indexCounts;
  196. let indices = polygons._indices;
  197. let batchIds = polygons._transferrableBatchIds;
  198. let batchTableColors = polygons._batchTableColors;
  199. let packedBuffer = polygons._packedBuffer;
  200. if (!defined(batchTableColors)) {
  201. // Copy because they may be the views on the same buffer.
  202. positions = polygons._positions = arraySlice(polygons._positions);
  203. counts = polygons._counts = arraySlice(polygons._counts);
  204. indexCounts = polygons._indexCounts = arraySlice(polygons._indexCounts);
  205. indices = polygons._indices = arraySlice(polygons._indices);
  206. polygons._center = polygons._ellipsoid.cartographicToCartesian(
  207. Rectangle.center(polygons._rectangle)
  208. );
  209. batchIds = polygons._transferrableBatchIds = new Uint32Array(
  210. polygons._batchIds
  211. );
  212. batchTableColors = polygons._batchTableColors = new Uint32Array(
  213. batchIds.length
  214. );
  215. const batchTable = polygons._batchTable;
  216. const length = batchTableColors.length;
  217. for (let i = 0; i < length; ++i) {
  218. const color = batchTable.getColor(i, scratchColor);
  219. batchTableColors[i] = color.toRgba();
  220. }
  221. packedBuffer = polygons._packedBuffer = packBuffer(polygons);
  222. }
  223. const transferrableObjects = [
  224. positions.buffer,
  225. counts.buffer,
  226. indexCounts.buffer,
  227. indices.buffer,
  228. batchIds.buffer,
  229. batchTableColors.buffer,
  230. packedBuffer.buffer,
  231. ];
  232. const parameters = {
  233. packedBuffer: packedBuffer.buffer,
  234. positions: positions.buffer,
  235. counts: counts.buffer,
  236. indexCounts: indexCounts.buffer,
  237. indices: indices.buffer,
  238. batchIds: batchIds.buffer,
  239. batchTableColors: batchTableColors.buffer,
  240. };
  241. let minimumHeights = polygons._polygonMinimumHeights;
  242. let maximumHeights = polygons._polygonMaximumHeights;
  243. if (defined(minimumHeights) && defined(maximumHeights)) {
  244. minimumHeights = arraySlice(minimumHeights);
  245. maximumHeights = arraySlice(maximumHeights);
  246. transferrableObjects.push(minimumHeights.buffer, maximumHeights.buffer);
  247. parameters.minimumHeights = minimumHeights;
  248. parameters.maximumHeights = maximumHeights;
  249. }
  250. const verticesPromise = (polygons._verticesPromise = createVerticesTaskProcessor.scheduleTask(
  251. parameters,
  252. transferrableObjects
  253. ));
  254. if (!defined(verticesPromise)) {
  255. // Postponed
  256. return;
  257. }
  258. verticesPromise.then(function (result) {
  259. polygons._positions = undefined;
  260. polygons._counts = undefined;
  261. polygons._polygonMinimumHeights = undefined;
  262. polygons._polygonMaximumHeights = undefined;
  263. const packedBuffer = new Float64Array(result.packedBuffer);
  264. const indexDatatype = packedBuffer[0];
  265. unpackBuffer(polygons, packedBuffer);
  266. polygons._indices =
  267. IndexDatatype.getSizeInBytes(indexDatatype) === 2
  268. ? new Uint16Array(result.indices)
  269. : new Uint32Array(result.indices);
  270. polygons._indexOffsets = new Uint32Array(result.indexOffsets);
  271. polygons._indexCounts = new Uint32Array(result.indexCounts);
  272. // will be released
  273. polygons._batchedPositions = new Float32Array(result.positions);
  274. polygons._vertexBatchIds = new Uint16Array(result.batchIds);
  275. polygons._ready = true;
  276. });
  277. }
  278. if (polygons._ready && !defined(polygons._primitive)) {
  279. polygons._primitive = new Vector3DTilePrimitive({
  280. batchTable: polygons._batchTable,
  281. positions: polygons._batchedPositions,
  282. batchIds: polygons._batchIds,
  283. vertexBatchIds: polygons._vertexBatchIds,
  284. indices: polygons._indices,
  285. indexOffsets: polygons._indexOffsets,
  286. indexCounts: polygons._indexCounts,
  287. batchedIndices: polygons._batchedIndices,
  288. boundingVolume: polygons._boundingVolume,
  289. boundingVolumes: polygons._boundingVolumes,
  290. center: polygons._center,
  291. });
  292. polygons._batchTable = undefined;
  293. polygons._batchIds = undefined;
  294. polygons._positions = undefined;
  295. polygons._counts = undefined;
  296. polygons._indices = undefined;
  297. polygons._indexCounts = undefined;
  298. polygons._indexOffsets = undefined;
  299. polygons._batchTableColors = undefined;
  300. polygons._packedBuffer = undefined;
  301. polygons._batchedPositions = undefined;
  302. polygons._transferrableBatchIds = undefined;
  303. polygons._vertexBatchIds = undefined;
  304. polygons._ellipsoid = undefined;
  305. polygons._minimumHeight = undefined;
  306. polygons._maximumHeight = undefined;
  307. polygons._polygonMinimumHeights = undefined;
  308. polygons._polygonMaximumHeights = undefined;
  309. polygons._center = undefined;
  310. polygons._rectangle = undefined;
  311. polygons._boundingVolume = undefined;
  312. polygons._boundingVolumes = undefined;
  313. polygons._batchedIndices = undefined;
  314. polygons._verticesPromise = undefined;
  315. polygons._readyPromise.resolve();
  316. }
  317. }
  318. /**
  319. * Creates features for each polygon and places it at the batch id index of features.
  320. *
  321. * @param {Vector3DTileContent} content The vector tile content.
  322. * @param {Cesium3DTileFeature[]} features An array of features where the polygon features will be placed.
  323. */
  324. Vector3DTilePolygons.prototype.createFeatures = function (content, features) {
  325. this._primitive.createFeatures(content, features);
  326. };
  327. /**
  328. * Colors the entire tile when enabled is true. The resulting color will be (polygon batch table color * color).
  329. *
  330. * @param {Boolean} enabled Whether to enable debug coloring.
  331. * @param {Color} color The debug color.
  332. */
  333. Vector3DTilePolygons.prototype.applyDebugSettings = function (enabled, color) {
  334. this._primitive.applyDebugSettings(enabled, color);
  335. };
  336. /**
  337. * Apply a style to the content.
  338. *
  339. * @param {Cesium3DTileStyle} style The style.
  340. * @param {Cesium3DTileFeature[]} features The array of features.
  341. */
  342. Vector3DTilePolygons.prototype.applyStyle = function (style, features) {
  343. this._primitive.applyStyle(style, features);
  344. };
  345. /**
  346. * Call when updating the color of a polygon with batchId changes color. The polygons will need to be re-batched
  347. * on the next update.
  348. *
  349. * @param {Number} batchId The batch id of the polygon whose color has changed.
  350. * @param {Color} color The new polygon color.
  351. */
  352. Vector3DTilePolygons.prototype.updateCommands = function (batchId, color) {
  353. this._primitive.updateCommands(batchId, color);
  354. };
  355. /**
  356. * Updates the batches and queues the commands for rendering.
  357. *
  358. * @param {FrameState} frameState The current frame state.
  359. */
  360. Vector3DTilePolygons.prototype.update = function (frameState) {
  361. createPrimitive(this);
  362. if (!this._ready) {
  363. return;
  364. }
  365. this._primitive.debugWireframe = this.debugWireframe;
  366. this._primitive.forceRebatch = this.forceRebatch;
  367. this._primitive.classificationType = this.classificationType;
  368. this._primitive.update(frameState);
  369. };
  370. /**
  371. * Returns true if this object was destroyed; otherwise, false.
  372. * <p>
  373. * If this object was destroyed, it should not be used; calling any function other than
  374. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
  375. * </p>
  376. *
  377. * @returns {Boolean} <code>true</code> if this object was destroyed; otherwise, <code>false</code>.
  378. */
  379. Vector3DTilePolygons.prototype.isDestroyed = function () {
  380. return false;
  381. };
  382. /**
  383. * Destroys the WebGL resources held by this object. Destroying an object allows for deterministic
  384. * release of WebGL resources, instead of relying on the garbage collector to destroy this object.
  385. * <p>
  386. * Once an object is destroyed, it should not be used; calling any function other than
  387. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  388. * assign the return value (<code>undefined</code>) to the object as done in the example.
  389. * </p>
  390. *
  391. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  392. */
  393. Vector3DTilePolygons.prototype.destroy = function () {
  394. this._primitive = this._primitive && this._primitive.destroy();
  395. return destroyObject(this);
  396. };
  397. export default Vector3DTilePolygons;