Skip to content

Commit 6b3efd4

Browse files
committed
Added changed files
1 parent fe29b9e commit 6b3efd4

File tree

5 files changed

+206
-142
lines changed

5 files changed

+206
-142
lines changed

src/actions.ts

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,46 @@
11
type SetExaggeration = {
2-
type: 'set-exaggeration',
3-
value: number
4-
}
2+
type: "set-exaggeration";
3+
value: number;
4+
};
55

66
enum DisplayQuality {
7-
High = 'high',
8-
Low = 'low'
7+
High = "high",
8+
Low = "low",
99
}
1010

1111
type SetDisplayQuality = {
12-
type: 'set-display-quality',
13-
value: DisplayQuality
14-
}
12+
type: "set-display-quality";
13+
value: DisplayQuality;
14+
};
1515

16+
type SetShowInspector = {
17+
type: "set-show-inspector";
18+
value: boolean;
19+
};
1620

17-
type GlobeAction = SetExaggeration | SetDisplayQuality
21+
type GlobeAction = SetExaggeration | SetDisplayQuality | SetShowInspector;
1822

1923
interface GlobeState {
20-
verticalExaggeration: number
21-
displayQuality: DisplayQuality
24+
verticalExaggeration: number;
25+
displayQuality: DisplayQuality;
2226
}
2327

2428
const initialState = {
2529
verticalExaggeration: 1,
26-
displayQuality: DisplayQuality.Low
27-
}
30+
displayQuality: DisplayQuality.Low,
31+
};
2832

29-
const reducer = (state: GlobeState = initialState, action: GlobeAction)=>{
33+
const reducer = (state: GlobeState = initialState, action: GlobeAction) => {
3034
switch (action.type) {
31-
case 'set-exaggeration':
32-
return {...state, verticalExaggeration: action.value}
33-
case 'set-display-quality':
34-
return {...state, displayQuality: action.value}
35-
default:
36-
return state
35+
case "set-exaggeration":
36+
return { ...state, verticalExaggeration: action.value };
37+
case "set-display-quality":
38+
return { ...state, displayQuality: action.value };
39+
case "set-show-inspector":
40+
return { ...state, showInspector: action.value };
41+
default:
42+
return state;
3743
}
38-
}
44+
};
3945

40-
export {reducer as globeReducer, GlobeAction, DisplayQuality}
46+
export { reducer as globeReducer, GlobeAction, DisplayQuality };

src/index.ts

Lines changed: 58 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,63 @@
1-
import "cesiumSource/Widgets/widgets.css"
2-
import * as Cesium from "cesiumSource/Cesium"
3-
import {hyperStyled} from '@macrostrat/hyper'
4-
import styles from "./main.styl"
5-
const h = hyperStyled(styles)
6-
import {GlobeViewer} from './viewer'
7-
import {GeologyLayer, SatelliteLayer, HillshadeLayer} from './layers'
8-
import {DisplayQuality} from './actions'
9-
import {MapClickHandler, SelectedPoint, MapChangeTracker, FlyToInitialPosition} from './position'
10-
import {Fog, Globe, Scene} from 'resium'
11-
import {useSelector} from 'react-redux'
12-
import {terrainProvider} from './layers'
1+
import "cesiumSource/Widgets/widgets.css";
2+
import * as Cesium from "cesiumSource/Cesium";
3+
import { hyperStyled } from "@macrostrat/hyper";
4+
import styles from "./main.styl";
5+
const h = hyperStyled(styles);
6+
import { GlobeViewer } from "./viewer";
7+
import { GeologyLayer, SatelliteLayer, HillshadeLayer } from "./layers";
8+
import { DisplayQuality } from "./actions";
9+
import {
10+
MapClickHandler,
11+
SelectedPoint,
12+
MapChangeTracker,
13+
FlyToInitialPosition,
14+
} from "./position";
15+
import { Fog, Globe, Scene } from "resium";
16+
import { useSelector } from "react-redux";
17+
import { terrainProvider } from "./layers";
1318

