|
| 1 | +/** |
| 2 | + * @author James Baicoianu / http://www.baicoianu.com/ |
| 3 | + */ |
| 4 | + |
| 5 | +THREE.FlyControls = function ( object, domElement ) { |
| 6 | + |
| 7 | + this.object = object; |
| 8 | + |
| 9 | + this.domElement = ( domElement !== undefined ) ? domElement : document; |
| 10 | + if ( domElement ) this.domElement.setAttribute( 'tabindex', -1 ); |
| 11 | + |
| 12 | + // API |
| 13 | + |
| 14 | + this.movementSpeed = 1.0; |
| 15 | + this.rollSpeed = 0.005; |
| 16 | + |
| 17 | + this.dragToLook = false; |
| 18 | + this.autoForward = false; |
| 19 | + |
| 20 | + // disable default target object behavior |
| 21 | + |
| 22 | + // internals |
| 23 | + |
| 24 | + this.tmpQuaternion = new THREE.Quaternion(); |
| 25 | + |
| 26 | + this.mouseStatus = 0; |
| 27 | + |
| 28 | + 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 }; |
| 29 | + this.moveVector = new THREE.Vector3( 0, 0, 0 ); |
| 30 | + this.rotationVector = new THREE.Vector3( 0, 0, 0 ); |
| 31 | + |
| 32 | + this.handleEvent = function ( event ) { |
| 33 | + |
| 34 | + if ( typeof this[ event.type ] == 'function' ) { |
| 35 | + |
| 36 | + this[ event.type ]( event ); |
| 37 | + |
| 38 | + } |
| 39 | + |
| 40 | + }; |
| 41 | + |
| 42 | + this.keydown = function( event ) { |
| 43 | + |
| 44 | + if ( event.altKey ) { |
| 45 | + |
| 46 | + return; |
| 47 | + |
| 48 | + } |
| 49 | + |
| 50 | + //event.preventDefault(); |
| 51 | + |
| 52 | + switch ( event.keyCode ) { |
| 53 | + |
| 54 | + case 16: /* shift */ this.movementSpeedMultiplier = .1; break; |
| 55 | + |
| 56 | + case 87: /*W*/ this.moveState.forward = 1; break; |
| 57 | + case 83: /*S*/ this.moveState.back = 1; break; |
| 58 | + |
| 59 | + case 65: /*A*/ this.moveState.left = 1; break; |
| 60 | + case 68: /*D*/ this.moveState.right = 1; break; |
| 61 | + |
| 62 | + case 82: /*R*/ this.moveState.up = 1; break; |
| 63 | + case 70: /*F*/ this.moveState.down = 1; break; |
| 64 | + |
| 65 | + case 38: /*up*/ this.moveState.pitchUp = 1; break; |
| 66 | + case 40: /*down*/ this.moveState.pitchDown = 1; break; |
| 67 | + |
| 68 | + case 37: /*left*/ this.moveState.yawLeft = 1; break; |
| 69 | + case 39: /*right*/ this.moveState.yawRight = 1; break; |
| 70 | + |
| 71 | + case 81: /*Q*/ this.moveState.rollLeft = 1; break; |
| 72 | + case 69: /*E*/ this.moveState.rollRight = 1; break; |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + this.updateMovementVector(); |
| 77 | + this.updateRotationVector(); |
| 78 | + |
| 79 | + }; |
| 80 | + |
| 81 | + this.keyup = function( event ) { |
| 82 | + |
| 83 | + switch( event.keyCode ) { |
| 84 | + |
| 85 | + case 16: /* shift */ this.movementSpeedMultiplier = 1; break; |
| 86 | + |
| 87 | + case 87: /*W*/ this.moveState.forward = 0; break; |
| 88 | + case 83: /*S*/ this.moveState.back = 0; break; |
| 89 | + |
| 90 | + case 65: /*A*/ this.moveState.left = 0; break; |
| 91 | + case 68: /*D*/ this.moveState.right = 0; break; |
| 92 | + |
| 93 | + case 82: /*R*/ this.moveState.up = 0; break; |
| 94 | + case 70: /*F*/ this.moveState.down = 0; break; |
| 95 | + |
| 96 | + case 38: /*up*/ this.moveState.pitchUp = 0; break; |
| 97 | + case 40: /*down*/ this.moveState.pitchDown = 0; break; |
| 98 | + |
| 99 | + case 37: /*left*/ this.moveState.yawLeft = 0; break; |
| 100 | + case 39: /*right*/ this.moveState.yawRight = 0; break; |
| 101 | + |
| 102 | + case 81: /*Q*/ this.moveState.rollLeft = 0; break; |
| 103 | + case 69: /*E*/ this.moveState.rollRight = 0; break; |
| 104 | + |
| 105 | + } |
| 106 | + |
| 107 | + this.updateMovementVector(); |
| 108 | + this.updateRotationVector(); |
| 109 | + |
| 110 | + }; |
| 111 | + |
| 112 | + this.mousedown = function( event ) { |
| 113 | + |
| 114 | + if ( this.domElement !== document ) { |
| 115 | + |
| 116 | + this.domElement.focus(); |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | + event.preventDefault(); |
| 121 | + event.stopPropagation(); |
| 122 | + |
| 123 | + if ( this.dragToLook ) { |
| 124 | + |
| 125 | + this.mouseStatus ++; |
| 126 | + |
| 127 | + } else { |
| 128 | + |
| 129 | + switch ( event.button ) { |
| 130 | + |
| 131 | + case 0: this.moveState.forward = 1; break; |
| 132 | + case 2: this.moveState.back = 1; break; |
| 133 | + |
| 134 | + } |
| 135 | + |
| 136 | + this.updateMovementVector(); |
| 137 | + |
| 138 | + } |
| 139 | + |
| 140 | + }; |
| 141 | + |
| 142 | + this.mousemove = function( event ) { |
| 143 | + |
| 144 | + if ( !this.dragToLook || this.mouseStatus > 0 ) { |
| 145 | + |
| 146 | + var container = this.getContainerDimensions(); |
| 147 | + var halfWidth = container.size[ 0 ] / 2; |
| 148 | + var halfHeight = container.size[ 1 ] / 2; |
| 149 | + |
| 150 | + this.moveState.yawLeft = - ( ( event.pageX - container.offset[ 0 ] ) - halfWidth ) / halfWidth; |
| 151 | + this.moveState.pitchDown = ( ( event.pageY - container.offset[ 1 ] ) - halfHeight ) / halfHeight; |
| 152 | + |
| 153 | + this.updateRotationVector(); |
| 154 | + |
| 155 | + } |
| 156 | + |
| 157 | + }; |
| 158 | + |
| 159 | + this.mouseup = function( event ) { |
| 160 | + |
| 161 | + event.preventDefault(); |
| 162 | + event.stopPropagation(); |
| 163 | + |
| 164 | + if ( this.dragToLook ) { |
| 165 | + |
| 166 | + this.mouseStatus --; |
| 167 | + |
| 168 | + this.moveState.yawLeft = this.moveState.pitchDown = 0; |
| 169 | + |
| 170 | + } else { |
| 171 | + |
| 172 | + switch ( event.button ) { |
| 173 | + |
| 174 | + case 0: this.moveState.forward = 0; break; |
| 175 | + case 2: this.moveState.back = 0; break; |
| 176 | + |
| 177 | + } |
| 178 | + |
| 179 | + this.updateMovementVector(); |
| 180 | + |
| 181 | + } |
| 182 | + |
| 183 | + this.updateRotationVector(); |
| 184 | + |
| 185 | + }; |
| 186 | + |
| 187 | + this.update = function( delta ) { |
| 188 | + |
| 189 | + var moveMult = delta * this.movementSpeed; |
| 190 | + var rotMult = delta * this.rollSpeed; |
| 191 | + |
| 192 | + this.object.translateX( this.moveVector.x * moveMult ); |
| 193 | + this.object.translateY( this.moveVector.y * moveMult ); |
| 194 | + this.object.translateZ( this.moveVector.z * moveMult ); |
| 195 | + |
| 196 | + this.tmpQuaternion.set( this.rotationVector.x * rotMult, this.rotationVector.y * rotMult, this.rotationVector.z * rotMult, 1 ).normalize(); |
| 197 | + this.object.quaternion.multiply( this.tmpQuaternion ); |
| 198 | + |
| 199 | + // expose the rotation vector for convenience |
| 200 | + this.object.rotation.setFromQuaternion( this.object.quaternion, this.object.rotation.order ); |
| 201 | + |
| 202 | + |
| 203 | + }; |
| 204 | + |
| 205 | + this.updateMovementVector = function() { |
| 206 | + |
| 207 | + var forward = ( this.moveState.forward || ( this.autoForward && !this.moveState.back ) ) ? 1 : 0; |
| 208 | + |
| 209 | + this.moveVector.x = ( -this.moveState.left + this.moveState.right ); |
| 210 | + this.moveVector.y = ( -this.moveState.down + this.moveState.up ); |
| 211 | + this.moveVector.z = ( -forward + this.moveState.back ); |
| 212 | + |
| 213 | + //console.log( 'move:', [ this.moveVector.x, this.moveVector.y, this.moveVector.z ] ); |
| 214 | + |
| 215 | + }; |
| 216 | + |
| 217 | + this.updateRotationVector = function() { |
| 218 | + |
| 219 | + this.rotationVector.x = ( -this.moveState.pitchDown + this.moveState.pitchUp ); |
| 220 | + this.rotationVector.y = ( -this.moveState.yawRight + this.moveState.yawLeft ); |
| 221 | + this.rotationVector.z = ( -this.moveState.rollRight + this.moveState.rollLeft ); |
| 222 | + |
| 223 | + //console.log( 'rotate:', [ this.rotationVector.x, this.rotationVector.y, this.rotationVector.z ] ); |
| 224 | + |
| 225 | + }; |
| 226 | + |
| 227 | + this.getContainerDimensions = function() { |
| 228 | + |
| 229 | + if ( this.domElement != document ) { |
| 230 | + |
| 231 | + return { |
| 232 | + size : [ this.domElement.offsetWidth, this.domElement.offsetHeight ], |
| 233 | + offset : [ this.domElement.offsetLeft, this.domElement.offsetTop ] |
| 234 | + }; |
| 235 | + |
| 236 | + } else { |
| 237 | + |
| 238 | + return { |
| 239 | + size : [ window.innerWidth, window.innerHeight ], |
| 240 | + offset : [ 0, 0 ] |
| 241 | + }; |
| 242 | + |
| 243 | + } |
| 244 | + |
| 245 | + }; |
| 246 | + |
| 247 | + function bind( scope, fn ) { |
| 248 | + |
| 249 | + return function () { |
| 250 | + |
| 251 | + fn.apply( scope, arguments ); |
| 252 | + |
| 253 | + }; |
| 254 | + |
| 255 | + }; |
| 256 | + |
| 257 | + this.domElement.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false ); |
| 258 | + |
| 259 | + this.domElement.addEventListener( 'mousemove', bind( this, this.mousemove ), false ); |
| 260 | + this.domElement.addEventListener( 'mousedown', bind( this, this.mousedown ), false ); |
| 261 | + this.domElement.addEventListener( 'mouseup', bind( this, this.mouseup ), false ); |
| 262 | + |
| 263 | + window.addEventListener( 'keydown', bind( this, this.keydown ), false ); |
| 264 | + window.addEventListener( 'keyup', bind( this, this.keyup ), false ); |
| 265 | + |
| 266 | + this.updateMovementVector(); |
| 267 | + this.updateRotationVector(); |
| 268 | + |
| 269 | +}; |
0 commit comments