-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hillshading support to the DeckGL map (#294)
* Initial 2d hillshading iteration. * Use separate shader for colormap, like the hillshading * Clear depth in minimap * Add value decoder params * Add missing newline * Add hillshading in the python example * Merge valueDecoder object with defaultProps
- Loading branch information
1 parent
cadadf6
commit 66cee7f
Showing
15 changed files
with
229 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/lib/components/DeckGLMap/layers/colormap/colormap.fs.glsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#define SHADER_NAME colormap-shader | ||
|
||
#ifdef GL_ES | ||
precision highp float; | ||
#endif | ||
|
||
varying vec2 vTexCoord; | ||
|
||
uniform sampler2D bitmapTexture; | ||
uniform sampler2D colormap; | ||
|
||
uniform float opacity; | ||
|
||
void main(void) { | ||
vec4 bitmapColor = texture2D(bitmapTexture, vTexCoord); | ||
|
||
float val = decode_rgb2float(bitmapColor.rgb); | ||
vec4 color = texture2D(colormap, vec2(val, 0.5)); | ||
|
||
gl_FragColor = vec4(color.rgb, color.a * bitmapColor.a * opacity); | ||
|
||
geometry.uv = vTexCoord; | ||
DECKGL_FILTER_COLOR(gl_FragColor, geometry); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/lib/components/DeckGLMap/layers/hillshading2d/hillshading2d.fs.glsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#define SHADER_NAME hillshading2d-shader | ||
|
||
#ifdef GL_ES | ||
precision highp float; | ||
#endif | ||
|
||
varying vec2 vTexCoord; | ||
|
||
uniform sampler2D bitmapTexture; | ||
uniform vec2 bitmapResolution; | ||
|
||
uniform float valueRange; | ||
|
||
uniform vec3 lightDirection; | ||
uniform float ambientLightIntensity; | ||
uniform float diffuseLightIntensity; | ||
uniform float opacity; | ||
|
||
vec3 normal(float val) { | ||
vec2 dr = 1.0 / bitmapResolution; | ||
float p0 = valueRange * val; | ||
float px = valueRange * decode_rgb2float(texture2D(bitmapTexture, vTexCoord + vec2(1.0, 0.0) / bitmapResolution).rgb); | ||
float py = valueRange * decode_rgb2float(texture2D(bitmapTexture, vTexCoord + vec2(0.0, 1.0) / bitmapResolution).rgb); | ||
vec3 dx = vec3(1.0, 0.0, px - p0); | ||
vec3 dy = vec3(0.0, 1.0, py - p0); | ||
|
||
return normalize(cross(dx, dy)); | ||
} | ||
|
||
float shadow(vec3 normal) { | ||
float diffuse = diffuseLightIntensity * dot(normal, normalize(lightDirection)); | ||
return clamp(ambientLightIntensity + diffuse, 0.0, 1.0); | ||
} | ||
|
||
void main(void) { | ||
vec4 bitmapColor = texture2D(bitmapTexture, vTexCoord); | ||
|
||
float val = decode_rgb2float(bitmapColor.rgb); | ||
float shadow = shadow(normal(val)); | ||
|
||
gl_FragColor = vec4(vec3(0.0), (1.0-shadow) * bitmapColor.a * opacity); | ||
|
||
geometry.uv = vTexCoord; | ||
DECKGL_FILTER_COLOR(gl_FragColor, geometry); | ||
} |
64 changes: 64 additions & 0 deletions
64
src/lib/components/DeckGLMap/layers/hillshading2d/hillshading2dLayer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// DeckGL typescript declarations are not great, so for now it's just js. | ||
|
||
import { BitmapLayer } from "@deck.gl/layers"; | ||
|
||
import { decoder } from "../shader_modules"; | ||
import fsHillshading from "./hillshading2d.fs.glsl"; | ||
|
||
const defaultProps = { | ||
valueRange: { type: "number" }, | ||
lightDirection: { type: "array", value: [1, 1, 1] }, | ||
ambientLightIntensity: { type: "number", value: 0.5 }, | ||
diffuseLightIntensity: { type: "number", value: 0.5 }, | ||
opacity: { type: "number", min: 0, max: 1, value: 1 }, | ||
valueDecoder: { | ||
type: "object", | ||
value: { | ||
rgbScaler: [1, 1, 1], | ||
// Scale [0, 256*256*256-1] to [0, 1] | ||
floatScaler: 1.0 / (256.0 * 256.0 * 256.0 - 1.0), | ||
offset: 0, | ||
}, | ||
}, | ||
}; | ||
|
||
export default class Hillshading2DLayer extends BitmapLayer { | ||
draw({ moduleParameters, uniforms }) { | ||
if (this.state.bitmapTexture) { | ||
// The prop objects are not merged with the defaultProps by default. | ||
// See https://github.com/facebook/react/issues/2568 | ||
const mergedDecoder = { | ||
...defaultProps.valueDecoder.value, | ||
...moduleParameters.valueDecoder, | ||
}; | ||
super.setModuleParameters({ | ||
...moduleParameters, | ||
valueDecoder: mergedDecoder, | ||
}); | ||
super.draw({ | ||
uniforms: { | ||
...uniforms, | ||
bitmapResolution: [ | ||
this.state.bitmapTexture.width, | ||
this.state.bitmapTexture.height, | ||
], | ||
valueRange: this.props.valueRange, | ||
lightDirection: this.props.lightDirection, | ||
ambientLightIntensity: this.props.ambientLightIntensity, | ||
diffuseLightIntensity: this.props.diffuseLightIntensity, | ||
opacity: this.props.opacity, | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
getShaders() { | ||
let parentShaders = super.getShaders(); | ||
parentShaders.fs = fsHillshading; | ||
parentShaders.modules.push(decoder); | ||
return parentShaders; | ||
} | ||
} | ||
|
||
Hillshading2DLayer.layerName = "Hillshading2DLayer"; | ||
Hillshading2DLayer.defaultProps = defaultProps; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { default as ColormapLayer } from "./colormap/colormapLayer"; | ||
export { default as Hillshading2DLayer } from "./hillshading2d/hillshading2dLayer"; |
17 changes: 17 additions & 0 deletions
17
src/lib/components/DeckGLMap/layers/shader_modules/decoder.fs.glsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
struct Decoder | ||
{ | ||
vec3 rgbScaler; | ||
float floatScaler; | ||
float offset; | ||
}; | ||
|
||
uniform Decoder decoder; | ||
|
||
float decode_rgb2float(vec3 rgb, Decoder dec) { | ||
rgb *= dec.rgbScaler * vec3(16711680.0, 65280.0, 255.0); //255*256*256, 255*256, 255 | ||
return (rgb.r + rgb.g + rgb.b + dec.offset) * dec.floatScaler; | ||
} | ||
|
||
float decode_rgb2float(vec3 rgb) { | ||
return decode_rgb2float(rgb, decoder); | ||
} |
30 changes: 30 additions & 0 deletions
30
src/lib/components/DeckGLMap/layers/shader_modules/decoder.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import fs from "./decoder.fs.glsl"; | ||
|
||
const DEFAULT_DECODER = { | ||
rgbScaler: [1, 1, 1], | ||
floatScaler: 1, | ||
offset: 0, | ||
}; | ||
|
||
function getUniforms(opts) { | ||
if (opts && opts.valueDecoder) { | ||
const { | ||
rgbScaler = DEFAULT_DECODER.rgbScaler, | ||
floatScaler = DEFAULT_DECODER.floatScaler, | ||
offset = DEFAULT_DECODER.offset, | ||
} = opts.valueDecoder; | ||
return { | ||
"decoder.rgbScaler": rgbScaler, | ||
"decoder.floatScaler": floatScaler, | ||
"decoder.offset": offset, | ||
}; | ||
} | ||
|
||
return {}; | ||
} | ||
|
||
export default { | ||
name: "decoder", | ||
fs, | ||
getUniforms, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as decoder } from "./decoder"; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.