Unverified Commit 6c1cfc10 authored by Henrik Wenz's avatar Henrik Wenz Committed by GitHub

[example] Fix bird animations in with-three-js example (#36053)

* Fix bird animations

* Use memo for birds generation
parent 16f7084e
...@@ -6,11 +6,11 @@ ...@@ -6,11 +6,11 @@
"start": "next start" "start": "next start"
}, },
"dependencies": { "dependencies": {
"@react-three/drei": "7.6.1", "@react-three/drei": "9.3.4",
"@react-three/fiber": "7.0.6", "@react-three/fiber": "8.0.10",
"next": "latest", "next": "latest",
"react": "^17.0.2", "react": "^18.0.0",
"react-dom": "^17.0.2", "react-dom": "^18.0.0",
"three": "0.128.0" "three": "0.139.2"
} }
} }
import { Suspense } from 'react' import { Suspense, useMemo } from 'react'
import { Canvas } from '@react-three/fiber' import { Canvas } from '@react-three/fiber'
import { OrbitControls } from '@react-three/drei' import { OrbitControls } from '@react-three/drei'
import Bird from '../components/Bird' import Bird from '../components/Bird'
export default function BirdsPage() { export default function BirdsPage() {
return ( const birds = useMemo(
<> () =>
<Canvas camera={{ position: [0, 0, 35] }}> new Array(10).fill().map((_, index) => {
<ambientLight intensity={2} /> const x =
<pointLight position={[40, 40, 40]} /> (15 + Math.random() * 30) * (Math.round(Math.random()) ? -1 : 1)
<OrbitControls /> const y = -10 + Math.random() * 20
<Suspense fallback={null}> const z = -5 + Math.random() * 10
{new Array(6).fill().map((_, i) => { const bird = ['stork', 'parrot', 'flamingo'][
const x = Math.round(Math.random() * 2)
(15 + Math.random() * 30) * (Math.round(Math.random()) ? -1 : 1) ]
const y = -10 + Math.random() * 20 const speed = bird === 'stork' ? 0.5 : bird === 'flamingo' ? 2 : 5
const z = -5 + Math.random() * 10 const factor =
const bird = ['stork', 'parrot', 'flamingo'][ bird === 'stork'
Math.round(Math.random() * 2) ? 0.5 + Math.random()
] : bird === 'flamingo'
let speed = bird === 'stork' ? 0.5 : bird === 'flamingo' ? 2 : 5 ? 0.25 + Math.random()
let factor = : 1 + Math.random() - 0.5
bird === 'stork'
? 0.5 + Math.random() return {
: bird === 'flamingo' key: index,
? 0.25 + Math.random() position: [x, y, z],
: 1 + Math.random() - 0.5 rotation: [0, x > 0 ? Math.PI : 0, 0],
speed,
factor,
url: `/glb/${bird}.glb`,
}
}),
[]
)
return ( return (
<Bird <Canvas camera={{ position: [0, 0, 35] }}>
key={i} <ambientLight intensity={2} />
position={[x, y, z]} <pointLight position={[40, 40, 40]} />
rotation={[0, x > 0 ? Math.PI : 0, 0]} <OrbitControls />
speed={speed} <Suspense fallback={null}>
factor={factor} {birds.map((props) => (
url={`/glb/${bird}.glb`} <Bird {...props} key={props.key} />
/> ))}
) </Suspense>
})} </Canvas>
</Suspense>
</Canvas>
</>
) )
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment