Commit 0b293821

0b29382152e3Mergedon cedricvidal-66f663

cedricvidal · 2 months ago · 4 files · ~4 modified

Details

Commit
0b29382152e3
Parent
81ff5eb2478e

src/cube.tsmodifiedView file

@@ -1,10 +1,14 @@
11 import * as THREE from 'three';
2+import { RoundedBoxGeometry } from 'three/addons/geometries/RoundedBoxGeometry.js';
23
34 export const CUBIE_SPACING = 1;
45 const CUBIE_SIZE = 0.95;
5-const PLASTIC_COLOR = 0x111111;
6+const CORNER_RADIUS = 0.09;
7+const STICKER_SIZE = 0.78;
8+const STICKER_RADIUS = 0.1;
9+const STICKER_OFFSET = CUBIE_SIZE / 2 + 0.005;
610
7-// Face order matches BoxGeometry material groups: +x, -x, +y, -y, +z, -z
11+// Face order: +x, -x, +y, -y, +z, -z (R, L, U, D, F, B)
812 export const FACE_NORMALS: readonly THREE.Vector3[] = [
913 new THREE.Vector3(1, 0, 0),
1014 new THREE.Vector3(-1, 0, 0),
@@ -14,8 +18,7 @@
1418 new THREE.Vector3(0, 0, -1),
1519 ];
1620
17-// Classic sticker colors: R, L, U, D, F, B
18-const FACE_COLORS = [0xb71234, 0xff5800, 0xffffff, 0xffd500, 0x009b48, 0x0046ad];
21+const FACE_COLORS = [0xff2244, 0xff7b1c, 0xf4f6ff, 0xffd21f, 0x21e06a, 0x2079ff];
1922
2023 export interface Cubie {
2124 mesh: THREE.Mesh;
@@ -23,30 +26,67 @@
2326 stickers: (number | null)[];
2427 }
2528
29+function roundedRectGeometry(size: number, radius: number): THREE.ShapeGeometry {
30+ const h = size / 2;
31+ const shape = new THREE.Shape();
32+ shape.moveTo(-h + radius, -h);
33+ shape.lineTo(h - radius, -h);
34+ shape.quadraticCurveTo(h, -h, h, -h + radius);
35+ shape.lineTo(h, h - radius);
36+ shape.quadraticCurveTo(h, h, h - radius, h);
37+ shape.lineTo(-h + radius, h);
38+ shape.quadraticCurveTo(-h, h, -h, h - radius);
39+ shape.lineTo(-h, -h + radius);
40+ shape.quadraticCurveTo(-h, -h, -h + radius, -h);
41+ return new THREE.ShapeGeometry(shape, 8);
42+}
43+
2644 export function createCube(): { group: THREE.Group; cubies: Cubie[] } {
2745 const group = new THREE.Group();
2846 const cubies: Cubie[] = [];
29- const geometry = new THREE.BoxGeometry(CUBIE_SIZE, CUBIE_SIZE, CUBIE_SIZE);
3047
48+ const bodyGeometry = new RoundedBoxGeometry(CUBIE_SIZE, CUBIE_SIZE, CUBIE_SIZE, 4, CORNER_RADIUS);
49+ const bodyMaterial = new THREE.MeshStandardMaterial({
50+ color: 0x15161c,
51+ roughness: 0.25,
52+ metalness: 0.55,
53+ });
54+ const stickerGeometry = roundedRectGeometry(STICKER_SIZE, STICKER_RADIUS);
55+ const stickerMaterials = FACE_COLORS.map((color, i) => {
56+ // White has far higher luminance than the hues; give it a gentler
57+ // emissive push or it blows out under bloom.
58+ const isWhite = i === 2;
59+ return new THREE.MeshStandardMaterial({
60+ color,
61+ emissive: color,
62+ emissiveIntensity: isWhite ? 0.18 : 0.5,
63+ roughness: 0.3,
64+ metalness: 0,
65+ side: THREE.DoubleSide,
66+ });
67+ });
68+ const zAxis = new THREE.Vector3(0, 0, 1);
69+
3170 for (let x = -1; x <= 1; x++) {
3271 for (let y = -1; y <= 1; y++) {
3372 for (let z = -1; z <= 1; z++) {
3473 if (x === 0 && y === 0 && z === 0) continue;
3574
3675 const pos = new THREE.Vector3(x, y, z);
76+ const mesh = new THREE.Mesh(bodyGeometry, bodyMaterial);
77+ mesh.position.copy(pos).multiplyScalar(CUBIE_SPACING);
78+
3779 const stickers: (number | null)[] = [];
38- const materials = FACE_NORMALS.map((normal, i) => {
80+ FACE_NORMALS.forEach((normal, i) => {
3981 const isExterior = pos.dot(normal) > 0.5;
4082 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- });
83+ if (!isExterior) return;
84+ const sticker = new THREE.Mesh(stickerGeometry, stickerMaterials[i]);
85+ sticker.quaternion.setFromUnitVectors(zAxis, normal);
86+ sticker.position.copy(normal).multiplyScalar(STICKER_OFFSET);
87+ mesh.add(sticker);
4688 });
4789
48- const mesh = new THREE.Mesh(geometry, materials);
49- mesh.position.copy(pos).multiplyScalar(CUBIE_SPACING);
5090 group.add(mesh);
5191 cubies.push({ mesh, stickers });
5292 }