92 lines
2.1 KiB
JavaScript

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 geometries = {
black: new THREE.BoxGeometry(2, 2, 2),
silver: new THREE.SphereGeometry(1.5, 32, 32),
gold: new THREE.TorusGeometry(1.2, 0.4, 16, 100),
};
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,
}),
};
let currentVariant = "black";
let mesh = new THREE.Mesh(
geometries[currentVariant],
materials[currentVariant]
);
scene.add(mesh);
scene.add(new THREE.AmbientLight(0xffffff, 0.6));
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;
function updateScene() {
mesh.geometry = geometries[currentVariant];
mesh.material = materials[currentVariant];
}
const buttons = document.querySelectorAll("button");
buttons.forEach((btn) => {
btn.addEventListener("click", () => {
const variant = btn.getAttribute("data-variant");
currentVariant = variant;
updateScene();
buttons.forEach((b) => b.classList.remove("active"));
btn.classList.add("active");
});
});
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
animate();
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});