.
18516761980
2022-03-19 9ff5e64187837a3bbd78370c1cfaadf2329f97f0
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
 * @author mrdoob / http://mrdoob.com/
 * @author Mugen87 / https://github.com/Mugen87
 */
 
THREE.PointerLockControls = function ( camera, domElement ) {
 
    this.domElement = domElement || document.body;
    this.isLocked = false;
 
    //
    // internals
    //
 
    var scope = this;
 
    var changeEvent = { type: 'change' };
    var lockEvent = { type: 'lock' };
    var unlockEvent = { type: 'unlock' };
 
    var euler = new THREE.Euler( 0, 0, 0, 'YXZ' );
 
    var PI_2 = Math.PI / 2;
 
    function onMouseMove( event ) {
 
        if ( scope.isLocked === false ) return;
 
        var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
        var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
 
        euler.setFromQuaternion( camera.quaternion );
 
        euler.y -= movementX * 0.002;
        euler.x -= movementY * 0.002;
 
        euler.x = Math.max( - PI_2, Math.min( PI_2, euler.x ) );
 
        camera.quaternion.setFromEuler( euler );
 
        scope.dispatchEvent( changeEvent );
 
    }
 
    function onPointerlockChange() {
 
        if ( document.pointerLockElement === scope.domElement ) {
 
            scope.dispatchEvent( lockEvent );
 
            scope.isLocked = true;
 
        } else {
 
            scope.dispatchEvent( unlockEvent );
 
            scope.isLocked = false;
 
        }
 
    }
 
    function onPointerlockError() {
 
        console.error( 'THREE.PointerLockControls: Unable to use Pointer Lock API' );
 
    }
 
    this.connect = function () {
 
        document.addEventListener( 'mousemove', onMouseMove, false );
        document.addEventListener( 'pointerlockchange', onPointerlockChange, false );
        document.addEventListener( 'pointerlockerror', onPointerlockError, false );
 
    };
 
    this.disconnect = function () {
 
        document.removeEventListener( 'mousemove', onMouseMove, false );
        document.removeEventListener( 'pointerlockchange', onPointerlockChange, false );
        document.removeEventListener( 'pointerlockerror', onPointerlockError, false );
 
    };
 
    this.dispose = function () {
 
        this.disconnect();
 
    };
 
    this.getObject = function () { // retaining this method for backward compatibility
 
        return camera;
 
    };
 
    this.getDirection = function () {
 
        var direction = new THREE.Vector3( 0, 0, - 1 );
 
        return function ( v ) {
 
            return v.copy( direction ).applyQuaternion( camera.quaternion );
 
        };
 
    }();
 
    this.lock = function () {
 
        this.domElement.requestPointerLock();
 
    };
 
    this.unlock = function () {
 
        document.exitPointerLock();
 
    };
 
    this.connect();
 
};
 
THREE.PointerLockControls.prototype = Object.create( THREE.EventDispatcher.prototype );
THREE.PointerLockControls.prototype.constructor = THREE.PointerLockControls;