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
feat(camera): CameraView.pointerToWorld / pointerRay take the pointer as Input reports it
yhhb339b4d68457
diff --git a/sdk/src/camera/Camera.ts b/sdk/src/camera/Camera.tsindex 0d61904ec..d0b113f7a 100644--- a/sdk/src/camera/Camera.ts+++ b/sdk/src/camera/Camera.ts@@ -3,9 +3,10 @@import type { App } from '../app/app';import { defineResource } from '../ecs/resource';import { UICameraInfo, type UICameraData } from '../ui/core/ui-camera-info';+import { platformDevicePixelRatio } from '../platform';import { CameraCommit } from './CameraCommit';import type { Vec3 } from '../types';-import { screenToWorld, projectWorldPoint, projectDirectionAt, nearPlaneSide, isProjectable, createInvVPCache, screenRay, type WorldRay } from '../ui/util/math';+import { pointerToScreen, screenToWorld, projectWorldPoint, projectDirectionAt, nearPlaneSide, isProjectable, createInvVPCache, screenRay, type WorldRay } from '../ui/util/math';/*** Per-App camera-space query API: screen<->world conversions, the world-space@@ -80,6 +81,10 @@ export class CameraViewAPI {/*** Where a screen point lands on the world plane at @p planeZ.*+ * @p screenX / @p screenY are FRAMEBUFFER pixels, y growing up from the+ * bottom edge. For the pointer use {@link pointerToWorld}, which takes+ * `input.mouseX` / `input.mouseY` as they come.+ ** A screen point is a ray. Orthographically its x/y do not vary along it, so* the plane is irrelevant and the default answers for everyone. Under a* perspective camera it decides the answer, and the right plane is the one@@ -94,6 +99,32 @@ export class CameraViewAPI {return screenToWorld(screenX, screenY, invVP, cam.vpX, cam.vpY, cam.vpW, cam.vpH, planeZ);}+ /**+ * Where the POINTER lands on the world plane at @p planeZ, taking the+ * coordinates {@link Input} reports (`input.mouseX` / `input.mouseY`, a+ * touch's x / y): CSS pixels, y down from the top.+ *+ * `screenToWorld` takes framebuffer pixels with y UP — what the renderer and+ * the editor viewport work in. Feeding it the pointer directly mirrors the+ * answer about the viewport's centre; this is the call a game wants.+ */+ pointerToWorld(pointerX: number, pointerY: number, planeZ = 0): { x: number; y: number } | null {+ const s = this.pointerScreen(pointerX, pointerY);+ return s ? this.screenToWorld(s.x, s.y, planeZ) : null;+ }++ /** The world ray under the pointer, in {@link Input}'s coordinates — see {@link pointerToWorld}. */+ pointerRay(pointerX: number, pointerY: number): WorldRay | null {+ const s = this.pointerScreen(pointerX, pointerY);+ return s ? this.screenRay(s.x, s.y) : null;+ }++ private pointerScreen(pointerX: number, pointerY: number): { x: number; y: number } | null {+ const surface = this.surface();+ if (!surface) return null;+ return pointerToScreen(pointerX, pointerY, platformDevicePixelRatio(), surface.screenH);+ }+/*** The world ray a screen point names — what `screenToWorld` intersects with* the z plane. Callers that drag along an arbitrary plane (an editor moving@@ -184,7 +215,8 @@ export class CameraViewAPI {}/**- * Per-App camera-query API resource, published by `corePlugin`. Read it as+ * Per-App camera-query API resource, published by `cameraPlugin` on every core+ * (web and native alike). Read it as* `app.getResource(CameraView)` to convert screen<->world etc. (Named* `CameraView` rather than `Camera` because `Camera` is the ECS component.)*/diff --git a/sdk/src/ui/input/interaction.ts b/sdk/src/ui/input/interaction.tsindex 1b28aa2a8..bae676b1c 100644--- a/sdk/src/ui/input/interaction.ts+++ b/sdk/src/ui/input/interaction.ts@@ -18,6 +18,7 @@ import { UICameraInfo } from '../core/ui-camera-info';import type { UICameraData } from '../core/ui-camera-info';import type { InteractableData } from './interactable';import { screenToUiWorld, screenToUiLayout, uiPointerRay, uiLayoutRay, uiHitTestWorld } from '../util/ui-pick';+import { pointerToScreen } from '../util/math';import { ScreenOverlay, type ScreenOverlayData } from '../core/screen-overlay';import { platformDevicePixelRatio } from '../../platform';import { ensureComponent, walkParentChain } from '../util/helpers';@@ -179,8 +180,7 @@ export class UIInteractionPlugin implements Plugin {const hit = (pointer: UiPointerSample): Entity | null => {if (!camera.valid && !overlay.active) return null;- const glX = pointer.x * dpr;- const glY = surfaceH - pointer.y * dpr;+ const { x: glX, y: glY } = pointerToScreen(pointer.x, pointer.y, dpr, surfaceH);// The pointer, once per domain. Neither is derived from the// other: each is the inverse of the projection that domain is// drawn with, which is what keeps a click where the pixel is.diff --git a/sdk/src/ui/util/math.ts b/sdk/src/ui/util/math.tsindex 47ffa7293..d9c071845 100644--- a/sdk/src/ui/util/math.ts+++ b/sdk/src/ui/util/math.ts@@ -54,6 +54,21 @@ export interface WorldRay {dir: Vec3;}+/**+ * A pointer position as {@link Input} reports it — CSS pixels, y growing DOWN+ * from the top edge — in the framebuffer pixels every projection here takes:+ * device pixels, y growing UP from the bottom edge.+ *+ * @details The one place the two spaces meet. Passing `input.mouseY` straight to+ * a screen-space query mirrors the answer about the viewport's centre,+ * which is easy to miss in a symmetric scene.+ */+export function pointerToScreen(+ pointerX: number, pointerY: number, dpr: number, surfaceH: number,+): { x: number; y: number } {+ return { x: pointerX * dpr, y: surfaceH - pointerY * dpr };+}+export function screenRay(screenX: number, screenY: number,inverseVP: Float32Array,