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
refactor(camera): the viewport rect is turned into pixels in one place, and input follows the picture
yhh32b97035eb84
diff --git a/sdk/src/camera/CameraPlugin.ts b/sdk/src/camera/CameraPlugin.tsindex 47e0d8d5e..20fce225d 100644--- a/sdk/src/camera/CameraPlugin.ts+++ b/sdk/src/camera/CameraPlugin.ts@@ -20,6 +20,7 @@ import { ProjectionType, SceneOwner, ClearFlags } from '../ecs/component';import { uiLayoutRect, computeEffectiveOrthoSize, EDITOR_VIEW_ENTITY, type CanvasScale } from './uiLayoutRect';import { EditorView, DEFAULT_EDITOR_VIEW, editorViewStandoff, editorViewClipFar, editorViewClipNear, type EditorViewData } from './EditorView';import { ScreenScaling, DEFAULT_SCREEN_SCALING, SCREEN_FIT_OFF } from './ScreenScaling';+import { planPresent, viewportPixels, RenderResolution } from './presentPlan';import { CameraDirector, createDirectorState, resolveMainPOV } from './CameraDirector';import { RenderPipeline } from '../render/renderPipeline';import { renderTargetSize } from '../render/renderTexture';@@ -563,12 +564,21 @@ function syncUICameraInfo(const uiCam = app.getResource(UICameraInfo);if (cameras.length > 0) {const cam = cameras[0];- const vr = cam.viewportRect;uiCam.viewProjection.set(cam.viewProjection);- uiCam.vpX = Math.round(vr.x * width);- uiCam.vpY = Math.round((1 - vr.y - vr.h) * height);- uiCam.vpW = Math.round(vr.w * width);- uiCam.vpH = Math.round(vr.h * height);+ // Against the WINDOW, not the camera's render target: this rect is what a+ // pointer's coordinates are normalised by, and a pointer is on the window.+ const box = viewportPixels(cam.viewportRect, width, height);+ // ...and then where the image actually lands inside it, which is the same+ // rect only while the present does not letterbox. screenToWorld maps a+ // point through THIS, so it has to be the picture's rect, not the camera's.+ const policy = app.hasResource(ScreenScaling)+ ? app.getResource(ScreenScaling).renderPolicy+ : undefined;+ const shown = planPresent(policy ?? RenderResolution.Surface, 2 * cam.halfH, box.w, box.h);+ uiCam.vpX = box.x + shown.x;+ uiCam.vpY = box.y + shown.y;+ uiCam.vpW = shown.width;+ uiCam.vpH = shown.height;uiCam.screenW = width;uiCam.screenH = height;// The box UI lays out within. Scene cameras carry design-scaled extents;@@ -715,10 +725,7 @@ export function cameraPlugin(? renderTargetSize(cam.renderTarget) : null;const w = target?.width ?? width;const h = target?.height ?? height;- const px = Math.round(vp.x * w);- const py = Math.round((1 - vp.y - vp.h) * h);- const pw = Math.round(vp.w * w);- const ph = Math.round(vp.h * h);+ const { x: px, y: py, w: pw, h: ph } = viewportPixels(vp, w, h);pipeline.renderCamera({registry: { _cpp: cppRegistry },viewProjection: cam.viewProjection,diff --git a/sdk/src/camera/presentPlan.ts b/sdk/src/camera/presentPlan.tsindex 5094a33e6..3a48e151a 100644--- a/sdk/src/camera/presentPlan.ts+++ b/sdk/src/camera/presentPlan.ts@@ -55,6 +55,34 @@ export interface PresentPlan {oneToOne: boolean;}+/** A camera's viewport as a fraction of some surface, y-UP from the bottom. */+export interface ViewportFraction {+ x: number;+ y: number;+ w: number;+ h: number;+}++/**+ * A viewport fraction in pixels of the surface it is measured against, y-DOWN.+ *+ * The flip and the rounding drift, so they live here. WHICH surface stays the+ * caller's: input measures the window (a pointer is there), drawing measures the+ * render target (pixels go there).+ */+export function viewportPixels(+ rect: ViewportFraction,+ surfaceWidth: number,+ surfaceHeight: number,+): { x: number; y: number; w: number; h: number } {+ return {+ x: Math.round(rect.x * surfaceWidth),+ y: Math.round((1 - rect.y - rect.h) * surfaceHeight),+ w: Math.round(rect.w * surfaceWidth),+ h: Math.round(rect.h * surfaceHeight),+ };+}+function clampDim(v: number): number {return Math.max(1, Math.round(v));}diff --git a/sdk/tests/present-plan.test.ts b/sdk/tests/present-plan.test.tsindex 5a303994a..d94f40867 100644--- a/sdk/tests/present-plan.test.ts+++ b/sdk/tests/present-plan.test.ts@@ -9,7 +9,7 @@*/import { describe, it, expect } from 'vitest';import {- planPresent, worldPerRenderedPixel, RenderResolution,+ planPresent, worldPerRenderedPixel, viewportPixels, RenderResolution,} from '../src/camera/presentPlan';/** The surfaces a 1080-tall design actually meets: a panel, a window, a big screen. */@@ -133,3 +133,38 @@ describe('planPresent — IntegerMultiple', () => {expect(worldPerRenderedPixel(plan, 2160)).toBe(1);});});++describe('viewportPixels', () => {+ it('a full-screen camera covers the whole surface', () => {+ expect(viewportPixels({ x: 0, y: 0, w: 1, h: 1 }, 1920, 1080))+ .toEqual({ x: 0, y: 0, w: 1920, h: 1080 });+ });++ it('FLIPS y — the rect is y-up from the bottom, pixels are y-down from the top', () => {+ // The trap, and the reason this lives in one place. A top-half camera is+ // y = 0.5 in the fraction and y = 0 in pixels; backwards puts every+ // pointer in the wrong half and nothing else complains.+ expect(viewportPixels({ x: 0, y: 0.5, w: 1, h: 0.5 }, 800, 600))+ .toEqual({ x: 0, y: 0, w: 800, h: 300 });+ expect(viewportPixels({ x: 0, y: 0, w: 1, h: 0.5 }, 800, 600))+ .toEqual({ x: 0, y: 300, w: 800, h: 300 });+ });++ it('split-screen quadrants tile the surface with no gap and no overlap', () => {+ const quads = [+ { x: 0, y: 0.5, w: 0.5, h: 0.5 }, { x: 0.5, y: 0.5, w: 0.5, h: 0.5 },+ { x: 0, y: 0, w: 0.5, h: 0.5 }, { x: 0.5, y: 0, w: 0.5, h: 0.5 },+ ].map((r) => viewportPixels(r, 1024, 768));+ const covered = quads.reduce((n, q) => n + q.w * q.h, 0);+ expect(covered).toBe(1024 * 768);+ expect(new Set(quads.map((q) => `${q.x},${q.y}`)).size).toBe(4);+ });++ it('measures against whatever surface it is given, which stays the caller choice', () => {+ // Input measures against the window, drawing against the render target.+ // Same rect, two answers, and both are right.+ const rect = { x: 0.25, y: 0.25, w: 0.5, h: 0.5 };+ expect(viewportPixels(rect, 1920, 1080)).toEqual({ x: 480, y: 270, w: 960, h: 540 });+ expect(viewportPixels(rect, 256, 256)).toEqual({ x: 64, y: 64, w: 128, h: 128 });+ });+});