Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply fog in fragment shader in terrain_raster program #12423

Merged
merged 3 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/render/painter.js
Original file line number Diff line number Diff line change
Expand Up @@ -859,11 +859,13 @@ class Painter {
*/
currentGlobalDefines(): string[] {
const rtt = this.terrain && this.terrain.renderingToTexture;
const zeroExaggeration = this.terrain && this.terrain.exaggeration() === 0.0;
const fog = this.style && this.style.fog;
const defines = [];

if (this.terrainRenderModeElevated()) defines.push('TERRAIN');
if (this.transform.projection.name === 'globe') defines.push('GLOBE');
if (zeroExaggeration) defines.push('ZERO_EXAGGERATION');
// When terrain is active, fog is rendered as part of draping, not as part of tile
// rendering. Removing the fog flag during tile rendering avoids additional defines.
if (fog && !rtt && fog.getOpacity(this.transform.pitch) !== 0.0) {
Expand Down
6 changes: 6 additions & 0 deletions src/shaders/_prelude_terrain.vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,18 @@ float prevElevation(vec2 apos) {

#ifdef TERRAIN_VERTEX_MORPHING
float elevation(vec2 apos) {
#ifdef ZERO_EXAGGERATION
return 0.0;
#endif
float nextElevation = currentElevation(apos);
float prevElevation = prevElevation(apos);
return mix(prevElevation, nextElevation, u_dem_lerp);
}
#else
float elevation(vec2 apos) {
#ifdef ZERO_EXAGGERATION
return 0.0;
#endif
return currentElevation(apos);
}
#endif
Expand Down
4 changes: 4 additions & 0 deletions src/shaders/terrain_raster.fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ void main() {
#endif

#ifdef FOG
#ifdef ZERO_EXAGGERATION
color = fog_dither(fog_apply_premultiplied(color, v_fog_pos));
#else
color = fog_dither(fog_apply_from_vert(color, v_fog_opacity));
#endif
#endif
gl_FragColor = color;
#ifdef TERRAIN_WIREFRAME
Expand Down
4 changes: 4 additions & 0 deletions src/shaders/terrain_raster.vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ void main() {
gl_Position = u_matrix * vec4(decodedPos, elevation, 1.0);

#ifdef FOG
#ifdef ZERO_EXAGGERATION
v_fog_pos = fog_position(decodedPos);
#else
v_fog_opacity = fog(fog_position(vec3(decodedPos, elevation)));
#endif
#endif

#ifdef RENDER_SHADOWS
vec3 pos = vec3(decodedPos, elevation);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"version": 8,
"metadata": {
"test": {
"height": 256,
"width": 256
}
},
"center": [-113.32296, 35.94662],
"zoom": 5.5,
"pitch": 80.0,
"bearing": 64.5,
"terrain": {
"source": "rgbterrain",
"exaggeration": 0.0
},
"fog": {
"range": [-0.2, 0.5],
"color": "white",
"horizon-blend": 0.15
},
"sources": {
"rgbterrain": {
"type": "raster-dem",
"tiles": [
"local://tiles/{z}-{x}-{y}.terrain.png"
],
"maxzoom": 12,
"tileSize": 256
},
"satellite": {
"type": "raster",
"tiles": [
"local://tiles/{z}-{x}-{y}.satellite.png"
],
"maxzoom": 17,
"tileSize": 256
}
},
"layers": [
{
"id": "sky",
"type": "sky",
"paint": {
"sky-type": "atmosphere",
"sky-atmosphere-sun": [0, 0],
"sky-atmosphere-sun-intensity": 15
}
},
{
"id": "raster",
"type": "raster",
"source": "satellite",
"paint": {
"raster-fade-duration": 0
}
}
]
}