| | |
| | | import { MathUtils } from 'three'; |
| | | import { PerspectiveCamera, CameraHelper } from 'three'; |
| | | import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'; |
| | | |
| | |
| | | export default class Camera { |
| | | constructor(experience) { |
| | | this.experience = experience; |
| | | this.scene = this.experience.scene; |
| | | this.canvas = this.experience.canvas; |
| | | this.sizes = this.experience.sizes; |
| | | this.scene = experience.scene; |
| | | this.canvas = experience.canvas; |
| | | this.sizes = experience.sizes; |
| | | this.setInstance(); |
| | | this.setOrbitControls(); |
| | | |
| | | } |
| | | |
| | | // 设置透视相机 |
| | | setInstance() { |
| | | this.instance = new PerspectiveCamera(45,this.sizes.width / this.sizes.height, 0.001, 90000000); |
| | | this.instance.position.set(0, 45, 45); |
| | | this.instance = new PerspectiveCamera(45, this.sizes.width / this.sizes.height, 0.1, 200); |
| | | this.instance.position.set(0, 37, 37); |
| | | this.scene.add(this.instance); |
| | | // const help = new CameraHelper(this.instance); |
| | | // this.scene.add(help); |
| | |
| | | this.controls = new OrbitControls(this.instance, this.canvas); |
| | | this.controls.target.set(0, 0, 5); |
| | | this.controls.enableDamping = true; |
| | | this.controls.minDistance = 20; |
| | | this.controls.maxDistance = 80; |
| | | this.controls.maxPolarAngle = MathUtils.degToRad(80); |
| | | // this.controls.maxPolarAngle = (-Math.PI / 2); |
| | | } |
| | | |
| | | resize() { |
| | | // 重新计算比例 |
| | | this.cameraAspect = (this.sizes.width / this.sizes.height) * this.frustrum; |
| | | this.instance.left = -this.cameraAspect / 2; |
| | | this.instance.right = this.cameraAspect / 2; |
| | | this.instance.top = this.frustrum / 2; |
| | | this.instance.bottom = -this.frustrum / 2; |
| | | this.cameraAspect = this.sizes.width / this.sizes.height; |
| | | this.instance.updateProjectionMatrix(); |
| | | } |
| | | |
| | |
| | | update() { |
| | | this.controls.update(); |
| | | } |
| | | |
| | | destroy() { |
| | | this.disposeObject(); |
| | | this.removeObject(); |
| | | this.resetObject(); |
| | | } |
| | | |
| | | disposeObject() { |
| | | this.controls.dispose(); |
| | | } |
| | | |
| | | removeObject() { |
| | | this.scene.remove(this.instance); |
| | | } |
| | | resetObject() { |
| | | this.controls = null; |
| | | this.instance = null; |
| | | this.scene = null; |
| | | this.canvas = null; |
| | | this.sizes = null; |
| | | } |
| | | } |