Spaces:
Sleeping
Sleeping
update cube-piece
Browse files- src/components/canvas.tsx +1 -1
- src/components/cube-piece.tsx +72 -2
src/components/canvas.tsx
CHANGED
|
@@ -13,7 +13,7 @@ export const Canvas = () => {
|
|
| 13 |
|
| 14 |
return (
|
| 15 |
<ThreeCanvas shadows camera={{ position: [0, 0, 4.5], fov: 50 }}>
|
| 16 |
-
<CubePiece roughness={roughness} />
|
| 17 |
<Env />
|
| 18 |
</ThreeCanvas>
|
| 19 |
);
|
|
|
|
| 13 |
|
| 14 |
return (
|
| 15 |
<ThreeCanvas shadows camera={{ position: [0, 0, 4.5], fov: 50 }}>
|
| 16 |
+
<CubePiece roughness={roughness} position={[0, 0, 1]} />
|
| 17 |
<Env />
|
| 18 |
</ThreeCanvas>
|
| 19 |
);
|
src/components/cube-piece.tsx
CHANGED
|
@@ -1,12 +1,34 @@
|
|
| 1 |
import { RoundedBox } from "@react-three/drei";
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
type CubePieceProps = {
|
| 4 |
roughness: number;
|
|
|
|
| 5 |
};
|
| 6 |
|
| 7 |
-
export const CubePiece = ({ roughness }: CubePieceProps) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
return (
|
| 9 |
-
<mesh>
|
| 10 |
<RoundedBox args={[0.95, 0.95, 0.95]} radius={0.05} smoothness={4}>
|
| 11 |
<meshStandardMaterial
|
| 12 |
color="#2a2a2a"
|
|
@@ -14,6 +36,54 @@ export const CubePiece = ({ roughness }: CubePieceProps) => {
|
|
| 14 |
roughness={roughness}
|
| 15 |
/>
|
| 16 |
</RoundedBox>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
</mesh>
|
| 18 |
);
|
| 19 |
};
|
|
|
|
| 1 |
import { RoundedBox } from "@react-three/drei";
|
| 2 |
|
| 3 |
+
// Standard Rubik's cube colors
|
| 4 |
+
const CUBE_COLORS = {
|
| 5 |
+
front: "#ff4444", // Red
|
| 6 |
+
back: "#ff8800", // Orange
|
| 7 |
+
left: "#44ff44", // Green
|
| 8 |
+
right: "#4444ff", // Blue
|
| 9 |
+
top: "#ffff44", // Yellow
|
| 10 |
+
bottom: "#ffffff", // White
|
| 11 |
+
};
|
| 12 |
+
|
| 13 |
type CubePieceProps = {
|
| 14 |
roughness: number;
|
| 15 |
+
position: [number, number, number];
|
| 16 |
};
|
| 17 |
|
| 18 |
+
export const CubePiece = ({ roughness, position }: CubePieceProps) => {
|
| 19 |
+
const [x, y, z] = position;
|
| 20 |
+
|
| 21 |
+
const visibleFaces: Record<keyof typeof CUBE_COLORS, boolean> = {
|
| 22 |
+
front: z > 0, // Front face (positive z)
|
| 23 |
+
back: z < 0, // Back face (negative z)
|
| 24 |
+
right: x > 0, // Right face (positive x)
|
| 25 |
+
left: x < 0, // Left face (negative x)
|
| 26 |
+
top: y > 0, // Top face (positive y)
|
| 27 |
+
bottom: y < 0, // Bottom face (negative y)
|
| 28 |
+
};
|
| 29 |
+
|
| 30 |
return (
|
| 31 |
+
<mesh position={position}>
|
| 32 |
<RoundedBox args={[0.95, 0.95, 0.95]} radius={0.05} smoothness={4}>
|
| 33 |
<meshStandardMaterial
|
| 34 |
color="#2a2a2a"
|
|
|
|
| 36 |
roughness={roughness}
|
| 37 |
/>
|
| 38 |
</RoundedBox>
|
| 39 |
+
|
| 40 |
+
{Object.entries(visibleFaces).map(([face, isVisible]) => {
|
| 41 |
+
if (!isVisible) return null;
|
| 42 |
+
const color = CUBE_COLORS[face as keyof typeof CUBE_COLORS];
|
| 43 |
+
let stickerPosition: [number, number, number] = [0, 0, 0];
|
| 44 |
+
let stickerRotation: [number, number, number] = [0, 0, 0];
|
| 45 |
+
|
| 46 |
+
switch (face) {
|
| 47 |
+
case "front":
|
| 48 |
+
stickerPosition = [0, 0, 0.48];
|
| 49 |
+
break;
|
| 50 |
+
case "back":
|
| 51 |
+
stickerPosition = [0, 0, -0.48];
|
| 52 |
+
stickerRotation = [0, Math.PI, 0];
|
| 53 |
+
break;
|
| 54 |
+
case "right":
|
| 55 |
+
stickerPosition = [0.48, 0, 0];
|
| 56 |
+
stickerRotation = [0, Math.PI / 2, 0];
|
| 57 |
+
break;
|
| 58 |
+
case "left":
|
| 59 |
+
stickerPosition = [-0.48, 0, 0];
|
| 60 |
+
stickerRotation = [0, -Math.PI / 2, 0];
|
| 61 |
+
break;
|
| 62 |
+
case "top":
|
| 63 |
+
stickerPosition = [0, 0.48, 0];
|
| 64 |
+
stickerRotation = [-Math.PI / 2, 0, 0];
|
| 65 |
+
break;
|
| 66 |
+
case "bottom":
|
| 67 |
+
stickerPosition = [0, -0.48, 0];
|
| 68 |
+
stickerRotation = [Math.PI / 2, 0, 0];
|
| 69 |
+
break;
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
return (
|
| 73 |
+
<mesh
|
| 74 |
+
key={face}
|
| 75 |
+
position={stickerPosition}
|
| 76 |
+
rotation={stickerRotation}
|
| 77 |
+
>
|
| 78 |
+
<planeGeometry args={[0.8, 0.8]} />
|
| 79 |
+
<meshStandardMaterial
|
| 80 |
+
color={color}
|
| 81 |
+
metalness={1}
|
| 82 |
+
roughness={roughness}
|
| 83 |
+
/>
|
| 84 |
+
</mesh>
|
| 85 |
+
);
|
| 86 |
+
})}
|
| 87 |
</mesh>
|
| 88 |
);
|
| 89 |
};
|