CustomHeightmapTerrainProvider.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. import Check from "./Check.js";
  2. import Credit from "./Credit.js";
  3. import defaultValue from "./defaultValue.js";
  4. import defined from "./defined.js";
  5. import Ellipsoid from "./Ellipsoid.js";
  6. import Event from "./Event.js";
  7. import GeographicTilingScheme from "./GeographicTilingScheme.js";
  8. import HeightmapTerrainData from "./HeightmapTerrainData.js";
  9. import TerrainProvider from "./TerrainProvider.js";
  10. /**
  11. * @callback CustomHeightmapTerrainProvider.GeometryCallback
  12. * @param {Number} x The X coordinate of the tile for which to request geometry.
  13. * @param {Number} y The Y coordinate of the tile for which to request geometry.
  14. * @param {Number} level The level of the tile for which to request geometry.
  15. * @returns {Int8Array|Uint8Array|Int16Array|Uint16Array|Int32Array|Uint32Array|Float32Array|Float64Array|Number[]|Promise.<Int8Array|Uint8Array|Int16Array|Uint16Array|Int32Array|Uint32Array|Float32Array|Float64Array|Number[]>|undefined} An array or a promise to an array of heights in row-major order. If undefined, the globe will render the parent tile.
  16. */
  17. /**
  18. * A simple {@link TerrainProvider} that gets height values from a callback function.
  19. * It can be used for procedurally generated terrain or as a way to load custom
  20. * heightmap data without creating a subclass of {@link TerrainProvider}.
  21. *
  22. * There are some limitations such as no water mask, no vertex normals, and no
  23. * availability, so a full-fledged {@link TerrainProvider} subclass is better suited
  24. * for these more sophisticated use cases.
  25. *
  26. * @alias CustomHeightmapTerrainProvider
  27. * @constructor
  28. *
  29. * @param {Object} options Object with the following properties:
  30. * @param {CustomHeightmapTerrainProvider.GeometryCallback} options.callback The callback function for requesting tile geometry.
  31. * @param {Number} options.width The number of columns per heightmap tile.
  32. * @param {Number} options.height The number of rows per heightmap tile.
  33. * @param {TilingScheme} [options.tilingScheme] The tiling scheme specifying how the ellipsoidal
  34. * surface is broken into tiles. If this parameter is not provided, a {@link GeographicTilingScheme}
  35. * is used.
  36. * @param {Ellipsoid} [options.ellipsoid] The ellipsoid. If the tilingScheme is specified,
  37. * this parameter is ignored and the tiling scheme's ellipsoid is used instead. If neither
  38. * parameter is specified, the WGS84 ellipsoid is used.
  39. * @param {Credit|String} [options.credit] A credit for the data source, which is displayed on the canvas.
  40. *
  41. * @example
  42. * const viewer = new Cesium.Viewer("cesiumContainer", {
  43. * terrainProvider: new Cesium.CustomHeightmapTerrainProvider({
  44. * width: 32,
  45. * height: 32,
  46. * callback: function (x, y, level) {
  47. * return new Float32Array(32 * 32); // all zeros
  48. * },
  49. * }),
  50. * });
  51. *
  52. * @see TerrainProvider
  53. */
  54. function CustomHeightmapTerrainProvider(options) {
  55. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  56. //>>includeStart('debug', pragmas.debug);
  57. Check.defined("options.callback", options.callback);
  58. Check.defined("options.width", options.width);
  59. Check.defined("options.height", options.height);
  60. //>>includeEnd('debug');
  61. this._callback = options.callback;
  62. this._tilingScheme = options.tilingScheme;
  63. if (!defined(this._tilingScheme)) {
  64. this._tilingScheme = new GeographicTilingScheme({
  65. ellipsoid: defaultValue(options.ellipsoid, Ellipsoid.WGS84),
  66. });
  67. }
  68. this._width = options.width;
  69. this._height = options.height;
  70. const maxTileDimensions = Math.max(this._width, this._height);
  71. this._levelZeroMaximumGeometricError = TerrainProvider.getEstimatedLevelZeroGeometricErrorForAHeightmap(
  72. this._tilingScheme.ellipsoid,
  73. maxTileDimensions,
  74. this._tilingScheme.getNumberOfXTilesAtLevel(0)
  75. );
  76. this._errorEvent = new Event();
  77. let credit = options.credit;
  78. if (typeof credit === "string") {
  79. credit = new Credit(credit);
  80. }
  81. this._credit = credit;
  82. this._readyPromise = Promise.resolve(true);
  83. }
  84. Object.defineProperties(CustomHeightmapTerrainProvider.prototype, {
  85. /**
  86. * Gets an event that is raised when the terrain provider encounters an asynchronous error. By subscribing
  87. * to the event, you will be notified of the error and can potentially recover from it. Event listeners
  88. * are passed an instance of {@link TileProviderError}.
  89. * @memberof CustomHeightmapTerrainProvider.prototype
  90. * @type {Event}
  91. * @readonly
  92. */
  93. errorEvent: {
  94. get: function () {
  95. return this._errorEvent;
  96. },
  97. },
  98. /**
  99. * Gets the credit to display when this terrain provider is active. Typically this is used to credit
  100. * the source of the terrain.
  101. * @memberof CustomHeightmapTerrainProvider.prototype
  102. * @type {Credit}
  103. * @readonly
  104. */
  105. credit: {
  106. get: function () {
  107. return this._credit;
  108. },
  109. },
  110. /**
  111. * Gets the tiling scheme used by this provider.
  112. * @memberof CustomHeightmapTerrainProvider.prototype
  113. * @type {TilingScheme}
  114. * @readonly
  115. */
  116. tilingScheme: {
  117. get: function () {
  118. return this._tilingScheme;
  119. },
  120. },
  121. /**
  122. * Gets a value indicating whether or not the provider is ready for use.
  123. * @memberof CustomHeightmapTerrainProvider.prototype
  124. * @type {Boolean}
  125. * @readonly
  126. */
  127. ready: {
  128. get: function () {
  129. return true;
  130. },
  131. },
  132. /**
  133. * Gets a promise that resolves to true when the provider is ready for use.
  134. * @memberof CustomHeightmapTerrainProvider.prototype
  135. * @type {Promise.<Boolean>}
  136. * @readonly
  137. */
  138. readyPromise: {
  139. get: function () {
  140. return this._readyPromise;
  141. },
  142. },
  143. /**
  144. * Gets a value indicating whether or not the provider includes a water mask. The water mask
  145. * indicates which areas of the globe are water rather than land, so they can be rendered
  146. * as a reflective surface with animated waves.
  147. * Water mask is not supported by {@link CustomHeightmapTerrainProvider}, so the return
  148. * value will always be false.
  149. * @memberof CustomHeightmapTerrainProvider.prototype
  150. * @type {Boolean}
  151. * @readonly
  152. */
  153. hasWaterMask: {
  154. get: function () {
  155. return false;
  156. },
  157. },
  158. /**
  159. * Gets a value indicating whether or not the requested tiles include vertex normals.
  160. * Vertex normals are not supported by {@link CustomHeightmapTerrainProvider}, so the return
  161. * value will always be false.
  162. * @memberof CustomHeightmapTerrainProvider.prototype
  163. * @type {Boolean}
  164. * @readonly
  165. */
  166. hasVertexNormals: {
  167. get: function () {
  168. return false;
  169. },
  170. },
  171. /**
  172. * Gets the number of columns per heightmap tile.
  173. * @memberof CustomHeightmapTerrainProvider.prototype
  174. * @type {Boolean}
  175. * @readonly
  176. */
  177. width: {
  178. get: function () {
  179. return this._width;
  180. },
  181. },
  182. /**
  183. * Gets the number of rows per heightmap tile.
  184. * @memberof CustomHeightmapTerrainProvider.prototype
  185. * @type {Boolean}
  186. * @readonly
  187. */
  188. height: {
  189. get: function () {
  190. return this._height;
  191. },
  192. },
  193. });
  194. /**
  195. * Requests the geometry for a given tile. The result includes terrain
  196. * data and indicates that all child tiles are available.
  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. * @param {Request} [request] The request object. Intended for internal use only.
  202. *
  203. * @returns {Promise.<TerrainData>|undefined} A promise for the requested geometry. If this method
  204. * returns undefined instead of a promise, it is an indication that too many requests are already
  205. * pending and the request will be retried later.
  206. */
  207. CustomHeightmapTerrainProvider.prototype.requestTileGeometry = function (
  208. x,
  209. y,
  210. level,
  211. request
  212. ) {
  213. const promise = this._callback(x, y, level);
  214. if (!defined(promise)) {
  215. return undefined;
  216. }
  217. const width = this._width;
  218. const height = this._height;
  219. return Promise.resolve(promise).then(function (heightmapData) {
  220. let buffer = heightmapData;
  221. if (Array.isArray(buffer)) {
  222. // HeightmapTerrainData expects a TypedArray, so convert from Number[] to Float64Array
  223. buffer = new Float64Array(buffer);
  224. }
  225. return new HeightmapTerrainData({
  226. buffer: buffer,
  227. width: width,
  228. height: height,
  229. });
  230. });
  231. };
  232. /**
  233. * Gets the maximum geometric error allowed in a tile at a given level.
  234. *
  235. * @param {Number} level The tile level for which to get the maximum geometric error.
  236. * @returns {Number} The maximum geometric error.
  237. */
  238. CustomHeightmapTerrainProvider.prototype.getLevelMaximumGeometricError = function (
  239. level
  240. ) {
  241. return this._levelZeroMaximumGeometricError / (1 << level);
  242. };
  243. /**
  244. * Determines whether data for a tile is available to be loaded.
  245. *
  246. * @param {Number} x The X coordinate of the tile for which to request geometry.
  247. * @param {Number} y The Y coordinate of the tile for which to request geometry.
  248. * @param {Number} level The level of the tile for which to request geometry.
  249. * @returns {Boolean|undefined} Undefined if not supported, otherwise true or false.
  250. */
  251. CustomHeightmapTerrainProvider.prototype.getTileDataAvailable = function (
  252. x,
  253. y,
  254. level
  255. ) {
  256. return undefined;
  257. };
  258. /**
  259. * Makes sure we load availability data for a tile
  260. *
  261. * @param {Number} x The X coordinate of the tile for which to request geometry.
  262. * @param {Number} y The Y coordinate of the tile for which to request geometry.
  263. * @param {Number} level The level of the tile for which to request geometry.
  264. * @returns {undefined|Promise<void>} Undefined if nothing need to be loaded or a Promise that resolves when all required tiles are loaded
  265. */
  266. CustomHeightmapTerrainProvider.prototype.loadTileDataAvailability = function (
  267. x,
  268. y,
  269. level
  270. ) {
  271. return undefined;
  272. };
  273. export default CustomHeightmapTerrainProvider;