14-
const CesiumView = (props)=>{
19+
const CesiumView = (props) => {
20+
const exaggeration =
21+
useSelector((state) => state.globe.verticalExaggeration) ?? 1.0;
22+
const displayQuality = useSelector((state) => state.globe.displayQuality);
1523

16-
const exaggeration = useSelector(state => state.globe.verticalExaggeration) ?? 1.0
17-
const displayQuality = useSelector(state => state.globe.displayQuality)
24+
const showInspector = useSelector((state) => state.globe.showInspector);
1825

19-
return h(GlobeViewer, {
20-
terrainProvider,
21-
// not sure why we have to do this...
22-
terrainExaggeration: exaggeration,
23-
highResolution: displayQuality,
24-
skyBox: false
25-
//terrainShadows: Cesium.ShadowMode.ENABLED
26-
}, [
27-
h(Globe, {
28-
baseColor: Cesium.Color.LIGHTGRAY,
29-
enableLighting: false,
30-
showGroundAtmosphere: true,
31-
maximumScreenSpaceError: displayQuality == DisplayQuality.High ? 1.5 : 2
32-
//shadowMode: Cesium.ShadowMode.ENABLED
33-
}, null),
34-
h(Scene, {requestRenderMode: true}),
35-
h(MapChangeTracker),
36-
h(SatelliteLayer),
37-
h(HillshadeLayer),
38-
h(GeologyLayer, {alpha: 0.5}),
39-
h(MapClickHandler),
40-
h(SelectedPoint),
41-
h(FlyToInitialPosition),
42-
h(Fog, {density: 1e-4})
43-
])
44-
}
26+
return h(
27+
GlobeViewer,
28+
{
29+
terrainProvider,
30+
// not sure why we have to do this...
31+
terrainExaggeration: exaggeration,
32+
highResolution: displayQuality == DisplayQuality.High,
33+
skyBox: false,
34+
showInspector,
35+
//terrainShadows: Cesium.ShadowMode.ENABLED
36+
},
37+
[
38+
h(
39+
Globe,
40+
{
41+
baseColor: Cesium.Color.LIGHTGRAY,
42+
enableLighting: false,
43+
showGroundAtmosphere: true,
44+
maximumScreenSpaceError:
45+
displayQuality == DisplayQuality.High ? 2 : 3,
46+
//shadowMode: Cesium.ShadowMode.ENABLED
47+
},
48+
null
49+
),
50+
h(Scene, { requestRenderMode: true }),
51+
h(MapChangeTracker),
52+
h(SatelliteLayer),
53+
h(HillshadeLayer),
54+
h(GeologyLayer, { alpha: 0.5 }),
55+
h(MapClickHandler),
56+
h(SelectedPoint),
57+
h(FlyToInitialPosition),
58+
h(Fog, { density: 1e-4 }),
59+
]
60+
);
61+
};
4562

4663
export default CesiumView;

src/layers/terrain/hillshade.ts

Lines changed: 57 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,43 @@
1-
import { useRef} from 'react'
2-
import {MapboxImageryProvider} from "cesium"
3-
import h from '@macrostrat/hyper'
4-
import {ImageryLayer} from "resium"
5-
import {useSelector} from 'react-redux'
6-
import REGL from 'regl'
7-
import {vec3} from 'gl-matrix'
1+
import { useRef } from "react";
2+
import { MapboxImageryProvider } from "cesium";
3+
import h from "@macrostrat/hyper";
4+
import { ImageryLayer } from "resium";
5+
import { useSelector } from "react-redux";
6+
import REGL from "regl";
7+
import { vec3 } from "gl-matrix";
8+
import { terrainProvider } from "./provider";
89
// https://wwwtyro.net/2019/03/21/advanced-map-shading.html
910

10-
type Img = HTMLImageElement|HTMLCanvasElement
11+
type Img = HTMLImageElement | HTMLCanvasElement;
1112

