Created
June 9, 2025 16:11
-
-
Save mikebuss/8a9364499c236b77d3c2042ee796b8c0 to your computer and use it in GitHub Desktop.
Grid in React-Three-Fiber
This file contains hidden or 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
const Grid = ({ number = 10, lineWidth = 0.026, height = 0.1 }) => { | |
const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches; | |
return ( | |
// Renders a grid and crosses as instances | |
<Instances position={[0, -3.02, 0]}> | |
<planeGeometry args={[lineWidth, height]} /> | |
<meshBasicMaterial color="#999" /> | |
{Array.from({ length: number }, (_, y) => | |
Array.from({ length: number }, (_, x) => ( | |
<group key={x + ':' + y} position={[x * 2 - Math.floor(number / 2) * 2, -0.01, y * 2 - Math.floor(number / 2) * 2]}> | |
<Instance rotation={[-Math.PI / 2, 0, 0]} /> | |
<Instance rotation={[-Math.PI / 2, 0, Math.PI / 2]} /> | |
</group> | |
)) | |
)} | |
{isDarkMode && <gridHelper args={[50, 50, '#293D59', '#293D59']} position={[0, 0, 0]} />} | |
{!isDarkMode && <gridHelper args={[50, 50, '#bbb', '#bbb']} position={[0, 0, 0]} />} | |
</Instances> | |
)} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment