Skip to content

Commit cf0ee2e

Browse files
author
Ian Lilley
committed
Merge pull request #7 in NSWCCD/nswccd-cesium from ~IAN.LILLEY/nswccd-cesium:bathyProvider to undersea
* commit 'e5819850d1562773ab31033e2704cfaf54d804e4': Added ProxyHeightmapTerrainProvider
2 parents 2b6c876 + e581985 commit cf0ee2e

File tree

2 files changed

+279
-0
lines changed

2 files changed

+279
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
7+
<meta name="description" content="Visualize worldwide, high-resolution terrain.">
8+
<meta name="cesium-sandcastle-labels" content="Tutorials, Showcases">
9+
<title>Cesium Demo</title>
10+
<script type="text/javascript" src="../Sandcastle-header.js"></script>
11+
<script type="text/javascript" src="../../../Build/CesiumUnminified/Cesium.js" nomodule></script>
12+
<script type="module" src="../load-cesium-es6.js"></script>
13+
</head>
14+
<body class="sandcastle-loading" data-sandcastle-bucket="bucket-requirejs.html">
15+
<style>
16+
@import url(../templates/bucket.css);
17+
</style>
18+
<div id="cesiumContainer" class="fullSize"></div>
19+
<div id="loadingOverlay"><h1>Loading...</h1></div>
20+
<div id="toolbar">
21+
<div id="terrainMenu"></div>
22+
<div id="zoomButtons"></div>
23+
<div id="sampleButtons"></div>
24+
</div>
25+
<script id="cesium_sandcastle_script">
26+
function startup(Cesium) {
27+
'use strict';
28+
//Sandcastle_Begin
29+
30+
function getHeightmapTile(x, y, level) {
31+
var width = 16;
32+
var height = 16;
33+
var buffer = new Float32Array(width * height);
34+
35+
for (var yy = 0; yy < height; yy++) {
36+
for (var xx = 0; xx < width; xx++) {
37+
var index = yy * width + xx;
38+
var v = (y + yy / (height - 1)) / Math.pow(2, level);
39+
v = Cesium.Math.PI_OVER_TWO * (2.0 * v - 1.0);
40+
v = Math.sin(20.0 * v);
41+
var value = 100000.0 * v;
42+
buffer[index] = value;
43+
}
44+
}
45+
46+
return Cesium.when.resolve({
47+
buffer : buffer,
48+
width : width,
49+
height : height
50+
});
51+
}
52+
var proxyHeightmapTerrainProvider = new Cesium.ProxyHeightmapTerrainProvider({
53+
callback : getHeightmapTile
54+
});
55+
56+
var viewer = new Cesium.Viewer('cesiumContainer', {
57+
terrainProvider: proxyHeightmapTerrainProvider
58+
});
59+
60+
Sandcastle.addToggleButton('Skirts', viewer.scene.globe.showSkirts, function(checked) {
61+
viewer.scene.globe.showSkirts = checked;
62+
});
63+
viewer.extend(Cesium.viewerCesiumInspectorMixin);
64+
65+
//Sandcastle_End
66+
Sandcastle.finishedLoading();
67+
}
68+
if (typeof Cesium !== 'undefined') {
69+
window.startupCalled = true;
70+
startup(Cesium);
71+
}
72+
</script>
73+
</body>
74+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
import when from '../ThirdParty/when.js';
2+
import defaultValue from './defaultValue.js';
3+
import defined from './defined.js';
4+
import DeveloperError from './DeveloperError.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+
/**
12+
* A simple {@link TerrainProvider} that gets heightmap geometry from a callback function.
13+
*
14+
* @alias ProxyHeightmapTerrainProvider
15+
* @constructor
16+
*
17+
* @param {Object} [options] Object with the following properties:
18+
* @param {TilingScheme} [options.tilingScheme] The tiling scheme specifying how the ellipsoidal
19+
* surface is broken into tiles. If this parameter is not provided, a {@link GeographicTilingScheme}
20+
* is used.
21+
* @param {Ellipsoid} [options.ellipsoid] The ellipsoid. If the tilingScheme is specified,
22+
* this parameter is ignored and the tiling scheme's ellipsoid is used instead. If neither
23+
* parameter is specified, the WGS84 ellipsoid is used.
24+
* @param {Function} [options.callback] The callback function for requesting tiles. The function
25+
* is passed the x, y, and level of the tile and returns a promise to a Float32Array, width, and height.
26+
*
27+
* @see TerrainProvider
28+
*/
29+
function ProxyHeightmapTerrainProvider(options) {
30+
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
31+
32+
//>>includeStart('debug', pragmas.debug);
33+
if (!defined(options.callback)) {
34+
throw new DeveloperError('options.callback is required.');
35+
}
36+
//>>includeEnd('debug');
37+
38+
this._callback = options.callback;
39+
40+
this._tilingScheme = options.tilingScheme;
41+
if (!defined(this._tilingScheme)) {
42+
this._tilingScheme = new GeographicTilingScheme({
43+
ellipsoid : defaultValue(options.ellipsoid, Ellipsoid.WGS84)
44+
});
45+
}
46+
47+
// Note: the 64 below does NOT need to match the actual vertex dimensions, because
48+
// the ellipsoid is significantly smoother than actual terrain.
49+
this._levelZeroMaximumGeometricError = TerrainProvider.getEstimatedLevelZeroGeometricErrorForAHeightmap(this._tilingScheme.ellipsoid, 64, this._tilingScheme.getNumberOfXTilesAtLevel(0));
50+
51+
this._errorEvent = new Event();
52+
this._readyPromise = when.resolve(true);
53+
}
54+
55+
Object.defineProperties(ProxyHeightmapTerrainProvider.prototype, {
56+
/**
57+
* Gets an event that is raised when the terrain provider encounters an asynchronous error. By subscribing
58+
* to the event, you will be notified of the error and can potentially recover from it. Event listeners
59+
* are passed an instance of {@link TileProviderError}.
60+
* @memberof ProxyHeightmapTerrainProvider.prototype
61+
* @type {Event}
62+
*/
63+
errorEvent : {
64+
get : function() {
65+
return this._errorEvent;
66+
}
67+
},
68+
69+
/**
70+
* Gets the credit to display when this terrain provider is active. Typically this is used to credit
71+
* the source of the terrain. This function should not be called before {@link ProxyHeightmapTerrainProvider#ready} returns true.
72+
* @memberof ProxyHeightmapTerrainProvider.prototype
73+
* @type {Credit}
74+
*/
75+
credit : {
76+
get : function() {
77+
return undefined;
78+
}
79+
},
80+
81+
/**
82+
* Gets the tiling scheme used by this provider. This function should
83+
* not be called before {@link ProxyHeightmapTerrainProvider#ready} returns true.
84+
* @memberof ProxyHeightmapTerrainProvider.prototype
85+
* @type {GeographicTilingScheme}
86+
*/
87+
tilingScheme : {
88+
get : function() {
89+
return this._tilingScheme;
90+
}
91+
},
92+
93+
/**
94+
* Gets a value indicating whether or not the provider is ready for use.
95+
* @memberof ProxyHeightmapTerrainProvider.prototype
96+
* @type {Boolean}
97+
*/
98+
ready : {
99+
get : function() {
100+
return true;
101+
}
102+
},
103+
104+
/**
105+
* Gets a promise that resolves to true when the provider is ready for use.
106+
* @memberof ProxyHeightmapTerrainProvider.prototype
107+
* @type {Promise.<Boolean>}
108+
* @readonly
109+
*/
110+
readyPromise : {
111+
get : function() {
112+
return this._readyPromise;
113+
}
114+
},
115+
116+
/**
117+
* Gets a value indicating whether or not the provider includes a water mask. The water mask
118+
* indicates which areas of the globe are water rather than land, so they can be rendered
119+
* as a reflective surface with animated waves. This function should not be
120+
* called before {@link ProxyHeightmapTerrainProvider#ready} returns true.
121+
* @memberof ProxyHeightmapTerrainProvider.prototype
122+
* @type {Boolean}
123+
*/
124+
hasWaterMask : {
125+
get : function() {
126+
return false;
127+
}
128+
},
129+
130+
/**
131+
* Gets a value indicating whether or not the requested tiles include vertex normals.
132+
* This function should not be called before {@link ProxyHeightmapTerrainProvider#ready} returns true.
133+
* @memberof ProxyHeightmapTerrainProvider.prototype
134+
* @type {Boolean}
135+
*/
136+
hasVertexNormals : {
137+
get : function() {
138+
return false;
139+
}
140+
}
141+
});
142+
143+
/**
144+
* Requests the geometry for a given tile. This function should not be called before
145+
* {@link TerrainProvider#ready} returns true. The result includes terrain
146+
* data and indicates that all child tiles are available.
147+
*
148+
* @param {Number} x The X coordinate of the tile for which to request geometry.
149+
* @param {Number} y The Y coordinate of the tile for which to request geometry.
150+
* @param {Number} level The level of the tile for which to request geometry.
151+
* @param {Request} [request] The request object. Intended for internal use only.
152+
*
153+
* @returns {Promise.<TerrainData>|undefined} A promise for the requested geometry. If this method
154+
* returns undefined instead of a promise, it is an indication that too many requests are already
155+
* pending and the request will be retried later.
156+
*/
157+
ProxyHeightmapTerrainProvider.prototype.requestTileGeometry = function(x, y, level, request) {
158+
var promise = this._callback(x, y, level);
159+
if (!defined(promise)) {
160+
return undefined;
161+
}
162+
163+
return when(promise).then(function(result) {
164+
return new HeightmapTerrainData({
165+
buffer : result.buffer,
166+
width : result.width,
167+
height : result.height
168+
});
169+
});
170+
};
171+
172+
/**
173+
* Gets the maximum geometric error allowed in a tile at a given level.
174+
*
175+
* @param {Number} level The tile level for which to get the maximum geometric error.
176+
* @returns {Number} The maximum geometric error.
177+
*/
178+
ProxyHeightmapTerrainProvider.prototype.getLevelMaximumGeometricError = function(level) {
179+
return this._levelZeroMaximumGeometricError / (1 << level);
180+
};
181+
182+
/**
183+
* Determines whether data for a tile is available to be loaded.
184+
*
185+
* @param {Number} x The X coordinate of the tile for which to request geometry.
186+
* @param {Number} y The Y coordinate of the tile for which to request geometry.
187+
* @param {Number} level The level of the tile for which to request geometry.
188+
* @returns {Boolean} Undefined if not supported, otherwise true or false.
189+
*/
190+
ProxyHeightmapTerrainProvider.prototype.getTileDataAvailable = function(x, y, level) {
191+
return undefined;
192+
};
193+
194+
/**
195+
* Makes sure we load availability data for a tile
196+
*
197+
* @param {Number} x The X coordinate of the tile for which to request geometry.
198+
* @param {Number} y The Y coordinate of the tile for which to request geometry.
199+
* @param {Number} level The level of the tile for which to request geometry.
200+
* @returns {undefined|Promise} Undefined if nothing need to be loaded or a Promise that resolves when all required tiles are loaded
201+
*/
202+
ProxyHeightmapTerrainProvider.prototype.loadTileDataAvailability = function(x, y, level) {
203+
return undefined;
204+
};
205+
export default ProxyHeightmapTerrainProvider;

0 commit comments

Comments
 (0)