Terrain.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import Check from "../Core/Check.js";
  2. import Event from "../Core/Event.js";
  3. import createWorldTerrainAsync from "../Core/createWorldTerrainAsync.js";
  4. /**
  5. * A helper to manage async operations of a terrain provider.
  6. *
  7. * @alias Terrain
  8. * @constructor
  9. *
  10. * @see Terrain.fromWorldTerrain
  11. * @see CesiumTerrainProvider
  12. * @see VRTheWorldTerrainProvider
  13. * @see GoogleEarthEnterpriseTerrainProvider
  14. *
  15. * @example
  16. * // Create
  17. * const viewer = new Cesium.Viewer("cesiumContainer", {
  18. * terrain: new Cesium.Terrain(Cesium.CesiumTerrainProvider.fromUrl("https://myTestTerrain.com"));
  19. * });
  20. *
  21. * @example
  22. * // Handle loading events
  23. * const terrain = new Cesium.Terrain(Cesium.CesiumTerrainProvider.fromUrl("https://myTestTerrain.com"));
  24. *
  25. * scene.setTerrain(terrain);
  26. *
  27. * terrain.readyEvent.addEventListener(provider => {
  28. * scene.globe.enableLighting = true;
  29. *
  30. * terrain.provider.errorEvent.addEventListener(error => {
  31. * alert(`Encountered an error while loading terrain tiles! ${error}`);
  32. * });
  33. * });
  34. *
  35. * terrain.errorEvent.addEventListener(error => {
  36. * alert(`Encountered an error while creating terrain! ${error}`);
  37. * });
  38. *
  39. * @param {Promise<TerrainProvider>} terrainProviderPromise A promise which resolves to a terrain provider
  40. */
  41. function Terrain(terrainProviderPromise) {
  42. //>>includeStart('debug', pragmas.debug);
  43. Check.typeOf.object("terrainProviderPromise", terrainProviderPromise);
  44. //>>includeEnd('debug');
  45. this._ready = false;
  46. this._provider = undefined;
  47. this._errorEvent = new Event();
  48. this._readyEvent = new Event();
  49. handlePromise(this, terrainProviderPromise);
  50. }
  51. Object.defineProperties(Terrain.prototype, {
  52. /**
  53. * Gets an event that is raised when the terrain provider encounters an asynchronous error. By subscribing
  54. * to the event, you will be notified of the error and can potentially recover from it. Event listeners
  55. * are passed an instance of the thrown error.
  56. * @memberof Terrain.prototype
  57. * @type {Event<Terrain.ErrorEventCallback>}
  58. * @readonly
  59. */
  60. errorEvent: {
  61. get: function () {
  62. return this._errorEvent;
  63. },
  64. },
  65. /**
  66. * Gets an event that is raised when the terrain provider has been successfully created. Event listeners
  67. * are passed the created instance of {@link TerrainProvider}.
  68. * @memberof Terrain.prototype
  69. * @type {Event<Terrain.ReadyEventCallback>}
  70. * @readonly
  71. */
  72. readyEvent: {
  73. get: function () {
  74. return this._readyEvent;
  75. },
  76. },
  77. /**
  78. * Returns true when the terrain provider has been successfully created. Otherwise, returns false.
  79. * @memberof Viewer.prototype
  80. *
  81. * @type {boolean}
  82. * @readonly
  83. */
  84. ready: {
  85. get: function () {
  86. return this._ready;
  87. },
  88. },
  89. /**
  90. * The terrain provider providing surface geometry to a globe. Do not use until {@link Terrain.readyEvent} is raised.
  91. * @memberof Viewer.prototype
  92. *
  93. * @type {TerrainProvider}
  94. * @readonly
  95. */
  96. provider: {
  97. get: function () {
  98. return this._provider;
  99. },
  100. },
  101. });
  102. /**
  103. * Creates a {@link Terrain} instance for {@link https://cesium.com/content/#cesium-world-terrain|Cesium World Terrain}.
  104. *
  105. * @function
  106. *
  107. * @param {Object} [options] Object with the following properties:
  108. * @param {Boolean} [options.requestVertexNormals=false] Flag that indicates if the client should request additional lighting information from the server if available.
  109. * @param {Boolean} [options.requestWaterMask=false] Flag that indicates if the client should request per tile water masks from the server if available.
  110. * @returns {Terrain} An asynchronous helper object for a CesiumTerrainProvider
  111. *
  112. * @see Ion
  113. * @see createWorldTerrainAsync
  114. *
  115. * @example
  116. * // Create Cesium World Terrain with default settings
  117. * const viewer = new Cesium.Viewer("cesiumContainer", {
  118. * terrain: Cesium.Terrain.fromWorldTerrain()
  119. * });
  120. *
  121. * @example
  122. * // Create Cesium World Terrain with water and normals.
  123. * const viewer1 = new Cesium.Viewer("cesiumContainer", {
  124. * terrain: Cesium.Terrain.fromWorldTerrain({
  125. * requestWaterMask: true,
  126. * requestVertexNormals: true
  127. * });
  128. * });
  129. *
  130. * @example
  131. * // Handle loading events
  132. * const terrain = Cesium.Terrain.fromWorldTerrain();
  133. *
  134. * scene.setTerrain(terrain);
  135. *
  136. * terrain.readyEvent.addEventListener(provider => {
  137. * scene.globe.enableLighting = true;
  138. *
  139. * terrain.provider.errorEvent.addEventListener(error => {
  140. * alert(`Encountered an error while loading terrain tiles! ${error}`);
  141. * });
  142. * });
  143. *
  144. * terrain.errorEvent.addEventListener(error => {
  145. * alert(`Encountered an error while creating terrain! ${error}`);
  146. * });
  147. */
  148. Terrain.fromWorldTerrain = function (options) {
  149. return new Terrain(createWorldTerrainAsync(options));
  150. };
  151. function handleError(errorEvent, error) {
  152. if (errorEvent.numberOfListeners > 0) {
  153. errorEvent.raiseEvent(error);
  154. } else {
  155. // Default handler is to log to the console
  156. console.error(error);
  157. }
  158. }
  159. async function handlePromise(instance, promise) {
  160. let provider;
  161. try {
  162. provider = await Promise.resolve(promise);
  163. instance._provider = provider;
  164. instance._ready = true;
  165. instance._readyEvent.raiseEvent(provider);
  166. } catch (error) {
  167. handleError(instance._errorEvent, error);
  168. }
  169. }
  170. export default Terrain;
  171. /**
  172. * A function that is called when an error occurs.
  173. * @callback Terrain.ErrorEventCallback
  174. *
  175. * @this Terrain
  176. * @param {Error} err An object holding details about the error that occurred.
  177. */
  178. /**
  179. * A function that is called when the provider has been created
  180. * @callback Terrain.ReadyEventCallback
  181. *
  182. * @this Terrain
  183. * @param {TerrainProvider} provider The created terrain provider.
  184. */