#
luxiaotao1123
2022-08-19 162eca31378c749b8f70c32ce2ed988b1101e881
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
 * originally from https://github.com/mrdoob/three.js/blob/dev/examples/js/controls/PointerLockControls.js
 * @author mrdoob / http://mrdoob.com/
 *
 * edited by Erich Loftis (erichlof on GitHub)
 * https://github.com/erichlof
 * Btw, this is the most consice and elegant way to implement first person camera rotation/movement that I've ever seen -
 * look at how short it is, without spaces/braces it would be around 30 lines!  Way to go, mrdoob!
 */
 
var FirstPersonCameraControls = function ( camera ) {
 
    camera.rotation.set( 0, 0, 0 );
 
    var pitchObject = new THREE.Object3D();
    pitchObject.add( camera );
 
    var yawObject = new THREE.Object3D();
    yawObject.add( pitchObject );
 
    var movementX = 0;
    var movementY = 0;
            
    var onMouseMove = function ( event ) {
 
        if (isPaused)
            return;
        movementX = event.movementX || event.mozMovementX || 0;
        movementY = event.movementY || event.mozMovementY || 0;
 
        yawObject.rotation.y -= movementX * 0.0012 * cameraRotationSpeed;
        pitchObject.rotation.x -= movementY * 0.001 * cameraRotationSpeed;
        // clamp the camera's vertical movement (around the x-axis) to the scene's 'ceiling' and 'floor'
        pitchObject.rotation.x = Math.max( - PI_2, Math.min( PI_2, pitchObject.rotation.x ) );
            
    };
 
    document.addEventListener( 'mousemove', onMouseMove, false );
    
 
    this.getObject = function () {
 
        return yawObject;
 
    };
    
    this.getYawObject = function () {
 
        return yawObject;
 
    };
    
    this.getPitchObject = function () {
 
        return pitchObject;
 
    };
 
    this.getDirection = function() {
 
        var te = pitchObject.matrixWorld.elements;
 
        return function( v ) {
            
            v.set( te[ 8 ], te[ 9 ], te[ 10 ] ).negate();
            
            return v;
 
        };
 
    }();
    
    this.getUpVector = function() {
 
        var te = pitchObject.matrixWorld.elements;
 
        return function( v ) {
            
            v.set( te[ 4 ], te[ 5 ], te[ 6 ] );
            
            return v;
 
        };
 
    }();
    
    this.getRightVector = function() {
 
        var te = pitchObject.matrixWorld.elements;
 
        return function( v ) {
            
            v.set( te[ 0 ], te[ 1 ], te[ 2 ] );
            
            return v;
 
        };
 
    }();
 
};