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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(postprocess): a colour written among a volume's uniforms is read as a colour, not blended into NaN
yhhafcc6761723f
diff --git a/sdk/src/postprocess/volumeBlending.ts b/sdk/src/postprocess/volumeBlending.tsindex cb81e6c3b..169390b7a 100644--- a/sdk/src/postprocess/volumeBlending.ts+++ b/sdk/src/postprocess/volumeBlending.ts@@ -2,6 +2,7 @@// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Teamimport type { PostProcessVolumeData } from '../ecs/component';import { getEffectDef } from './effects';+import { log } from '../util/logger';export interface VolumeTransform {x: number;@@ -100,6 +101,36 @@ export function computeVolumeFactor(return (1 - dist / volume.blendDistance) * volume.weight;}+const isRgb = (v: unknown): v is { r: number; g: number; b: number; a?: number } =>+ !!v && typeof v === 'object' && ['r', 'g', 'b'].every((k) => typeof (v as Record<string, unknown>)[k] === 'number');++const warned = new Set<string>();+const warnOnce = (key: string, message: string): void => {+ if (warned.has(key)) return;+ warned.add(key);+ log.warn('postprocess', message);+};++/**+ * An effect's floats and colours as the volume states them. A colour belongs in `colors`; one written among the floats (by hand,+ * by a tool) was blended as a number into NaN, and the effect drew garbage — Height Fog turned a whole scene teal. Its meaning is+ * not in doubt, so it is read as the colour it is and the author is told where it goes; anything else that is not a number is left out.+ */+function effectParams(effect: PostProcessVolumeData['effects'][number]): { floats: [string, number][]; colors: [string, Rgba][] } {+ const floats: [string, number][] = [];+ const colors: [string, Rgba][] = Object.entries(effect.colors ?? {});+ for (const [key, value] of Object.entries(effect.uniforms as Record<string, unknown>)) {+ if (typeof value === 'number' && Number.isFinite(value)) { floats.push([key, value]); continue; }+ if (isRgb(value)) {+ warnOnce(`${effect.type}.${key}:colour`, `${effect.type}: ${key} is a colour written among the uniforms; read as a colour (it belongs in "colors")`);+ if (!colors.some(([k]) => k === key)) colors.push([key, { r: value.r, g: value.g, b: value.b, a: value.a ?? 1 }]);+ continue;+ }+ warnOnce(`${effect.type}.${key}:nan`, `${effect.type}: ${key} is not a number (${JSON.stringify(value)}); left out`);+ }+ return { floats, colors };+}+export function blendVolumeEffects(activeVolumes: ActiveVolume[]): Map<string, BlendedEffect> {@@ -114,13 +145,14 @@ export function blendVolumeEffects(if (!effect.enabled) continue;const existing = result.get(effect.type);+ const params = effectParams(effect);if (!existing) {// Blend from each param's neutral ("effect does nothing") value, not// from 0 — else a multiplicative param (contrast/saturation/zoom,// neutral 1) fades to 0 (black / extreme distortion) at the edge.const def = getEffectDef(effect.type);const uniforms = new Map<string, number>();- for (const [key, value] of Object.entries(effect.uniforms)) {+ for (const [key, value] of params.floats) {const neutral = def?.uniforms.find((u) => u.name === key)?.neutralValue ?? 0;uniforms.set(key, neutral + (value - neutral) * factor);}@@ -133,11 +165,9 @@ export function blendVolumeEffects(// A colour fades from the effect's declared default, as a float fades// from its neutral: a half-weight volume is half the way to its colour.const colors = new Map<string, Rgba>();- if (effect.colors) {- for (const [key, value] of Object.entries(effect.colors)) {- const base = def?.colors?.find((c) => c.name === key)?.defaultValue ?? value;- colors.set(key, lerpRgba(base, value, factor));- }+ for (const [key, value] of params.colors) {+ const base = def?.colors?.find((c) => c.name === key)?.defaultValue ?? value;+ colors.set(key, lerpRgba(base, value, factor));}result.set(effect.type, { enabled: true, uniforms, textures, colors });} else {@@ -146,17 +176,15 @@ export function blendVolumeEffects(if (ref) existing.textures.set(key, ref);}}- for (const [key, value] of Object.entries(effect.uniforms)) {+ for (const [key, value] of params.floats) {const prev = existing.uniforms.get(key) ?? 0;existing.uniforms.set(key, prev + (value - prev) * factor);}- if (effect.colors) {- const def = getEffectDef(effect.type);- for (const [key, value] of Object.entries(effect.colors)) {- const prev = existing.colors.get(key)- ?? def?.colors?.find((c) => c.name === key)?.defaultValue ?? value;- existing.colors.set(key, lerpRgba(prev, value, factor));- }+ const def = getEffectDef(effect.type);+ for (const [key, value] of params.colors) {+ const prev = existing.colors.get(key)+ ?? def?.colors?.find((c) => c.name === key)?.defaultValue ?? value;+ existing.colors.set(key, lerpRgba(prev, value, factor));}}}diff --git a/sdk/tests/volumeBlending.test.ts b/sdk/tests/volumeBlending.test.tsindex 68e357df2..85d1dd5f0 100644--- a/sdk/tests/volumeBlending.test.ts+++ b/sdk/tests/volumeBlending.test.ts@@ -199,6 +199,32 @@ describe('volumeBlending', () => {});describe('blendVolumeEffects', () => {+ const volumeOf = (uniforms: Record<string, unknown>, colors?: Record<string, unknown>) => ({+ data: {+ effects: [{ type: 'heightFog', enabled: true, uniforms, ...(colors ? { colors } : {}) }],+ isGlobal: true, shape: 'box', size: { x: 5, y: 5 }, priority: 0, weight: 1, blendDistance: 0,+ } as never,+ factor: 1,+ });++ it('reads a colour written among the uniforms as the colour it is, not as NaN', () => {+ const fog = blendVolumeEffects([volumeOf({ u_density: 0.01, u_fogColor: { r: 0.5, g: 0.6, b: 0.7, a: 1 } })]).get('heightFog')!;+ expect(fog.uniforms.has('u_fogColor')).toBe(false);+ expect(fog.colors.get('u_fogColor')).toEqual({ r: 0.5, g: 0.6, b: 0.7, a: 1 });+ expect([...fog.uniforms.values()].every(Number.isFinite)).toBe(true);+ });++ it('lets the colours block win over the same colour among the uniforms', () => {+ const fog = blendVolumeEffects([volumeOf({ u_fogColor: { r: 1, g: 0, b: 0, a: 1 } }, { u_fogColor: { r: 0, g: 0, b: 1, a: 1 } })]).get('heightFog')!;+ expect(fog.colors.get('u_fogColor')).toEqual({ r: 0, g: 0, b: 1, a: 1 });+ });++ it('leaves out a uniform that is not a number', () => {+ const fog = blendVolumeEffects([volumeOf({ u_density: 'thick', u_startDistance: 2 })]).get('heightFog')!;+ expect(fog.uniforms.has('u_density')).toBe(false);+ expect(fog.uniforms.get('u_startDistance')).toBe(2);+ });+it('returns empty map when no active volumes', () => {const result = blendVolumeEffects([]);expect(result.size).toBe(0);