/
elevation-layer.js
129 lines (107 loc) · 3.11 KB
/
elevation-layer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import {Layer} from 'deck.gl';
import GL from '@luma.gl/constants';
import {Model, loadTextures} from 'luma.gl';
import {ELEVATION_DATA_IMAGE, ELEVATION_DATA_BOUNDS, ELEVATION_RANGE} from '../../defaults';
import GridGeometry from './grid-geometry';
import vertex from './elevation-layer-vertex.glsl';
import fragment from './elevation-layer-fragment.glsl';
const defaultProps = {
boundingBox: null, // : {minLng, minLat, maxLng, maxLat}, lngResolution, latResolution}
lngResolution: 100,
latResolution: 100,
zScale: 1
};
export default class ElevationLayer extends Layer {
getShaders() {
return {
vs: vertex,
fs: fragment,
modules: ['lighting'],
shaderCache: this.context.shaderCache
};
}
initializeState() {
const {gl} = this.context;
loadTextures(gl, {
urls: [ELEVATION_DATA_IMAGE],
// TODO open bug for this, refine the loadTextures interface
parameters: {
parameters: {
[GL.TEXTURE_MAG_FILTER]: GL.LINEAR,
[GL.TEXTURE_MIN_FILTER]: GL.LINEAR,
[GL.TEXTURE_WRAP_S]: GL.CLAMP_TO_EDGE,
[GL.TEXTURE_WRAP_T]: GL.CLAMP_TO_EDGE
}
}
}).then(textures => {
this.setState({data: textures[0]});
});
const model = this.getModel(gl);
this.setState({model});
model.setUniforms({
elevationBounds: ELEVATION_DATA_BOUNDS,
elevationRange: ELEVATION_RANGE
});
}
updateState({oldProps, props, changeFlags}) {
if (changeFlags.propsChanged) {
const {boundingBox, lngResolution, latResolution} = props;
const propsChanged =
boundingBox !== oldProps.boundingBox ||
lngResolution !== oldProps.lngResolution ||
latResolution !== oldProps.latResolution;
if (propsChanged) {
this.setState({
vertexCount: lngResolution * latResolution
});
this.state.attributeManager.invalidateAll();
}
}
}
draw({uniforms}) {
const {gl} = this.context;
const {zScale} = this.props;
const {data, model} = this.state;
if (!data || !model) {
return;
}
const parameters = {
depthTest: true,
depthFunc: gl.LEQUAL,
blend: true,
blendFunc: [gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA],
blendEquation: gl.FUNC_ADD
};
uniforms = Object.assign({}, uniforms, {
elevationTexture: data,
zScale
});
model.draw({uniforms, parameters});
}
getModel(gl) {
const shaders = this.getShaders();
// 3d surface
const vsShader = shaders.vs;
const fsSource = shaders.fs;
const fsShader = `\
precision highp float;
${fsSource}`;
const {lngResolution, latResolution, boundingBox} = this.props;
const geometry = new GridGeometry({
xResolution: lngResolution,
yResolution: latResolution,
boundingBox
});
return new Model(gl, {
id: this.props.id,
vs: vsShader,
fs: fsShader,
geometry,
// FIXME - isIndexed should be set in "GridGeometry"
isIndexed: true,
modules: ['lighting']
});
}
}
ElevationLayer.layerName = 'ElevationLayer';
ElevationLayer.defaultProps = defaultProps;