Created
November 16, 2022 16:07
-
-
Save robksawyer/e2585499a328ba8ea5b64ff2ba2c20b4 to your computer and use it in GitHub Desktop.
Here's a clever/clean way to randomly position geometry.
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
function Noodle() { | |
const { viewport, camera } = useThree() | |
const { nodes } = useGLTF('/worms-transformed.glb') | |
const [geometry] = useState(() => nodes[`noodle_${Math.ceil(Math.random() * 4)}`].geometry) | |
const [speed] = useState(() => 0.1 + Math.random() / 10) | |
const position = useMemo(() => { | |
const z = Math.random() * -30 | |
const bounds = viewport.getCurrentViewport(camera, [0, 0, z]) | |
return [THREE.MathUtils.randFloatSpread(bounds.width), THREE.MathUtils.randFloatSpread(bounds.height * 0.75), z] | |
}, [viewport]) | |
return ( | |
<Float position={position} speed={speed} rotationIntensity={10} floatIntensity={40} dispose={null}> | |
<mesh scale={5} geometry={geometry} material={material} /> | |
</Float> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment