feat: added object duplication and group duplication in vanilla for task3
This commit is contained in:
parent
9d6c9a8c59
commit
8a9d1e83e6
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",
|
"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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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()],
|
||||||
|
})
|
||||||
68
Week-1/Task-3/vanilla/group.js
Normal file
68
Week-1/Task-3/vanilla/group.js
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
import * as THREE from "three"
|
||||||
|
|
||||||
|
const renderer = new THREE.WebGLRenderer({ antialias: true })
|
||||||
|
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
|
||||||
|
document.body.appendChild(renderer.domElement)
|
||||||
|
|
||||||
|
const scene = new THREE.Scene()
|
||||||
|
scene.background = new THREE.Color(0x101418)
|
||||||
|
|
||||||
|
const camera = new THREE.PerspectiveCamera(60, 2, 0.1, 100)
|
||||||
|
camera.position.set(0, 1.2, 8)
|
||||||
|
|
||||||
|
scene.add(new THREE.AmbientLight(0xffffff, 0.65))
|
||||||
|
|
||||||
|
const light = new THREE.DirectionalLight(0xffffff, 1.1)
|
||||||
|
light.position.set(4, 6, 5)
|
||||||
|
scene.add(light)
|
||||||
|
|
||||||
|
const pillarGeometry = new THREE.BoxGeometry(0.7, 1.6, 0.7)
|
||||||
|
const pillarMaterial = new THREE.MeshStandardMaterial({ color: 0xf6bd60 })
|
||||||
|
const capGeometry = new THREE.SphereGeometry(0.4, 24, 24)
|
||||||
|
const capMaterial = new THREE.MeshStandardMaterial({ color: 0x84a59d })
|
||||||
|
|
||||||
|
const groupedStructure = new THREE.Group()
|
||||||
|
|
||||||
|
const pillar = new THREE.Mesh(pillarGeometry, pillarMaterial)
|
||||||
|
pillar.position.y = -0.2
|
||||||
|
groupedStructure.add(pillar)
|
||||||
|
|
||||||
|
const cap = new THREE.Mesh(capGeometry, capMaterial)
|
||||||
|
cap.position.y = 0.95
|
||||||
|
groupedStructure.add(cap)
|
||||||
|
|
||||||
|
groupedStructure.position.set(-1.4, 0, 0)
|
||||||
|
scene.add(groupedStructure)
|
||||||
|
|
||||||
|
const groupedStructureClone = groupedStructure.clone()
|
||||||
|
groupedStructureClone.position.set(1.4, 0, 0)
|
||||||
|
scene.add(groupedStructureClone)
|
||||||
|
|
||||||
|
function resizeRendererToDisplaySize() {
|
||||||
|
const width = window.innerWidth
|
||||||
|
const height = window.innerHeight
|
||||||
|
const needResize = renderer.domElement.width !== width || renderer.domElement.height !== height
|
||||||
|
|
||||||
|
if (needResize) {
|
||||||
|
renderer.setSize(width, height, false)
|
||||||
|
camera.aspect = width / height
|
||||||
|
camera.updateProjectionMatrix()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function animate(time) {
|
||||||
|
const seconds = time * 0.001
|
||||||
|
|
||||||
|
resizeRendererToDisplaySize()
|
||||||
|
|
||||||
|
groupedStructure.rotation.y = seconds * 0.7
|
||||||
|
groupedStructure.rotation.x = Math.sin(seconds * 1.1) * 0.2
|
||||||
|
|
||||||
|
groupedStructureClone.rotation.y = -seconds * 0.7
|
||||||
|
groupedStructureClone.rotation.x = Math.sin(seconds * 1.1) * 0.2
|
||||||
|
|
||||||
|
renderer.render(scene, camera)
|
||||||
|
requestAnimationFrame(animate)
|
||||||
|
}
|
||||||
|
|
||||||
|
requestAnimationFrame(animate)
|
||||||
38
Week-1/Task-3/vanilla/index.html
Normal file
38
Week-1/Task-3/vanilla/index.html
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<title>Task 3 Vanilla - Duplication Tests</title>
|
||||||
|
<style>
|
||||||
|
html,
|
||||||
|
body {
|
||||||
|
margin: 0;
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
overflow: hidden;
|
||||||
|
background: #101418;
|
||||||
|
}
|
||||||
|
|
||||||
|
canvas {
|
||||||
|
display: block;
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<script type="module">
|
||||||
|
const mode = new URLSearchParams(window.location.search).get('test')
|
||||||
|
|
||||||
|
if (mode === 'group') {
|
||||||
|
import('./group.js')
|
||||||
|
} else {
|
||||||
|
import('./main.js')
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
||||||
56
Week-1/Task-3/vanilla/main.js
Normal file
56
Week-1/Task-3/vanilla/main.js
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
import * as THREE from "three"
|
||||||
|
const renderer = new THREE.WebGLRenderer({ antialias: true });
|
||||||
|
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
|
||||||
|
document.body.appendChild(renderer.domElement);
|
||||||
|
|
||||||
|
const scene = new THREE.Scene();
|
||||||
|
scene.background = new THREE.Color(0x101418);
|
||||||
|
|
||||||
|
const camera = new THREE.PerspectiveCamera(60, 2, 0.1, 100);
|
||||||
|
camera.position.set(0, 0, 6);
|
||||||
|
|
||||||
|
scene.add(new THREE.AmbientLight(0xffffff, 0.6));
|
||||||
|
|
||||||
|
const light = new THREE.DirectionalLight(0xffffff, 1.1);
|
||||||
|
light.position.set(4, 6, 5);
|
||||||
|
scene.add(light);
|
||||||
|
|
||||||
|
const geometry = new THREE.BoxGeometry(1, 1, 1);
|
||||||
|
const material = new THREE.MeshStandardMaterial({ color: 0x5bc0eb });
|
||||||
|
|
||||||
|
const cube = new THREE.Mesh(geometry, material);
|
||||||
|
cube.position.x = -1.2;
|
||||||
|
scene.add(cube);
|
||||||
|
|
||||||
|
const cubeClone = cube.clone();
|
||||||
|
cubeClone.position.x = 1.2;
|
||||||
|
scene.add(cubeClone);
|
||||||
|
|
||||||
|
function resizeRendererToDisplaySize() {
|
||||||
|
const width = window.innerWidth;
|
||||||
|
const height = window.innerHeight;
|
||||||
|
const needResize = renderer.domElement.width !== width || renderer.domElement.height !== height;
|
||||||
|
|
||||||
|
if (needResize) {
|
||||||
|
renderer.setSize(width, height, false);
|
||||||
|
camera.aspect = width / height;
|
||||||
|
camera.updateProjectionMatrix();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function animate(time) {
|
||||||
|
const seconds = time * 0.001;
|
||||||
|
|
||||||
|
resizeRendererToDisplaySize();
|
||||||
|
|
||||||
|
cube.rotation.x = seconds;
|
||||||
|
cube.rotation.y = seconds;
|
||||||
|
|
||||||
|
cubeClone.rotation.x = seconds;
|
||||||
|
cubeClone.rotation.y = -seconds;
|
||||||
|
|
||||||
|
renderer.render(scene, camera);
|
||||||
|
requestAnimationFrame(animate);
|
||||||
|
}
|
||||||
|
|
||||||
|
requestAnimationFrame(animate);
|
||||||
@ -4,8 +4,10 @@
|
|||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"packageManager": "yarn@1.22.22",
|
"packageManager": "yarn@1.22.22",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "echo 'Add your Three.js dev script here'",
|
"dev": "vite",
|
||||||
"build": "echo 'Add your Three.js build script here'",
|
"dev:object": "vite --open '/'",
|
||||||
|
"dev:group": "vite --open '/?test=group'",
|
||||||
|
"build": "vite build",
|
||||||
"lint": "echo 'Add your lint script here'",
|
"lint": "echo 'Add your lint script here'",
|
||||||
"clean": "rm -rf dist build"
|
"clean": "rm -rf dist build"
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user