createVerticesFromQuantizedTerrainMesh.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. define(['./AxisAlignedBoundingBox-df2331b2', './Matrix2-69c32d33', './defaultValue-94c3e563', './TerrainEncoding-12c7d791', './IndexDatatype-c4099fe9', './ComponentDatatype-b1ea011a', './RuntimeError-c581ca93', './Transforms-323408fe', './WebMercatorProjection-f88d3d05', './createTaskProcessorWorker', './AttributeCompression-3cfab808', './WebGLConstants-7dccdc96', './_commonjsHelpers-3aae1032-f55dc0c4', './combine-761d9c3f'], (function (AxisAlignedBoundingBox, Matrix2, defaultValue, TerrainEncoding, IndexDatatype, ComponentDatatype, RuntimeError, Transforms, WebMercatorProjection, createTaskProcessorWorker, AttributeCompression, WebGLConstants, _commonjsHelpers3aae1032, combine) { 'use strict';
  3. /**
  4. * Provides terrain or other geometry for the surface of an ellipsoid. The surface geometry is
  5. * organized into a pyramid of tiles according to a {@link TilingScheme}. This type describes an
  6. * interface and is not intended to be instantiated directly.
  7. *
  8. * @alias TerrainProvider
  9. * @constructor
  10. *
  11. * @see EllipsoidTerrainProvider
  12. * @see CesiumTerrainProvider
  13. * @see VRTheWorldTerrainProvider
  14. * @see GoogleEarthEnterpriseTerrainProvider
  15. */
  16. function TerrainProvider() {
  17. RuntimeError.DeveloperError.throwInstantiationError();
  18. }
  19. Object.defineProperties(TerrainProvider.prototype, {
  20. /**
  21. * Gets an event that is raised when the terrain provider encounters an asynchronous error.. By subscribing
  22. * to the event, you will be notified of the error and can potentially recover from it. Event listeners
  23. * are passed an instance of {@link TileProviderError}.
  24. * @memberof TerrainProvider.prototype
  25. * @type {Event<TerrainProvider.ErrorEvent>}
  26. * @readonly
  27. */
  28. errorEvent: {
  29. get: RuntimeError.DeveloperError.throwInstantiationError,
  30. },
  31. /**
  32. * Gets the credit to display when this terrain provider is active. Typically this is used to credit
  33. * the source of the terrain. This function should
  34. * not be called before {@link TerrainProvider#ready} returns true.
  35. * @memberof TerrainProvider.prototype
  36. * @type {Credit}
  37. * @readonly
  38. */
  39. credit: {
  40. get: RuntimeError.DeveloperError.throwInstantiationError,
  41. },
  42. /**
  43. * Gets the tiling scheme used by the provider. This function should
  44. * not be called before {@link TerrainProvider#ready} returns true.
  45. * @memberof TerrainProvider.prototype
  46. * @type {TilingScheme}
  47. * @readonly
  48. */
  49. tilingScheme: {
  50. get: RuntimeError.DeveloperError.throwInstantiationError,
  51. },
  52. /**
  53. * Gets a value indicating whether or not the provider is ready for use.
  54. * @memberof TerrainProvider.prototype
  55. * @type {Boolean}
  56. * @readonly
  57. */
  58. ready: {
  59. get: RuntimeError.DeveloperError.throwInstantiationError,
  60. },
  61. /**
  62. * Gets a promise that resolves to true when the provider is ready for use.
  63. * @memberof TerrainProvider.prototype
  64. * @type {Promise.<Boolean>}
  65. * @readonly
  66. */
  67. readyPromise: {
  68. get: RuntimeError.DeveloperError.throwInstantiationError,
  69. },
  70. /**
  71. * Gets a value indicating whether or not the provider includes a water mask. The water mask
  72. * indicates which areas of the globe are water rather than land, so they can be rendered
  73. * as a reflective surface with animated waves. This function should not be
  74. * called before {@link TerrainProvider#ready} returns true.
  75. * @memberof TerrainProvider.prototype
  76. * @type {Boolean}
  77. * @readonly
  78. */
  79. hasWaterMask: {
  80. get: RuntimeError.DeveloperError.throwInstantiationError,
  81. },
  82. /**
  83. * Gets a value indicating whether or not the requested tiles include vertex normals.
  84. * This function should not be called before {@link TerrainProvider#ready} returns true.
  85. * @memberof TerrainProvider.prototype
  86. * @type {Boolean}
  87. * @readonly
  88. */
  89. hasVertexNormals: {
  90. get: RuntimeError.DeveloperError.throwInstantiationError,
  91. },
  92. /**
  93. * Gets an object that can be used to determine availability of terrain from this provider, such as
  94. * at points and in rectangles. This function should not be called before
  95. * {@link TerrainProvider#ready} returns true. This property may be undefined if availability
  96. * information is not available.
  97. * @memberof TerrainProvider.prototype
  98. * @type {TileAvailability}
  99. * @readonly
  100. */
  101. availability: {
  102. get: RuntimeError.DeveloperError.throwInstantiationError,
  103. },
  104. });
  105. const regularGridIndicesCache = [];
  106. /**
  107. * Gets a list of indices for a triangle mesh representing a regular grid. Calling
  108. * this function multiple times with the same grid width and height returns the
  109. * same list of indices. The total number of vertices must be less than or equal
  110. * to 65536.
  111. *
  112. * @param {Number} width The number of vertices in the regular grid in the horizontal direction.
  113. * @param {Number} height The number of vertices in the regular grid in the vertical direction.
  114. * @returns {Uint16Array|Uint32Array} The list of indices. Uint16Array gets returned for 64KB or less and Uint32Array for 4GB or less.
  115. */
  116. TerrainProvider.getRegularGridIndices = function (width, height) {
  117. //>>includeStart('debug', pragmas.debug);
  118. if (width * height >= ComponentDatatype.CesiumMath.FOUR_GIGABYTES) {
  119. throw new RuntimeError.DeveloperError(
  120. "The total number of vertices (width * height) must be less than 4,294,967,296."
  121. );
  122. }
  123. //>>includeEnd('debug');
  124. let byWidth = regularGridIndicesCache[width];
  125. if (!defaultValue.defined(byWidth)) {
  126. regularGridIndicesCache[width] = byWidth = [];
  127. }
  128. let indices = byWidth[height];
  129. if (!defaultValue.defined(indices)) {
  130. if (width * height < ComponentDatatype.CesiumMath.SIXTY_FOUR_KILOBYTES) {
  131. indices = byWidth[height] = new Uint16Array(
  132. (width - 1) * (height - 1) * 6
  133. );
  134. } else {
  135. indices = byWidth[height] = new Uint32Array(
  136. (width - 1) * (height - 1) * 6
  137. );
  138. }
  139. addRegularGridIndices(width, height, indices, 0);
  140. }
  141. return indices;
  142. };
  143. const regularGridAndEdgeIndicesCache = [];
  144. /**
  145. * @private
  146. */
  147. TerrainProvider.getRegularGridIndicesAndEdgeIndices = function (width, height) {
  148. //>>includeStart('debug', pragmas.debug);
  149. if (width * height >= ComponentDatatype.CesiumMath.FOUR_GIGABYTES) {
  150. throw new RuntimeError.DeveloperError(
  151. "The total number of vertices (width * height) must be less than 4,294,967,296."
  152. );
  153. }
  154. //>>includeEnd('debug');
  155. let byWidth = regularGridAndEdgeIndicesCache[width];
  156. if (!defaultValue.defined(byWidth)) {
  157. regularGridAndEdgeIndicesCache[width] = byWidth = [];
  158. }
  159. let indicesAndEdges = byWidth[height];
  160. if (!defaultValue.defined(indicesAndEdges)) {
  161. const indices = TerrainProvider.getRegularGridIndices(width, height);
  162. const edgeIndices = getEdgeIndices(width, height);
  163. const westIndicesSouthToNorth = edgeIndices.westIndicesSouthToNorth;
  164. const southIndicesEastToWest = edgeIndices.southIndicesEastToWest;
  165. const eastIndicesNorthToSouth = edgeIndices.eastIndicesNorthToSouth;
  166. const northIndicesWestToEast = edgeIndices.northIndicesWestToEast;
  167. indicesAndEdges = byWidth[height] = {
  168. indices: indices,
  169. westIndicesSouthToNorth: westIndicesSouthToNorth,
  170. southIndicesEastToWest: southIndicesEastToWest,
  171. eastIndicesNorthToSouth: eastIndicesNorthToSouth,
  172. northIndicesWestToEast: northIndicesWestToEast,
  173. };
  174. }
  175. return indicesAndEdges;
  176. };
  177. const regularGridAndSkirtAndEdgeIndicesCache = [];
  178. /**
  179. * @private
  180. */
  181. TerrainProvider.getRegularGridAndSkirtIndicesAndEdgeIndices = function (
  182. width,
  183. height
  184. ) {
  185. //>>includeStart('debug', pragmas.debug);
  186. if (width * height >= ComponentDatatype.CesiumMath.FOUR_GIGABYTES) {
  187. throw new RuntimeError.DeveloperError(
  188. "The total number of vertices (width * height) must be less than 4,294,967,296."
  189. );
  190. }
  191. //>>includeEnd('debug');
  192. let byWidth = regularGridAndSkirtAndEdgeIndicesCache[width];
  193. if (!defaultValue.defined(byWidth)) {
  194. regularGridAndSkirtAndEdgeIndicesCache[width] = byWidth = [];
  195. }
  196. let indicesAndEdges = byWidth[height];
  197. if (!defaultValue.defined(indicesAndEdges)) {
  198. const gridVertexCount = width * height;
  199. const gridIndexCount = (width - 1) * (height - 1) * 6;
  200. const edgeVertexCount = width * 2 + height * 2;
  201. const edgeIndexCount = Math.max(0, edgeVertexCount - 4) * 6;
  202. const vertexCount = gridVertexCount + edgeVertexCount;
  203. const indexCount = gridIndexCount + edgeIndexCount;
  204. const edgeIndices = getEdgeIndices(width, height);
  205. const westIndicesSouthToNorth = edgeIndices.westIndicesSouthToNorth;
  206. const southIndicesEastToWest = edgeIndices.southIndicesEastToWest;
  207. const eastIndicesNorthToSouth = edgeIndices.eastIndicesNorthToSouth;
  208. const northIndicesWestToEast = edgeIndices.northIndicesWestToEast;
  209. const indices = IndexDatatype.IndexDatatype.createTypedArray(vertexCount, indexCount);
  210. addRegularGridIndices(width, height, indices, 0);
  211. TerrainProvider.addSkirtIndices(
  212. westIndicesSouthToNorth,
  213. southIndicesEastToWest,
  214. eastIndicesNorthToSouth,
  215. northIndicesWestToEast,
  216. gridVertexCount,
  217. indices,
  218. gridIndexCount
  219. );
  220. indicesAndEdges = byWidth[height] = {
  221. indices: indices,
  222. westIndicesSouthToNorth: westIndicesSouthToNorth,
  223. southIndicesEastToWest: southIndicesEastToWest,
  224. eastIndicesNorthToSouth: eastIndicesNorthToSouth,
  225. northIndicesWestToEast: northIndicesWestToEast,
  226. indexCountWithoutSkirts: gridIndexCount,
  227. };
  228. }
  229. return indicesAndEdges;
  230. };
  231. /**
  232. * @private
  233. */
  234. TerrainProvider.addSkirtIndices = function (
  235. westIndicesSouthToNorth,
  236. southIndicesEastToWest,
  237. eastIndicesNorthToSouth,
  238. northIndicesWestToEast,
  239. vertexCount,
  240. indices,
  241. offset
  242. ) {
  243. let vertexIndex = vertexCount;
  244. offset = addSkirtIndices(
  245. westIndicesSouthToNorth,
  246. vertexIndex,
  247. indices,
  248. offset
  249. );
  250. vertexIndex += westIndicesSouthToNorth.length;
  251. offset = addSkirtIndices(
  252. southIndicesEastToWest,
  253. vertexIndex,
  254. indices,
  255. offset
  256. );
  257. vertexIndex += southIndicesEastToWest.length;
  258. offset = addSkirtIndices(
  259. eastIndicesNorthToSouth,
  260. vertexIndex,
  261. indices,
  262. offset
  263. );
  264. vertexIndex += eastIndicesNorthToSouth.length;
  265. addSkirtIndices(northIndicesWestToEast, vertexIndex, indices, offset);
  266. };
  267. function getEdgeIndices(width, height) {
  268. const westIndicesSouthToNorth = new Array(height);
  269. const southIndicesEastToWest = new Array(width);
  270. const eastIndicesNorthToSouth = new Array(height);
  271. const northIndicesWestToEast = new Array(width);
  272. let i;
  273. for (i = 0; i < width; ++i) {
  274. northIndicesWestToEast[i] = i;
  275. southIndicesEastToWest[i] = width * height - 1 - i;
  276. }
  277. for (i = 0; i < height; ++i) {
  278. eastIndicesNorthToSouth[i] = (i + 1) * width - 1;
  279. westIndicesSouthToNorth[i] = (height - i - 1) * width;
  280. }
  281. return {
  282. westIndicesSouthToNorth: westIndicesSouthToNorth,
  283. southIndicesEastToWest: southIndicesEastToWest,
  284. eastIndicesNorthToSouth: eastIndicesNorthToSouth,
  285. northIndicesWestToEast: northIndicesWestToEast,
  286. };
  287. }
  288. function addRegularGridIndices(width, height, indices, offset) {
  289. let index = 0;
  290. for (let j = 0; j < height - 1; ++j) {
  291. for (let i = 0; i < width - 1; ++i) {
  292. const upperLeft = index;
  293. const lowerLeft = upperLeft + width;
  294. const lowerRight = lowerLeft + 1;
  295. const upperRight = upperLeft + 1;
  296. indices[offset++] = upperLeft;
  297. indices[offset++] = lowerLeft;
  298. indices[offset++] = upperRight;
  299. indices[offset++] = upperRight;
  300. indices[offset++] = lowerLeft;
  301. indices[offset++] = lowerRight;
  302. ++index;
  303. }
  304. ++index;
  305. }
  306. }
  307. function addSkirtIndices(edgeIndices, vertexIndex, indices, offset) {
  308. let previousIndex = edgeIndices[0];
  309. const length = edgeIndices.length;
  310. for (let i = 1; i < length; ++i) {
  311. const index = edgeIndices[i];
  312. indices[offset++] = previousIndex;
  313. indices[offset++] = index;
  314. indices[offset++] = vertexIndex;
  315. indices[offset++] = vertexIndex;
  316. indices[offset++] = index;
  317. indices[offset++] = vertexIndex + 1;
  318. previousIndex = index;
  319. ++vertexIndex;
  320. }
  321. return offset;
  322. }
  323. /**
  324. * Specifies the quality of terrain created from heightmaps. A value of 1.0 will
  325. * ensure that adjacent heightmap vertices are separated by no more than
  326. * {@link Globe.maximumScreenSpaceError} screen pixels and will probably go very slowly.
  327. * A value of 0.5 will cut the estimated level zero geometric error in half, allowing twice the
  328. * screen pixels between adjacent heightmap vertices and thus rendering more quickly.
  329. * @type {Number}
  330. */
  331. TerrainProvider.heightmapTerrainQuality = 0.25;
  332. /**
  333. * Determines an appropriate geometric error estimate when the geometry comes from a heightmap.
  334. *
  335. * @param {Ellipsoid} ellipsoid The ellipsoid to which the terrain is attached.
  336. * @param {Number} tileImageWidth The width, in pixels, of the heightmap associated with a single tile.
  337. * @param {Number} numberOfTilesAtLevelZero The number of tiles in the horizontal direction at tile level zero.
  338. * @returns {Number} An estimated geometric error.
  339. */
  340. TerrainProvider.getEstimatedLevelZeroGeometricErrorForAHeightmap = function (
  341. ellipsoid,
  342. tileImageWidth,
  343. numberOfTilesAtLevelZero
  344. ) {
  345. return (
  346. (ellipsoid.maximumRadius *
  347. 2 *
  348. Math.PI *
  349. TerrainProvider.heightmapTerrainQuality) /
  350. (tileImageWidth * numberOfTilesAtLevelZero)
  351. );
  352. };
  353. /**
  354. * Requests the geometry for a given tile. This function should not be called before
  355. * {@link TerrainProvider#ready} returns true. The result must include terrain data and
  356. * may optionally include a water mask and an indication of which child tiles are available.
  357. * @function
  358. *
  359. * @param {Number} x The X coordinate of the tile for which to request geometry.
  360. * @param {Number} y The Y coordinate of the tile for which to request geometry.
  361. * @param {Number} level The level of the tile for which to request geometry.
  362. * @param {Request} [request] The request object. Intended for internal use only.
  363. *
  364. * @returns {Promise.<TerrainData>|undefined} A promise for the requested geometry. If this method
  365. * returns undefined instead of a promise, it is an indication that too many requests are already
  366. * pending and the request will be retried later.
  367. */
  368. TerrainProvider.prototype.requestTileGeometry =
  369. RuntimeError.DeveloperError.throwInstantiationError;
  370. /**
  371. * Gets the maximum geometric error allowed in a tile at a given level. This function should not be
  372. * called before {@link TerrainProvider#ready} returns true.
  373. * @function
  374. *
  375. * @param {Number} level The tile level for which to get the maximum geometric error.
  376. * @returns {Number} The maximum geometric error.
  377. */
  378. TerrainProvider.prototype.getLevelMaximumGeometricError =
  379. RuntimeError.DeveloperError.throwInstantiationError;
  380. /**
  381. * Determines whether data for a tile is available to be loaded.
  382. * @function
  383. *
  384. * @param {Number} x The X coordinate of the tile for which to request geometry.
  385. * @param {Number} y The Y coordinate of the tile for which to request geometry.
  386. * @param {Number} level The level of the tile for which to request geometry.
  387. * @returns {Boolean|undefined} Undefined if not supported by the terrain provider, otherwise true or false.
  388. */
  389. TerrainProvider.prototype.getTileDataAvailable =
  390. RuntimeError.DeveloperError.throwInstantiationError;
  391. /**
  392. * Makes sure we load availability data for a tile
  393. * @function
  394. *
  395. * @param {Number} x The X coordinate of the tile for which to request geometry.
  396. * @param {Number} y The Y coordinate of the tile for which to request geometry.
  397. * @param {Number} level The level of the tile for which to request geometry.
  398. * @returns {undefined|Promise<void>} Undefined if nothing need to be loaded or a Promise that resolves when all required tiles are loaded
  399. */
  400. TerrainProvider.prototype.loadTileDataAvailability =
  401. RuntimeError.DeveloperError.throwInstantiationError;
  402. /**
  403. * A function that is called when an error occurs.
  404. * @callback TerrainProvider.ErrorEvent
  405. *
  406. * @this TerrainProvider
  407. * @param {TileProviderError} err An object holding details about the error that occurred.
  408. */
  409. const maxShort = 32767;
  410. const cartesian3Scratch = new Matrix2.Cartesian3();
  411. const scratchMinimum = new Matrix2.Cartesian3();
  412. const scratchMaximum = new Matrix2.Cartesian3();
  413. const cartographicScratch = new Matrix2.Cartographic();
  414. const toPack = new Matrix2.Cartesian2();
  415. function createVerticesFromQuantizedTerrainMesh(
  416. parameters,
  417. transferableObjects
  418. ) {
  419. const quantizedVertices = parameters.quantizedVertices;
  420. const quantizedVertexCount = quantizedVertices.length / 3;
  421. const octEncodedNormals = parameters.octEncodedNormals;
  422. const edgeVertexCount =
  423. parameters.westIndices.length +
  424. parameters.eastIndices.length +
  425. parameters.southIndices.length +
  426. parameters.northIndices.length;
  427. const includeWebMercatorT = parameters.includeWebMercatorT;
  428. const exaggeration = parameters.exaggeration;
  429. const exaggerationRelativeHeight = parameters.exaggerationRelativeHeight;
  430. const hasExaggeration = exaggeration !== 1.0;
  431. const includeGeodeticSurfaceNormals = hasExaggeration;
  432. const rectangle = Matrix2.Rectangle.clone(parameters.rectangle);
  433. const west = rectangle.west;
  434. const south = rectangle.south;
  435. const east = rectangle.east;
  436. const north = rectangle.north;
  437. const ellipsoid = Matrix2.Ellipsoid.clone(parameters.ellipsoid);
  438. const minimumHeight = parameters.minimumHeight;
  439. const maximumHeight = parameters.maximumHeight;
  440. const center = parameters.relativeToCenter;
  441. const fromENU = Transforms.Transforms.eastNorthUpToFixedFrame(center, ellipsoid);
  442. const toENU = Matrix2.Matrix4.inverseTransformation(fromENU, new Matrix2.Matrix4());
  443. let southMercatorY;
  444. let oneOverMercatorHeight;
  445. if (includeWebMercatorT) {
  446. southMercatorY = WebMercatorProjection.WebMercatorProjection.geodeticLatitudeToMercatorAngle(
  447. south
  448. );
  449. oneOverMercatorHeight =
  450. 1.0 /
  451. (WebMercatorProjection.WebMercatorProjection.geodeticLatitudeToMercatorAngle(north) -
  452. southMercatorY);
  453. }
  454. const uBuffer = quantizedVertices.subarray(0, quantizedVertexCount);
  455. const vBuffer = quantizedVertices.subarray(
  456. quantizedVertexCount,
  457. 2 * quantizedVertexCount
  458. );
  459. const heightBuffer = quantizedVertices.subarray(
  460. quantizedVertexCount * 2,
  461. 3 * quantizedVertexCount
  462. );
  463. const hasVertexNormals = defaultValue.defined(octEncodedNormals);
  464. const uvs = new Array(quantizedVertexCount);
  465. const heights = new Array(quantizedVertexCount);
  466. const positions = new Array(quantizedVertexCount);
  467. const webMercatorTs = includeWebMercatorT
  468. ? new Array(quantizedVertexCount)
  469. : [];
  470. const geodeticSurfaceNormals = includeGeodeticSurfaceNormals
  471. ? new Array(quantizedVertexCount)
  472. : [];
  473. const minimum = scratchMinimum;
  474. minimum.x = Number.POSITIVE_INFINITY;
  475. minimum.y = Number.POSITIVE_INFINITY;
  476. minimum.z = Number.POSITIVE_INFINITY;
  477. const maximum = scratchMaximum;
  478. maximum.x = Number.NEGATIVE_INFINITY;
  479. maximum.y = Number.NEGATIVE_INFINITY;
  480. maximum.z = Number.NEGATIVE_INFINITY;
  481. let minLongitude = Number.POSITIVE_INFINITY;
  482. let maxLongitude = Number.NEGATIVE_INFINITY;
  483. let minLatitude = Number.POSITIVE_INFINITY;
  484. let maxLatitude = Number.NEGATIVE_INFINITY;
  485. for (let i = 0; i < quantizedVertexCount; ++i) {
  486. const rawU = uBuffer[i];
  487. const rawV = vBuffer[i];
  488. const u = rawU / maxShort;
  489. const v = rawV / maxShort;
  490. const height = ComponentDatatype.CesiumMath.lerp(
  491. minimumHeight,
  492. maximumHeight,
  493. heightBuffer[i] / maxShort
  494. );
  495. cartographicScratch.longitude = ComponentDatatype.CesiumMath.lerp(west, east, u);
  496. cartographicScratch.latitude = ComponentDatatype.CesiumMath.lerp(south, north, v);
  497. cartographicScratch.height = height;
  498. minLongitude = Math.min(cartographicScratch.longitude, minLongitude);
  499. maxLongitude = Math.max(cartographicScratch.longitude, maxLongitude);
  500. minLatitude = Math.min(cartographicScratch.latitude, minLatitude);
  501. maxLatitude = Math.max(cartographicScratch.latitude, maxLatitude);
  502. const position = ellipsoid.cartographicToCartesian(cartographicScratch);
  503. uvs[i] = new Matrix2.Cartesian2(u, v);
  504. heights[i] = height;
  505. positions[i] = position;
  506. if (includeWebMercatorT) {
  507. webMercatorTs[i] =
  508. (WebMercatorProjection.WebMercatorProjection.geodeticLatitudeToMercatorAngle(
  509. cartographicScratch.latitude
  510. ) -
  511. southMercatorY) *
  512. oneOverMercatorHeight;
  513. }
  514. if (includeGeodeticSurfaceNormals) {
  515. geodeticSurfaceNormals[i] = ellipsoid.geodeticSurfaceNormal(position);
  516. }
  517. Matrix2.Matrix4.multiplyByPoint(toENU, position, cartesian3Scratch);
  518. Matrix2.Cartesian3.minimumByComponent(cartesian3Scratch, minimum, minimum);
  519. Matrix2.Cartesian3.maximumByComponent(cartesian3Scratch, maximum, maximum);
  520. }
  521. const westIndicesSouthToNorth = copyAndSort(parameters.westIndices, function (
  522. a,
  523. b
  524. ) {
  525. return uvs[a].y - uvs[b].y;
  526. });
  527. const eastIndicesNorthToSouth = copyAndSort(parameters.eastIndices, function (
  528. a,
  529. b
  530. ) {
  531. return uvs[b].y - uvs[a].y;
  532. });
  533. const southIndicesEastToWest = copyAndSort(parameters.southIndices, function (
  534. a,
  535. b
  536. ) {
  537. return uvs[b].x - uvs[a].x;
  538. });
  539. const northIndicesWestToEast = copyAndSort(parameters.northIndices, function (
  540. a,
  541. b
  542. ) {
  543. return uvs[a].x - uvs[b].x;
  544. });
  545. let occludeePointInScaledSpace;
  546. if (minimumHeight < 0.0) {
  547. // Horizon culling point needs to be recomputed since the tile is at least partly under the ellipsoid.
  548. const occluder = new TerrainEncoding.EllipsoidalOccluder(ellipsoid);
  549. occludeePointInScaledSpace = occluder.computeHorizonCullingPointPossiblyUnderEllipsoid(
  550. center,
  551. positions,
  552. minimumHeight
  553. );
  554. }
  555. let hMin = minimumHeight;
  556. hMin = Math.min(
  557. hMin,
  558. findMinMaxSkirts(
  559. parameters.westIndices,
  560. parameters.westSkirtHeight,
  561. heights,
  562. uvs,
  563. rectangle,
  564. ellipsoid,
  565. toENU,
  566. minimum,
  567. maximum
  568. )
  569. );
  570. hMin = Math.min(
  571. hMin,
  572. findMinMaxSkirts(
  573. parameters.southIndices,
  574. parameters.southSkirtHeight,
  575. heights,
  576. uvs,
  577. rectangle,
  578. ellipsoid,
  579. toENU,
  580. minimum,
  581. maximum
  582. )
  583. );
  584. hMin = Math.min(
  585. hMin,
  586. findMinMaxSkirts(
  587. parameters.eastIndices,
  588. parameters.eastSkirtHeight,
  589. heights,
  590. uvs,
  591. rectangle,
  592. ellipsoid,
  593. toENU,
  594. minimum,
  595. maximum
  596. )
  597. );
  598. hMin = Math.min(
  599. hMin,
  600. findMinMaxSkirts(
  601. parameters.northIndices,
  602. parameters.northSkirtHeight,
  603. heights,
  604. uvs,
  605. rectangle,
  606. ellipsoid,
  607. toENU,
  608. minimum,
  609. maximum
  610. )
  611. );
  612. const aaBox = new AxisAlignedBoundingBox.AxisAlignedBoundingBox(minimum, maximum, center);
  613. const encoding = new TerrainEncoding.TerrainEncoding(
  614. center,
  615. aaBox,
  616. hMin,
  617. maximumHeight,
  618. fromENU,
  619. hasVertexNormals,
  620. includeWebMercatorT,
  621. includeGeodeticSurfaceNormals,
  622. exaggeration,
  623. exaggerationRelativeHeight
  624. );
  625. const vertexStride = encoding.stride;
  626. const size =
  627. quantizedVertexCount * vertexStride + edgeVertexCount * vertexStride;
  628. const vertexBuffer = new Float32Array(size);
  629. let bufferIndex = 0;
  630. for (let j = 0; j < quantizedVertexCount; ++j) {
  631. if (hasVertexNormals) {
  632. const n = j * 2.0;
  633. toPack.x = octEncodedNormals[n];
  634. toPack.y = octEncodedNormals[n + 1];
  635. }
  636. bufferIndex = encoding.encode(
  637. vertexBuffer,
  638. bufferIndex,
  639. positions[j],
  640. uvs[j],
  641. heights[j],
  642. toPack,
  643. webMercatorTs[j],
  644. geodeticSurfaceNormals[j]
  645. );
  646. }
  647. const edgeTriangleCount = Math.max(0, (edgeVertexCount - 4) * 2);
  648. const indexBufferLength = parameters.indices.length + edgeTriangleCount * 3;
  649. const indexBuffer = IndexDatatype.IndexDatatype.createTypedArray(
  650. quantizedVertexCount + edgeVertexCount,
  651. indexBufferLength
  652. );
  653. indexBuffer.set(parameters.indices, 0);
  654. const percentage = 0.0001;
  655. const lonOffset = (maxLongitude - minLongitude) * percentage;
  656. const latOffset = (maxLatitude - minLatitude) * percentage;
  657. const westLongitudeOffset = -lonOffset;
  658. const westLatitudeOffset = 0.0;
  659. const eastLongitudeOffset = lonOffset;
  660. const eastLatitudeOffset = 0.0;
  661. const northLongitudeOffset = 0.0;
  662. const northLatitudeOffset = latOffset;
  663. const southLongitudeOffset = 0.0;
  664. const southLatitudeOffset = -latOffset;
  665. // Add skirts.
  666. let vertexBufferIndex = quantizedVertexCount * vertexStride;
  667. addSkirt(
  668. vertexBuffer,
  669. vertexBufferIndex,
  670. westIndicesSouthToNorth,
  671. encoding,
  672. heights,
  673. uvs,
  674. octEncodedNormals,
  675. ellipsoid,
  676. rectangle,
  677. parameters.westSkirtHeight,
  678. southMercatorY,
  679. oneOverMercatorHeight,
  680. westLongitudeOffset,
  681. westLatitudeOffset
  682. );
  683. vertexBufferIndex += parameters.westIndices.length * vertexStride;
  684. addSkirt(
  685. vertexBuffer,
  686. vertexBufferIndex,
  687. southIndicesEastToWest,
  688. encoding,
  689. heights,
  690. uvs,
  691. octEncodedNormals,
  692. ellipsoid,
  693. rectangle,
  694. parameters.southSkirtHeight,
  695. southMercatorY,
  696. oneOverMercatorHeight,
  697. southLongitudeOffset,
  698. southLatitudeOffset
  699. );
  700. vertexBufferIndex += parameters.southIndices.length * vertexStride;
  701. addSkirt(
  702. vertexBuffer,
  703. vertexBufferIndex,
  704. eastIndicesNorthToSouth,
  705. encoding,
  706. heights,
  707. uvs,
  708. octEncodedNormals,
  709. ellipsoid,
  710. rectangle,
  711. parameters.eastSkirtHeight,
  712. southMercatorY,
  713. oneOverMercatorHeight,
  714. eastLongitudeOffset,
  715. eastLatitudeOffset
  716. );
  717. vertexBufferIndex += parameters.eastIndices.length * vertexStride;
  718. addSkirt(
  719. vertexBuffer,
  720. vertexBufferIndex,
  721. northIndicesWestToEast,
  722. encoding,
  723. heights,
  724. uvs,
  725. octEncodedNormals,
  726. ellipsoid,
  727. rectangle,
  728. parameters.northSkirtHeight,
  729. southMercatorY,
  730. oneOverMercatorHeight,
  731. northLongitudeOffset,
  732. northLatitudeOffset
  733. );
  734. TerrainProvider.addSkirtIndices(
  735. westIndicesSouthToNorth,
  736. southIndicesEastToWest,
  737. eastIndicesNorthToSouth,
  738. northIndicesWestToEast,
  739. quantizedVertexCount,
  740. indexBuffer,
  741. parameters.indices.length
  742. );
  743. transferableObjects.push(vertexBuffer.buffer, indexBuffer.buffer);
  744. return {
  745. vertices: vertexBuffer.buffer,
  746. indices: indexBuffer.buffer,
  747. westIndicesSouthToNorth: westIndicesSouthToNorth,
  748. southIndicesEastToWest: southIndicesEastToWest,
  749. eastIndicesNorthToSouth: eastIndicesNorthToSouth,
  750. northIndicesWestToEast: northIndicesWestToEast,
  751. vertexStride: vertexStride,
  752. center: center,
  753. minimumHeight: minimumHeight,
  754. maximumHeight: maximumHeight,
  755. occludeePointInScaledSpace: occludeePointInScaledSpace,
  756. encoding: encoding,
  757. indexCountWithoutSkirts: parameters.indices.length,
  758. };
  759. }
  760. function findMinMaxSkirts(
  761. edgeIndices,
  762. edgeHeight,
  763. heights,
  764. uvs,
  765. rectangle,
  766. ellipsoid,
  767. toENU,
  768. minimum,
  769. maximum
  770. ) {
  771. let hMin = Number.POSITIVE_INFINITY;
  772. const north = rectangle.north;
  773. const south = rectangle.south;
  774. let east = rectangle.east;
  775. const west = rectangle.west;
  776. if (east < west) {
  777. east += ComponentDatatype.CesiumMath.TWO_PI;
  778. }
  779. const length = edgeIndices.length;
  780. for (let i = 0; i < length; ++i) {
  781. const index = edgeIndices[i];
  782. const h = heights[index];
  783. const uv = uvs[index];
  784. cartographicScratch.longitude = ComponentDatatype.CesiumMath.lerp(west, east, uv.x);
  785. cartographicScratch.latitude = ComponentDatatype.CesiumMath.lerp(south, north, uv.y);
  786. cartographicScratch.height = h - edgeHeight;
  787. const position = ellipsoid.cartographicToCartesian(
  788. cartographicScratch,
  789. cartesian3Scratch
  790. );
  791. Matrix2.Matrix4.multiplyByPoint(toENU, position, position);
  792. Matrix2.Cartesian3.minimumByComponent(position, minimum, minimum);
  793. Matrix2.Cartesian3.maximumByComponent(position, maximum, maximum);
  794. hMin = Math.min(hMin, cartographicScratch.height);
  795. }
  796. return hMin;
  797. }
  798. function addSkirt(
  799. vertexBuffer,
  800. vertexBufferIndex,
  801. edgeVertices,
  802. encoding,
  803. heights,
  804. uvs,
  805. octEncodedNormals,
  806. ellipsoid,
  807. rectangle,
  808. skirtLength,
  809. southMercatorY,
  810. oneOverMercatorHeight,
  811. longitudeOffset,
  812. latitudeOffset
  813. ) {
  814. const hasVertexNormals = defaultValue.defined(octEncodedNormals);
  815. const north = rectangle.north;
  816. const south = rectangle.south;
  817. let east = rectangle.east;
  818. const west = rectangle.west;
  819. if (east < west) {
  820. east += ComponentDatatype.CesiumMath.TWO_PI;
  821. }
  822. const length = edgeVertices.length;
  823. for (let i = 0; i < length; ++i) {
  824. const index = edgeVertices[i];
  825. const h = heights[index];
  826. const uv = uvs[index];
  827. cartographicScratch.longitude =
  828. ComponentDatatype.CesiumMath.lerp(west, east, uv.x) + longitudeOffset;
  829. cartographicScratch.latitude =
  830. ComponentDatatype.CesiumMath.lerp(south, north, uv.y) + latitudeOffset;
  831. cartographicScratch.height = h - skirtLength;
  832. const position = ellipsoid.cartographicToCartesian(
  833. cartographicScratch,
  834. cartesian3Scratch
  835. );
  836. if (hasVertexNormals) {
  837. const n = index * 2.0;
  838. toPack.x = octEncodedNormals[n];
  839. toPack.y = octEncodedNormals[n + 1];
  840. }
  841. let webMercatorT;
  842. if (encoding.hasWebMercatorT) {
  843. webMercatorT =
  844. (WebMercatorProjection.WebMercatorProjection.geodeticLatitudeToMercatorAngle(
  845. cartographicScratch.latitude
  846. ) -
  847. southMercatorY) *
  848. oneOverMercatorHeight;
  849. }
  850. let geodeticSurfaceNormal;
  851. if (encoding.hasGeodeticSurfaceNormals) {
  852. geodeticSurfaceNormal = ellipsoid.geodeticSurfaceNormal(position);
  853. }
  854. vertexBufferIndex = encoding.encode(
  855. vertexBuffer,
  856. vertexBufferIndex,
  857. position,
  858. uv,
  859. cartographicScratch.height,
  860. toPack,
  861. webMercatorT,
  862. geodeticSurfaceNormal
  863. );
  864. }
  865. }
  866. function copyAndSort(typedArray, comparator) {
  867. let copy;
  868. if (typeof typedArray.slice === "function") {
  869. copy = typedArray.slice();
  870. if (typeof copy.sort !== "function") {
  871. // Sliced typed array isn't sortable, so we can't use it.
  872. copy = undefined;
  873. }
  874. }
  875. if (!defaultValue.defined(copy)) {
  876. copy = Array.prototype.slice.call(typedArray);
  877. }
  878. copy.sort(comparator);
  879. return copy;
  880. }
  881. var createVerticesFromQuantizedTerrainMesh$1 = createTaskProcessorWorker(
  882. createVerticesFromQuantizedTerrainMesh
  883. );
  884. return createVerticesFromQuantizedTerrainMesh$1;
  885. }));