RenderTexture는 재질의 map 속성에 사용할 수 있는 텍스쳐를 별도의 카메라와 광원 그리고 모델로 구성된 씬을 통해 만들어 주는 컴포넌트로 그 활용성이 매우 높습니다.
RenderTexture가 적용된 Cube 컴포넌트입니다.
function Cube() {
const textRef = useRef()
useFrame((state) => (textRef.current.position.x = Math.sin(state.clock.elapsedTime) * 2))
return (
<mesh>
<cylinderGeometry />
<meshStandardMaterial>
<RenderTexture attach="map" anisotropy={16}>
<PerspectiveCamera makeDefault manual aspect={1 / 1} position={[0, 0, 5]} />
<color attach="background" args={['orange']} />
<ambientLight intensity={0.5} />
<directionalLight position={[10, 10, 5]} />
<Text font={suspend(inter).default} ref={textRef} fontSize={4} color="#555">
hello
</Text>
<Dodecahedron />
</RenderTexture>
</meshStandardMaterial>
</mesh>
)
}
위의 코드에서 Dodecahedron 컴포넌트가 보이는데, 코드는 다음과 같습니다.
function Dodecahedron(props) {
const meshRef = useRef()
const [hovered, hover] = useState(false)
const [clicked, click] = useState(false)
useFrame(() => (meshRef.current.rotation.x += 0.01))
return (
<group {...props}>
<mesh
ref={meshRef}
scale={clicked ? 1.5 : 1}
onClick={() => click(!clicked)}
onPointerOver={() => hover(true)}
onPointerOut={() => hover(false)}>
<dodecahedronGeometry args={[0.75]} />
<meshStandardMaterial color={hovered ? 'hotpink' : '#5de4c7'} />
</mesh>
</group>
)
}
실제 Cube는 다음처럼 렌더링됩니다.
