createVerticesFromQuantizedTerrainMesh.js 32 KB

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