Skip to content

Commit

Permalink
Switch fog from percentage window height to CSS vh units (#10632)
Browse files Browse the repository at this point in the history
* Switch fog from percentage window height to CSS vh units

* Fix tests

* Fix more tests

* Fix still more tests

* Update src/style-spec/reference/v8.json
  • Loading branch information
rreusser authored and karimnaaji committed May 4, 2021
1 parent 742e6c2 commit 6047053
Show file tree
Hide file tree
Showing 27 changed files with 41 additions and 40 deletions.
8 changes: 4 additions & 4 deletions debug/fog.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
this.enableSky = true;
this.terrainExaggeration = 1.5;
this.style = 'satellite-streets-v11';
this.start = 2;
this.end = 12.0;
this.start = 200;
this.end = 1200;
this.color = [255, 255, 255];
this.horizonBlend = 0.1;
this.showTileBoundaries = false;
Expand Down Expand Up @@ -104,14 +104,14 @@
} : null);
});

var start = fog.add(guiParams, 'start', 0.0, 15.0);
var start = fog.add(guiParams, 'start', 0.0, 1500.0);
start.onChange((value) => {
map.setFog({
"range": [value, guiParams.end],
});
});

var end = fog.add(guiParams, 'end', 0.0, 15.0);
var end = fog.add(guiParams, 'end', 0.0, 1500.0);
end.onChange((value) => {
map.setFog({
"range": [guiParams.start, value],
Expand Down
4 changes: 2 additions & 2 deletions src/geo/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -1486,11 +1486,11 @@ class Transform {

// The mercator fog matrix encodes transformation necessary to transform a position to camera fog space (in meters):
// translates p to camera origin and transforms it from pixels to meters. The windowScaleFactor is used to have a
// consistent transformation across different window sizes.
// consistent transformation across different window sizes. The 100 in windowScaleFactor accounts for vh units (percentage of window height).
// - p = p - cameraOrigin
// - p.xy = p.xy * cameraWorldSize * windowScaleFactor
// - p.z = p.z * cameraPixelsPerMeter * windowScaleFactor
const windowScaleFactor = 1.0 / this.height;
const windowScaleFactor = 100 / this.height;
const metersToPixel = [cameraWorldSize, cameraWorldSize, cameraPixelsPerMeter];
vec3.scale(metersToPixel, metersToPixel, windowScaleFactor);
vec3.scale(cameraPos, cameraPos, -1);
Expand Down
9 changes: 5 additions & 4 deletions src/style-spec/reference/v8.json
Original file line number Diff line number Diff line change
Expand Up @@ -3685,7 +3685,8 @@
12
],
"minimum": 0,
"maximum": 20,
"maximum": 2000,
"units": "vh",
"length": 2,
"value": "number",
"property-type": "data-constant",
Expand All @@ -3696,10 +3697,10 @@
"zoom"
]
},
"doc": "The near and far limits of the fog layer. Units are relative to the map height, so that 2.0 corresponds to twice the map height, in pixels.",
"doc": "The start and end distance range in which fog fades from fully transparent to fully opaque. Units are relative to the map window height in CSS `vh` units, so that 200 corresponds to 200% the map window height.",
"example": [
2,
12
200,
1200
],
"sdk-support": {
"basic functionality": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"pitch": 80,
"zoom": 12,
"fog": {
"range": [1, 2],
"range": [100, 200],
"color": "white"
},
"layers": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
}
],
"fog": {
"range": [0.5, 1.5],
"range": [50, 150],
"color": "white"
}
}
2 changes: 1 addition & 1 deletion test/integration/render-tests/fog/2d/basic/style.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"pitch": 80,
"bearing": 64.5,
"fog": {
"range": [1, 2],
"range": [100, 200],
"color": "white"
},
"sources": {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/render-tests/fog/2d/fill-color/style.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
}
],
"fog": {
"range": [1, 2],
"range": [100, 200],
"color": "white"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
}
],
"fog": {
"range": [1, 2],
"range": [100, 200],
"color": "white"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
}
],
"fog": {
"range": [1, 2],
"range": [100, 200],
"color": "white"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
}
],
"fog": {
"range": [1, 2],
"range": [100, 200],
"color": "white"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
}
],
"fog": {
"range": [1, 2],
"range": [100, 200],
"color": "black"
}
}
2 changes: 1 addition & 1 deletion test/integration/render-tests/fog/2d/heatmap/style.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"pitch": 70,
"zoom": 14,
"fog": {
"range": [1.25, 1.75],
"range": [125, 175],
"color": "white"
},
"sources": {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/render-tests/fog/2d/hillshade/style.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"zoom": 11,
"pitch": 70,
"fog": {
"range": [1, 2],
"range": [100, 200],
"color": "white"
},
"sources": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"zoom": 2,
"pitch": 70,
"fog": {
"range": [1, 2],
"range": [100, 200],
"color": "white"
},
"sources": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"zoom": 17,
"pitch": 70,
"fog": {
"range": [1, 2],
"range": [100, 200],
"color": "white"
},
"sources": {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/render-tests/fog/2d/line-sdf/style.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"zoom": 17,
"pitch": 70,
"fog": {
"range": [1, 2],
"range": [100, 200],
"color": "white"
},
"sources": {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/render-tests/fog/2d/line/style.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"zoom": 14,
"pitch": 70,
"fog": {
"range": [1, 2],
"range": [100, 200],
"color": "white"
},
"sources": {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/render-tests/fog/2d/raster/style.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"zoom": 16,
"pitch": 70,
"fog": {
"range": [1, 2],
"range": [100, 200],
"color": "white"
},
"sources": {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/render-tests/fog/color/style.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"zoom": 16,
"pitch": 70,
"fog": {
"range": [1, 4],
"range": [100, 400],
"color": "rgba(255, 30, 35, 1)"
},
"sources": {},
Expand Down
2 changes: 1 addition & 1 deletion test/integration/render-tests/fog/culling/close/style.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
}
},
"fog": {
"range": [1, 1.2],
"range": [100, 120],
"color": "blue"
},
"layers": [
Expand Down
2 changes: 1 addition & 1 deletion test/integration/render-tests/fog/culling/far/style.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
}
},
"fog": {
"range": [5, 6],
"range": [500, 600],
"color": "blue"
},
"layers": [
Expand Down
2 changes: 1 addition & 1 deletion test/integration/render-tests/fog/culling/mid/style.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
}
},
"fog": {
"range": [1.7, 2],
"range": [170, 200],
"color": "blue"
},
"layers": [
Expand Down
4 changes: 2 additions & 2 deletions test/integration/render-tests/fog/terrain/basic/style.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"source": "rgbterrain"
},
"fog": {
"range": [1, 2],
"range": [100, 200],
"color": "white"
},
"sources": {
Expand Down Expand Up @@ -45,4 +45,4 @@
}
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"source": "rgbterrain"
},
"fog": {
"range": [1, 2],
"range": [100, 200],
"color": "white",
"horizon-blend": 0.15
},
Expand Down Expand Up @@ -55,4 +55,4 @@
}
}
]
}
}
2 changes: 1 addition & 1 deletion test/unit/geo/transform.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ test('transform', (t) => {
transform.resize(200, 200);
transform.center = {lng: -0.01, lat: 0.01};
transform.zoom = 0;
transform.fogCullDistSq = 1.5;
transform.fogCullDistSq = 15000;
transform.pitch = 85.0;
t.deepEqual(transform.coveringTiles(options), []);

Expand Down
10 changes: 5 additions & 5 deletions test/unit/ui/map.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1247,7 +1247,7 @@ test('Map', (t) => {
const map = createMap(t, {style});
map.on('load', () => {
map.setFog({
"range": [2, 12]
"range": [200, 1200]
});

t.ok(map.getFog());
Expand All @@ -1269,10 +1269,10 @@ test('Map', (t) => {

t.deepEqual(map.queryFogOpacity([0, 0]), 0.0);

t.deepEqual(map.queryFogOpacity([0.5, 0]), 0.5963390859543484);
t.deepEqual(map.queryFogOpacity([0, 0.5]), 0.31817612773293763);
t.deepEqual(map.queryFogOpacity([-0.5, 0]), 0.0021931905967484703);
t.deepEqual(map.queryFogOpacity([-0.5, -0.5]), 0.4147318524978687);
t.deepEqual(map.queryFogOpacity([0.5, 0]), 0.5963360993372026);
t.deepEqual(map.queryFogOpacity([0, 0.5]), 0.3181747292975157);
t.deepEqual(map.queryFogOpacity([-0.5, 0]), 0.0021935811432850054);
t.deepEqual(map.queryFogOpacity([-0.5, -0.5]), 0.4147338484563584);

t.deepEqual(map.queryFogOpacity([2, 0]), 1.0);
t.deepEqual(map.queryFogOpacity([0, 2]), 1.0);
Expand Down
2 changes: 1 addition & 1 deletion test/unit/ui/marker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ test('Marker and fog', (t) => {

map.on('load', () => {
map.setFog({
"range": [2, 12]
"range": [200, 1200]
});

t.ok(map.getFog());
Expand Down

0 comments on commit 6047053

Please sign in to comment.