Skip to content

Commit

Permalink
Apply fog in fragment shader in terrain_raster program (#12423)
Browse files Browse the repository at this point in the history
* Add render test for fog with terrain and zero exaggeration

* Return early in elevation function when zero exaggeration is used

* Calculate fog opacity in terrain raster fragment shader when zero exaggeration is used
  • Loading branch information
endanke authored Nov 30, 2022
1 parent 5094a44 commit 31f03a6
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 0 deletions.
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
}
}
]
}

0 comments on commit 31f03a6

Please sign in to comment.