TerrainProvider.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import defined from './defined.js';
  2. import defineProperties from './defineProperties.js';
  3. import DeveloperError from './DeveloperError.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. 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}
  28. */
  29. errorEvent : {
  30. get : DeveloperError.throwInstantiationError
  31. },
  32. /**
  33. * Gets the credit to display when this terrain provider is active. Typically this is used to credit
  34. * the source of the terrain. This function should
  35. * not be called before {@link TerrainProvider#ready} returns true.
  36. * @memberof TerrainProvider.prototype
  37. * @type {Credit}
  38. */
  39. credit : {
  40. get : DeveloperError.throwInstantiationError
  41. },
  42. /**
  43. * Gets the tiling scheme used by the provider. This function should
  44. * not be called before {@link TerrainProvider#ready} returns true.
  45. * @memberof TerrainProvider.prototype
  46. * @type {TilingScheme}
  47. */
  48. tilingScheme : {
  49. get : DeveloperError.throwInstantiationError
  50. },
  51. /**
  52. * Gets a value indicating whether or not the provider is ready for use.
  53. * @memberof TerrainProvider.prototype
  54. * @type {Boolean}
  55. */
  56. ready : {
  57. get : DeveloperError.throwInstantiationError
  58. },
  59. /**
  60. * Gets a promise that resolves to true when the provider is ready for use.
  61. * @memberof TerrainProvider.prototype
  62. * @type {Promise.<Boolean>}
  63. * @readonly
  64. */
  65. readyPromise : {
  66. get : DeveloperError.throwInstantiationError
  67. },
  68. /**
  69. * Gets a value indicating whether or not the provider includes a water mask. The water mask
  70. * indicates which areas of the globe are water rather than land, so they can be rendered
  71. * as a reflective surface with animated waves. This function should not be
  72. * called before {@link TerrainProvider#ready} returns true.
  73. * @memberof TerrainProvider.prototype
  74. * @type {Boolean}
  75. */
  76. hasWaterMask : {
  77. get : DeveloperError.throwInstantiationError
  78. },
  79. /**
  80. * Gets a value indicating whether or not the requested tiles include vertex normals.
  81. * This function should not be called before {@link TerrainProvider#ready} returns true.
  82. * @memberof TerrainProvider.prototype
  83. * @type {Boolean}
  84. */
  85. hasVertexNormals : {
  86. get : DeveloperError.throwInstantiationError
  87. },
  88. /**
  89. * Gets an object that can be used to determine availability of terrain from this provider, such as
  90. * at points and in rectangles. This function should not be called before
  91. * {@link TerrainProvider#ready} returns true. This property may be undefined if availability
  92. * information is not available.
  93. * @memberof TerrainProvider.prototype
  94. * @type {TileAvailability}
  95. */
  96. availability : {
  97. get : DeveloperError.throwInstantiationError
  98. }
  99. });
  100. var regularGridIndexArrays = [];
  101. /**
  102. * Gets a list of indices for a triangle mesh representing a regular grid. Calling
  103. * this function multiple times with the same grid width and height returns the
  104. * same list of indices. The total number of vertices must be less than or equal
  105. * to 65536.
  106. *
  107. * @param {Number} width The number of vertices in the regular grid in the horizontal direction.
  108. * @param {Number} height The number of vertices in the regular grid in the vertical direction.
  109. * @returns {Uint16Array|Uint32Array} The list of indices. Uint16Array gets returned for 64KB or less and Uint32Array for 4GB or less.
  110. */
  111. TerrainProvider.getRegularGridIndices = function(width, height) {
  112. //>>includeStart('debug', pragmas.debug);
  113. if (width * height >= CesiumMath.FOUR_GIGABYTES) {
  114. throw new DeveloperError('The total number of vertices (width * height) must be less than 4,294,967,295.');
  115. }
  116. //>>includeEnd('debug');
  117. var byWidth = regularGridIndexArrays[width];
  118. if (!defined(byWidth)) {
  119. regularGridIndexArrays[width] = byWidth = [];
  120. }
  121. var indices = byWidth[height];
  122. if (!defined(indices)) {
  123. if (width * height < CesiumMath.SIXTY_FOUR_KILOBYTES) {
  124. indices = byWidth[height] = new Uint16Array((width - 1) * (height - 1) * 6);
  125. } else {
  126. indices = byWidth[height] = new Uint32Array((width - 1) * (height - 1) * 6);
  127. }
  128. var index = 0;
  129. var indicesIndex = 0;
  130. for (var j = 0; j < height - 1; ++j) {
  131. for (var i = 0; i < width - 1; ++i) {
  132. var upperLeft = index;
  133. var lowerLeft = upperLeft + width;
  134. var lowerRight = lowerLeft + 1;
  135. var upperRight = upperLeft + 1;
  136. indices[indicesIndex++] = upperLeft;
  137. indices[indicesIndex++] = lowerLeft;
  138. indices[indicesIndex++] = upperRight;
  139. indices[indicesIndex++] = upperRight;
  140. indices[indicesIndex++] = lowerLeft;
  141. indices[indicesIndex++] = lowerRight;
  142. ++index;
  143. }
  144. ++index;
  145. }
  146. }
  147. return indices;
  148. };
  149. /**
  150. * Specifies the quality of terrain created from heightmaps. A value of 1.0 will
  151. * ensure that adjacent heightmap vertices are separated by no more than
  152. * {@link Globe.maximumScreenSpaceError} screen pixels and will probably go very slowly.
  153. * A value of 0.5 will cut the estimated level zero geometric error in half, allowing twice the
  154. * screen pixels between adjacent heightmap vertices and thus rendering more quickly.
  155. * @type {Number}
  156. */
  157. TerrainProvider.heightmapTerrainQuality = 0.25;
  158. /**
  159. * Determines an appropriate geometric error estimate when the geometry comes from a heightmap.
  160. *
  161. * @param {Ellipsoid} ellipsoid The ellipsoid to which the terrain is attached.
  162. * @param {Number} tileImageWidth The width, in pixels, of the heightmap associated with a single tile.
  163. * @param {Number} numberOfTilesAtLevelZero The number of tiles in the horizontal direction at tile level zero.
  164. * @returns {Number} An estimated geometric error.
  165. */
  166. TerrainProvider.getEstimatedLevelZeroGeometricErrorForAHeightmap = function(ellipsoid, tileImageWidth, numberOfTilesAtLevelZero) {
  167. return ellipsoid.maximumRadius * 2 * Math.PI * TerrainProvider.heightmapTerrainQuality / (tileImageWidth * numberOfTilesAtLevelZero);
  168. };
  169. /**
  170. * Requests the geometry for a given tile. This function should not be called before
  171. * {@link TerrainProvider#ready} returns true. The result must include terrain data and
  172. * may optionally include a water mask and an indication of which child tiles are available.
  173. * @function
  174. *
  175. * @param {Number} x The X coordinate of the tile for which to request geometry.
  176. * @param {Number} y The Y coordinate of the tile for which to request geometry.
  177. * @param {Number} level The level of the tile for which to request geometry.
  178. * @param {Request} [request] The request object. Intended for internal use only.
  179. *
  180. * @returns {Promise.<TerrainData>|undefined} A promise for the requested geometry. If this method
  181. * returns undefined instead of a promise, it is an indication that too many requests are already
  182. * pending and the request will be retried later.
  183. */
  184. TerrainProvider.prototype.requestTileGeometry = DeveloperError.throwInstantiationError;
  185. /**
  186. * Gets the maximum geometric error allowed in a tile at a given level. This function should not be
  187. * called before {@link TerrainProvider#ready} returns true.
  188. * @function
  189. *
  190. * @param {Number} level The tile level for which to get the maximum geometric error.
  191. * @returns {Number} The maximum geometric error.
  192. */
  193. TerrainProvider.prototype.getLevelMaximumGeometricError = DeveloperError.throwInstantiationError;
  194. /**
  195. * Determines whether data for a tile is available to be loaded.
  196. * @function
  197. *
  198. * @param {Number} x The X coordinate of the tile for which to request geometry.
  199. * @param {Number} y The Y coordinate of the tile for which to request geometry.
  200. * @param {Number} level The level of the tile for which to request geometry.
  201. * @returns {Boolean} Undefined if not supported by the terrain provider, otherwise true or false.
  202. */
  203. TerrainProvider.prototype.getTileDataAvailable = DeveloperError.throwInstantiationError;
  204. /**
  205. * Makes sure we load availability data for a tile
  206. * @function
  207. *
  208. * @param {Number} x The X coordinate of the tile for which to request geometry.
  209. * @param {Number} y The Y coordinate of the tile for which to request geometry.
  210. * @param {Number} level The level of the tile for which to request geometry.
  211. * @returns {undefined|Promise} Undefined if nothing need to be loaded or a Promise that resolves when all required tiles are loaded
  212. */
  213. TerrainProvider.prototype.loadTileDataAvailability = DeveloperError.throwInstantiationError;
  214. export default TerrainProvider;