diff --git a/Week-2/Task-1/vanilla/index.html b/Week-2/Task-1/vanilla/index.html index cd47c68..3e7a905 100644 --- a/Week-2/Task-1/vanilla/index.html +++ b/Week-2/Task-1/vanilla/index.html @@ -1,11 +1,17 @@ - - - Task 1 — Material Variant Switcher + + Material Variant Switcher - + +
+ + + +
+ + \ No newline at end of file diff --git a/Week-2/Task-1/vanilla/main.js b/Week-2/Task-1/vanilla/main.js index e69de29..7e3da1e 100644 --- a/Week-2/Task-1/vanilla/main.js +++ b/Week-2/Task-1/vanilla/main.js @@ -0,0 +1,80 @@ +import * as THREE from "three"; +import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js"; + +const scene = new THREE.Scene(); +scene.background = new THREE.Color("white"); + +const camera = new THREE.PerspectiveCamera( + 75, + window.innerWidth / window.innerHeight, + 0.1, + 1000 +); +camera.position.z = 5; + +const renderer = new THREE.WebGLRenderer(); +renderer.setSize(window.innerWidth, window.innerHeight); +document.body.appendChild(renderer.domElement); + +const geometry = new THREE.TorusKnotGeometry(1.5, 0.5, 100, 16); + +const materials = { + black: new THREE.MeshStandardMaterial({ + color: "black", + roughness: 1, + metalness: 0, + }), + silver: new THREE.MeshStandardMaterial({ + color: "#C0C0C0", + roughness: 0.3, + metalness: 1, + }), + gold: new THREE.MeshStandardMaterial({ + color: "#FFD700", + roughness: 0.4, + metalness: 1, + }), +}; + +const sphere = new THREE.Mesh(geometry, materials.black); +scene.add(sphere); + +const ambientLight = new THREE.AmbientLight(0xffffff, 0.6); +scene.add(ambientLight); + +const pointLight = new THREE.PointLight(0xffffff, 1); +pointLight.position.set(5, 5, 5); +scene.add(pointLight); + +const controls = new OrbitControls(camera, renderer.domElement); +controls.enableDamping = true; + +const buttons = document.querySelectorAll("button"); + +buttons.forEach((btn) => { + btn.addEventListener("click", () => { + const variant = btn.getAttribute("data-variant"); + + sphere.material = materials[variant]; + + buttons.forEach((b) => b.classList.remove("active")); + btn.classList.add("active"); + }); +}); + +function animate() { + requestAnimationFrame(animate); + + controls.update(); + + sphere.rotation.y += 0.01; + + renderer.render(scene, camera); +} +animate(); + +window.addEventListener("resize", () => { + camera.aspect = window.innerWidth / window.innerHeight; + camera.updateProjectionMatrix(); + renderer.setSize(window.innerWidth, window.innerHeight); +}); \ No newline at end of file diff --git a/Week-2/Task-1/vanilla/style.css b/Week-2/Task-1/vanilla/style.css new file mode 100644 index 0000000..e69de29