1213
class HillshadeImageryProvider extends MapboxImageryProvider {
14+
// Fib about tile size in order to download fewer elevation tiles
15+
tileWidth = 512;
16+
tileHeight = 512;
17+
terrainProvider = terrainProvider;
18+
1319
processImage(image: Img, rect: Cesium.Rectangle): Img {
1420
const canvas = document.createElement("canvas");
1521
canvas.width = image.width;
1622
canvas.height = image.height;
17-
const regl = REGL({ canvas, extensions: ["OES_texture_float"]});
23+
const regl = REGL({
24+
canvas,
25+
extensions: ["OES_texture_float", "WEBGL_color_buffer_float"],
26+
});
1827

1928
const tElevation = regl.texture({
2029
data: image,
21-
flipY: true
30+
flipY: true,
2231
});
2332

24-
const angle = rect.east-rect.west
33+
const angle = rect.east - rect.west;
2534
// rough meters per pixel (could get directly from zoom level)
26-
const pixelScale = 6371000*angle/image.width
35+
const pixelScale = (6371000 * angle) / image.width;
2736

2837
const fboElevation = regl.framebuffer({
2938
width: image.width,
3039
height: image.height,
31-
colorType: "float"
40+
colorType: "float",
3241
});
3342

3443
const cmdProcessElevation = regl({
@@ -59,24 +68,24 @@ class HillshadeImageryProvider extends MapboxImageryProvider {
5968
}
6069
`,
6170
attributes: {
62-
position: [-1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1]
71+
position: [-1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1],
6372
},
6473
uniforms: {
6574
tElevation: tElevation,
6675
elevationScale: 1.0,
67-
resolution: [image.width, image.height]
76+
resolution: [image.width, image.height],
6877
},
6978
viewport: { x: 0, y: 0, width: image.width, height: image.height },
7079
count: 6,
7180
framebuffer: fboElevation,
7281
});
7382

74-
cmdProcessElevation()
83+
cmdProcessElevation();
7584

7685
const fboNormal = regl.framebuffer({
7786
width: image.width,
7887
height: image.height,
79-
colorType: "float"
88+
colorType: "float",
8089
});
8190

8291
const cmdNormal = regl({
@@ -110,29 +119,32 @@ class HillshadeImageryProvider extends MapboxImageryProvider {
110119
}
111120
`,
112121
attributes: {
113-
position: [-1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1]
122+
position: [-1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1],
114123
},
115124
uniforms: {
116125
tElevation: fboElevation,
117126
pixelScale: pixelScale,
118-
resolution: [image.width, image.height]
127+
resolution: [image.width, image.height],
119128
},
120129
viewport: { x: 0, y: 0, width: image.width, height: image.height },
121130
count: 6,
122-
framebuffer: fboNormal
131+
framebuffer: fboNormal,
123132
});
124133

125134
cmdNormal();
126135

127136
const cmdDirect = regl({
137+
// The vertex shader tells the GPU where to draw the vertices.
128138
vert: `
129139
precision highp float;
130140
attribute vec2 position;
141+
uniform vec2 scale;
131142
132143
void main() {
133-
gl_Position = vec4(position, 0, 1);
144+
gl_Position = vec4(scale*position, 0, 1);
134145
}
135146
`,
147+
// The fragment shader tells the GPU what color to draw.
136148
frag: `
137149
precision highp float;
138150
@@ -149,41 +161,45 @@ class HillshadeImageryProvider extends MapboxImageryProvider {
149161
}
150162
`,
151163
attributes: {
152-
position: [-1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1]
164+
position: [-1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1],
153165
},
154166
uniforms: {
155167
tNormal: fboNormal,
156168
tElevation: fboElevation,
169+
scale: [1, 1],
157170
resolution: [image.width, image.height],
158-
sunDirection: vec3.normalize([], [1, 1, 0.5])
171+
sunDirection: vec3.normalize([], [1, 1, 0.5]),
159172
},
160173
viewport: { x: 0, y: 0, width: image.width, height: image.height },
161-
count: 6
174+
count: 6,
162175
});
163176

164177
cmdDirect();
165178

166-
return canvas
179+
return canvas;
167180
}
168-
requestImage(x,y,z,request) {
169-
const res = super.requestImage(x,y,z,request)
170-
const rect = this.tilingScheme.tileXYToRectangle(x, y, z)
171-
return res?.then(d=>this.processImage(d, rect))
181+
requestImage(x, y, z, request) {
182+
const res = this.terrainProvider.backend.requestImage(x, y, z, request);
183+
if (res == null) return undefined;
184+
const rect = this.tilingScheme.tileXYToRectangle(x, y, z);
185+
return res.then((d) => this.processImage(d, rect));
172186
}
173187
}
174188

175-
const HillshadeLayer = (props)=>{
176-
const hasSatellite = useSelector(state => state.update.mapHasSatellite)
189+
const HillshadeLayer = (props) => {
190+
const hasSatellite = useSelector((state) => state.update.mapHasSatellite);
177191

178-
let hillshade = useRef(new HillshadeImageryProvider({
179-
mapId : 'mapbox.terrain-rgb',
180-
maximumLevel : 14,
181-
accessToken: process.env.MAPBOX_API_TOKEN
182-
}))
183-
184-
if (hasSatellite) return null
185-
return h(ImageryLayer, {imageryProvider: hillshade.current, ...props})
186-
}
192+
let hillshade = useRef(
193+
new HillshadeImageryProvider({
194+
mapId: "mapbox.terrain-rgb",
195+
maximumLevel: 14,
196+
accessToken: process.env.MAPBOX_API_TOKEN,
197+
format: "@2x.webp",
198+
})
199+
);
187200

201+
if (hasSatellite) return null;
202+
return h(ImageryLayer, { imageryProvider: hillshade.current, ...props });
203+
};
188204

189-
export {HillshadeLayer}
205+
export { HillshadeLayer };

0 commit comments

Comments
 (0)