TerrainProvider.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. import defined from "./defined.js";
  2. import DeveloperError from "./DeveloperError.js";
  3. import IndexDatatype from "./IndexDatatype.js";
  4. import CesiumMath from "./Math.js";
  5. /**
  6. * Provides terrain or other geometry for the surface of an ellipsoid. The surface geometry is
  7. * organized into a pyramid of tiles according to a {@link TilingScheme}. This type describes an
  8. * interface and is not intended to be instantiated directly.
  9. *
  10. * @alias TerrainProvider
  11. * @constructor
  12. *
  13. * @see EllipsoidTerrainProvider
  14. * @see CesiumTerrainProvider
  15. * @see VRTheWorldTerrainProvider
  16. * @see GoogleEarthEnterpriseTerrainProvider
  17. */
  18. function TerrainProvider() {
  19. DeveloperError.throwInstantiationError();
  20. }
  21. Object.defineProperties(TerrainProvider.prototype, {
  22. /**
  23. * Gets an event that is raised when the terrain provider encounters an asynchronous error.. By subscribing
  24. * to the event, you will be notified of the error and can potentially recover from it. Event listeners
  25. * are passed an instance of {@link TileProviderError}.
  26. * @memberof TerrainProvider.prototype
  27. * @type {Event<TerrainProvider.ErrorEvent>}
  28. * @readonly
  29. */
  30. errorEvent: {
  31. get: DeveloperError.throwInstantiationError,
  32. },
  33. /**
  34. * Gets the credit to display when this terrain provider is active. Typically this is used to credit
  35. * the source of the terrain. This function should
  36. * not be called before {@link TerrainProvider#ready} returns true.
  37. * @memberof TerrainProvider.prototype
  38. * @type {Credit}
  39. * @readonly
  40. */
  41. credit: {
  42. get: DeveloperError.throwInstantiationError,
  43. },
  44. /**
  45. * Gets the tiling scheme used by the provider. This function should
  46. * not be called before {@link TerrainProvider#ready} returns true.
  47. * @memberof TerrainProvider.prototype
  48. * @type {TilingScheme}
  49. * @readonly
  50. */
  51. tilingScheme: {
  52. get: DeveloperError.throwInstantiationError,
  53. },
  54. /**
  55. * Gets a value indicating whether or not the provider is ready for use.
  56. * @memberof TerrainProvider.prototype
  57. * @type {Boolean}
  58. * @readonly
  59. */
  60. ready: {
  61. get: DeveloperError.throwInstantiationError,
  62. },
  63. /**
  64. * Gets a promise that resolves to true when the provider is ready for use.
  65. * @memberof TerrainProvider.prototype
  66. * @type {Promise.<Boolean>}
  67. * @readonly
  68. */
  69. readyPromise: {
  70. get: DeveloperError.throwInstantiationError,
  71. },
  72. /**
  73. * Gets a value indicating whether or not the provider includes a water mask. The water mask
  74. * indicates which areas of the globe are water rather than land, so they can be rendered
  75. * as a reflective surface with animated waves. This function should not be
  76. * called before {@link TerrainProvider#ready} returns true.
  77. * @memberof TerrainProvider.prototype
  78. * @type {Boolean}
  79. * @readonly
  80. */
  81. hasWaterMask: {
  82. get: DeveloperError.throwInstantiationError,
  83. },
  84. /**
  85. * Gets a value indicating whether or not the requested tiles include vertex normals.
  86. * This function should not be called before {@link TerrainProvider#ready} returns true.
  87. * @memberof TerrainProvider.prototype
  88. * @type {Boolean}
  89. * @readonly
  90. */
  91. hasVertexNormals: {
  92. get: DeveloperError.throwInstantiationError,
  93. },
  94. /**
  95. * Gets an object that can be used to determine availability of terrain from this provider, such as
  96. * at points and in rectangles. This function should not be called before
  97. * {@link TerrainProvider#ready} returns true. This property may be undefined if availability
  98. * information is not available.
  99. * @memberof TerrainProvider.prototype
  100. * @type {TileAvailability}
  101. * @readonly
  102. */
  103. availability: {
  104. get: DeveloperError.throwInstantiationError,
  105. },
  106. });
  107. const regularGridIndicesCache = [];
  108. /**
  109. * Gets a list of indices for a triangle mesh representing a regular grid. Calling
  110. * this function multiple times with the same grid width and height returns the
  111. * same list of indices. The total number of vertices must be less than or equal
  112. * to 65536.
  113. *
  114. * @param {Number} width The number of vertices in the regular grid in the horizontal direction.
  115. * @param {Number} height The number of vertices in the regular grid in the vertical direction.
  116. * @returns {Uint16Array|Uint32Array} The list of indices. Uint16Array gets returned for 64KB or less and Uint32Array for 4GB or less.
  117. */
  118. TerrainProvider.getRegularGridIndices = function (width, height) {
  119. //>>includeStart('debug', pragmas.debug);
  120. if (width * height >= CesiumMath.FOUR_GIGABYTES) {
  121. throw new DeveloperError(
  122. "The total number of vertices (width * height) must be less than 4,294,967,296."
  123. );
  124. }
  125. //>>includeEnd('debug');
  126. let byWidth = regularGridIndicesCache[width];
  127. if (!defined(byWidth)) {
  128. regularGridIndicesCache[width] = byWidth = [];
  129. }
  130. let indices = byWidth[height];
  131. if (!defined(indices)) {
  132. if (width * height < CesiumMath.SIXTY_FOUR_KILOBYTES) {
  133. indices = byWidth[height] = new Uint16Array(
  134. (width - 1) * (height - 1) * 6
  135. );
  136. } else {
  137. indices = byWidth[height] = new Uint32Array(
  138. (width - 1) * (height - 1) * 6
  139. );
  140. }
  141. addRegularGridIndices(width, height, indices, 0);
  142. }
  143. return indices;
  144. };
  145. const regularGridAndEdgeIndicesCache = [];
  146. /**
  147. * @private
  148. */
  149. TerrainProvider.getRegularGridIndicesAndEdgeIndices = function (width, height) {
  150. //>>includeStart('debug', pragmas.debug);
  151. if (width * height >= CesiumMath.FOUR_GIGABYTES) {
  152. throw new DeveloperError(
  153. "The total number of vertices (width * height) must be less than 4,294,967,296."
  154. );
  155. }
  156. //>>includeEnd('debug');
  157. let byWidth = regularGridAndEdgeIndicesCache[width];
  158. if (!defined(byWidth)) {
  159. regularGridAndEdgeIndicesCache[width] = byWidth = [];
  160. }
  161. let indicesAndEdges = byWidth[height];
  162. if (!defined(indicesAndEdges)) {
  163. const indices = TerrainProvider.getRegularGridIndices(width, height);
  164. const edgeIndices = getEdgeIndices(width, height);
  165. const westIndicesSouthToNorth = edgeIndices.westIndicesSouthToNorth;
  166. const southIndicesEastToWest = edgeIndices.southIndicesEastToWest;
  167. const eastIndicesNorthToSouth = edgeIndices.eastIndicesNorthToSouth;
  168. const northIndicesWestToEast = edgeIndices.northIndicesWestToEast;
  169. indicesAndEdges = byWidth[height] = {
  170. indices: indices,
  171. westIndicesSouthToNorth: westIndicesSouthToNorth,
  172. southIndicesEastToWest: southIndicesEastToWest,
  173. eastIndicesNorthToSouth: eastIndicesNorthToSouth,
  174. northIndicesWestToEast: northIndicesWestToEast,
  175. };
  176. }
  177. return indicesAndEdges;
  178. };
  179. const regularGridAndSkirtAndEdgeIndicesCache = [];
  180. /**
  181. * @private
  182. */
  183. TerrainProvider.getRegularGridAndSkirtIndicesAndEdgeIndices = function (
  184. width,
  185. height
  186. ) {
  187. //>>includeStart('debug', pragmas.debug);
  188. if (width * height >= CesiumMath.FOUR_GIGABYTES) {
  189. throw new DeveloperError(
  190. "The total number of vertices (width * height) must be less than 4,294,967,296."
  191. );
  192. }
  193. //>>includeEnd('debug');
  194. let byWidth = regularGridAndSkirtAndEdgeIndicesCache[width];
  195. if (!defined(byWidth)) {
  196. regularGridAndSkirtAndEdgeIndicesCache[width] = byWidth = [];
  197. }
  198. let indicesAndEdges = byWidth[height];
  199. if (!defined(indicesAndEdges)) {
  200. const gridVertexCount = width * height;
  201. const gridIndexCount = (width - 1) * (height - 1) * 6;
  202. const edgeVertexCount = width * 2 + height * 2;
  203. const edgeIndexCount = Math.max(0, edgeVertexCount - 4) * 6;
  204. const vertexCount = gridVertexCount + edgeVertexCount;
  205. const indexCount = gridIndexCount + edgeIndexCount;
  206. const edgeIndices = getEdgeIndices(width, height);
  207. const westIndicesSouthToNorth = edgeIndices.westIndicesSouthToNorth;
  208. const southIndicesEastToWest = edgeIndices.southIndicesEastToWest;
  209. const eastIndicesNorthToSouth = edgeIndices.eastIndicesNorthToSouth;
  210. const northIndicesWestToEast = edgeIndices.northIndicesWestToEast;
  211. const indices = IndexDatatype.createTypedArray(vertexCount, indexCount);
  212. addRegularGridIndices(width, height, indices, 0);
  213. TerrainProvider.addSkirtIndices(
  214. westIndicesSouthToNorth,
  215. southIndicesEastToWest,
  216. eastIndicesNorthToSouth,
  217. northIndicesWestToEast,
  218. gridVertexCount,
  219. indices,
  220. gridIndexCount
  221. );
  222. indicesAndEdges = byWidth[height] = {
  223. indices: indices,
  224. westIndicesSouthToNorth: westIndicesSouthToNorth,
  225. southIndicesEastToWest: southIndicesEastToWest,
  226. eastIndicesNorthToSouth: eastIndicesNorthToSouth,
  227. northIndicesWestToEast: northIndicesWestToEast,
  228. indexCountWithoutSkirts: gridIndexCount,
  229. };
  230. }
  231. return indicesAndEdges;
  232. };
  233. /**
  234. * @private
  235. */
  236. TerrainProvider.addSkirtIndices = function (
  237. westIndicesSouthToNorth,
  238. southIndicesEastToWest,
  239. eastIndicesNorthToSouth,
  240. northIndicesWestToEast,
  241. vertexCount,
  242. indices,
  243. offset
  244. ) {
  245. let vertexIndex = vertexCount;
  246. offset = addSkirtIndices(
  247. westIndicesSouthToNorth,
  248. vertexIndex,
  249. indices,
  250. offset
  251. );
  252. vertexIndex += westIndicesSouthToNorth.length;
  253. offset = addSkirtIndices(
  254. southIndicesEastToWest,
  255. vertexIndex,
  256. indices,
  257. offset
  258. );
  259. vertexIndex += southIndicesEastToWest.length;
  260. offset = addSkirtIndices(
  261. eastIndicesNorthToSouth,
  262. vertexIndex,
  263. indices,
  264. offset
  265. );
  266. vertexIndex += eastIndicesNorthToSouth.length;
  267. addSkirtIndices(northIndicesWestToEast, vertexIndex, indices, offset);
  268. };
  269. function getEdgeIndices(width, height) {
  270. const westIndicesSouthToNorth = new Array(height);
  271. const southIndicesEastToWest = new Array(width);
  272. const eastIndicesNorthToSouth = new Array(height);
  273. const northIndicesWestToEast = new Array(width);
  274. let i;
  275. for (i = 0; i < width; ++i) {
  276. northIndicesWestToEast[i] = i;
  277. southIndicesEastToWest[i] = width * height - 1 - i;
  278. }
  279. for (i = 0; i < height; ++i) {
  280. eastIndicesNorthToSouth[i] = (i + 1) * width - 1;
  281. westIndicesSouthToNorth[i] = (height - i - 1) * width;
  282. }
  283. return {
  284. westIndicesSouthToNorth: westIndicesSouthToNorth,
  285. southIndicesEastToWest: southIndicesEastToWest,
  286. eastIndicesNorthToSouth: eastIndicesNorthToSouth,
  287. northIndicesWestToEast: northIndicesWestToEast,
  288. };
  289. }
  290. function addRegularGridIndices(width, height, indices, offset) {
  291. let index = 0;
  292. for (let j = 0; j < height - 1; ++j) {
  293. for (let i = 0; i < width - 1; ++i) {
  294. const upperLeft = index;
  295. const lowerLeft = upperLeft + width;
  296. const lowerRight = lowerLeft + 1;
  297. const upperRight = upperLeft + 1;
  298. indices[offset++] = upperLeft;
  299. indices[offset++] = lowerLeft;
  300. indices[offset++] = upperRight;
  301. indices[offset++] = upperRight;
  302. indices[offset++] = lowerLeft;
  303. indices[offset++] = lowerRight;
  304. ++index;
  305. }
  306. ++index;
  307. }
  308. }
  309. function addSkirtIndices(edgeIndices, vertexIndex, indices, offset) {
  310. let previousIndex = edgeIndices[0];
  311. const length = edgeIndices.length;
  312. for (let i = 1; i < length; ++i) {
  313. const index = edgeIndices[i];
  314. indices[offset++] = previousIndex;
  315. indices[offset++] = index;
  316. indices[offset++] = vertexIndex;
  317. indices[offset++] = vertexIndex;
  318. indices[offset++] = index;
  319. indices[offset++] = vertexIndex + 1;
  320. previousIndex = index;
  321. ++vertexIndex;
  322. }
  323. return offset;
  324. }
  325. /**
  326. * Specifies the quality of terrain created from heightmaps. A value of 1.0 will
  327. * ensure that adjacent heightmap vertices are separated by no more than
  328. * {@link Globe.maximumScreenSpaceError} screen pixels and will probably go very slowly.
  329. * A value of 0.5 will cut the estimated level zero geometric error in half, allowing twice the
  330. * screen pixels between adjacent heightmap vertices and thus rendering more quickly.
  331. * @type {Number}
  332. */
  333. TerrainProvider.heightmapTerrainQuality = 0.25;
  334. /**
  335. * Determines an appropriate geometric error estimate when the geometry comes from a heightmap.
  336. *
  337. * @param {Ellipsoid} ellipsoid The ellipsoid to which the terrain is attached.
  338. * @param {Number} tileImageWidth The width, in pixels, of the heightmap associated with a single tile.
  339. * @param {Number} numberOfTilesAtLevelZero The number of tiles in the horizontal direction at tile level zero.
  340. * @returns {Number} An estimated geometric error.
  341. */
  342. TerrainProvider.getEstimatedLevelZeroGeometricErrorForAHeightmap = function (
  343. ellipsoid,
  344. tileImageWidth,
  345. numberOfTilesAtLevelZero
  346. ) {
  347. return (
  348. (ellipsoid.maximumRadius *
  349. 2 *
  350. Math.PI *
  351. TerrainProvider.heightmapTerrainQuality) /
  352. (tileImageWidth * numberOfTilesAtLevelZero)
  353. );
  354. };
  355. /**
  356. * Requests the geometry for a given tile. This function should not be called before
  357. * {@link TerrainProvider#ready} returns true. The result must include terrain data and
  358. * may optionally include a water mask and an indication of which child tiles are available.
  359. * @function
  360. *
  361. * @param {Number} x The X coordinate of the tile for which to request geometry.
  362. * @param {Number} y The Y coordinate of the tile for which to request geometry.
  363. * @param {Number} level The level of the tile for which to request geometry.
  364. * @param {Request} [request] The request object. Intended for internal use only.
  365. *
  366. * @returns {Promise.<TerrainData>|undefined} A promise for the requested geometry. If this method
  367. * returns undefined instead of a promise, it is an indication that too many requests are already
  368. * pending and the request will be retried later.
  369. */
  370. TerrainProvider.prototype.requestTileGeometry =
  371. DeveloperError.throwInstantiationError;
  372. /**
  373. * Gets the maximum geometric error allowed in a tile at a given level. This function should not be
  374. * called before {@link TerrainProvider#ready} returns true.
  375. * @function
  376. *
  377. * @param {Number} level The tile level for which to get the maximum geometric error.
  378. * @returns {Number} The maximum geometric error.
  379. */
  380. TerrainProvider.prototype.getLevelMaximumGeometricError =
  381. DeveloperError.throwInstantiationError;
  382. /**
  383. * Determines whether data for a tile is available to be loaded.
  384. * @function
  385. *
  386. * @param {Number} x The X coordinate of the tile for which to request geometry.
  387. * @param {Number} y The Y coordinate of the tile for which to request geometry.
  388. * @param {Number} level The level of the tile for which to request geometry.
  389. * @returns {Boolean|undefined} Undefined if not supported by the terrain provider, otherwise true or false.
  390. */
  391. TerrainProvider.prototype.getTileDataAvailable =
  392. DeveloperError.throwInstantiationError;
  393. /**
  394. * Makes sure we load availability data for a tile
  395. * @function
  396. *
  397. * @param {Number} x The X coordinate of the tile for which to request geometry.
  398. * @param {Number} y The Y coordinate of the tile for which to request geometry.
  399. * @param {Number} level The level of the tile for which to request geometry.
  400. * @returns {undefined|Promise<void>} Undefined if nothing need to be loaded or a Promise that resolves when all required tiles are loaded
  401. */
  402. TerrainProvider.prototype.loadTileDataAvailability =
  403. DeveloperError.throwInstantiationError;
  404. export default TerrainProvider;
  405. /**
  406. * A function that is called when an error occurs.
  407. * @callback TerrainProvider.ErrorEvent
  408. *
  409. * @this TerrainProvider
  410. * @param {TileProviderError} err An object holding details about the error that occurred.
  411. */