Skip to content

Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.

fix: Light & Shadow layer order shading and top-view occlusion (#3681)

Igor Dykhta995995857a63
diff --git a/src/effects/src/custom-deck-lighting-effect.spec.ts b/src/effects/src/custom-deck-lighting-effect.spec.ts
index b043dd5b9..20ec1329a 100644
--- a/src/effects/src/custom-deck-lighting-effect.spec.ts
+++ b/src/effects/src/custom-deck-lighting-effect.spec.ts
@@ -1,7 +1,28 @@
// SPDX-License-Identifier: MIT
// Copyright contributors to the kepler.gl project
-import {alignExportShadowPass} from './custom-deck-lighting-effect';
+import {alignExportShadowPass, patchShadowPassDepth} from './custom-deck-lighting-effect';
+
+describe('patchShadowPassDepth', () => {
+ test('forces WebGL depth writes and disables polygon offset', () => {
+ const pass = {
+ getLayerParameters: (_layer: unknown, _layerIndex: number, _viewport: unknown) => ({
+ depthTest: true,
+ depthMask: false,
+ blend: true
+ })
+ };
+
+ patchShadowPassDepth(pass);
+
+ expect(pass.getLayerParameters({}, 0, null)).toMatchObject({
+ depthTest: true,
+ depthMask: true,
+ polygonOffsetFill: false,
+ polygonOffset: [0, 0]
+ });
+ });
+});
describe('alignExportShadowPass', () => {
function createShadowPass() {
diff --git a/src/effects/src/custom-deck-lighting-effect.ts b/src/effects/src/custom-deck-lighting-effect.ts
index 7f5887010..bc38dd854 100644
--- a/src/effects/src/custom-deck-lighting-effect.ts
+++ b/src/effects/src/custom-deck-lighting-effect.ts
@@ -18,11 +18,43 @@ const DEFAULT_LIGHTING_EFFECT = new LightingEffect();
*/
interface LightingEffectPrivate {
shadow: boolean;
- shadowPasses: {delete(): void; render(params: Record<string, unknown>): void}[];
+ shadowPasses: {
+ delete(): void;
+ render(params: Record<string, unknown>): void;
+ getLayerParameters: (
+ layer: unknown,
+ layerIndex: number,
+ viewport: unknown
+ ) => Record<string, unknown>;
+ }[];
dummyShadowMap: Texture | null;
_createShadowPasses(device: unknown): void;
}
+// WebGL applies GL-style depthTest/depthMask; deck.gl's ShadowPass only sets
+// depthWriteEnabled. Disable polygonOffset so a later ground plane is not
+// pulled toward the sun and cannot overwrite building walls in the shadow map.
+const SHADOW_PASS_DEPTH_PARAMETERS = {
+ depthTest: true,
+ depthMask: true,
+ polygonOffsetFill: false,
+ polygonOffset: [0, 0]
+};
+
+export function patchShadowPassDepth(pass: {
+ getLayerParameters: (
+ layer: unknown,
+ layerIndex: number,
+ viewport: unknown
+ ) => Record<string, unknown>;
+}) {
+ const originalGetLayerParameters = pass.getLayerParameters.bind(pass);
+ pass.getLayerParameters = (layer, layerIndex, viewport) => ({
+ ...originalGetLayerParameters(layer, layerIndex, viewport),
+ ...SHADOW_PASS_DEPTH_PARAMETERS
+ });
+}
+
/** Extended shadow module props with our custom field. */
interface CustomShadowProps {
outputUniformShadow?: boolean;
@@ -202,6 +234,7 @@ class CustomDeckLightingEffect extends LightingEffect {
if (this._private.shadow && !this._private.dummyShadowMap) {
this._private._createShadowPasses(device);
for (const shadowPass of this._private.shadowPasses) {
+ patchShadowPassDepth(shadowPass);
alignExportShadowPass(shadowPass, () => this.isExportMode);
}
deck._addDefaultShaderModule(CustomShadowModule || shadow);
diff --git a/src/layers/src/base-layer.ts b/src/layers/src/base-layer.ts
index 647dc0a88..f2e7bb82a 100644
--- a/src/layers/src/base-layer.ts
+++ b/src/layers/src/base-layer.ts
@@ -1554,6 +1554,11 @@ class Layer implements KeplerLayer {
visible: boolean;
}) {
const blendingParameters = mapState.layerParameters ?? {};
+ const enable3d = Boolean(this.config.visConfig.enable3d);
+ const is3dView = Boolean(mapState.dragRotate);
+ // Always depth-test so a flat layer cannot paint over closer extruded
+ // geometry (e.g. a ground plane covering buildings in top view). Only 3D
+ // layers / 3D view write depth, so coplanar 2D layers still stack by order.
return {
id: this.id,
idx,
@@ -1561,7 +1566,8 @@ class Layer implements KeplerLayer {
pickable: true,
wrapLongitude: true,
parameters: {
- depthTest: Boolean(mapState.dragRotate || this.config.visConfig.enable3d),
+ depthTest: true,
+ depthMask: is3dView || enable3d,
...blendingParameters
},
hidden: this.config.hidden,
diff --git a/src/layers/src/geojson-layer/geojson-layer.spec.ts b/src/layers/src/geojson-layer/geojson-layer.spec.ts
index cfad89a73..354c56bee 100644
--- a/src/layers/src/geojson-layer/geojson-layer.spec.ts
+++ b/src/layers/src/geojson-layer/geojson-layer.spec.ts
@@ -3,6 +3,45 @@
import GeoJsonLayer from './geojson-layer';
+describe('GeoJsonLayer default deck parameters', () => {
+ const gpuFilter = null as any;
+ const layerCallbacks = {};
+
+ const propsFor = (layer: GeoJsonLayer, mapState: Record<string, unknown>) =>
+ layer.getDefaultDeckLayerProps({
+ idx: 0,
+ gpuFilter,
+ mapState: mapState as any,
+ layerCallbacks,
+ visible: true
+ }).parameters;
+
+ test('flat layer in top view depth-tests but does not write depth', () => {
+ const layer = new GeoJsonLayer({id: 'geojson_depth'});
+ expect(propsFor(layer, {dragRotate: false})).toMatchObject({
+ depthTest: true,
+ depthMask: false
+ });
+ });
+
+ test('extruded layer writes depth even in top view so it can occlude a flat plane', () => {
+ const layer = new GeoJsonLayer({id: 'geojson_depth'});
+ layer.config.visConfig.enable3d = true;
+ expect(propsFor(layer, {dragRotate: false})).toMatchObject({
+ depthTest: true,
+ depthMask: true
+ });
+ });
+
+ test('3D view writes depth for flat layers so they participate in occlusion', () => {
+ const layer = new GeoJsonLayer({id: 'geojson_depth'});
+ expect(propsFor(layer, {dragRotate: true})).toMatchObject({
+ depthTest: true,
+ depthMask: true
+ });
+ });
+});
+
describe('GeoJsonLayer hover overlay cache', () => {
const polygonFeature = {
type: 'Feature' as const,
diff --git a/test/browser/layer-tests/geojson-layer-specs.js b/test/browser/layer-tests/geojson-layer-specs.js
index e64d0c46f..44069dac1 100644
--- a/test/browser/layer-tests/geojson-layer-specs.js
+++ b/test/browser/layer-tests/geojson-layer-specs.js
@@ -563,7 +563,7 @@ test('#GeojsonLayer -> renderLayer', t => {
filled: false,
wireframe: false,
opacity: 0.8,
- parameters: {depthTest: false},
+ parameters: {depthTest: true, depthMask: false},
visible: true,
autoHighlight: false,
wrapLongitude: false,