VRTheWorldTerrainProvider.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. import Credit from "./Credit.js";
  2. import defaultValue from "./defaultValue.js";
  3. import defer from "./defer.js";
  4. import defined from "./defined.js";
  5. import DeveloperError from "./DeveloperError.js";
  6. import Ellipsoid from "./Ellipsoid.js";
  7. import Event from "./Event.js";
  8. import GeographicTilingScheme from "./GeographicTilingScheme.js";
  9. import getImagePixels from "./getImagePixels.js";
  10. import HeightmapTerrainData from "./HeightmapTerrainData.js";
  11. import CesiumMath from "./Math.js";
  12. import Rectangle from "./Rectangle.js";
  13. import Resource from "./Resource.js";
  14. import TerrainProvider from "./TerrainProvider.js";
  15. import TileProviderError from "./TileProviderError.js";
  16. function DataRectangle(rectangle, maxLevel) {
  17. this.rectangle = rectangle;
  18. this.maxLevel = maxLevel;
  19. }
  20. /**
  21. * A {@link TerrainProvider} that produces terrain geometry by tessellating height maps
  22. * retrieved from a {@link http://vr-theworld.com/|VT MÄK VR-TheWorld server}.
  23. *
  24. * @alias VRTheWorldTerrainProvider
  25. * @constructor
  26. *
  27. * @param {Object} options Object with the following properties:
  28. * @param {Resource|String} options.url The URL of the VR-TheWorld TileMap.
  29. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid. If this parameter is not
  30. * specified, the WGS84 ellipsoid is used.
  31. * @param {Credit|String} [options.credit] A credit for the data source, which is displayed on the canvas.
  32. *
  33. *
  34. * @example
  35. * const terrainProvider = new Cesium.VRTheWorldTerrainProvider({
  36. * url : 'https://www.vr-theworld.com/vr-theworld/tiles1.0.0/73/'
  37. * });
  38. * viewer.terrainProvider = terrainProvider;
  39. *
  40. * @see TerrainProvider
  41. */
  42. function VRTheWorldTerrainProvider(options) {
  43. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  44. //>>includeStart('debug', pragmas.debug);
  45. if (!defined(options.url)) {
  46. throw new DeveloperError("options.url is required.");
  47. }
  48. //>>includeEnd('debug');
  49. const resource = Resource.createIfNeeded(options.url);
  50. this._resource = resource;
  51. this._errorEvent = new Event();
  52. this._ready = false;
  53. this._readyPromise = defer();
  54. this._terrainDataStructure = {
  55. heightScale: 1.0 / 1000.0,
  56. heightOffset: -1000.0,
  57. elementsPerHeight: 3,
  58. stride: 4,
  59. elementMultiplier: 256.0,
  60. isBigEndian: true,
  61. lowestEncodedHeight: 0,
  62. highestEncodedHeight: 256 * 256 * 256 - 1,
  63. };
  64. let credit = options.credit;
  65. if (typeof credit === "string") {
  66. credit = new Credit(credit);
  67. }
  68. this._credit = credit;
  69. this._tilingScheme = undefined;
  70. this._rectangles = [];
  71. const that = this;
  72. let metadataError;
  73. const ellipsoid = defaultValue(options.ellipsoid, Ellipsoid.WGS84);
  74. function metadataSuccess(xml) {
  75. const srs = xml.getElementsByTagName("SRS")[0].textContent;
  76. if (srs === "EPSG:4326") {
  77. that._tilingScheme = new GeographicTilingScheme({ ellipsoid: ellipsoid });
  78. } else {
  79. metadataFailure(`SRS ${srs} is not supported.`);
  80. return;
  81. }
  82. const tileFormat = xml.getElementsByTagName("TileFormat")[0];
  83. that._heightmapWidth = parseInt(tileFormat.getAttribute("width"), 10);
  84. that._heightmapHeight = parseInt(tileFormat.getAttribute("height"), 10);
  85. that._levelZeroMaximumGeometricError = TerrainProvider.getEstimatedLevelZeroGeometricErrorForAHeightmap(
  86. ellipsoid,
  87. Math.min(that._heightmapWidth, that._heightmapHeight),
  88. that._tilingScheme.getNumberOfXTilesAtLevel(0)
  89. );
  90. const dataRectangles = xml.getElementsByTagName("DataExtent");
  91. for (let i = 0; i < dataRectangles.length; ++i) {
  92. const dataRectangle = dataRectangles[i];
  93. const west = CesiumMath.toRadians(
  94. parseFloat(dataRectangle.getAttribute("minx"))
  95. );
  96. const south = CesiumMath.toRadians(
  97. parseFloat(dataRectangle.getAttribute("miny"))
  98. );
  99. const east = CesiumMath.toRadians(
  100. parseFloat(dataRectangle.getAttribute("maxx"))
  101. );
  102. const north = CesiumMath.toRadians(
  103. parseFloat(dataRectangle.getAttribute("maxy"))
  104. );
  105. const maxLevel = parseInt(dataRectangle.getAttribute("maxlevel"), 10);
  106. that._rectangles.push(
  107. new DataRectangle(new Rectangle(west, south, east, north), maxLevel)
  108. );
  109. }
  110. that._ready = true;
  111. that._readyPromise.resolve(true);
  112. }
  113. function metadataFailure(e) {
  114. const message = defaultValue(
  115. e,
  116. `An error occurred while accessing ${that._resource.url}.`
  117. );
  118. metadataError = TileProviderError.handleError(
  119. metadataError,
  120. that,
  121. that._errorEvent,
  122. message,
  123. undefined,
  124. undefined,
  125. undefined,
  126. requestMetadata
  127. );
  128. }
  129. function requestMetadata() {
  130. that._resource.fetchXML().then(metadataSuccess).catch(metadataFailure);
  131. }
  132. requestMetadata();
  133. }
  134. Object.defineProperties(VRTheWorldTerrainProvider.prototype, {
  135. /**
  136. * Gets an event that is raised when the terrain provider encounters an asynchronous error. By subscribing
  137. * to the event, you will be notified of the error and can potentially recover from it. Event listeners
  138. * are passed an instance of {@link TileProviderError}.
  139. * @memberof VRTheWorldTerrainProvider.prototype
  140. * @type {Event}
  141. * @readonly
  142. */
  143. errorEvent: {
  144. get: function () {
  145. return this._errorEvent;
  146. },
  147. },
  148. /**
  149. * Gets the credit to display when this terrain provider is active. Typically this is used to credit
  150. * the source of the terrain. This function should not be called before {@link VRTheWorldTerrainProvider#ready} returns true.
  151. * @memberof VRTheWorldTerrainProvider.prototype
  152. * @type {Credit}
  153. * @readonly
  154. */
  155. credit: {
  156. get: function () {
  157. return this._credit;
  158. },
  159. },
  160. /**
  161. * Gets the tiling scheme used by this provider. This function should
  162. * not be called before {@link VRTheWorldTerrainProvider#ready} returns true.
  163. * @memberof VRTheWorldTerrainProvider.prototype
  164. * @type {GeographicTilingScheme}
  165. * @readonly
  166. */
  167. tilingScheme: {
  168. get: function () {
  169. //>>includeStart('debug', pragmas.debug);
  170. if (!this.ready) {
  171. throw new DeveloperError(
  172. "requestTileGeometry must not be called before ready returns true."
  173. );
  174. }
  175. //>>includeEnd('debug');
  176. return this._tilingScheme;
  177. },
  178. },
  179. /**
  180. * Gets a value indicating whether or not the provider is ready for use.
  181. * @memberof VRTheWorldTerrainProvider.prototype
  182. * @type {Boolean}
  183. * @readonly
  184. */
  185. ready: {
  186. get: function () {
  187. return this._ready;
  188. },
  189. },
  190. /**
  191. * Gets a promise that resolves to true when the provider is ready for use.
  192. * @memberof VRTheWorldTerrainProvider.prototype
  193. * @type {Promise.<Boolean>}
  194. * @readonly
  195. */
  196. readyPromise: {
  197. get: function () {
  198. return this._readyPromise.promise;
  199. },
  200. },
  201. /**
  202. * Gets a value indicating whether or not the provider includes a water mask. The water mask
  203. * indicates which areas of the globe are water rather than land, so they can be rendered
  204. * as a reflective surface with animated waves. This function should not be
  205. * called before {@link VRTheWorldTerrainProvider#ready} returns true.
  206. * @memberof VRTheWorldTerrainProvider.prototype
  207. * @type {Boolean}
  208. * @readonly
  209. */
  210. hasWaterMask: {
  211. get: function () {
  212. return false;
  213. },
  214. },
  215. /**
  216. * Gets a value indicating whether or not the requested tiles include vertex normals.
  217. * This function should not be called before {@link VRTheWorldTerrainProvider#ready} returns true.
  218. * @memberof VRTheWorldTerrainProvider.prototype
  219. * @type {Boolean}
  220. * @readonly
  221. */
  222. hasVertexNormals: {
  223. get: function () {
  224. return false;
  225. },
  226. },
  227. /**
  228. * Gets an object that can be used to determine availability of terrain from this provider, such as
  229. * at points and in rectangles. This function should not be called before
  230. * {@link TerrainProvider#ready} returns true. This property may be undefined if availability
  231. * information is not available.
  232. * @memberof VRTheWorldTerrainProvider.prototype
  233. * @type {TileAvailability}
  234. * @readonly
  235. */
  236. availability: {
  237. get: function () {
  238. return undefined;
  239. },
  240. },
  241. });
  242. /**
  243. * Requests the geometry for a given tile. This function should not be called before
  244. * {@link VRTheWorldTerrainProvider#ready} returns true. The result includes terrain
  245. * data and indicates that all child tiles are available.
  246. *
  247. * @param {Number} x The X coordinate of the tile for which to request geometry.
  248. * @param {Number} y The Y coordinate of the tile for which to request geometry.
  249. * @param {Number} level The level of the tile for which to request geometry.
  250. * @param {Request} [request] The request object. Intended for internal use only.
  251. * @returns {Promise.<TerrainData>|undefined} A promise for the requested geometry. If this method
  252. * returns undefined instead of a promise, it is an indication that too many requests are already
  253. * pending and the request will be retried later.
  254. */
  255. VRTheWorldTerrainProvider.prototype.requestTileGeometry = function (
  256. x,
  257. y,
  258. level,
  259. request
  260. ) {
  261. //>>includeStart('debug', pragmas.debug);
  262. if (!this.ready) {
  263. throw new DeveloperError(
  264. "requestTileGeometry must not be called before ready returns true."
  265. );
  266. }
  267. //>>includeEnd('debug');
  268. const yTiles = this._tilingScheme.getNumberOfYTilesAtLevel(level);
  269. const resource = this._resource.getDerivedResource({
  270. url: `${level}/${x}/${yTiles - y - 1}.tif`,
  271. queryParameters: {
  272. cesium: true,
  273. },
  274. request: request,
  275. });
  276. const promise = resource.fetchImage({
  277. preferImageBitmap: true,
  278. });
  279. if (!defined(promise)) {
  280. return undefined;
  281. }
  282. const that = this;
  283. return Promise.resolve(promise).then(function (image) {
  284. return new HeightmapTerrainData({
  285. buffer: getImagePixels(image),
  286. width: that._heightmapWidth,
  287. height: that._heightmapHeight,
  288. childTileMask: getChildMask(that, x, y, level),
  289. structure: that._terrainDataStructure,
  290. });
  291. });
  292. };
  293. /**
  294. * Gets the maximum geometric error allowed in a tile at a given level.
  295. *
  296. * @param {Number} level The tile level for which to get the maximum geometric error.
  297. * @returns {Number} The maximum geometric error.
  298. */
  299. VRTheWorldTerrainProvider.prototype.getLevelMaximumGeometricError = function (
  300. level
  301. ) {
  302. //>>includeStart('debug', pragmas.debug);
  303. if (!this.ready) {
  304. throw new DeveloperError(
  305. "requestTileGeometry must not be called before ready returns true."
  306. );
  307. }
  308. //>>includeEnd('debug');
  309. return this._levelZeroMaximumGeometricError / (1 << level);
  310. };
  311. const rectangleScratch = new Rectangle();
  312. function getChildMask(provider, x, y, level) {
  313. const tilingScheme = provider._tilingScheme;
  314. const rectangles = provider._rectangles;
  315. const parentRectangle = tilingScheme.tileXYToRectangle(x, y, level);
  316. let childMask = 0;
  317. for (let i = 0; i < rectangles.length && childMask !== 15; ++i) {
  318. const rectangle = rectangles[i];
  319. if (rectangle.maxLevel <= level) {
  320. continue;
  321. }
  322. const testRectangle = rectangle.rectangle;
  323. const intersection = Rectangle.intersection(
  324. testRectangle,
  325. parentRectangle,
  326. rectangleScratch
  327. );
  328. if (defined(intersection)) {
  329. // Parent tile is inside this rectangle, so at least one child is, too.
  330. if (
  331. isTileInRectangle(tilingScheme, testRectangle, x * 2, y * 2, level + 1)
  332. ) {
  333. childMask |= 4; // northwest
  334. }
  335. if (
  336. isTileInRectangle(
  337. tilingScheme,
  338. testRectangle,
  339. x * 2 + 1,
  340. y * 2,
  341. level + 1
  342. )
  343. ) {
  344. childMask |= 8; // northeast
  345. }
  346. if (
  347. isTileInRectangle(
  348. tilingScheme,
  349. testRectangle,
  350. x * 2,
  351. y * 2 + 1,
  352. level + 1
  353. )
  354. ) {
  355. childMask |= 1; // southwest
  356. }
  357. if (
  358. isTileInRectangle(
  359. tilingScheme,
  360. testRectangle,
  361. x * 2 + 1,
  362. y * 2 + 1,
  363. level + 1
  364. )
  365. ) {
  366. childMask |= 2; // southeast
  367. }
  368. }
  369. }
  370. return childMask;
  371. }
  372. function isTileInRectangle(tilingScheme, rectangle, x, y, level) {
  373. const tileRectangle = tilingScheme.tileXYToRectangle(x, y, level);
  374. return defined(
  375. Rectangle.intersection(tileRectangle, rectangle, rectangleScratch)
  376. );
  377. }
  378. /**
  379. * Determines whether data for a tile is available to be loaded.
  380. *
  381. * @param {Number} x The X coordinate of the tile for which to request geometry.
  382. * @param {Number} y The Y coordinate of the tile for which to request geometry.
  383. * @param {Number} level The level of the tile for which to request geometry.
  384. * @returns {Boolean|undefined} Undefined if not supported, otherwise true or false.
  385. */
  386. VRTheWorldTerrainProvider.prototype.getTileDataAvailable = function (
  387. x,
  388. y,
  389. level
  390. ) {
  391. return undefined;
  392. };
  393. /**
  394. * Makes sure we load availability data for a tile
  395. *
  396. * @param {Number} x The X coordinate of the tile for which to request geometry.
  397. * @param {Number} y The Y coordinate of the tile for which to request geometry.
  398. * @param {Number} level The level of the tile for which to request geometry.
  399. * @returns {undefined|Promise<void>} Undefined if nothing need to be loaded or a Promise that resolves when all required tiles are loaded
  400. */
  401. VRTheWorldTerrainProvider.prototype.loadTileDataAvailability = function (
  402. x,
  403. y,
  404. level
  405. ) {
  406. return undefined;
  407. };
  408. export default VRTheWorldTerrainProvider;