Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[example] Fix bird animations in with-three-js example (#36053)
* Fix bird animations * Use memo for birds generation
- Loading branch information
Showing
2 changed files
with
46 additions
and
42 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,49 @@ | ||
import { Suspense } from 'react' | ||
import { Suspense, useMemo } from 'react' | ||
import { Canvas } from '@react-three/fiber' | ||
import { OrbitControls } from '@react-three/drei' | ||
import Bird from '../components/Bird' | ||
|
||
export default function BirdsPage() { | ||
return ( | ||
<> | ||
<Canvas camera={{ position: [0, 0, 35] }}> | ||
<ambientLight intensity={2} /> | ||
<pointLight position={[40, 40, 40]} /> | ||
<OrbitControls /> | ||
<Suspense fallback={null}> | ||
{new Array(6).fill().map((_, i) => { | ||
const x = | ||
(15 + Math.random() * 30) * (Math.round(Math.random()) ? -1 : 1) | ||
const y = -10 + Math.random() * 20 | ||
const z = -5 + Math.random() * 10 | ||
const bird = ['stork', 'parrot', 'flamingo'][ | ||
Math.round(Math.random() * 2) | ||
] | ||
let speed = bird === 'stork' ? 0.5 : bird === 'flamingo' ? 2 : 5 | ||
let factor = | ||
bird === 'stork' | ||
? 0.5 + Math.random() | ||
: bird === 'flamingo' | ||
? 0.25 + Math.random() | ||
: 1 + Math.random() - 0.5 | ||
const birds = useMemo( | ||
() => | ||
new Array(10).fill().map((_, index) => { | ||
const x = | ||
(15 + Math.random() * 30) * (Math.round(Math.random()) ? -1 : 1) | ||
const y = -10 + Math.random() * 20 | ||
const z = -5 + Math.random() * 10 | ||
const bird = ['stork', 'parrot', 'flamingo'][ | ||
Math.round(Math.random() * 2) | ||
] | ||
const speed = bird === 'stork' ? 0.5 : bird === 'flamingo' ? 2 : 5 | ||
const factor = | ||
bird === 'stork' | ||
? 0.5 + Math.random() | ||
: bird === 'flamingo' | ||
? 0.25 + Math.random() | ||
: 1 + Math.random() - 0.5 | ||
|
||
return { | ||
key: index, | ||
position: [x, y, z], | ||
rotation: [0, x > 0 ? Math.PI : 0, 0], | ||
speed, | ||
factor, | ||
url: `/glb/${bird}.glb`, | ||
} | ||
}), | ||
[] | ||
) | ||
|
||
return ( | ||
<Bird | ||
key={i} | ||
position={[x, y, z]} | ||
rotation={[0, x > 0 ? Math.PI : 0, 0]} | ||
speed={speed} | ||
factor={factor} | ||
url={`/glb/${bird}.glb`} | ||
/> | ||
) | ||
})} | ||
</Suspense> | ||
</Canvas> | ||
</> | ||
return ( | ||
<Canvas camera={{ position: [0, 0, 35] }}> | ||
<ambientLight intensity={2} /> | ||
<pointLight position={[40, 40, 40]} /> | ||
<OrbitControls /> | ||
<Suspense fallback={null}> | ||
{birds.map((props) => ( | ||
<Bird {...props} key={props.key} /> | ||
))} | ||
</Suspense> | ||
</Canvas> | ||
) | ||
} |