Skip to content

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

fix: Light & Shadow regression in video export (#3680)

Igor Dykhta78ab050c80f7
diff --git a/src/effects/src/custom-deck-lighting-effect.spec.ts b/src/effects/src/custom-deck-lighting-effect.spec.ts
new file mode 100644
index 000000000..b043dd5b9
--- /dev/null
+++ b/src/effects/src/custom-deck-lighting-effect.spec.ts
@@ -0,0 +1,58 @@
+// SPDX-License-Identifier: MIT
+// Copyright contributors to the kepler.gl project
+
+import {alignExportShadowPass} from './custom-deck-lighting-effect';
+
+describe('alignExportShadowPass', () => {
+ function createShadowPass() {
+ const canvasContext = {
+ getDrawingBufferSize: jest.fn((): [number, number] => [1080, 608]),
+ cssToDeviceRatio: jest.fn(() => 2)
+ };
+ const gl = {
+ drawingBufferWidth: 1920,
+ drawingBufferHeight: 1080,
+ DEPTH_RANGE: 0x0b70,
+ depthRange: jest.fn(),
+ getParameter: jest.fn(() => [0, 0.979])
+ };
+ const shadowPass = {
+ device: {gl, canvasContext},
+ render: jest.fn((_params?: unknown) => ({
+ size: canvasContext.getDrawingBufferSize(),
+ ratio: canvasContext.cssToDeviceRatio()
+ }))
+ };
+ return {shadowPass, canvasContext, gl};
+ }
+
+ test('sizes the shadow pass to the live GL buffer during export, then restores', () => {
+ const {shadowPass, canvasContext, gl} = createShadowPass();
+ const originalSize = canvasContext.getDrawingBufferSize;
+ const originalRatio = canvasContext.cssToDeviceRatio;
+ alignExportShadowPass(shadowPass, () => true);
+
+ expect(shadowPass.render({viewports: [{width: 540}]})).toEqual({
+ size: [1920, 1080],
+ ratio: 1920 / 540
+ });
+ expect(canvasContext.getDrawingBufferSize).toBe(originalSize);
+ expect(canvasContext.cssToDeviceRatio).toBe(originalRatio);
+ expect(canvasContext.getDrawingBufferSize()).toEqual([1080, 608]);
+ expect(canvasContext.cssToDeviceRatio()).toBe(2);
+ expect(gl.depthRange.mock.calls).toEqual([
+ [0, 1],
+ [0, 0.979]
+ ]);
+ });
+
+ test('does not override luma sizes outside export mode', () => {
+ const {shadowPass} = createShadowPass();
+ alignExportShadowPass(shadowPass, () => false);
+
+ expect(shadowPass.render({viewports: [{width: 540}]})).toEqual({
+ size: [1080, 608],
+ ratio: 2
+ });
+ });
+});
diff --git a/src/effects/src/custom-deck-lighting-effect.ts b/src/effects/src/custom-deck-lighting-effect.ts
index 0d8130d7d..7f5887010 100644
--- a/src/effects/src/custom-deck-lighting-effect.ts
+++ b/src/effects/src/custom-deck-lighting-effect.ts
@@ -31,6 +31,42 @@ interface CustomShadowProps {
[key: string]: unknown;
}
+/**
+ * Video export shares MapLibre's canvas. Luma's cssToDeviceRatio() can lag that
+ * bitmap, so ShadowPass draws into a corner of its FBO and the rest samples as
+ * fully shadowed. For the shadow pass only, read the live GL buffer; restore
+ * afterward so the color pass / basemap stay aligned.
+ */
+export function alignExportShadowPass(shadowPass: any, isExportMode: () => boolean): void {
+ const render = shadowPass.render.bind(shadowPass);
+ shadowPass.render = params => {
+ if (!isExportMode()) return render(params);
+
+ const gl = shadowPass.device?.gl;
+ const canvasContext = shadowPass.device?.canvasContext;
+ const viewport = params?.viewports?.[0];
+ const width = gl?.drawingBufferWidth;
+ const height = gl?.drawingBufferHeight;
+ if (!gl || !canvasContext || !viewport?.width || !width || !height) {
+ return render(params);
+ }
+
+ const prevSize = canvasContext.getDrawingBufferSize;
+ const prevRatio = canvasContext.cssToDeviceRatio;
+ const prevDepth = gl.getParameter?.(gl.DEPTH_RANGE ?? 0x0b70);
+ canvasContext.getDrawingBufferSize = () => [width, height];
+ canvasContext.cssToDeviceRatio = () => width / viewport.width;
+ gl.depthRange?.(0, 1);
+ try {
+ return render(params);
+ } finally {
+ canvasContext.getDrawingBufferSize = prevSize;
+ canvasContext.cssToDeviceRatio = prevRatio;
+ if (prevDepth) gl.depthRange?.(prevDepth[0], prevDepth[1]);
+ }
+ };
+}
+
/**
* Insert text before a target string in shader source.
*/
@@ -165,6 +201,9 @@ class CustomDeckLightingEffect extends LightingEffect {
const {device, deck} = context;
if (this._private.shadow && !this._private.dummyShadowMap) {
this._private._createShadowPasses(device);
+ for (const shadowPass of this._private.shadowPasses) {
+ alignExportShadowPass(shadowPass, () => this.isExportMode);
+ }
deck._addDefaultShaderModule(CustomShadowModule || shadow);
this._private.dummyShadowMap = device.createTexture({width: 1, height: 1});
}