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.

fix(render): a camera keeps to the rect it asked for

esengine074dfa56910d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a8dd7cd54..5f3787721 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -14,6 +14,21 @@ published separately; it ships inside the editor.
## [Unreleased]
+### Fixed
+
+- **A camera drawing half the screen now keeps to its half.** A camera states the rect it
+ draws into when it opens its frame; the engine took that rect back off the DEVICE later,
+ after the collect, where it was whatever the last thing to touch the device had left
+ behind. So a second camera covered the first, and split screen — two cameras with half
+ the frame each — drew one of them over the whole of it. What it looked like was the
+ second camera's view at full size, which reads as a camera that was never set up rather
+ than as a viewport that was ignored.
+
+ The rect is now taken where it is stated, and a zero size still means the whole target.
+ Two criteria cover it: two cameras with a half each, and the same split with 2D shadows
+ in it — the mask is screen space, so what a fragment reads has to be the part of it its
+ own camera filled.
+
### Changed
- **2D shadows are drawn, not solved.** A 2D shadow used to be an analytic test inside
diff --git a/fixtures/scenes/camera-split.esscene b/fixtures/scenes/camera-split.esscene
new file mode 100644
index 000000000..bb3a6d058
--- /dev/null
+++ b/fixtures/scenes/camera-split.esscene
@@ -0,0 +1,142 @@
+{
+ "version": 4,
+ "name": "Main",
+ "entities": [
+ {
+ "id": 0,
+ "name": "CameraLeft",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": -2000,
+ "y": 0,
+ "z": 10
+ }
+ }
+ },
+ {
+ "type": "Camera",
+ "data": {
+ "projectionType": 1,
+ "orthoSize": 300,
+ "isActive": true,
+ "priority": 0,
+ "viewport": {
+ "x": 0,
+ "y": 0,
+ "z": 0.5,
+ "w": 1
+ }
+ }
+ }
+ ]
+ },
+ {
+ "id": 1,
+ "name": "CameraRight",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 2000,
+ "y": 0,
+ "z": 10
+ }
+ }
+ },
+ {
+ "type": "Camera",
+ "data": {
+ "projectionType": 1,
+ "orthoSize": 300,
+ "isActive": true,
+ "priority": 1,
+ "viewport": {
+ "x": 0.5,
+ "y": 0,
+ "z": 0.5,
+ "w": 1
+ }
+ }
+ }
+ ]
+ },
+ {
+ "id": 10,
+ "name": "Quad10",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": -2000,
+ "y": 0,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Sprite",
+ "data": {
+ "size": {
+ "x": 1200,
+ "y": 1200
+ },
+ "color": {
+ "r": 0,
+ "g": 1,
+ "b": 0,
+ "a": 1
+ }
+ }
+ }
+ ]
+ },
+ {
+ "id": 11,
+ "name": "Quad11",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 2000,
+ "y": 0,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Sprite",
+ "data": {
+ "size": {
+ "x": 1200,
+ "y": 1200
+ },
+ "color": {
+ "r": 0,
+ "g": 0,
+ "b": 1,
+ "a": 1
+ }
+ }
+ }
+ ]
+ }
+ ]
+}
diff --git a/fixtures/scenes/camera-split.textures.json b/fixtures/scenes/camera-split.textures.json
new file mode 100644
index 000000000..0967ef424
--- /dev/null
+++ b/fixtures/scenes/camera-split.textures.json
@@ -0,0 +1 @@
+{}
diff --git a/fixtures/scenes/shadow2d-split.esscene b/fixtures/scenes/shadow2d-split.esscene
new file mode 100644
index 000000000..c71e1c28b
--- /dev/null
+++ b/fixtures/scenes/shadow2d-split.esscene
@@ -0,0 +1,270 @@
+{
+ "version": 4,
+ "name": "Main",
+ "entities": [
+ {
+ "id": 0,
+ "name": "CameraLeft",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": -2000,
+ "y": 0,
+ "z": 10
+ }
+ }
+ },
+ {
+ "type": "Camera",
+ "data": {
+ "projectionType": 1,
+ "orthoSize": 300,
+ "isActive": true,
+ "priority": 0,
+ "viewport": {
+ "x": 0,
+ "y": 0,
+ "z": 0.5,
+ "w": 1
+ }
+ }
+ }
+ ]
+ },
+ {
+ "id": 1,
+ "name": "CameraRight",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 2000,
+ "y": 0,
+ "z": 10
+ }
+ }
+ },
+ {
+ "type": "Camera",
+ "data": {
+ "projectionType": 1,
+ "orthoSize": 300,
+ "isActive": true,
+ "priority": 1,
+ "viewport": {
+ "x": 0.5,
+ "y": 0,
+ "z": 0.5,
+ "w": 1
+ }
+ }
+ }
+ ]
+ },
+ {
+ "id": 10,
+ "name": "Floor10",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": -2000,
+ "y": 0,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Sprite",
+ "data": {
+ "size": {
+ "x": 1200,
+ "y": 1200
+ },
+ "color": {
+ "r": 1,
+ "g": 1,
+ "b": 1,
+ "a": 1
+ },
+ "material": "/scenes/mat-lit/lit.esmaterial"
+ }
+ }
+ ]
+ },
+ {
+ "id": 11,
+ "name": "Light10",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": -2120,
+ "y": 0,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Light",
+ "data": {
+ "type": 0,
+ "color": {
+ "r": 0,
+ "g": 1,
+ "b": 0,
+ "a": 1
+ },
+ "intensity": 1,
+ "radius": 1200,
+ "shadowSoftness": 0
+ }
+ }
+ ]
+ },
+ {
+ "id": 12,
+ "name": "Wall10",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": -2000,
+ "y": 0,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "ShadowCaster2D",
+ "data": {
+ "size": {
+ "x": 40,
+ "y": 300
+ },
+ "enabled": true
+ }
+ }
+ ]
+ },
+ {
+ "id": 20,
+ "name": "Floor20",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 2000,
+ "y": 0,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Sprite",
+ "data": {
+ "size": {
+ "x": 1200,
+ "y": 1200
+ },
+ "color": {
+ "r": 1,
+ "g": 1,
+ "b": 1,
+ "a": 1
+ },
+ "material": "/scenes/mat-lit/lit.esmaterial"
+ }
+ }
+ ]
+ },
+ {
+ "id": 21,
+ "name": "Light20",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 1880,
+ "y": 0,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Light",
+ "data": {
+ "type": 0,
+ "color": {
+ "r": 0,
+ "g": 0,
+ "b": 1,
+ "a": 1
+ },
+ "intensity": 1,
+ "radius": 1200,
+ "shadowSoftness": 0
+ }
+ }
+ ]
+ },
+ {
+ "id": 22,
+ "name": "Wall20",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 2000,
+ "y": 0,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "ShadowCaster2D",
+ "data": {
+ "size": {
+ "x": 40,
+ "y": 300
+ },
+ "enabled": true
+ }
+ }
+ ]
+ }
+ ]
+}
diff --git a/fixtures/scenes/shadow2d-split.textures.json b/fixtures/scenes/shadow2d-split.textures.json
new file mode 100644
index 000000000..0967ef424
--- /dev/null
+++ b/fixtures/scenes/shadow2d-split.textures.json
@@ -0,0 +1 @@
+{}
diff --git a/src/esengine/renderer/frame/RenderFrame.cpp b/src/esengine/renderer/frame/RenderFrame.cpp
index ccfee0f02..ed74eeeb2 100644
--- a/src/esengine/renderer/frame/RenderFrame.cpp
+++ b/src/esengine/renderer/frame/RenderFrame.cpp
@@ -364,6 +364,11 @@ void RenderFrame::begin(const glm::mat4& view_projection, RenderTargetManager::H
openPass(clear, target);
+ // The rect this camera draws into, taken where the camera STATES it — a zero size
+ // means the whole target, as it does to a pass. Read off the device at flush it is
+ // whatever the collect between left there, and one camera covered the other.
+ scene_viewport_ = {clear.x, clear.y, clear.w, clear.h};
+
// What the scene pass writes. With a chain engaged the capture is already a
// graph resource (the pipeline declared it when it opened); without one the
// frame's own target is imported, and the graph culls back from it.
@@ -515,10 +520,6 @@ void RenderFrame::flush() {
}
}
- // The rect the scene draws into. Opening a pass resets the viewport, so the
- // pass carries the camera's own rather than inheriting it.
- scene_viewport_ = device_.viewport();
-
accumulateStats(draw_list_);
// What the frame's render targets cost. The atlas and every chain
diff --git a/tools/renderScenes.mjs b/tools/renderScenes.mjs
index 04efb55ea..9ec322bda 100644
--- a/tools/renderScenes.mjs
+++ b/tools/renderScenes.mjs
@@ -560,6 +560,13 @@ export const SCENES = [
// material sampler — so a recovered material sampling every texture from unit
// 0 looked exactly like one with no normal map, and nothing could tell.
{ id: "device-loss-material", tier: "pr", env: { ESTELLA_VERIFY_DEVICE_LOSS: "1", ESTELLA_VERIFY_SCENE: "/scenes/mat-lit-normal.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/mat-lit-normal.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.333,\"y\":0.5,\"rgb\":[0,252,0],\"tol\":45},{\"x\":0.667,\"y\":0.5,\"rgb\":[0,36,0],\"tol\":50}]" } },
+ // Two cameras with half the frame each: what a camera draws is bounded by the rect
+ // it asked for, and one covering the other is what taking that rect off the device
+ // after the collect came to.
+ { id: "camera-split-viewport", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_PLAY: "1", ESTELLA_VERIFY_SCENE: "/scenes/camera-split.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/camera-split.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "4", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.25,\"y\":0.5,\"rgb\":[0,255,0],\"tol\":40},{\"x\":0.75,\"y\":0.5,\"rgb\":[0,0,255],\"tol\":40}]" } },
+ // The same split with 2D shadows in it. The mask is screen space, so what a fragment
+ // reads has to be the part of it its OWN camera filled.
+ { id: "shadow2d-split", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_PLAY: "1", ESTELLA_VERIFY_SCENE: "/scenes/shadow2d-split.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/shadow2d-split.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "4", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.15,\"y\":0.5,\"rgb\":[0,255,0],\"tol\":70},{\"x\":0.40,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":40},{\"x\":0.65,\"y\":0.5,\"rgb\":[0,0,255],\"tol\":70},{\"x\":0.90,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":40}]" } },
// The mask again after the device it was drawn on went away: the pass owns a
// buffer, a layout and a shader, and one kept across a loss names nothing.
{ id: "device-loss-shadow2d", tier: "pr", env: { ESTELLA_VERIFY_DEVICE_LOSS: "1", ESTELLA_VERIFY_SCENE: "/scenes/mat-lit-shadow-many.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/mat-lit-shadow-many.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.25,\"y\":0.5,\"rgb\":[0,255,0],\"tol\":60},{\"x\":0.75,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":40}]" } },