Skip to content

Commit 76a01c1

Browse files
authored
Fix issues with setting sky to undefined (#4587)
* Fix issues with undefining a style * Update changelog * Fix test name
1 parent c8b97c9 commit 76a01c1

8 files changed

Lines changed: 57 additions & 16 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
- Fix 3D map freezing when camera is adjusted against map bounds. ([#4537](https://github.com/maplibre/maplibre-gl-js/issues/4537))
1313
- Fix `getStyle()` to return a clone so the object cannot be internally changed ([#4488](https://github.com/maplibre/maplibre-gl-js/issues/4488))
1414
- Prefer local glyph rendering for all CJKV characters, not just those in the CJK Unified Ideographs, Hiragana, Katakana, and Hangul Syllables blocks. ([#4560](https://github.com/maplibre/maplibre-gl-js/pull/4560)))
15-
- - _...Add new stuff here..._
15+
- Fix issues with setting sky to undefined ([#4587](https://github.com/maplibre/maplibre-gl-js/pull/4587)))
16+
- _...Add new stuff here..._
1617

1718
## 4.5.2
1819

src/render/painter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ export class Painter {
409409
this.clearStencil();
410410

411411
// draw sky first to not overwrite symbols
412-
if (this.style.stylesheet?.sky) drawSky(this, this.style.sky);
412+
if (this.style.sky) drawSky(this, this.style.sky);
413413

414414
this._showOverdrawInspector = options.showOverdrawInspector;
415415
this.depthRangeFor3D = [0, 1 - ((style._order.length + 2) * this.numSublayers * this.depthEpsilon)];

src/style/sky.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,20 @@ export class Sky extends Evented {
5555
this._transitionable = new Transitionable(properties);
5656
this.setSky(sky);
5757
this._transitioning = this._transitionable.untransitioned();
58+
this.recalculate(new EvaluationParameters(0));
5859
}
5960

6061
setSky(sky?: SkySpecification, options: StyleSetterOptions = {}) {
6162
if (this._validate(validateSky, sky, options)) return;
6263

64+
if (!sky) {
65+
sky = {
66+
'sky-color': 'transparent',
67+
'horizon-color': 'transparent',
68+
'fog-color': 'transparent',
69+
};
70+
}
71+
6372
for (const name in sky) {
6473
const value = sky[name];
6574
if (name.endsWith(TRANSITION_SUFFIX)) {

src/style/style.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2592,6 +2592,24 @@ describe('Style#serialize', () => {
25922592
expect(style.getSky()).toBeUndefined();
25932593
});
25942594

2595+
test('sky should be defined even after setting it to undefined and back', async () => {
2596+
const sky: SkySpecification = {
2597+
'horizon-fog-blend': 0.5,
2598+
'fog-color': '#fff'
2599+
};
2600+
const styleJson = createStyleJSON({sky});
2601+
const style = new Style(getStubMap());
2602+
style.loadJSON(styleJson);
2603+
2604+
await style.once('style.load');
2605+
style.setSky(undefined);
2606+
expect(style.serialize().sky).toBeUndefined();
2607+
style.setSky(sky);
2608+
expect(style.serialize().sky).toBeDefined();
2609+
style.setSky(undefined);
2610+
expect(style.serialize().sky).toBeUndefined();
2611+
});
2612+
25952613
test('do not include sky property after removing sky from the map', async () => {
25962614
const sky: SkySpecification = {
25972615
'horizon-fog-blend': 0.5,

src/style/style.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,17 +1496,20 @@ export class Style extends Evented {
14961496
}
14971497

14981498
setSky(skyOptions?: SkySpecification, options: StyleSetterOptions = {}) {
1499-
const sky = this.sky.getSky();
1499+
const sky = this.getSky();
15001500
let update = false;
1501-
if (!skyOptions) {
1502-
if (sky) {
1503-
update = true;
1504-
}
1505-
}
1506-
for (const key in skyOptions) {
1507-
if (!deepEqual(skyOptions[key], sky[key])) {
1508-
update = true;
1509-
break;
1501+
if (!skyOptions && !sky) return;
1502+
1503+
if (skyOptions && !sky) {
1504+
update = true;
1505+
} else if (!skyOptions && sky) {
1506+
update = true;
1507+
} else {
1508+
for (const key in skyOptions) {
1509+
if (!deepEqual(skyOptions[key], sky[key])) {
1510+
update = true;
1511+
break;
1512+
}
15101513
}
15111514
}
15121515
if (!update) return;

src/ui/map.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2665,7 +2665,7 @@ export class Map extends Camera {
26652665
/**
26662666
* Loads sky and fog defined by {@link SkySpecification} onto the map.
26672667
* Note: The fog only shows when using the terrain 3D feature.
2668-
* @param sky - Sky properties to set. Must conform to the [MapLibre Style Specification](https://maplibre.org/maplibre-gl-js-docs/style-spec/#sky).
2668+
* @param sky - Sky properties to set. Must conform to the [MapLibre Style Specification](https://maplibre.org/maplibre-style-spec/sky/).
26692669
* @returns `this`
26702670
* @example
26712671
* ```ts
@@ -2681,7 +2681,11 @@ export class Map extends Camera {
26812681
/**
26822682
* Returns the value of the sky object.
26832683
*
2684-
* @returns sky Sky properties of the style.
2684+
* @returns the sky properties of the style.
2685+
* @example
2686+
* ```ts
2687+
* map.getSky();
2688+
* ```
26852689
*/
26862690
getSky() {
26872691
return this.style.getSky();

src/util/test/util.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ export function createStyleSource() {
172172
} as SourceSpecification;
173173
}
174174

175-
export function createStyle() {
175+
export function createStyle(): StyleSpecification {
176176
return {
177177
version: 8,
178178
center: [-73.9749, 40.7736],
@@ -181,5 +181,5 @@ export function createStyle() {
181181
pitch: 50,
182182
sources: {},
183183
layers: []
184-
} as StyleSpecification;
184+
};
185185
}

test/examples/sky-with-fog-and-terrain.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<body>
2626
<div id="map"></div>
2727
<table id="listing-group" class="listing-group">
28+
<tr><td><label for="sky-enabled">enabled</label></td><td><input type="checkbox" id="sky-enabled" checked/></td></tr>
2829
<tr><td><label for="sky-color-picker">sky-color</label></td><td><input type="color" id="sky-color-picker" value="#0000ff" style="width: 100%"/></td></tr>
2930
<tr><td><label for="horizon-color-picker">horizon-color</label></td><td><input type="color" id="horizon-color-picker" value="#00ff00" style="width: 100%"/></td></tr>
3031
<tr><td><label for="fog-color-picker">fog-color</label></td><td><input type="color" id="fog-color-picker" value="#ff0000" style="width: 100%"/></td></tr>
@@ -34,6 +35,10 @@
3435
</table>
3536
<script>
3637
function setSkyFromUi() {
38+
if (!document.getElementById('sky-enabled').checked) {
39+
map.setSky(undefined);
40+
return;
41+
}
3742
map.setSky({
3843
'sky-color': document.getElementById('sky-color-picker').value,
3944
'sky-horizon-blend': +document.getElementById('sky-horizon-blend-slider').value,
@@ -116,6 +121,7 @@
116121
document.getElementById('sky-horizon-blend-slider').addEventListener('change', setSkyFromUi);
117122
document.getElementById('horizon-fog-blend-slider').addEventListener('change', setSkyFromUi);
118123
document.getElementById('fog-ground-blend-slider').addEventListener('change', setSkyFromUi);
124+
document.getElementById('sky-enabled').addEventListener('change', setSkyFromUi);
119125
setSkyFromUi();
120126
});
121127
</script>

0 commit comments

Comments
 (0)