CustomHeightmapTerrainProvider.js 10 KB

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