Vector3DTilePolygons.js 17 KB

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