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

Add documentation of the public facing fog APIs + revise defaults #10545

Merged
merged 9 commits into from
Apr 8, 2021
Merged
6 changes: 3 additions & 3 deletions debug/fog.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
this.enableSky = true;
this.terrainExaggeration = 1.5;
this.style = 'satellite-streets-v11';
this.fogRangeStart = 1.5;
this.fogRangeEnd = 2.0;
this.fogRangeStart = 2;
this.fogRangeEnd = 12.0;
this.fogColor = [255, 255, 255];
this.fogHazeColor = [109, 123, 180];
this.fogHazeEnergy = 1.0;
this.fogHazeEnergy = 0.8;
this.fogStrength = 1.0;
this.fogSkyBlend = 0.1;
this.showTileBoundaries = false;
Expand Down
56 changes: 19 additions & 37 deletions src/style-spec/reference/v8.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
},
"fog": {
"type": "fog",
"doc": ""
"doc": "A global effect that fades layers and markers based on their distance to the camera. The fog can be used to approximate the effect of atmosphere on distant objects and enhance the depth perception of the map when used with terrain or 3D features."
},
"sources": {
"required": true,
Expand Down Expand Up @@ -3681,8 +3681,8 @@
"range": {
"type": "array",
"default": [
5,
10
2,
12
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These seem like reasonable defaults to me. For reference, here's [2,12]:

fog-2-12 copy

and [5,12]:
fog-5-12

],
"minimum": 0,
"maximum": 20,
Expand All @@ -3696,17 +3696,14 @@
"zoom"
]
},
"doc": "",
"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.",
"example": [
5,
10
2,
12
],
"sdk-support": {
"basic functionality": {
"js": "",
"android": "",
"ios": "",
"macos": ""
"js": "2.3.0"
}
}
},
Expand All @@ -3721,13 +3718,10 @@
]
},
"transition": true,
"doc": "",
"doc": "The color of the fog. If provided as an `rgba` color, the alpha component is unused.",
"sdk-support": {
"basic functionality": {
"js": "",
"android": "",
"ios": "",
"macos": ""
"js": "2.3.0"
}
}
},
Expand All @@ -3742,13 +3736,10 @@
]
},
"transition": true,
"doc": "",
"doc": "The color of the haze component of the fog, used only when haze energy is nonzero. If provided as an `rgba` color, the alpha component is unused.",
"sdk-support": {
"basic functionality": {
"js": "",
"android": "",
"ios": "",
"macos": ""
"js": "2.3.0"
}
}
},
Expand All @@ -3764,20 +3755,17 @@
]
},
"transition": true,
"doc": "",
"doc": "Controls the strength of the fog effect. Reducing the fog strength pushes the fog farther toward the horizon, leaving more visible space for haze, if enabled. When haze energy is zero, this will usually be 1.0.",
"sdk-support": {
"basic functionality": {
"js": "",
"android": "",
"ios": "",
"macos": ""
"js": "2.3.0"
}
}
},
"haze-energy": {
"type": "number",
"property-type": "data-constant",
"default": 1.0,
"default": 0.0,
"minimum": 0.0,
"expression": {
"interpolated": true,
Expand All @@ -3786,20 +3774,17 @@
]
},
"transition": true,
"doc": "",
"doc": "Controls the strength of haze in the fog layer. Haze is an additive effect which approximates the look of an atmosphere. Increasing this strengthens the haze. Reducing the fog strength pushes fog toward the horizon, making haze visible.",
"sdk-support": {
"basic functionality": {
"js": "",
"android": "",
"ios": "",
"macos": ""
"js": "2.3.0"
}
}
},
"sky-blend": {
"type": "number",
"property-type": "data-constant",
"default": 1,
"default": 0.1,
"minimum": 0,
"maximum": 1,
"expression": {
Expand All @@ -3809,13 +3794,10 @@
]
},
"transition": true,
"doc": "",
"doc": "The sky blend allows to blend the effect of the fog above the horizon line. A value of 1 will entirely blend the fog onto the sky, while a value of 0 will leave the sky unaffected by fog.",
"sdk-support": {
"basic functionality": {
"js": "",
"android": "",
"ios": "",
"macos": ""
"js": "2.3.0"
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/style/fog.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ type Props = {|
"sky-blend": DataConstantProperty<number>,
|};

const properties: Properties<Props> = new Properties({
const fogProperties: Properties<Props> = new Properties({
"range": new DataConstantProperty(styleSpec.fog.range),
"color": new DataConstantProperty(styleSpec.fog.color),
"haze-color": new DataConstantProperty(styleSpec.fog["haze-color"]),
"haze-energy": new DataConstantProperty(styleSpec.fog["strength"]),
"strength": new DataConstantProperty(styleSpec.fog["haze-energy"]),
"haze-energy": new DataConstantProperty(styleSpec.fog["haze-energy"]),
"strength": new DataConstantProperty(styleSpec.fog["strength"]),
"sky-blend": new DataConstantProperty(styleSpec.fog["sky-blend"]),
});

Expand All @@ -41,7 +41,7 @@ class Fog extends Evented {

constructor(fogOptions?: FogSpecification) {
super();
this._transitionable = new Transitionable(properties);
this._transitionable = new Transitionable(fogProperties);
this.set(fogOptions);
this._transitioning = this._transitionable.untransitioned();
}
Expand Down
32 changes: 31 additions & 1 deletion src/ui/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -2174,17 +2174,47 @@ class Map extends Camera {
return this.style ? this.style.getTerrain() : null;
}

/**
* Sets the fog property of the style.
* @param fog The fog properties to set. Must conform the [Mapbox Style Specification](https://docs.mapbox.com/mapbox-gl-js/style-spec/root/#fog).
* If `null` or `undefined` is provided, this function call removes the fog from the map.
* @returns {Map} `this`
* @example
* map.setFog({
* "range": [1.0, 12.0],
* "color": 'white',
* "haze-color": 'rgba(109, 123, 180, 1.0)',
* "strength": 1.0,
* "haze-energy": 1.0,
* "sky-blend": 0.1
* });
*/
setFog(fog: FogSpecification) {
this._lazyInitEmptyStyle();
this.style.setFog(fog);
return this._update(true);
}

// NOTE: Make fog non-optional
/**
* Returns the fog specification or `null` if fog is not set on the map.
*
* @returns {Object} fog Fog specification properties of the style.
*/
getFog(): Fog | null {
return this.style ? this.style.getFog() : null;
}

/**
* Returns the fog opacity for a given geolocation.
*
* An opacity of 0 means that there is no fog contribution for the given geolocation
* while a fog opacity of 1.0 means the geolocation is fully obscured by the fog effect.
*
* If there is no fog set on the map, this function will simply return 0.
*
* @param {LngLatLike} lnglat The geographical location to evaluate the fog on.
* @returns {number} A value between 0 and 1 representing the fog opacity, where 1 means fully within, and 0 means not affected by the fog effect.
*/
getFogOpacity(lnglat: LngLatLike): number {
if (!this.style || !this.style.fog) return 0.0;
return this.style.fog.getOpacityAtLatLng(LngLat.convert(lnglat), this.transform);
Expand Down