Skip to content

Commit

Permalink
Switch fog to use window-scaled units (#10533)
Browse files Browse the repository at this point in the history
* Switch fog to use window-scaled units

* Update style spec for fog range

* Update fog validation tests to match new limits

* Update fog validations
  • Loading branch information
rreusser authored and karimnaaji committed Apr 7, 2021
1 parent fa7bc72 commit fde1179
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 37 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.fogRangeStart = 1000;
this.fogRangeEnd = 10000;
this.fogRangeStart = 1.5;
this.fogRangeEnd = 2.0;
this.fogColor = [255, 255, 255];
this.fogSkyBlend = 0.1;
this.showTileBoundaries = false;
Expand Down Expand Up @@ -111,14 +111,14 @@
});
});

var fogRangeStart = fog.add(guiParams, 'fogRangeStart', 0.0, 15000.0);
var fogRangeStart = fog.add(guiParams, 'fogRangeStart', 0.0, 15.0);
fogRangeStart.onFinishChange((value) => {
map.setFog({
"range": [value, guiParams.fogRangeEnd],
});
});

var fogRangeEnd = fog.add(guiParams, 'fogRangeEnd', 0.0, 15000.0);
var fogRangeEnd = fog.add(guiParams, 'fogRangeEnd', 0.0, 15.0);
fogRangeEnd.onFinishChange((value) => {
map.setFog({
"range": [guiParams.fogRangeStart, value],
Expand Down
5 changes: 3 additions & 2 deletions src/geo/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -1497,8 +1497,9 @@ class Transform {
mat4.scale(m, m, metersToPixel);
this.mercatorFogMatrix = m;

// matrix for conversion from tile coordinates to relative camera position in pixel coordinates
this.cameraMatrix = this._camera.getWorldToCameraPosition(cameraWorldSize, cameraPixelsPerMeter);
// matrix for conversion from tile coordinates to relative camera position in units of fractions of the map height
const windowScaleFactor = 1.0 / this.height;
this.cameraMatrix = this._camera.getWorldToCameraPosition(cameraWorldSize, cameraPixelsPerMeter, windowScaleFactor);

// inverse matrix for conversion from screen coordinates to location
m = mat4.invert(new Float64Array(16), this.pixelMatrix);
Expand Down
10 changes: 6 additions & 4 deletions src/style-spec/reference/v8.json
Original file line number Diff line number Diff line change
Expand Up @@ -3681,9 +3681,11 @@
"range": {
"type": "array",
"default": [
3000,
8000
5,
10
],
"minimum": 0,
"maximum": 20,
"length": 2,
"value": "number",
"property-type": "data-constant",
Expand All @@ -3696,8 +3698,8 @@
},
"doc": "",
"example": [
3000,
8000
5,
10
],
"sdk-support": {
"basic functionality": {
Expand Down
8 changes: 2 additions & 6 deletions src/style-spec/validate/validate_fog.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@ export default function validateFog(options) {
return errors;
}

if (fog.range && fog.range[0] > fog.range[1]) {
errors = errors.concat([new ValidationError('fog', fog, 'fog.range[0] can\'t be greater than fog.range[1]')]);
}

if (fog.range && (fog.range[0] < 0 || fog.range[1] < 0)) {
errors = errors.concat([new ValidationError('fog', fog, 'fog.range can\'t be negative')]);
if (fog.range && fog.range[0] >= fog.range[1]) {
errors = errors.concat([new ValidationError('fog', fog, 'fog.range[0] can\'t be greater than or equal to fog.range[1]')]);
}

for (const key in fog) {
Expand Down
7 changes: 4 additions & 3 deletions src/ui/free_camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,16 @@ class FreeCamera {
return cameraToWorld;
}

getWorldToCameraPosition(worldSize: number, pixelsPerMeter: number): Float64Array {
getWorldToCameraPosition(worldSize: number, pixelsPerMeter: number, uniformScale: number): Float64Array {
const invPosition = this.position;

vec3.scale(invPosition, invPosition, -worldSize);
const matrix = new Float64Array(16);
mat4.fromTranslation(matrix, invPosition);
mat4.fromScaling(matrix, [uniformScale, uniformScale, uniformScale]);
mat4.translate(matrix, matrix, invPosition);

// Adjust scale on z (3rd column 3rd row)
matrix[10] = pixelsPerMeter;
matrix[10] *= pixelsPerMeter;

return matrix;
}
Expand Down
4 changes: 2 additions & 2 deletions test/unit/style-spec/fixture/fog-invalid-input.input.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"layers": [],
"fog": {
"range": [-2000, -1000],
"range": [-2, -1],
"sky-blend": -4
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
[
{
"line": 10,
"message": "fog: fog.range can't be negative"
"line": 11,
"message": "range[0]: -2 is less than the minimum value 0"
},
{
"line": 11,
"message": "range[1]: -1 is less than the minimum value 0"
},
{
"line": 12,
"message": "sky-blend: -4 is less than the minimum value 0"
}
]
]
10 changes: 7 additions & 3 deletions test/unit/style-spec/fixture/fog-invalid-input.output.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
[
{
"line": 10,
"message": "fog: fog.range can't be negative"
"line": 11,
"message": "range[0]: -2 is less than the minimum value 0"
},
{
"line": 11,
"message": "range[1]: -1 is less than the minimum value 0"
},
{
"line": 12,
"message": "sky-blend: -4 is less than the minimum value 0"
}
]
]
4 changes: 2 additions & 2 deletions test/unit/style-spec/fixture/fog-invalid-range.input.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
},
"layers": [],
"fog": {
"range": [2000, 1000]
"range": [2, 1]
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"line": 10,
"message": "fog: fog.range[0] can't be greater than fog.range[1]"
"message": "fog: fog.range[0] can't be greater than or equal to fog.range[1]"
}
]
]
4 changes: 2 additions & 2 deletions test/unit/style-spec/fixture/fog-invalid-range.output.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"line": 10,
"message": "fog: fog.range[0] can't be greater than fog.range[1]"
"message": "fog: fog.range[0] can't be greater than or equal to fog.range[1]"
}
]
]
8 changes: 4 additions & 4 deletions test/unit/style/style.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2360,7 +2360,7 @@ test('Style#setFog', (t) => {
const style = new Style(new StubMap());
style.loadJSON({
"version": 8,
"fog": {"range": [1000, 2000], "color": "white", "sky-blend": 0.05},
"fog": {"range": [1, 2], "color": "white", "sky-blend": 0.05},
"sources": {},
"layers": []
});
Expand All @@ -2382,15 +2382,15 @@ test('Style#getFog', (t) => {
const style = new Style(new StubMap());
style.loadJSON({
"version": 8,
"fog": {"range": [1000, 2000], "color": "white", "sky-blend": 0.05},
"fog": {"range": [1, 2], "color": "white", "sky-blend": 0.05},
"sources": {},
"layers": []
});

style.on('style.load', () => {
style.setFog({"range": [0, 1000], "color": "white", "sky-blend": 0.0});
style.setFog({"range": [0, 1], "color": "white", "sky-blend": 0.0});
t.ok(style.getFog());
t.deepEqual(style.getFog(), {"range": [0, 1000], "color": "white", "sky-blend": 0.0});
t.deepEqual(style.getFog(), {"range": [0, 1], "color": "white", "sky-blend": 0.0});
t.end();
});
});
Expand Down

0 comments on commit fde1179

Please sign in to comment.