feat/add-three.js #3

Merged
servostar merged 5 commits from feat/add-three.js into main 2024-10-24 22:16:32 +00:00
6 changed files with 57 additions and 1 deletions
Showing only changes of commit 2be2a52925 - Show all commits

BIN
public/gear.glb Normal file

Binary file not shown.

View File

@ -1,3 +1,5 @@
import Renderer from './Renderer.tsx'
function App() { function App() {
return ( return (
@ -19,6 +21,7 @@ function App() {
<div className="hidden lg:flex lg:flex-1 lg:justify-end"/> <div className="hidden lg:flex lg:flex-1 lg:justify-end"/>
</nav> </nav>
</header> </header>
<Renderer/>
</> </>
) )
} }

45
src/Renderer.tsx Normal file
View File

@ -0,0 +1,45 @@
import { Canvas, useFrame, useLoader } from '@react-three/fiber';
import { Clone, ContactShadows, OrbitControls } from '@react-three/drei'
import { Suspense, useRef } from 'react';
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
function Gear(props: any) {
const gltf = useLoader(GLTFLoader, './gear.glb');
const gear1Ref = useRef<any>()
const gear2Ref = useRef<any>()
const speed = 2.5;
useFrame(({ clock }) => {
gear1Ref.current.rotation.z = clock.getElapsedTime() * speed + Math.sin(clock.getElapsedTime()) * speed
gear2Ref.current.rotation.z = -clock.getElapsedTime() * speed - Math.sin(clock.getElapsedTime()) * speed
})
return <>
<Clone ref={gear1Ref} {...props} object={gltf.scene} />
<Clone ref={gear2Ref} position={[2.5,0,0]} {...props} object={gltf.scene} />
</>
}
function Renderer() {
return (
<div className='flex flex-col h-screen gradient'>
<Canvas camera={{ position: [-8, 5, 8] }}>
<Suspense fallback={null}>
<Gear />
<ContactShadows
scale={60}
position={[0, -2, 0]}
opacity={2.0}
blur={2}
resolution={512}
/>
<OrbitControls target={[0, 0, 0]} maxPolarAngle={Math.PI / 2} />
</Suspense>
</Canvas>
</div>
);
}
export default Renderer

View File

@ -1,3 +1,8 @@
@tailwind base; @tailwind base;
@tailwind components; @tailwind components;
@tailwind utilities; @tailwind utilities;
.gradient {
background: rgb(255,255,255);
background: linear-gradient(0deg, rgba(255,255,255,1) 0%, rgba(190,190,190,1) 100%);
}

View File

@ -1 +1 @@
{"root":["./src/App.tsx","./src/main.tsx","./src/vite-env.d.ts"],"version":"5.6.3"} {"root":["./src/App.tsx","./src/Renderer.tsx","./src/main.tsx","./src/vite-env.d.ts"],"version":"5.6.3"}

View File

@ -10,4 +10,7 @@ export default defineConfig({
plugins: [tailwindcss()], plugins: [tailwindcss()],
}, },
}, },
assetsInclude: [
"**/*.glb"
]
}) })