-
[React / Three] LevaReactjs/Threejs 2023. 1. 20. 05:54
1. Leva는 React Three Fiber 애플리케이션 View와 상호 작용할 수 있는 GUI 구성 요소이다.
npm install leva
아래 예시는 Leva GUI에 색상 선택기를 표시하고 색상을 변경하면 캔버스의 배경색이 업데이트 한다. useControls는 React 재랜더링 간에 유지되며 useState 작동 방식과 유사하다.
import { useControls } from 'leva'
const color = useControls({ value: 'green', })
<Canvas> <color attach="background" args={[color.value]} /> </Canvas>
도형을 회전시키고 색상 변경, Visible 설정하는 예제
import { Canvas } from '@react-three/fiber' import Polyhedron from './Polyhedron' import * as THREE from 'three' import { useMemo } from 'react' import { Stats, OrbitControls } from '@react-three/drei' import { useControls } from 'leva' export default function App() { const polyhedron = useMemo( () => [ new THREE.BoxGeometry(), new THREE.SphereGeometry(0.785398), new THREE.DodecahedronGeometry(0.785398), ], [] ) const options = useMemo(() => { return { x: { value: 0, min: 0, max: Math.PI * 2, step: 0.01 }, y: { value: 0, min: 0, max: Math.PI * 2, step: 0.01 }, z: { value: 0, min: 0, max: Math.PI * 2, step: 0.01 }, visible: true, color: { value: 'lime' }, } }, []) const pA = useControls('Polyhedron A', options) const pB = useControls('Polyhedron B', options) return ( <Canvas camera={{ position: [1, 2, 3] }}> <Polyhedron position={[-1, 1, 0]} rotation={[pA.x, pA.y, pA.z]} visible={pA.visible} color={pA.color} polyhedron={polyhedron} /> <Polyhedron position={[1, 1, 0]} rotation={[pB.x, pB.y, pB.z]} visible={pB.visible} color={pB.color} polyhedron={polyhedron} /> <OrbitControls target-y={1} /> <axesHelper args={[5]} /> <gridHelper /> <Stats /> </Canvas> ) }
import { useRef, useState } from 'react' export default function Polyhedron({ polyhedron, color, ...props }) { const ref = useRef() const [count, setCount] = useState(2) console.log(polyhedron[count].uuid) return ( <mesh {...props} ref={ref} onPointerDown={() => { setCount((count + 1) % 3) }} geometry={polyhedron[count]} > <meshBasicMaterial color={color} wireframe /> </mesh> ) }
Leva 링크
'Reactjs > Threejs' 카테고리의 다른 글
[React / Three] Light / Shadow (0) 2023.01.25 [React / Three] Material (0) 2023.01.21 [React / Three] AxisHelper / GridHelper (0) 2023.01.19 [React / Three] Stats Panel / OrbitControls (0) 2023.01.19 [React / Three] Events (0) 2023.01.17