createVerticesFromQuantizedTerrainMesh.js 31 KB

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