import * as THREE from 'three';
|
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
|
|
export default class ShelfThree {
|
constructor(container) {
|
this.container = container;
|
this.scene = null;
|
this.camera = null;
|
this.renderer = null;
|
this.controls = null;
|
this.animationFrameId = null;
|
}
|
|
startup() {
|
this.initScene();
|
this.initCamera();
|
this.initRenderer();
|
this.initControls();
|
this.addLights();
|
this.addObjects();
|
this.animate();
|
window.addEventListener('resize', this.onWindowResize, false);
|
}
|
|
initScene() {
|
this.scene = new THREE.Scene();
|
this.scene.background = new THREE.Color(0x7a7a7a);
|
}
|
|
initCamera() {
|
const width = this.container.clientWidth;
|
const height = this.container.clientHeight;
|
this.camera = new THREE.PerspectiveCamera(70, width / height, 0.1, 1000);
|
this.camera.position.set(0, 5, 10);
|
}
|
|
initRenderer() {
|
this.renderer = new THREE.WebGLRenderer({ antialias: true });
|
this.renderer.setSize(this.container.clientWidth, this.container.clientHeight);
|
this.container.appendChild(this.renderer.domElement);
|
}
|
|
initControls() {
|
this.controls = new OrbitControls(this.camera, this.renderer.domElement);
|
this.controls.enableDamping = true;
|
}
|
|
addLights() {
|
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
|
this.scene.add(ambientLight);
|
|
const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
|
directionalLight.position.set(5, 10, 7.5);
|
this.scene.add(directionalLight);
|
}
|
|
addObjects() {
|
// 在这里添加您的货架模型或其他对象
|
const geometry = new THREE.BoxGeometry(1, 1, 1);
|
const material = new THREE.MeshStandardMaterial({ color: 0x4680BF });
|
const cube = new THREE.Mesh(geometry, material);
|
this.scene.add(cube);
|
}
|
|
animate = () => {
|
this.animationFrameId = requestAnimationFrame(this.animate);
|
this.controls.update();
|
this.renderer.render(this.scene, this.camera);
|
};
|
|
onWindowResize = () => {
|
const width = this.container.clientWidth;
|
const height = this.container.clientHeight;
|
this.camera.aspect = width / height;
|
this.camera.updateProjectionMatrix();
|
this.renderer.setSize(width, height);
|
};
|
|
destroy() {
|
cancelAnimationFrame(this.animationFrameId);
|
window.removeEventListener('resize', this.onWindowResize);
|
this.controls.dispose();
|
this.renderer.dispose();
|
this.scene = null;
|
this.camera = null;
|
this.renderer.domElement.remove();
|
}
|
}
|