Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat(postprocess): blend local volumes by camera position

yhhb7c3b6e458e3
diff --git a/sdk/src/postprocess/volumeSystem.ts b/sdk/src/postprocess/volumeSystem.ts
index 78951c8b3..16fc78800 100644
--- a/sdk/src/postprocess/volumeSystem.ts
+++ b/sdk/src/postprocess/volumeSystem.ts
@@ -7,7 +7,7 @@ import type { Entity } from '../types';
import { PostProcessVolume, Transform, Camera, type PostProcessVolumeData, type TransformData, type CameraData } from '../component';
import { PostProcess, type PostProcessApi } from './PostProcessAPI';
import { getEffectDef } from './effects';
-import { blendVolumeEffects, type ActiveVolume } from './volumeBlending';
+import { blendVolumeEffects, computeVolumeFactor, type ActiveVolume, type VolumeTransform } from './volumeBlending';
import type { ShaderHandle } from '../material';
import { Material } from '../material';
@@ -83,32 +83,35 @@ function applyBlendedEffects(
}
export const postProcessVolumeSystem = defineSystem(
- [Res(PostProcess), Query(PostProcessVolume, Transform), Query(Camera)],
+ [Res(PostProcess), Query(PostProcessVolume, Transform), Query(Camera, Transform)],
(
api: PostProcessApi,
volumeQuery: Iterable<[Entity, PostProcessVolumeData, TransformData]>,
- cameraQuery: Iterable<[Entity, CameraData]>,
+ cameraQuery: Iterable<[Entity, CameraData, TransformData]>,
) => {
- const volumes: { data: PostProcessVolumeData; tx: { x: number; y: number } }[] = [];
+ const volumes: { data: PostProcessVolumeData; tx: VolumeTransform }[] = [];
for (const [_entity, volumeData, transform] of volumeQuery) {
volumes.push({ data: volumeData, tx: { x: transform.position.x, y: transform.position.y } });
}
- const activeVolumes: ActiveVolume[] = [];
- for (const { data } of volumes) {
- if (data.isGlobal) {
- activeVolumes.push({ data, factor: 1 });
- }
- }
+ // Recomputed per camera: a local volume's weight depends on the camera position.
+ for (const [cameraEntity, cameraData, cameraTransform] of cameraQuery) {
+ if (!cameraData.isActive) continue;
- const blended = activeVolumes.length > 0
- ? blendVolumeEffects(activeVolumes)
- : new Map<string, { enabled: boolean; uniforms: Map<string, number> }>();
+ const cx = cameraTransform.position.x;
+ const cy = cameraTransform.position.y;
- for (const [cameraEntity, cameraData] of cameraQuery) {
- if (cameraData.isActive) {
- applyBlendedEffects(api, cameraEntity, blended);
+ const activeVolumes: ActiveVolume[] = [];
+ for (const { data, tx } of volumes) {
+ const factor = computeVolumeFactor(data, tx, cx, cy);
+ if (factor > 0) activeVolumes.push({ data, factor });
}
+
+ const blended = activeVolumes.length > 0
+ ? blendVolumeEffects(activeVolumes)
+ : new Map<string, { enabled: boolean; uniforms: Map<string, number> }>();
+
+ applyBlendedEffects(api, cameraEntity, blended);
}
},
{ name: 'PostProcessVolumeSystem' }
diff --git a/sdk/tests/volumeBlending.test.ts b/sdk/tests/volumeBlending.test.ts
index 9d86ac7b4..a712960a3 100644
--- a/sdk/tests/volumeBlending.test.ts
+++ b/sdk/tests/volumeBlending.test.ts
@@ -335,4 +335,35 @@ describe('volumeBlending', () => {
expect(result.size).toBe(0);
});
});
+
+ describe('local volume gating (per-camera integration shape)', () => {
+ const localBlur = {
+ effects: [{ type: 'blur', enabled: true, uniforms: { u_intensity: 5 } }],
+ isGlobal: false,
+ shape: 'box' as const,
+ size: { x: 100, y: 100 },
+ priority: 0,
+ weight: 1,
+ blendDistance: 0,
+ };
+
+ it('camera inside the volume applies its effect', () => {
+ const factor = computeVolumeFactor(localBlur, { x: 0, y: 0 }, 0, 0);
+ const blended = blendVolumeEffects([{ data: localBlur, factor }]);
+ expect(blended.get('blur')?.uniforms.get('u_intensity')).toBeCloseTo(5, 5);
+ });
+
+ it('camera outside the volume applies nothing', () => {
+ const factor = computeVolumeFactor(localBlur, { x: 0, y: 0 }, 500, 0);
+ const active = factor > 0 ? [{ data: localBlur, factor }] : [];
+ expect(blendVolumeEffects(active).size).toBe(0);
+ });
+
+ it('camera in the blend band applies a partial effect', () => {
+ const feathered = { ...localBlur, blendDistance: 50 };
+ const factor = computeVolumeFactor(feathered, { x: 0, y: 0 }, 125, 0);
+ const blended = blendVolumeEffects([{ data: feathered, factor }]);
+ expect(blended.get('blur')?.uniforms.get('u_intensity')).toBeCloseTo(2.5, 5);
+ });
+ });
});