비전공자 개발일기

three.js 본문

React & React Native

three.js

HiroDaegu 2022. 4. 11. 00:20
728x90
SMALL

 

* npm i three @react-three/fiber *

box.js
import { useFrame } from "@react-three/fiber";
import { useState, useRef } from "react";

export default function Box({ color, ...props }) {
  const [isHovered, setIsHovered] = useState(false);

  const ref = useRef();
  useFrame(() => {
    ref.current.rotation.x += 0.01;
    ref.current.rotation.y += 0.01;
  });

  return (
    <mesh
      ref={ref}
      scale={isHovered ? 1.5 : 1}
      onPointerOver={() => {
        setIsHovered(true);
      }}
      onPointerOut={() => {
        setIsHovered(false);
      }}
      {...props}
    >
      <pointLight></pointLight>
      <boxBufferGeometry></boxBufferGeometry>
      <meshPhysicalMaterial color={color}></meshPhysicalMaterial>
    </mesh>
  );
}
App.js

import { Canvas } from "@react-three/fiber";
import Box from "./component/box";

export default function App() {
  return (
    <div style={{ width: "100vw", height: "100vh" }}>
      <Canvas style={{ backgroundColor: "#000" }}>
        <ambientLight intensity={0.3}>
          <Box color="green" position={[3, 0, 0]}></Box>
          <Box color="red" position={[-3, 0, 0]}></Box>
          <Box color="yellow" position={[0, 0, 0]}></Box>
        </ambientLight>
      </Canvas>
    </div>
  );
}
728x90
LIST

'React & React Native' 카테고리의 다른 글

React & Vue & Svelte  (0) 2023.05.01
React Native Flipper-Glog Error  (0) 2022.05.23
React Native Login & Basic Page  (0) 2022.02.27
React Native Webview2  (0) 2022.02.14
React Native Webview  (0) 2022.02.13