Created
November 19, 2024 05:54
-
-
Save chrislanejones/71c588fe6976e6a8a969f7db284eae50 to your computer and use it in GitHub Desktop.
Create a chessboard in React Three Fiber with TypeScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { GroupProps } from "@react-three/fiber"; | |
interface ChessboardProps extends GroupProps { | |
tileSize?: number; | |
} | |
export function Chessboard({ tileSize = 1, ...props }: ChessboardProps) { | |
const boardSize = 8; // 8x8 chess board | |
return ( | |
<group {...props}> | |
{Array.from({ length: boardSize }, (_, row: number) => | |
Array.from({ length: boardSize }, (_, col: number) => { | |
const isWhite = (row + col) % 2 === 0; | |
return ( | |
<mesh | |
position={[ | |
col * tileSize - (boardSize * tileSize) / 2 + tileSize / 2, | |
0, | |
row * tileSize - (boardSize * tileSize) / 2 + tileSize / 2, | |
]} | |
> | |
<boxGeometry args={[tileSize, 0.1, tileSize]} /> | |
<meshStandardMaterial color={isWhite ? "#ffffff" : "#000000"} /> | |
</mesh> | |
); | |
}) | |
)} | |
</group> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment