nonocast / me

记录和分享技术的博客
http://nonocast.cn
MIT License
20 stars 0 forks source link

学习 three.js (Part 3: react-three-fiber) #326

Open nonocast opened 2 months ago

nonocast commented 2 months ago

ref: pmndrs/react-three-fiber: 🇨🇭 A React renderer for Three.js 通过r3f可以通过tag的方式来写threejs

  1. yarn create viteproject name: hello
  2. yarn add three @react-three/fiber
  3. yarn dev

App.jsx

import { Canvas } from '@react-three/fiber'
import './App.css'

function App() {
  return (
    <>
      <div className="canvas-container">
        <Canvas camera={{ position: [5, 3, 5], fov: 75 }}>
          <ambientLight intensity={0.5} />
          <directionalLight position={[0, 5, 5]} />
          <mesh>
            <boxGeometry args={[2, 2, 2]} />
            <meshStandardMaterial color="red" />
          </mesh>
        </Canvas>
      </div>
    </>
  );
}

export default App

App.css

.canvas-container {
  width: 100vw;
  height: 100vh;
  position: relative;
  overflow: hidden;
}

.canvas-container canvas {
  display: block;
}
Screenshot 2024-09-11 at 23 26 10

增加orbit control,

import { Stats, OrbitControls } from '@react-three/drei'

<Canvas camera={{ position: [5, 3, 5], fov: 75 }}>
  ...
  <OrbitControls />
  <Stats />
</Canvas>