Skip to content

Commit e7b74ad

Browse files
fix(web): render a continuous folding inner display
1 parent d0d7e29 commit e7b74ad

2 files changed

Lines changed: 62 additions & 19 deletions

File tree

‎packages/client-runtime/src/device/androidFoldScene.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ describe("Android fold scene", () => {
2121
const creaseWidth =
2222
rightScreen.geometry.boundingBox!.min.x - leftScreen.geometry.boundingBox!.max.x;
2323
expect(creaseWidth).toBeLessThan(0.01);
24+
const continuousScreen = scene.root.getObjectByName("continuous-inner-screen") as Mesh;
25+
const positions = continuousScreen.geometry.getAttribute("position");
26+
expect(continuousScreen.geometry.index).not.toBeNull();
27+
expect(Array.from({ length: positions.count }, (_, i) => positions.getX(i))).toContain(0);
2428
scene.setAngle(90);
2529
expect(moving.rotation.y).toBeCloseTo(Math.PI / 2);
2630
expect(fixed.rotation.y).toBe(0);

‎packages/client-runtime/src/device/androidFoldScene.ts‎

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
Mesh,
66
MeshBasicMaterial,
77
MeshStandardMaterial,
8+
PlaneGeometry,
89
Raycaster,
910
Shape,
1011
ShapeGeometry,
@@ -80,8 +81,9 @@ export function createAndroidFoldScene(
8081
const shell = new MeshStandardMaterial({ color: 0x48545b, metalness: 0.65, roughness: 0.38 });
8182
const bezel = new MeshStandardMaterial({ color: 0x14191c, metalness: 0.22, roughness: 0.5 });
8283
const displayMaterial = new MeshBasicMaterial({ map: texture, toneMapped: false });
84+
const hitMaterial = new MeshBasicMaterial({ colorWrite: false, depthWrite: false });
8385
const coverMaterial = new MeshBasicMaterial({ map: texture, toneMapped: false });
84-
const materials = [shell, bezel, displayMaterial, coverMaterial];
86+
const materials = [shell, bezel, displayMaterial, hitMaterial, coverMaterial];
8587

8688
function half(group: Group, side: "left" | "right") {
8789
const body = new Mesh(
@@ -98,7 +100,8 @@ export function createAndroidFoldScene(
98100
frame.position.z = DEPTH / 2 + 0.001;
99101
group.add(frame);
100102
const geometry = new ShapeGeometry(panelPath(side, INSET, 0.076));
101-
const display = new Mesh(geometry, displayMaterial);
103+
geometry.computeBoundingBox();
104+
const display = new Mesh(geometry, hitMaterial);
102105
display.name = `${side}-inner-screen`;
103106
display.position.z = DEPTH / 2 + 0.003;
104107
group.add(display);
@@ -107,6 +110,29 @@ export function createAndroidFoldScene(
107110

108111
const innerLeft = half(left, "left");
109112
const innerRight = half(right, "right");
113+
// One indexed surface keeps adjacent pixels joined at the crease. The
114+
// physical halves move separately underneath it.
115+
const screenWidth = 2 * (HALF_WIDTH - INSET);
116+
const screenHeight = HEIGHT - 2 * INSET;
117+
const screenGeometry = new PlaneGeometry(screenWidth, screenHeight, 40, 48);
118+
const screenPositions = screenGeometry.getAttribute("position");
119+
const screenUvs = screenGeometry.getAttribute("uv");
120+
const baseX = new Float32Array(screenPositions.count);
121+
for (let i = 0; i < screenPositions.count; i++) {
122+
const y = screenPositions.getY(i);
123+
const outerX = screenWidth / 2;
124+
const outerY = screenHeight / 2;
125+
const radius = 0.076;
126+
const cornerY = Math.max(0, Math.abs(y) - (outerY - radius));
127+
const limit = outerX - radius + Math.sqrt(Math.max(0, radius * radius - cornerY * cornerY));
128+
const x = Math.max(-limit, Math.min(limit, screenPositions.getX(i)));
129+
baseX[i] = x;
130+
screenUvs.setXY(i, x / screenWidth + 0.5, y / screenHeight + 0.5);
131+
}
132+
screenUvs.needsUpdate = true;
133+
const innerSurface = new Mesh(screenGeometry, displayMaterial);
134+
innerSurface.name = "continuous-inner-screen";
135+
orientation.add(innerSurface);
110136
const hinge = new Mesh(new CylinderGeometry(0.016, 0.016, HEIGHT - 0.05, 16), shell);
111137
hinge.position.z = -DEPTH / 2 - 0.01;
112138
orientation.add(hinge);
@@ -124,6 +150,7 @@ export function createAndroidFoldScene(
124150
let angle = initialAngle;
125151
const updateVisibleScreen = () => {
126152
const innerActive = angle >= 90;
153+
innerSurface.visible = innerActive;
127154
innerLeft.visible = innerActive;
128155
innerRight.visible = innerActive;
129156
cover.visible = !innerActive;
@@ -133,30 +160,42 @@ export function createAndroidFoldScene(
133160
left.rotation.y = Math.PI * (1 - angle / 180);
134161
// Keep the cover slightly in front of the fixed half when closed.
135162
left.position.z = 0.01 * (1 - angle / 180);
163+
const radians = left.rotation.y;
164+
const cosine = Math.cos(radians);
165+
const sine = Math.sin(radians);
166+
const frontZ = DEPTH / 2 + 0.004;
167+
for (let i = 0; i < screenPositions.count; i++) {
168+
const x = baseX[i]!;
169+
if (x < 0) {
170+
screenPositions.setXYZ(
171+
i,
172+
x * cosine + frontZ * sine,
173+
screenPositions.getY(i),
174+
-x * sine + frontZ * cosine + left.position.z,
175+
);
176+
} else {
177+
screenPositions.setXYZ(i, x, screenPositions.getY(i), frontZ);
178+
}
179+
}
180+
screenPositions.needsUpdate = true;
181+
screenGeometry.computeBoundingBox();
182+
screenGeometry.computeBoundingSphere();
136183
updateVisibleScreen();
137184
};
138185
const setDisplay = (nextTexture: Texture, nextLayout: PhoneDisplayLayout) => {
139186
activeLayout = nextLayout;
140187
displayMaterial.map = nextTexture;
141188
coverMaterial.map = nextTexture;
142-
// The raw Android framebuffer is landscape when the inner display is active.
143-
// Each inner half samples its own half of that frame; the cover samples all of it.
144-
for (const [mesh, start, end] of [
145-
[innerLeft, 0, 0.5],
146-
[innerRight, 0.5, 1],
147-
[cover, 0, 1],
148-
] as const) {
149-
mesh.geometry.computeBoundingBox();
150-
const bounds = mesh.geometry.boundingBox!;
151-
const uv = mesh.geometry.getAttribute("uv");
152-
for (let i = 0; i < uv.count; i++) {
153-
const position = mesh.geometry.getAttribute("position");
154-
const u = (position.getX(i) - bounds.min.x) / (bounds.max.x - bounds.min.x);
155-
const v = (position.getY(i) - bounds.min.y) / (bounds.max.y - bounds.min.y);
156-
uv.setXY(i, start + u * (end - start), v);
157-
}
158-
uv.needsUpdate = true;
189+
cover.geometry.computeBoundingBox();
190+
const bounds = cover.geometry.boundingBox!;
191+
const uv = cover.geometry.getAttribute("uv");
192+
const position = cover.geometry.getAttribute("position");
193+
for (let i = 0; i < uv.count; i++) {
194+
const u = (position.getX(i) - bounds.min.x) / (bounds.max.x - bounds.min.x);
195+
const v = (position.getY(i) - bounds.min.y) / (bounds.max.y - bounds.min.y);
196+
uv.setXY(i, u, v);
159197
}
198+
uv.needsUpdate = true;
160199
updateVisibleScreen();
161200
};
162201
setAngle(initialAngle);

0 commit comments

Comments
 (0)