| | |
| | | import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls' |
| | | import TWEEN from '@tweenjs/tween.js'; |
| | | |
| | | const help = true; |
| | | const debugCamera = { |
| | | x: 200, |
| | | y: 200, |
| | | z: 200 |
| | | } |
| | | const help = false; |
| | | |
| | | export default class ShelfThree { |
| | | |
| | |
| | | this.stats = this.initStats(container); |
| | | this.windowResize(); |
| | | this.initRaycaster(container); |
| | | // initLight(this.scene); |
| | | initLight(this.scene); |
| | | |
| | | this.objects = []; |
| | | } |
| | |
| | | } |
| | | |
| | | animate = () => { |
| | | requestAnimationFrame(this.animate); |
| | | this.animationFrame = requestAnimationFrame(this.animate); |
| | | this.render(); |
| | | TWEEN.update(); |
| | | } |
| | | |
| | | render = () => { |
| | | console.log(1); |
| | | this.renderer.shadowMap.enabled = true; |
| | | this.renderer.render(this.scene, this.camera); |
| | | this.stats.update(); |
| | |
| | | } |
| | | |
| | | initCamera = () => { |
| | | const camera = new THREE.PerspectiveCamera(70, this.fullWidth / this.fullHeight, 1, 10000); |
| | | camera.position.set(debugCamera.x, debugCamera.y, debugCamera.z); |
| | | const camera = new THREE.PerspectiveCamera(70, this.fullWidth / this.fullHeight, 1, 60000); |
| | | camera.position.set(200, 200, 200); |
| | | this.scene.add(camera); |
| | | return camera; |
| | | } |
| | |
| | | } |
| | | |
| | | windowResize = () => { |
| | | let that = this; |
| | | window.addEventListener('resize', function () { |
| | | that.camera.aspect = this.fullWidth / this.fullHeight; |
| | | that.camera.updateProjectionMatrix(); |
| | | that.renderer.setSize(that.fullWidth, that.fullHeight); |
| | | }, false); |
| | | this.resizeHandler = () => { |
| | | this.camera.aspect = this.fullWidth / this.fullHeight; |
| | | this.camera.updateProjectionMatrix(); |
| | | this.renderer.setSize(this.fullWidth, this.fullHeight); |
| | | }; |
| | | window.addEventListener('resize', this.resizeHandler, false); |
| | | } |
| | | |
| | | initRaycaster = (container) => { |
| | |
| | | handleClick = () => { |
| | | |
| | | } |
| | | |
| | | destroy = () => { |
| | | cancelAnimationFrame(this.animationFrame); |
| | | window.removeEventListener('resize', this.resizeHandler); |
| | | this.renderer.dispose(); |
| | | this.controls.dispose(); |
| | | } |
| | | }; |
| | | |
| | | const initLight = (scene) => { |
| | |
| | | scene.add(ambientLight); |
| | | |
| | | const spotLight = new THREE.SpotLight(0xffffff, 8); |
| | | spotLight.position.set(0, 300, 0); |
| | | spotLight.angle = Math.PI * 1; |
| | | spotLight.decay = 0; |
| | | spotLight.position.set(0, 1000, 0); |
| | | spotLight.angle = Math.PI / 4; // 角度 |
| | | spotLight.distance = 1500; // 距离 |
| | | spotLight.decay = 0; // 光衰 |
| | | // 光照阴影及其生效范围 |
| | | spotLight.castShadow = true; |
| | | spotLight.shadow.camera.near = 200; |
| | | spotLight.shadow.camera.near = 50; |
| | | spotLight.shadow.camera.far = 2000; |
| | | spotLight.shadow.bias = - 0.000222; |
| | | // 阴影特效高低值 |
| | | spotLight.shadow.mapSize.width = 1024; |
| | | spotLight.shadow.mapSize.height = 1024; |
| | | scene.add(spotLight); |