Last active
July 15, 2024 11:13
-
-
Save ektogamat/37bea6e271a7b47318f36e11b71a09b8 to your computer and use it in GitHub Desktop.
A simple FakeForest Component for React Three Fiber by Anderson Mancini
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
/* | |
Copyright(c) June 2024 Anderson Mancini | |
Permission is hereby granted, free of charge, to any person obtaining | |
a copy of this software and associated documentation files (the | |
"Software"), to deal in the Software without restriction, including | |
without limitation the rights to use, copy, modify, merge, publish, | |
distribute, sublicense, and/or sell copies of the Software, and to | |
permit persons to whom the Software is furnished to do so, subject to | |
the following conditions: | |
The above copyright notice and this permission notice shall be | |
included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
import { useFrame, useThree } from '@react-three/fiber' | |
import { useMemo, useRef } from 'react' | |
import { Instance, Instances, useTexture } from '@react-three/drei' | |
import * as THREE from 'three' | |
export default function Forest({ | |
numTrees, | |
innerRadius, | |
outerRadius, | |
position, | |
}) { | |
const { camera } = useThree() | |
const ref = useRef() | |
const particles = useMemo(() => { | |
return Array.from({ length: numTrees }, (_, index) => { | |
const angle = (index / numTrees) * Math.PI * 2 | |
const radius = innerRadius + Math.random() * (outerRadius - innerRadius) | |
const x = radius * Math.cos(angle) | |
const z = radius * Math.sin(angle) | |
const y = 0 | |
const scale = | |
2 + Math.random() * 3.5 * Math.random() * Math.random() * 2.5 | |
return { x, y, z, scale } | |
}) | |
}, [numTrees, innerRadius, outerRadius]) | |
const texture = useTexture('/fake_forest.webp') | |
texture.flipY = false | |
useFrame(() => { | |
if (ref.current) { | |
ref.current.children.forEach((child) => { | |
if (child) { | |
child.lookAt(camera.position) | |
child.needsUpdate = true | |
} | |
}) | |
} | |
}) | |
return ( | |
<group position={position}> | |
<Instances | |
castShadow | |
receiveShadow | |
ref={ref} | |
limit={2000} | |
frustumCulled={false} | |
renderOrder={2} | |
> | |
<planeGeometry args={[4, 4]} /> | |
<meshBasicMaterial | |
map={texture} | |
color={0xffff00} | |
transparent | |
side={THREE.DoubleSide} | |
depthWrite={true} | |
alphaTest={0.2} | |
/> | |
{particles.map((data, i) => ( | |
<Instance | |
key={i} | |
position={[data.x, data.y, data.z]} | |
scale={[data.scale, data.scale, 1]} | |
/> | |
))} | |
</Instances> | |
</group> | |
) | |
} |
Hi there! this texture is just a PNG of a tree. I can't give you the one that I'm using in that project but you can use any PNG texture and it will work, okay? You can even create multiple instances of this Forest component and use different textures, passing the texture name as a Prop. That would give you more variations!
I hope it helps!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
const texture = useTexture('/fake_forest.webp')
where can I get this texture?