Skip to content

Commit 68aaf83

Browse files
fix(client-runtime): attach rear cameras to the device body
1 parent 432ce79 commit 68aaf83

2 files changed

Lines changed: 41 additions & 12 deletions

File tree

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PerspectiveCamera, Texture, Vector3 } from "three";
1+
import { Box3, PerspectiveCamera, Texture, Vector3 } from "three";
22
import { describe, expect, it } from "vite-plus/test";
33
import { createPhoneScene, phoneDisplayLayout } from "./phoneScene.ts";
44

@@ -86,3 +86,24 @@ describe("3D phone input", () => {
8686
},
8787
);
8888
});
89+
90+
it("attaches the rear camera plate to the back and seats its lenses on the housing", () => {
91+
const texture = new Texture();
92+
const phone = createPhoneScene(texture, phoneDisplayLayout(null, 900, 1950));
93+
phone.root.updateMatrixWorld(true);
94+
const back = new Box3().setFromObject(phone.root.getObjectByName("device-back")!);
95+
const plate = new Box3().setFromObject(phone.root.getObjectByName("camera-plate")!);
96+
expect(plate.min.z).toBeLessThan(back.min.z);
97+
expect(plate.max.z).toBeGreaterThanOrEqual(back.min.z);
98+
const assembly = phone.root.getObjectByName("rear-camera")!;
99+
const rings = assembly.children.filter((part) => part.name === "camera-ring");
100+
const lenses = assembly.children.filter((part) => part.name === "camera-lens");
101+
for (const [index, ring] of rings.entries()) {
102+
const ringBounds = new Box3().setFromObject(ring);
103+
expect(ringBounds.intersectsBox(plate)).toBe(true);
104+
const lensBounds = new Box3().setFromObject(lenses[index]!);
105+
expect(Math.abs(lensBounds.max.z - ringBounds.min.z)).toBeLessThan(0.001);
106+
}
107+
phone.dispose();
108+
texture.dispose();
109+
});

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ export function createPhoneScene(texture: Texture, layout: ReturnType<typeof pho
116116
new ShapeGeometry(roundedPath(width - 0.012, height - 0.012, 0.14), 16),
117117
backMaterial,
118118
);
119+
back.name = "device-back";
119120
back.rotation.y = Math.PI;
120121
back.position.z = -0.075;
121122
orientation.add(back);
@@ -147,6 +148,14 @@ export function createPhoneScene(texture: Texture, layout: ReturnType<typeof pho
147148
button.position.set(x!, y!, -0.01);
148149
orientation.add(button);
149150
}
151+
// Rear components share a surface-relative coordinate system, with outward positive Z.
152+
const rearCamera = new Group();
153+
rearCamera.name = "rear-camera";
154+
rearCamera.position.set(width / 2 - 0.25, height / 2 - 0.29, back.position.z + 0.001);
155+
rearCamera.rotation.y = Math.PI;
156+
orientation.add(rearCamera);
157+
const plateFront = 0.025 + 0.007;
158+
const ringDepth = 0.025;
150159
const cameraPlate = new Mesh(
151160
new ExtrudeGeometry(roundedPath(0.39, 0.44, 0.095), {
152161
depth: 0.025,
@@ -157,26 +166,25 @@ export function createPhoneScene(texture: Texture, layout: ReturnType<typeof pho
157166
}),
158167
backMaterial,
159168
);
160-
cameraPlate.position.set(-width / 2 + 0.25, height / 2 - 0.29, -0.1);
161-
cameraPlate.rotation.y = Math.PI;
162-
orientation.add(cameraPlate);
169+
cameraPlate.name = "camera-plate";
170+
rearCamera.add(cameraPlate);
163171
for (const [x, y] of [
164172
[-0.08, 0.095],
165173
[0.08, -0.095],
166174
]) {
167175
const ring = new Mesh(new CylinderGeometry(0.082, 0.082, 0.025, 32), metal);
168176
ring.rotation.x = Math.PI / 2;
169-
ring.position.set(cameraPlate.position.x + x!, cameraPlate.position.y + y!, -0.143);
170-
orientation.add(ring);
177+
ring.name = "camera-ring";
178+
ring.position.set(x!, y!, plateFront + ringDepth / 2 - 0.003);
179+
rearCamera.add(ring);
171180
const lens = new Mesh(new CircleGeometry(0.068, 32), lensMaterial);
172-
lens.rotation.y = Math.PI;
173-
lens.position.set(ring.position.x, ring.position.y, -0.158);
174-
orientation.add(lens);
181+
lens.name = "camera-lens";
182+
lens.position.set(x!, y!, ring.position.z + ringDepth / 2 + 0.0005);
183+
rearCamera.add(lens);
175184
}
176185
const flash = new Mesh(new CircleGeometry(0.022, 20), new MeshBasicMaterial({ color: 0xf2ead6 }));
177-
flash.position.set(cameraPlate.position.x + 0.085, cameraPlate.position.y + 0.11, -0.139);
178-
flash.rotation.y = Math.PI;
179-
orientation.add(flash);
186+
flash.position.set(0.085, 0.11, plateFront + 0.0005);
187+
rearCamera.add(flash);
180188

181189
const raycaster = new Raycaster();
182190
const pointer = new Vector2();

0 commit comments

Comments
 (0)