R3F

React Three Fiber

React With Three.js

Renderer

// three.js์—์„œ ๋ Œ๋”๋Ÿฌ๋ฅผ ํ™”๋ฉด์— ๋ Œ๋”๋ง์‹œํ‚ค๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ 
function render() {
  renderer.render(scene, camera);
  requestAnimationFrame(render);
}

Three.js์—์„œ ๋ Œ๋”๋Ÿฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ™”๋ฉด์— ๋ฌด์–ธ๊ฐ€๋ฅผ ๊ทธ๋ฆฌ๋ ค๋ฉด ๋ณดํ†ต requestAnimationFrame()์„ ์‚ฌ์šฉํ•˜์—ฌ  ์—…๋ฐ์ดํŠธํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ r3f๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ด๋Ÿฌํ•œ ์„ธ๋ถ€์ ์ธ ๋ถ€๋ถ„์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋‚ด๋ถ€์—์„œ ์ฒ˜๋ฆฌ๋˜์–ด ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

useThree()

<Canvas/>์˜ ์ž์‹(์ž์†) ์ปดํฌ๋„ŒํŠธ์—์„œ๋งŒ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

property

import { useThree } from '@react-tree/fiber'

const { size, gl, scene, camera } = useThree();
  • size -> ๋ Œ๋”๋Ÿฌ ์‚ฌ์ด์ฆˆ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋‹ค. (resize์‹œ ์ž๋™์œผ๋กœ ์—…๋ฐ์ดํŠธ ๋œ๋‹ค.)

  • gl -> WebGLRenderer์˜ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋‹ค.

  • scene -> Scene ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋‹ค.

  • camera -> Camera ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋‹ค.

useFrame()

Scene์˜ ๋ณ€ํ™”๋ฅผ useFrame ์ฝœ๋ฐฑํ•จ์ˆ˜์—์„œ ๋‹ค๋ฃจ๊ฒŒ ๋œ๋‹ค.

import { useFrame } from '@react-three/fiber';

useFrame((state, delta, xrFrame) => {})
  • useFrame()์„ ์‚ฌ์šฉํ•˜๋ฉด requestAnimationFrame() ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ๋™์ž‘์„ ์ˆ˜ํ–‰

  • state -> useTree() ์—์„œ ์ œ๊ณต๋˜๋Š” gl ๋ฐ์ดํ„ฐ, ๋งค ํ”„๋ ˆ์ž„๋งˆ๋‹ค ๊ฐฑ์‹ 

  • delta -> ๋งค ํ”„๋ ˆ์ž„๋งˆ๋‹ค ๊ฒฝ๊ณผ๋œ ์‹œ๊ฐ„

    • three.js์—์„œ new THREE.Clock()์„ ์‚ฌ์šฉํ•˜์—ฌ clock.getElapsedTime() ๊ฐ’์„ ์–ป๊ณ , ์ด ๊ฐ’์„ ํ™œ์šฉํ•˜์—ฌ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๋ฐฉ์‹๊ณผ ์œ ์‚ฌํ•œ ๋™์ž‘์„ ์ˆ˜ํ–‰

Last updated