TypeScript
fix(web): size the Android fold model from the inner display (#13574)
Julius Marminge7b84431161a6
diff --git a/packages/client-runtime/src/device/androidFoldScene.test.ts b/packages/client-runtime/src/device/androidFoldScene.test.tsindex bb677c4818e..e1e237a9f13 100644--- a/packages/client-runtime/src/device/androidFoldScene.test.ts+++ b/packages/client-runtime/src/device/androidFoldScene.test.ts@@ -54,4 +54,25 @@ describe("Android fold scene", () => {scene.dispose();texture.dispose();});++ it("shapes the inner display to the raw frame, portrait or landscape", () => {+ const texture = new Texture();+ for (const [width, height] of [+ [2076, 2152],+ [2208, 1840],+ ] as const) {+ const scene = createAndroidFoldScene(+ texture,+ phoneDisplayLayout(null, width, height),+ 180,+ width / height,+ );+ const screen = scene.root.getObjectByName("continuous-inner-screen") as Mesh;+ screen.geometry.computeBoundingBox();+ const size = screen.geometry.boundingBox!.getSize(new Vector3());+ expect(size.x / size.y).toBeCloseTo(width / height, 2);+ scene.dispose();+ }+ texture.dispose();+ });});diff --git a/packages/client-runtime/src/device/androidFoldScene.ts b/packages/client-runtime/src/device/androidFoldScene.tsindex 3c30ecabc55..b2c1f84fe03 100644--- a/packages/client-runtime/src/device/androidFoldScene.ts+++ b/packages/client-runtime/src/device/androidFoldScene.ts@@ -19,7 +19,6 @@ import {} from "three";import type { PhoneDisplayLayout } from "./phoneScene.ts";-const HALF_WIDTH = 1.04;const HEIGHT = 2.2;const DEPTH = 0.075;const INSET = 0.026;@@ -28,10 +27,24 @@ const BEVEL = 0.008;// The hinge axis sits just above the inner screens so closed halves meet face to face.const PIVOT_Z = DEPTH / 2 + 0.005;const SPINE_RADIUS = PIVOT_Z + DEPTH / 2 - 0.002;+/** Width over height of the unfolded inner display until a live frame reports its own. */+export const DEFAULT_FOLD_INNER_ASPECT = 2076 / 2152;+/**+ * Unfolded inner displays are near square in either orientation. Cover displays are+ * phone shaped (about 0.4-0.5, or 2-2.6 when rotated), so they never retune the body.+ */+export const isFoldInnerAspect = (aspect: number) =>+ Number.isFinite(aspect) && aspect > 0.75 && aspect < 1.5;-function panelPath(side: "left" | "right", inset: number, radius: number, hingeInset = 0) {- const left = side === "left" ? -HALF_WIDTH + inset : CREASE / 2 + hingeInset;- const right = side === "left" ? -CREASE / 2 - hingeInset : HALF_WIDTH - inset;+function panelPath(+ halfWidth: number,+ side: "left" | "right",+ inset: number,+ radius: number,+ hingeInset = 0,+) {+ const left = side === "left" ? -halfWidth + inset : CREASE / 2 + hingeInset;+ const right = side === "left" ? -CREASE / 2 - hingeInset : halfWidth - inset;const bottom = -HEIGHT / 2 + inset;const top = HEIGHT / 2 - inset;const path = new Shape();@@ -70,16 +83,22 @@ function roundedRectPath(width: number, height: number, radius: number) {return path;}-function coverPath() {- return roundedRectPath(HALF_WIDTH - INSET * 2 - 0.02, HEIGHT - INSET * 2 - 0.04, 0.07);+function coverPath(halfWidth: number) {+ return roundedRectPath(halfWidth - INSET * 2 - 0.02, HEIGHT - INSET * 2 - 0.04, 0.07);}-/** A procedural book-style foldable: one fixed half, one half rotating around a shared hinge. */+/**+ * A procedural book-style foldable: one fixed half, one half rotating around a shared hinge.+ * The inner display keeps the raw framebuffer's native aspect, portrait or landscape.+ */export function createAndroidFoldScene(texture: Texture,layout: PhoneDisplayLayout,initialAngle: number,+ innerAspect = DEFAULT_FOLD_INNER_ASPECT,) {+ const screenHeight = HEIGHT - 2 * INSET;+ const halfWidth = (innerAspect * screenHeight) / 2 + INSET;const root = new Group();const orientation = new Group();root.add(orientation);@@ -146,7 +165,7 @@ export function createAndroidFoldScene(function half(group: Group, side: "left" | "right", back: MeshPhysicalMaterial) {const body = new Mesh(- new ExtrudeGeometry(panelPath(side, BEVEL, 0.1, BEVEL), {+ new ExtrudeGeometry(panelPath(halfWidth, side, BEVEL, 0.1, BEVEL), {depth: DEPTH - BEVEL * 2,bevelEnabled: true,bevelSize: BEVEL,@@ -158,19 +177,25 @@ export function createAndroidFoldScene();body.position.z = -DEPTH / 2 + BEVEL;group.add(body);- const frame = new Mesh(new ShapeGeometry(panelPath(side, 0.01, 0.095, 0.002), 12), bezel);+ const frame = new Mesh(+ new ShapeGeometry(panelPath(halfWidth, side, 0.01, 0.095, 0.002), 12),+ bezel,+ );frame.position.z = DEPTH / 2 + 0.001;group.add(frame);// A back-facing shape mirrors X, so it is drawn from the opposite side's outline.const rear = new Mesh(- new ShapeGeometry(panelPath(side === "left" ? "right" : "left", 0.01, 0.095, 0.002), 12),+ new ShapeGeometry(+ panelPath(halfWidth, side === "left" ? "right" : "left", 0.01, 0.095, 0.002),+ 12,+ ),back,);rear.name = `${side}-back`;rear.rotation.y = Math.PI;rear.position.set(0, 0, -DEPTH / 2 - 0.001);group.add(rear);- const geometry = new ShapeGeometry(panelPath(side, INSET, 0.076));+ const geometry = new ShapeGeometry(panelPath(halfWidth, side, INSET, 0.076));geometry.computeBoundingBox();const display = new Mesh(geometry, hitMaterial);display.name = `${side}-inner-screen`;@@ -183,8 +208,7 @@ export function createAndroidFoldScene(const innerRight = half(right, "right", backGlass);// One indexed surface keeps adjacent pixels joined at the crease. The// physical halves move separately underneath it.- const screenWidth = 2 * (HALF_WIDTH - INSET);- const screenHeight = HEIGHT - 2 * INSET;+ const screenWidth = 2 * (halfWidth - INSET);const screenGeometry = new PlaneGeometry(screenWidth, screenHeight, 40, 48);const screenPositions = screenGeometry.getAttribute("position");const screenUvs = screenGeometry.getAttribute("uv");@@ -224,9 +248,9 @@ export function createAndroidFoldScene(spine.position.z = PIVOT_Z;orientation.add(spine);- const cover = new Mesh(new ShapeGeometry(coverPath()), coverMaterial);+ const cover = new Mesh(new ShapeGeometry(coverPath(halfWidth)), coverMaterial);cover.name = "cover-screen";- cover.position.set(-HALF_WIDTH / 2, 0, -DEPTH / 2 - 0.003);+ cover.position.set(-halfWidth / 2, 0, -DEPTH / 2 - 0.003);cover.rotation.y = Math.PI;leftBody.add(cover);@@ -236,7 +260,7 @@ export function createAndroidFoldScene(const islandWidth = 0.46;const islandHeight = 0.2;rearCamera.position.set(- HALF_WIDTH - 0.07 - islandWidth / 2,+ halfWidth - 0.07 - islandWidth / 2,HEIGHT / 2 - 0.08 - islandHeight / 2,-DEPTH / 2 - 0.002,);@@ -284,7 +308,7 @@ export function createAndroidFoldScene([0.2, 0.3],] as const) {const key = new Mesh(new BoxGeometry(0.02, length, DEPTH * 0.45), frameMetal);- key.position.set(HALF_WIDTH + 0.008, y, 0);+ key.position.set(halfWidth + 0.008, y, 0);right.add(key);}@@ -349,8 +373,9 @@ export function createAndroidFoldScene(return {root,orientation,- width: HALF_WIDTH * 2,+ width: halfWidth * 2,height: HEIGHT,+ innerAspect,setAngle,setDisplay,screenPoint(x: number, y: number, camera: Camera, captured = false) {diff --git a/packages/client-runtime/src/device/phoneViewer.test.ts b/packages/client-runtime/src/device/phoneViewer.test.tsindex 61baa8293f1..d40ddc72b39 100644--- a/packages/client-runtime/src/device/phoneViewer.test.ts+++ b/packages/client-runtime/src/device/phoneViewer.test.ts@@ -80,7 +80,16 @@ vi.mock("./modelScene.ts", async () => {};});-import { BoxGeometry, Group, Mesh, MeshBasicMaterial, PlaneGeometry, Quaternion } from "three";+import {+ Box3,+ BoxGeometry,+ Group,+ Mesh,+ MeshBasicMaterial,+ PlaneGeometry,+ Quaternion,+ Vector3,+} from "three";import { disposeDeviceModel } from "./modelScene.ts";import { createPhoneViewer } from "./phoneViewer.ts";import {@@ -259,6 +268,53 @@ it("animates the Android hinge on the same scene through an encoder resize", ()viewer.dispose();});+it("resizes the fold body for a landscape inner display and keeps it through the cover frame", () => {+ const openProfile = resolveDeviceShape({ platform: "android", portraitAspect: 0.83 });+ const { viewer, draw, source, state } = fixture(openProfile);+ viewer.setFoldAngle(180);+ draw(0);+ const portraitWidth = new Box3()+ .setFromObject(state.frames.at(-1)!.phone!)+ .getSize(new Vector3()).x;+ source.width = 2208;+ source.height = 1840;+ viewer.setScreen({ width: 2208, height: 1840, orientation: "portrait" });+ viewer.frameUpdated();+ draw(10);+ const landscape = state.frames.at(-1)!.phone!;+ const landscapeWidth = new Box3().setFromObject(landscape).getSize(new Vector3()).x;+ expect(landscapeWidth / portraitWidth).toBeGreaterThan(1.15);+ source.width = 1080;+ source.height = 2092;+ viewer.setScreen({ width: 1080, height: 2092, orientation: "portrait" }, ANDROID_PHONE_SHAPE);+ viewer.frameUpdated();+ draw(20);+ expect(state.frames.at(-1)!.phone).toBe(landscape);+ expect(gpu.instances).toHaveLength(1);+ viewer.dispose();+});++it("keeps the fold body through a rotated cover frame and learns the inner shape before fold mode", () => {+ const { viewer, draw, source, state } = fixture(ANDROID_PHONE_SHAPE);+ source.width = 2208;+ source.height = 1840;+ viewer.setScreen({ width: 2208, height: 1840, orientation: "portrait" });+ viewer.frameUpdated();+ draw(0);+ viewer.setFoldAngle(180);+ draw(10);+ const landscape = state.frames.at(-1)!.phone!;+ const width = new Box3().setFromObject(landscape).getSize(new Vector3()).x;+ expect(width / new Box3().setFromObject(landscape).getSize(new Vector3()).y).toBeGreaterThan(1.1);+ source.width = 2092;+ source.height = 1080;+ viewer.setScreen({ width: 2092, height: 1080, orientation: "landscape_left" });+ viewer.frameUpdated();+ draw(20);+ expect(state.frames.at(-1)!.phone).toBe(landscape);+ viewer.dispose();+});+it("retargets an unfinished hinge turn from its visible angle", () => {const { viewer, draw, state } = fixture(ANDROID_PHONE_SHAPE);viewer.setFoldAngle(180);diff --git a/packages/client-runtime/src/device/phoneViewer.ts b/packages/client-runtime/src/device/phoneViewer.tsindex 00990744243..3dfc25048e0 100644--- a/packages/client-runtime/src/device/phoneViewer.ts+++ b/packages/client-runtime/src/device/phoneViewer.ts@@ -19,7 +19,11 @@ import {} from "./model.ts";import { createImportedPhoneScene, loadDeviceModel } from "./modelScene.ts";import { createPhoneScene, phoneDisplayLayout } from "./phoneScene.ts";-import { createAndroidFoldScene } from "./androidFoldScene.ts";+import {+ createAndroidFoldScene,+ DEFAULT_FOLD_INNER_ASPECT,+ isFoldInnerAspect,+} from "./androidFoldScene.ts";import { createRenderScheduler } from "./renderScheduler.ts";import { createDeviceMotion } from "./deviceMotion.ts";import { createDeviceFraming } from "./deviceFraming.ts";@@ -102,9 +106,21 @@ export function createPhoneViewer(options: {let accessory: Awaited<ReturnType<typeof loadDeviceModel>> | null = null;let accessoryBounds: Box3 | null = null;let foldTurn: { from: number; to: number; startedAt: number } | null = null;+ // The inner display's raw width over height. Cover frames leave the last unfolded shape.+ const rawAspect = () => options.source.width / options.source.height;+ let foldAspect = isFoldInnerAspect(rawAspect()) ? rawAspect() : DEFAULT_FOLD_INNER_ASPECT;+ const createFoldScene = (angle: number, displayLayout = layout) =>+ createAndroidFoldScene(texture, displayLayout, angle, foldAspect);+ /** The hinge angle currently on screen, including an unfinished turn. */+ const visibleFoldAngle = (fallback: number) => {+ if (!foldTurn) return fallback;+ const progress = Math.min(1, (performance.now() - foldTurn.startedAt) / ANDROID_FOLD_TURN_MS);+ const eased = progress * progress * (3 - 2 * progress);+ return foldTurn.from + (foldTurn.to - foldTurn.from) * eased;+ };let phone: ReturnType<typeof createPhoneScene> | ReturnType<typeof createAndroidFoldScene> =foldAngle !== null && profile.id.startsWith("android")- ? createAndroidFoldScene(texture, layout, foldAngle)+ ? createFoldScene(foldAngle): createPhoneScene(texture, layout, profile);scene.add(phone.root);let disposed = false;@@ -222,7 +238,18 @@ export function createPhoneViewer(options: {phone.setDisplay(texture, next);previous.dispose();}- if (+ // Learn the inner display shape from any unfolded frame, including before fold mode.+ const frameAspect = rawAspect();+ const innerChanged = isFoldInnerAspect(frameAspect) && frameAspect !== foldAspect;+ if (innerChanged) foldAspect = frameAspect;+ if (!imported && "setAngle" in phone && innerChanged) {+ // A new inner display shape resizes the body; the hinge keeps its visible angle.+ const angle = visibleFoldAngle(foldAngle ?? 180);+ scene.remove(phone.root);+ phone.dispose();+ phone = createFoldScene(angle, next);+ scene.add(phone.root);+ } else if (!imported &&!("setAngle" in phone) &&(nextProfile !== profile || next.aspect !== layout.aspect)@@ -267,7 +294,7 @@ export function createPhoneViewer(options: {: model? createImportedPhoneScene(model.asset, texture, layout): foldAngle !== null && profile.id.startsWith("android")- ? createAndroidFoldScene(texture, layout, foldAngle)+ ? createFoldScene(foldAngle): createPhoneScene(texture, layout, profile);foldTurn = null;scene.remove(phone.root);@@ -333,10 +360,7 @@ export function createPhoneViewer(options: {if (next === null || !("setAngle" in phone)) {scene.remove(phone.root);phone.dispose();- phone =- next === null- ? createPhoneScene(texture, layout, profile)- : createAndroidFoldScene(texture, layout, next);+ phone = next === null ? createPhoneScene(texture, layout, profile) : createFoldScene(next);scene.add(phone.root);orientationTurn = null;orientationAngle = next === null ? layout.rotation : 0;@@ -344,13 +368,7 @@ export function createPhoneViewer(options: {applyPose();fit(true);} else {- const progress = foldTurn- ? Math.min(1, (performance.now() - foldTurn.startedAt) / ANDROID_FOLD_TURN_MS)- : 1;- const eased = progress * progress * (3 - 2 * progress);- const from = foldTurn- ? foldTurn.from + (foldTurn.to - foldTurn.from) * eased- : (previous ?? next);+ const from = visibleFoldAngle(previous ?? next);if (reducedMotion()) {foldTurn = null;phone.setAngle(next);