From 7d6abc6f1d7b8f5cdf82c7f420193e52030273a7 Mon Sep 17 00:00:00 2001 From: Henrik Wenz Date: Thu, 6 May 2021 12:23:42 +0200 Subject: [PATCH] Update with-three-js example --- examples/with-three-js/components/Bird.js | 34 +++++------- examples/with-three-js/components/Box.js | 29 ++++++++++ examples/with-three-js/next.config.js | 3 -- examples/with-three-js/package.json | 15 +++--- examples/with-three-js/pages/birds.js | 65 +++++++++++------------ examples/with-three-js/pages/boxes.js | 58 ++++++-------------- examples/with-three-js/pages/index.js | 4 +- 7 files changed, 95 insertions(+), 113 deletions(-) create mode 100644 examples/with-three-js/components/Box.js delete mode 100644 examples/with-three-js/next.config.js diff --git a/examples/with-three-js/components/Bird.js b/examples/with-three-js/components/Bird.js index 024dcd5f06b9d..3babc15b76e3c 100644 --- a/examples/with-three-js/components/Bird.js +++ b/examples/with-three-js/components/Bird.js @@ -1,38 +1,32 @@ -import { useRef, useState, useEffect } from 'react' -import * as THREE from 'three' +import { useEffect } from 'react' +import { useFrame } from '@react-three/fiber' +import { useAnimations, useGLTF } from '@react-three/drei' -import { useFrame, useLoader } from 'react-three-fiber' -import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader' +export default function Bird({ speed, factor, url, ...props }) { + const { nodes, animations } = useGLTF(url) + const { ref, mixer } = useAnimations(animations) -const Bird = ({ speed, factor, url, ...props }) => { - const gltf = useLoader(GLTFLoader, url) - const group = useRef() - const [mixer] = useState(() => new THREE.AnimationMixer()) - - useEffect( - () => void mixer.clipAction(gltf.animations[0], group.current).play(), - [gltf.animations, mixer] - ) + useEffect(() => void mixer.clipAction(animations[0], ref.current).play(), [mixer, animations, ref]) useFrame((state, delta) => { - group.current.rotation.y += + ref.current.rotation.y += Math.sin((delta * factor) / 2) * Math.cos((delta * factor) / 2) * 1.5 mixer.update(delta * speed) }) return ( - + - + @@ -40,5 +34,3 @@ const Bird = ({ speed, factor, url, ...props }) => { ) } - -export default Bird diff --git a/examples/with-three-js/components/Box.js b/examples/with-three-js/components/Box.js new file mode 100644 index 0000000000000..7b1ae2df63af8 --- /dev/null +++ b/examples/with-three-js/components/Box.js @@ -0,0 +1,29 @@ +import { useRef, useState } from 'react' +import { useFrame } from '@react-three/fiber' +import { Box as NativeBox } from '@react-three/drei' + +export default function Box(props) { + const mesh = useRef() + + const [hovered, setHover] = useState(false) + const [active, setActive] = useState(false) + + useFrame(() => (mesh.current.rotation.x = mesh.current.rotation.y += 0.01)) + + return ( + setActive(!active)} + onPointerOver={() => setHover(true)} + onPointerOut={() => setHover(false)} + > + + + ) +} diff --git a/examples/with-three-js/next.config.js b/examples/with-three-js/next.config.js deleted file mode 100644 index a5dda6cbfc5d8..0000000000000 --- a/examples/with-three-js/next.config.js +++ /dev/null @@ -1,3 +0,0 @@ -const withTM = require('next-transpile-modules')(['@react-three/drei', 'three']) - -module.exports = withTM() diff --git a/examples/with-three-js/package.json b/examples/with-three-js/package.json index eaf073e0b45b5..bf1c405de7649 100644 --- a/examples/with-three-js/package.json +++ b/examples/with-three-js/package.json @@ -8,15 +8,12 @@ "start": "next start" }, "dependencies": { - "@react-three/drei": "3.8.6", - "next": "10.0.7", - "react": "17.0.1", - "react-dom": "17.0.1", - "react-three-fiber": "5.3.19", - "three": "0.126.0" - }, - "devDependencies": { - "next-transpile-modules": "6.3.0" + "@react-three/drei": "4.3.3", + "@react-three/fiber": "6.0.19", + "next": "10.2.0", + "react": "17.0.2", + "react-dom": "17.0.2", + "three": "0.128.0" }, "license": "MIT" } diff --git a/examples/with-three-js/pages/birds.js b/examples/with-three-js/pages/birds.js index 7a97047ddb65f..6d2245e0cfeca 100644 --- a/examples/with-three-js/pages/birds.js +++ b/examples/with-three-js/pages/birds.js @@ -1,38 +1,9 @@ -import dynamic from 'next/dynamic' import { Suspense } from 'react' -import { Canvas } from 'react-three-fiber' +import { Canvas } from '@react-three/fiber' import { OrbitControls } from '@react-three/drei' +import Bird from '../components/Bird' -const Bird = dynamic(() => import('../components/Bird'), { ssr: false }) - -const Birds = () => { - return new Array(5).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 - - return ( - 0 ? Math.PI : 0, 0]} - speed={speed} - factor={factor} - url={`/glb/${bird}.glb`} - /> - ) - }) -} - -const BirdsPage = (props) => { +export default function BirdsPage() { return ( <> @@ -40,11 +11,35 @@ const BirdsPage = (props) => { - + {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 + + return ( + 0 ? Math.PI : 0, 0]} + speed={speed} + factor={factor} + url={`/glb/${bird}.glb`} + /> + ) + })} ) } - -export default BirdsPage diff --git a/examples/with-three-js/pages/boxes.js b/examples/with-three-js/pages/boxes.js index 6c44b90c8851d..bfc19fc390773 100644 --- a/examples/with-three-js/pages/boxes.js +++ b/examples/with-three-js/pages/boxes.js @@ -1,46 +1,20 @@ -import { useRef, useState } from 'react' -import { Canvas, useFrame } from 'react-three-fiber' -import { OrbitControls, Box } from '@react-three/drei' - -const MyBox = (props) => { - const mesh = useRef() - - const [hovered, setHover] = useState(false) - const [active, setActive] = useState(false) - - useFrame(() => (mesh.current.rotation.x = mesh.current.rotation.y += 0.01)) +import { Canvas } from '@react-three/fiber' +import { OrbitControls } from '@react-three/drei' +import Box from '../components/Box' +export default function BoxesPage() { return ( - setActive(!active)} - onPointerOver={() => setHover(true)} - onPointerOut={() => setHover(false)} - > - - + <> +

Click on me - Hover me :)

+ + + + + + + + + + ) } - -const BoxesPage = () => { - return [ -

Click on me - Hover me :)

, - - - - - - - - - , - ] -} - -export default BoxesPage diff --git a/examples/with-three-js/pages/index.js b/examples/with-three-js/pages/index.js index 5fb26f1b44ef1..bbab8905619f1 100644 --- a/examples/with-three-js/pages/index.js +++ b/examples/with-three-js/pages/index.js @@ -1,6 +1,6 @@ import Link from 'next/link' -const Index = () => { +export default function IndexPage() { return (
@@ -12,5 +12,3 @@ const Index = () => {
) } - -export default Index