createVerticesFromQuantizedTerrainMesh.js 32 KB

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