Skip to content

Instantly share code, notes, and snippets.

@mkubdev
Forked from ektogamat/Forest.jsx
Created June 17, 2024 13:57
Show Gist options
  • Select an option

  • Save mkubdev/b0cc0b9d36bd8c47ae1d1c6c90b9953d to your computer and use it in GitHub Desktop.

Select an option

Save mkubdev/b0cc0b9d36bd8c47ae1d1c6c90b9953d to your computer and use it in GitHub Desktop.
A simple FakeForest Component for React Three Fiber by Anderson Mancini
/*
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>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment