Commit b0b6eae6

b0b6eae6db7bMergedon cedricvidal-66f663

cedricvidal · 2 months ago · 3 files · +2 added · ~1 modified

Details

Commit
b0b6eae6db7b
Parent
388f5e922594

src/cube.tsaddedView file

1+import * as THREE from 'three';
2+
3+export const CUBIE_SPACING = 1;
4+const CUBIE_SIZE = 0.95;
5+const PLASTIC_COLOR = 0x111111;
6+
7+// Face order matches BoxGeometry material groups: +x, -x, +y, -y, +z, -z
8+export const FACE_NORMALS: readonly THREE.Vector3[] = [
9+ new THREE.Vector3(1, 0, 0),
10+ new THREE.Vector3(-1, 0, 0),
11+ new THREE.Vector3(0, 1, 0),
12+ new THREE.Vector3(0, -1, 0),
13+ new THREE.Vector3(0, 0, 1),
14+ new THREE.Vector3(0, 0, -1),
15+];
16+
17+// Classic sticker colors: R, L, U, D, F, B
18+const FACE_COLORS = [0xb71234, 0xff5800, 0xffffff, 0xffd500, 0x009b48, 0x0046ad];
19+
20+export interface Cubie {
21+ mesh: THREE.Mesh;
22+ /** Sticker color index per face slot, or null for bare plastic. Fixed at creation. */
23+ stickers: (number | null)[];
24+}
25+
26+export function createCube(): { group: THREE.Group; cubies: Cubie[] } {
27+ const group = new THREE.Group();
28+ const cubies: Cubie[] = [];
29+ const geometry = new THREE.BoxGeometry(CUBIE_SIZE, CUBIE_SIZE, CUBIE_SIZE);
30+
31+ for (let x = -1; x <= 1; x++) {
32+ for (let y = -1; y <= 1; y++) {
33+ for (let z = -1; z <= 1; z++) {
34+ if (x === 0 && y === 0 && z === 0) continue;
35+
36+ const pos = new THREE.Vector3(x, y, z);
37+ const stickers: (number | null)[] = [];
38+ const materials = FACE_NORMALS.map((normal, i) => {
39+ const isExterior = pos.dot(normal) > 0.5;
40+ stickers.push(isExterior ? i : null);
41+ return new THREE.MeshStandardMaterial({
42+ color: isExterior ? FACE_COLORS[i] : PLASTIC_COLOR,
43+ roughness: 0.35,
44+ metalness: 0.05,
45+ });
46+ });
47+
48+ const mesh = new THREE.Mesh(geometry, materials);
49+ mesh.position.copy(pos).multiplyScalar(CUBIE_SPACING);
50+ group.add(mesh);
51+ cubies.push({ mesh, stickers });
52+ }
53+ }
54+ }
55+
56+ return { group, cubies };
57+}