fuliqi
2024-04-19 fed41b2fd390ae729c05f63fcbc9f5e93cfd8f71
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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 = 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.1, 200);
        this.instance.position.set(0, 45, 45);
        this.scene.add(this.instance);
        // const help = new CameraHelper(this.instance);
        // this.scene.add(help);
    }
 
    setOrbitControls() {
        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.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;
    }
}