Skip to content

Instantly share code, notes, and snippets.

@chrislanejones
Created November 19, 2024 05:54
Show Gist options
  • Save chrislanejones/71c588fe6976e6a8a969f7db284eae50 to your computer and use it in GitHub Desktop.
Save chrislanejones/71c588fe6976e6a8a969f7db284eae50 to your computer and use it in GitHub Desktop.
Create a chessboard in React Three Fiber with TypeScript
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