feat: added object duplication and group duplication in r3f for task3
This commit is contained in:
parent
e6ae7e20ac
commit
835e55a6e3
15
Week-1/Task-3/r3f/index.html
Normal file
15
Week-1/Task-3/r3f/index.html
Normal file
@ -0,0 +1,15 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Task 3 R3F - Cube Clone</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
<script type="module" src="/src/main.jsx"></script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@ -2,11 +2,23 @@
|
||||
"name": "week-1-task-3-r3f",
|
||||
"private": true,
|
||||
"version": "0.0.0",
|
||||
"type": "module",
|
||||
"packageManager": "yarn@1.22.22",
|
||||
"scripts": {
|
||||
"dev": "echo 'Add your React Three Fiber dev script here'",
|
||||
"build": "echo 'Add your React Three Fiber build script here'",
|
||||
"lint": "echo 'Add your lint script here'",
|
||||
"dev": "vite",
|
||||
"build": "vite build",
|
||||
"lint": "echo 'Lint not configured yet'",
|
||||
"preview": "vite preview",
|
||||
"clean": "rm -rf dist build .next"
|
||||
},
|
||||
"dependencies": {
|
||||
"@react-three/fiber": "^9.5.0",
|
||||
"react": "^19.2.4",
|
||||
"react-dom": "^19.2.4",
|
||||
"three": "^0.183.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@vitejs/plugin-react": "^6.0.0",
|
||||
"vite": "^8.0.3"
|
||||
}
|
||||
}
|
||||
16
Week-1/Task-3/r3f/src/App.jsx
Normal file
16
Week-1/Task-3/r3f/src/App.jsx
Normal file
@ -0,0 +1,16 @@
|
||||
import { Canvas } from '@react-three/fiber'
|
||||
import CubePair from './CubeDuplicate'
|
||||
import GroupedStructure from './Group'
|
||||
|
||||
export default function App() {
|
||||
return (
|
||||
<Canvas camera={{ fov: 60, near: 0.1, far: 100, position: [0, 0, 6] }}>
|
||||
<color attach="background" args={['#101418']} />
|
||||
<ambientLight intensity={0.6} />
|
||||
<directionalLight intensity={1.1} position={[4, 6, 5]} />
|
||||
{/* <CubePair /> */}
|
||||
<GroupedStructure position={[-1.4, 0, 0]} direction={1}/>
|
||||
<GroupedStructure position={[ 1.4, 0, 0]} direction={1}/>
|
||||
</Canvas>
|
||||
)
|
||||
}
|
||||
47
Week-1/Task-3/r3f/src/CubeDuplicate.jsx
Normal file
47
Week-1/Task-3/r3f/src/CubeDuplicate.jsx
Normal file
@ -0,0 +1,47 @@
|
||||
import { useFrame } from '@react-three/fiber'
|
||||
import { useEffect, useMemo, useRef } from 'react'
|
||||
import * as THREE from 'three'
|
||||
|
||||
function CubePair() {
|
||||
const leftCubeRef = useRef()
|
||||
const rightCubeRef = useRef()
|
||||
|
||||
const sharedGeometry = useMemo(() => new THREE.BoxGeometry(1, 1, 1), [])
|
||||
const sharedMaterial = useMemo(() => new THREE.MeshStandardMaterial({ color: '#5bc0eb' }), [])
|
||||
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
sharedGeometry.dispose()
|
||||
sharedMaterial.dispose()
|
||||
}
|
||||
}, [sharedGeometry, sharedMaterial])
|
||||
|
||||
useFrame((state) => {
|
||||
const seconds = state.clock.elapsedTime
|
||||
|
||||
if (leftCubeRef.current) {
|
||||
leftCubeRef.current.rotation.x = seconds
|
||||
leftCubeRef.current.rotation.y = seconds
|
||||
}
|
||||
|
||||
if (rightCubeRef.current) {
|
||||
rightCubeRef.current.rotation.x = seconds
|
||||
rightCubeRef.current.rotation.y = -seconds
|
||||
}
|
||||
})
|
||||
|
||||
return (
|
||||
<>
|
||||
<mesh ref={leftCubeRef} position={[-1.2, 0, 0]} geometry={sharedGeometry} material={sharedMaterial} />
|
||||
|
||||
<mesh
|
||||
ref={rightCubeRef}
|
||||
position={[1.2, 0, 0]}
|
||||
geometry={sharedGeometry}
|
||||
material={sharedMaterial}
|
||||
/>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export default CubePair
|
||||
44
Week-1/Task-3/r3f/src/Group.jsx
Normal file
44
Week-1/Task-3/r3f/src/Group.jsx
Normal file
@ -0,0 +1,44 @@
|
||||
import { Canvas, useFrame } from '@react-three/fiber'
|
||||
import { useRef } from 'react'
|
||||
|
||||
function GroupedStructure({ position = [0, 0, 0], direction = 1 }) {
|
||||
const groupRef = useRef()
|
||||
|
||||
useFrame((state) => {
|
||||
const seconds = state.clock.elapsedTime
|
||||
|
||||
if (!groupRef.current) return
|
||||
|
||||
groupRef.current.rotation.y = direction * seconds * 0.7
|
||||
groupRef.current.rotation.x = Math.sin(seconds * 1.1) * 0.2
|
||||
})
|
||||
|
||||
return (
|
||||
<group ref={groupRef} position={position}>
|
||||
<mesh position={[0, -0.2, 0]}>
|
||||
<boxGeometry args={[0.7, 1.6, 0.7]} />
|
||||
<meshStandardMaterial color="#f6bd60" />
|
||||
</mesh>
|
||||
|
||||
<mesh position={[0, 0.95, 0]}>
|
||||
<sphereGeometry args={[0.4, 24, 24]} />
|
||||
<meshStandardMaterial color="#84a59d" />
|
||||
</mesh>
|
||||
</group>
|
||||
)
|
||||
}
|
||||
|
||||
export default GroupedStructure
|
||||
|
||||
// export default function GroupApp() {
|
||||
// return (
|
||||
// <Canvas camera={{ fov: 60, near: 0.1, far: 100, position: [0, 1.2, 8] }}>
|
||||
// <color attach="background" args={['#101418']} />
|
||||
// <ambientLight intensity={0.65} />
|
||||
// <directionalLight intensity={1.1} position={[4, 6, 5]} />
|
||||
|
||||
// <GroupedStructure position={[-1.4, 0, 0]} direction={1} />
|
||||
// <GroupedStructure position={[1.4, 0, 0]} direction={-1} />
|
||||
// </Canvas>
|
||||
// )
|
||||
// }
|
||||
18
Week-1/Task-3/r3f/src/index.css
Normal file
18
Week-1/Task-3/r3f/src/index.css
Normal file
@ -0,0 +1,18 @@
|
||||
html,
|
||||
body,
|
||||
#root {
|
||||
margin: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
body {
|
||||
overflow: hidden;
|
||||
background: #101418;
|
||||
}
|
||||
|
||||
canvas {
|
||||
display: block;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
10
Week-1/Task-3/r3f/src/main.jsx
Normal file
10
Week-1/Task-3/r3f/src/main.jsx
Normal file
@ -0,0 +1,10 @@
|
||||
import { StrictMode } from 'react'
|
||||
import { createRoot } from 'react-dom/client'
|
||||
import App from './App.jsx'
|
||||
import './index.css'
|
||||
|
||||
createRoot(document.getElementById('root')).render(
|
||||
<StrictMode>
|
||||
<App />
|
||||
</StrictMode>,
|
||||
)
|
||||
6
Week-1/Task-3/r3f/vite.config.js
Normal file
6
Week-1/Task-3/r3f/vite.config.js
Normal file
@ -0,0 +1,6 @@
|
||||
import { defineConfig } from 'vite'
|
||||
import react from '@vitejs/plugin-react'
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [react()],
|
||||
})
|
||||
Loading…
x
Reference in New Issue
Block a user