ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [React / Three] Light / Shadow
    Reactjs/Threejs 2023. 1. 25. 06:29

    1. Light

     - AmbientLight : 장면의 모든 개체를 전체적으로 동일하게 비춘다. 이 Light는 방향이 없기 때문에 그림자를 만드는데 사용할 수 없다.

      https://threejs.org/docs/?q=ambient#api/en/lights/AmbientLight

     - DirectionalLight : 특정 방향에서 나오는 Light로 마치 무한히 멀리 떨어져 있고 모두 평행한 것처럼 작동한다. 일반적으로 태양빛을 구현하는 것과 같다.

      https://threejs.org/docs/?q=directional#api/en/lights/DirectionalLight

     - SpotLight : 빛에서 멀어질수록 크기가 커지는 원뿔을 따라 한 방향으로 단일 지점에서 방출된다.

      https://threejs.org/docs/?q=spotli#api/en/lights/SpotLight

     - PointLight : 한 지점에서 모든 방향으로 방출되는 Light로 전구에서 방출되는 빛을 만들 수 있다.

      https://threejs.org/docs/?q=pointli#api/en/lights/PointLight

     

    2. Shadow

     - Shadow 추가 방법

      1) Canvas에 Shadows 추가

    <Canvas shadows />

      2) Light에 cast shadows 설정

    <directionalLight castShadow />

      3) cast shadow할 개체 선택

    <mesh castShadow />

      4) receive shadow할 개체 선택

    <mesh receiveShadow />

     

     

    3. Light / Shadow 예제 코드

    import Polyhedron from './Polyhedron'
    import * as THREE from 'three'
    import { Canvas } from '@react-three/fiber'
    import { Stats, OrbitControls } from '@react-three/drei'
    import { useControls } from 'leva'
    import Floor from './Floor'
    
    function Lights() {
      const ambientCtl = useControls('Ambient Light', {
        visible: false,
        intensity: {
          value: 1.0,
          min: 0,
          max: 1.0,
          step: 0.1,
        },
      })
    
      const directionalCtl = useControls('Directional Light', {
        visible: true,
        position: {
          x: 3.3,
          y: 1.0,
          z: 4.4,
        },
        castShadow: true,
      })
    
      const pointCtl = useControls('Point Light', {
        visible: false,
        position: {
          x: 2,
          y: 0,
          z: 0,
        },
        castShadow: true,
      })
    
      const spotCtl = useControls('Spot Light', {
        visible: false,
        position: {
          x: 3,
          y: 2.5,
          z: 1,
        },
        castShadow: true,
      })
    
      return (
        <>
          <ambientLight
            visible={ambientCtl.visible}
            intensity={ambientCtl.intensity}
          />
          <directionalLight
            visible={directionalCtl.visible}
            position={[
              directionalCtl.position.x,
              directionalCtl.position.y,
              directionalCtl.position.z,
            ]}
            castShadow={directionalCtl.castShadow}
          />
          <pointLight
            visible={pointCtl.visible}
            position={[
              pointCtl.position.x,
              pointCtl.position.y,
              pointCtl.position.z,
            ]}
            castShadow={pointCtl.castShadow}
          />
          <spotLight
            visible={spotCtl.visible}
            position={[spotCtl.position.x, spotCtl.position.y, spotCtl.position.z]}
            castShadow={spotCtl.castShadow}
          />
        </>
      )
    }
    export default function App() {
      return (
        <Canvas camera={{ position: [4, 4, 1.5] }} shadows>
          <Lights />
          <Polyhedron
            name="meshBasicMaterial"
            position={[-3, 1, 0]}
            material={new THREE.MeshBasicMaterial({ color: 'yellow' })}
          />
          <Polyhedron
            name="meshNormalMaterial"
            position={[-1, 1, 0]}
            material={new THREE.MeshNormalMaterial({ flatShading: true })}
          />
          <Polyhedron
            name="meshPhongMaterial"
            position={[1, 1, 0]}
            material={
              new THREE.MeshPhongMaterial({ color: 'lime', flatShading: true })
            }
          />
          <Polyhedron
            name="meshStandardMaterial"
            position={[3, 1, 0]}
            material={
              new THREE.MeshStandardMaterial({
                color: 0xff0033,
                flatShading: true,
              })
            }
          />
          <Floor />
          <OrbitControls target={[2, 2, 0]} />
          <axesHelper args={[5]} />
          <Stats />
        </Canvas>
      )
    }
    import { useRef } from 'react'
    import { useFrame } from '@react-three/fiber'
    
    export default function Polyhedron(props) {
      const ref = useRef()
    
      useFrame((_, delta) => {
        ref.current.rotation.x += 0.2 * delta
        ref.current.rotation.y += 0.05 * delta
      })
    
      return (
        <mesh {...props} ref={ref} castShadow receiveShadow>
          <icosahedronGeometry args={[1, 1]} />
        </mesh>
      )
    }
    export default function Floor() {
      return (
        <mesh rotation-x={-Math.PI / 2} receiveShadow>
          <circleGeometry args={[10]} />
          <meshStandardMaterial />
        </mesh>
      )
    }

    'Reactjs > Threejs' 카테고리의 다른 글

    [React / Three] GLTFJSX  (0) 2023.07.03
    [React / Three] Material  (0) 2023.01.21
    [React / Three] Leva  (0) 2023.01.20
    [React / Three] AxisHelper / GridHelper  (0) 2023.01.19
    [React / Three] Stats Panel / OrbitControls  (0) 2023.01.19
Designed by Tistory.