feat: Task 1 - Material Variant Switcher (Vanilla)
This commit is contained in:
parent
18774bbd88
commit
044ee26de8
@ -1,11 +1,17 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<title>Material Variant Switcher</title>
|
||||||
<title>Task 1 — Material Variant Switcher</title>
|
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<script type="module" src="main.js"></script>
|
|
||||||
|
<div id="controls">
|
||||||
|
<button data-variant="black" class="active">Black</button>
|
||||||
|
<button data-variant="silver">Silver</button>
|
||||||
|
<button data-variant="gold">Gold</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script type="module" src="./main.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
@ -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);
|
||||||
|
});
|
||||||
0
Week-2/Task-1/vanilla/style.css
Normal file
0
Week-2/Task-1/vanilla/style.css
Normal file
Loading…
x
Reference in New Issue
Block a user