feat: added object duplication and group duplication in r3f for task3

This commit is contained in:
anshk 2026-03-30 20:20:49 +05:30
parent bfa16a1db3
commit d9742132c0
8 changed files with 171 additions and 3 deletions

View 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>

View File

@ -2,11 +2,23 @@
"name": "week-1-task-3-r3f", "name": "week-1-task-3-r3f",
"private": true, "private": true,
"version": "0.0.0", "version": "0.0.0",
"type": "module",
"packageManager": "yarn@1.22.22", "packageManager": "yarn@1.22.22",
"scripts": { "scripts": {
"dev": "echo 'Add your React Three Fiber dev script here'", "dev": "vite",
"build": "echo 'Add your React Three Fiber build script here'", "build": "vite build",
"lint": "echo 'Add your lint script here'", "lint": "echo 'Lint not configured yet'",
"preview": "vite preview",
"clean": "rm -rf dist build .next" "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"
} }
} }

View 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>
)
}

View 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

View 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>
// )
// }

View 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%;
}

View 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>,
)

View File

@ -0,0 +1,6 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
export default defineConfig({
plugins: [react()],
})