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(editor): the editor's own eye shows the scene's post-processing, so the sky is in the viewport
yhhfa94cdc18741
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 9d3f203b1..69bf88a4e 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -119,6 +119,7 @@ published separately; it ships inside the editor.### Fixed+- **The editor's own eye shows the scene's post-processing.** The sky, haze, exposure, anti-aliasing and the rest a scene's `PostProcessVolume` says were bound to the scene's camera entities only, and the editor viewport is not one, so a scene whose sky is `skyAtmosphere` showed a black sky while authoring and a blue one in the game. The editor's eye now resolves the scene's volumes at its own position and shows them, and lets them go when the game's camera is the one looking.- **Resizing a viewport no longer piles up render targets.** A viewport dragged or docked through many sizes borrowed a full set of targets (the scene, its depth, the post chain) at each size and left each idle for two seconds before freeing it, which grew the GPU process to ten gigabytes in a few seconds and lost the graphics context. Targets nobody borrows are now kept within a budget of 256 MB, the longest idle first.- **Many placed objects no longer crash the renderer.** The texture that holds every object's record in a frame grew by doubling but was created from storage sized to the frame's need, so a frame with many objects read past its end and the engine aborted with `memory access out of bounds`; stopping then reported one warning for every entity. The texture is created from storage as tall as itself, and an aborted engine is reported once.- **A cloud's shadow stays where the cloud is.** The shadow a sky's clouds cast on the scene added the camera's position to the cloud's, so it slid across the ground as the camera moved while the cloud in the sky stood still; it is taken from the same place the sky draws the cloud from.diff --git a/sdk/src/postprocess/PostProcessPlugin.ts b/sdk/src/postprocess/PostProcessPlugin.tsindex 40bb867b2..f9891bacc 100644--- a/sdk/src/postprocess/PostProcessPlugin.ts+++ b/sdk/src/postprocess/PostProcessPlugin.ts@@ -4,7 +4,7 @@ import type { App, Plugin } from '../app/app';import { Schedule } from '../ecs/system';import { Assets } from '../asset/AssetPlugin';import { PostProcess, PostProcessAPI } from './PostProcessAPI';-import { postProcessVolumeSystem, cleanupVolumeSystem, setVolumeTextureResolver, setBuiltinTextureMaker, PostProcessVolumeConfigResource } from './volumeSystem';+import { postProcessVolumeSystem, editorViewVolumeSystem, cleanupVolumeSystem, setVolumeTextureResolver, setBuiltinTextureMaker, PostProcessVolumeConfigResource } from './volumeSystem';import { createTextureFromPixels } from '../runtime/runtimeAssets';import { TextureContent } from '../wasm';import { weatherSystem, resetWeatherSystem } from './weatherSystem';@@ -32,6 +32,7 @@ export class PostProcessPlugin implements Plugin {// The weather first: the volumes read what it has decided.app.addSystemToSchedule(Schedule.PostUpdate, weatherSystem);app.addSystemToSchedule(Schedule.PostUpdate, postProcessVolumeSystem);+ app.addSystemToSchedule(Schedule.PostUpdate, editorViewVolumeSystem(app));}cleanup(app?: App): void {diff --git a/sdk/src/postprocess/volumeSystem.ts b/sdk/src/postprocess/volumeSystem.tsindex 1ce02a3f8..972001bfb 100644--- a/sdk/src/postprocess/volumeSystem.ts+++ b/sdk/src/postprocess/volumeSystem.ts@@ -1,6 +1,9 @@// SPDX-License-Identifier: Apache-2.0// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Teamimport { defineSystem } from '../ecs/system';+import type { App } from '../app/app';+import { EditorView, editorViewEye } from '../camera/EditorView';+import { EDITOR_VIEW_ENTITY } from '../camera/uiLayoutRect';import { defineResource, Res } from '../ecs/resource';import { Query } from '../ecs/query';import type { Entity } from '../types';@@ -264,6 +267,25 @@ function applyBlendedEffects(}}+/** The effects the scene's volumes resolve to for an eye at (x, y, z): blended, then moved by the weather, the time of day, the clouds and the water. */+export function effectsAt(volumes: { data: PostProcessVolumeData; tx: VolumeTransform }[], x: number, y: number, z: number): ResolvedEffects {+ const activeVolumes: ActiveVolume[] = [];+ for (const { data, tx } of volumes) {+ const factor = computeVolumeFactor(data, tx, x, y, z);+ if (factor > 0) activeVolumes.push({ data, factor });+ }+ const blended = activeVolumes.length > 0 ? blendVolumeEffects(activeVolumes) : new Map() as ResolvedEffects;+ // The weather, if a Weather component is in the scene: scales and tints what the volumes resolved, and brings the effects a weather implies.+ const weather = currentWeather();+ if (weather) applyWeatherToEffects(blended, weather, currentWeatherDrive());+ const clock = currentTimeOfDay();+ if (clock) applyTimeToEffects(blended, clock, currentTimeDrive(), timeLightsApplied());+ applyCloudsToFog(blended);+ // A camera under water: the effect the water module says it needs.+ applyUnderwaterToEffects(blended, currentUnderwater());+ return blended;+}+export const postProcessVolumeSystem = defineSystem([Res(PostProcess), Query(PostProcessVolume, Transform), Query(Camera, Transform)],(@@ -282,37 +304,38 @@ export const postProcessVolumeSystem = defineSystem(// 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 cx = cameraTransform.position.x;- const cy = cameraTransform.position.y;- const cz = cameraTransform.position.z;-- const activeVolumes: ActiveVolume[] = [];- for (const { data, tx } of volumes) {- const factor = computeVolumeFactor(data, tx, cx, cy, cz);- if (factor > 0) activeVolumes.push({ data, factor });- }-- const blended = activeVolumes.length > 0- ? blendVolumeEffects(activeVolumes)- : new Map() as ResolvedEffects;-- // The weather, if a Weather component is in the scene: scales and tints what the volumes resolved, and- // brings the effects a weather implies.- const weather = currentWeather();- if (weather) applyWeatherToEffects(blended, weather, currentWeatherDrive());- const clock = currentTimeOfDay();- if (clock) applyTimeToEffects(blended, clock, currentTimeDrive(), timeLightsApplied());- applyCloudsToFog(blended);- // A camera under water: the effect the water module says it needs.- applyUnderwaterToEffects(blended, currentUnderwater());-- applyBlendedEffects(api, cameraEntity, blended);+ const p = cameraTransform.position;+ applyBlendedEffects(api, cameraEntity, effectsAt(volumes, p.x, p.y, p.z));}},{ name: 'PostProcessVolumeSystem' });+/**+ * The same for the editor's own eye, which is not a camera entity: while the editor looks at the scene through it, the scene's volumes are resolved at the eye+ * and bound to it, so the sky, the haze and the look are in the viewport as they are in the game. When it stops looking (the game's camera is) its stack is let go.+ */+export function editorViewVolumeSystem(app: App) {+ return defineSystem(+ [Res(PostProcess), Query(PostProcessVolume, Transform)],+ (api: PostProcessAPI, volumeQuery: Iterable<[Entity, PostProcessVolumeData, TransformData]>) => {+ const view = app.hasResource(EditorView) ? app.getResource(EditorView) : null;+ const eye = EDITOR_VIEW_ENTITY as Entity;+ if (!view || !view.active || !app.getResource(PostProcessVolumeConfigResource).enabled) {+ if (api.volumeStacks.has(eye)) applyBlendedEffects(api, eye, new Map() as ResolvedEffects);+ return;+ }+ 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, z: transform.position.z } });+ }+ const at = view.perspective ? editorViewEye(view) : { x: view.x, y: view.y, z: view.z };+ applyBlendedEffects(api, eye, effectsAt(volumes, at.x, at.y, at.z));+ },+ { name: 'EditorViewVolumeSystem' },+ );+}+export function cleanupVolumeSystem(api: PostProcessAPI): void {for (const [camera, stack] of api.volumeStacks) {api.unbind(camera);diff --git a/sdk/tests/volumeBlending.test.ts b/sdk/tests/volumeBlending.test.tsindex 6ee19c31a..68e357df2 100644--- a/sdk/tests/volumeBlending.test.ts+++ b/sdk/tests/volumeBlending.test.ts@@ -463,3 +463,17 @@ describe('volumeBlending', () => {});});});++describe('the effects the scene resolves for an eye that is not a camera entity', () => {+ it('are the global volume\'s anywhere, and a local volume\'s only where the eye is inside it', async () => {+ const { effectsAt } = await import('../src/postprocess/volumeSystem');+ const volume = (isGlobal: boolean, type: string) => ({+ isGlobal, priority: 0, weight: 1, blendDistance: 0, shape: 'box', size: { x: 10, y: 10, z: 10 },+ effects: [{ type, enabled: true, uniforms: {}, colors: {}, textures: {} }],+ }) as never;+ const tx = { x: 0, y: 0, z: 0 };+ const volumes = [{ data: volume(true, 'skyAtmosphere'), tx }, { data: volume(false, 'bloom'), tx: { x: 500, y: 0, z: 0 } }];+ expect([...effectsAt(volumes, 4000, 20, 4000).keys()]).toEqual(['skyAtmosphere']);+ expect([...effectsAt(volumes, 500, 0, 0).keys()].sort()).toEqual(['bloom', 'skyAtmosphere']);+ });+});diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex 05e0c73b7..8ad87e29b 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -2912,6 +2912,8 @@ export const NOTED = {{"note":"A quality level sets how far plants keep their detail."},"feat(worldgen): a layer plans the far versions of its plants sparsely, and a failed asset load is tried again":{"note":"A far world is planned sparsely, and a failed asset load is tried again."},+ "fix(editor): the editor's own eye shows the scene's post-processing, so the sky is in the viewport":+ {"note":"The editor's own eye shows the scene's post-processing."},"feat(assets): a project names folders its asset database leaves alone":{"note":"A project can keep folders out of its asset database."},"feat(worldgen): a plant part may cast no shadow, and far cells are capped at two cells wide":