@@ -9,31 +9,28 @@ var FlyControls = function ( object, domElement ) {
9
9
this . object = object ;
10
10
11
11
this . domElement = ( domElement !== undefined ) ? domElement : document ;
12
- if ( domElement ) this . domElement . setAttribute ( 'tabindex' , - 1 ) ;
13
12
14
13
// API
15
14
15
+ // Set to false to disable this control
16
+ this . enabled = true ;
17
+
16
18
this . movementSpeed = 1.0 ;
17
19
this . rollSpeed = 0.05 ;
18
20
19
- this . dragToLook = false ;
20
- this . autoForward = false ;
21
-
22
- // disable default target object behavior
21
+ this . moveVector = new THREE . Vector3 ( 0 , 0 , 0 ) ;
22
+ this . rotationVector = new THREE . Vector3 ( 0 , 0 , 0 ) ;
23
23
24
24
// internals
25
25
26
26
this . tmpQuaternion = new THREE . Quaternion ( ) ;
27
27
var lastPosition = new THREE . Vector3 ( ) ;
28
28
var lastQuaternion = new THREE . Quaternion ( ) ;
29
- var scope = this ;
29
+ var scope = this ;
30
+ var EPS = 0.000001 ;
30
31
31
32
this . mouseStatus = 0 ;
32
33
33
- this . moveState = { up : 0 , down : 0 , left : 0 , right : 0 , forward : 0 , back : 0 , pitchUp : 0 , pitchDown : 0 , yawLeft : 0 , yawRight : 0 , rollLeft : 0 , rollRight : 0 } ;
34
- this . moveVector = new THREE . Vector3 ( 0 , 0 , 0 ) ;
35
- this . rotationVector = new THREE . Vector3 ( 0 , 0 , 0 ) ;
36
-
37
34
this . handleEvent = function ( event ) {
38
35
if ( typeof this [ event . type ] == 'function' ) {
39
36
this [ event . type ] ( event ) ;
@@ -45,48 +42,30 @@ var FlyControls = function ( object, domElement ) {
45
42
var moveMult = delta * this . movementSpeed ;
46
43
var rotMult = delta * this . rollSpeed ;
47
44
48
- this . object . translateX ( this . moveVector . x * moveMult ) ;
49
- this . object . translateY ( this . moveVector . y * moveMult ) ;
50
- this . object . translateZ ( this . moveVector . z * moveMult ) ;
45
+ this . object . position . addScaledVector ( this . moveVector , moveMult ) ;
51
46
52
47
this . tmpQuaternion . set ( this . rotationVector . x * rotMult , this . rotationVector . y * rotMult , this . rotationVector . z * rotMult , 1 ) . normalize ( ) ;
53
48
this . object . quaternion . multiply ( this . tmpQuaternion ) ;
54
49
55
50
// expose the rotation vector for convenience
56
51
this . object . rotation . setFromQuaternion ( this . object . quaternion , this . object . rotation . order ) ;
57
- this . dispatchEvent ( changeEvent ) ;
58
- lastPosition . copy ( this . object . position ) ;
59
- lastQuaternion . copy ( this . object . position ) ;
60
- } ;
52
+ if ( lastPosition . distanceToSquared ( this . object . position ) > EPS ||
53
+ 8 * ( 1 - lastQuaternion . dot ( this . object . quaternion ) ) > EPS ) {
61
54
62
- this . updateMovementVector = function ( ) {
63
- this . moveVector . x = ( - this . moveState . left + this . moveState . right ) ;
64
- this . moveVector . y = ( - this . moveState . down + this . moveState . up ) ;
65
- this . moveVector . z = ( - this . moveState . forward + this . moveState . back ) ;
66
- } ;
67
-
68
- this . updateRotationVector = function ( ) {
69
- this . rotationVector . x = ( - this . moveState . pitchDown + this . moveState . pitchUp ) ;
70
- this . rotationVector . y = ( - this . moveState . yawRight + this . moveState . yawLeft ) ;
71
- this . rotationVector . z = ( - this . moveState . rollRight + this . moveState . rollLeft ) ;
72
- } ;
73
-
74
- this . getContainerDimensions = function ( ) {
75
- if ( this . domElement != document ) {
76
- return {
77
- size : [ this . domElement . offsetWidth , this . domElement . offsetHeight ] ,
78
- offset : [ this . domElement . offsetLeft , this . domElement . offsetTop ]
79
- } ;
80
- } else {
81
- return {
82
- size : [ window . innerWidth , window . innerHeight ] ,
83
- offset : [ 0 , 0 ]
84
- } ;
55
+ this . dispatchEvent ( changeEvent ) ;
56
+ lastPosition . copy ( this . object . position ) ;
57
+ lastQuaternion . copy ( this . object . quaternion ) ;
58
+ return true ;
85
59
}
60
+ return false ;
86
61
} ;
87
62
88
63
this . dispose = function ( ) { } ;
89
- this . connectEvents = function ( ) { } ;
64
+ this . connectEvents = function ( element ) {
65
+ if ( element ) {
66
+ scope . domElement = element ;
67
+ }
68
+ } ;
90
69
91
70
function bind ( scope , fn ) {
92
71
return function ( ) {
@@ -98,9 +77,8 @@ var FlyControls = function ( object, domElement ) {
98
77
99
78
var changeEvent = { type : 'change' } ;
100
79
101
- this . updateMovementVector ( ) ;
102
- this . updateRotationVector ( ) ;
103
-
80
+ // Initialize lastPosition/Quaternion to initial values.
81
+ this . update ( 0 ) ;
104
82
} ;
105
83
106
84
FlyControls . prototype = Object . create ( THREE . EventDispatcher . prototype ) ;
0 commit comments