Created
June 17, 2016 18:44
-
-
Save floz/43c4ec4ac8785aa18edd2fb9f3e07e02 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const interactions = require( "fz/events/interactions" ) | |
const stage = require( "fz/core/stage" ) | |
const dgui = require( "fz/utils/dgui" ) | |
const timeout = require( "fz/utils/timeout" ) | |
class OrbitControls { | |
// camera = THREE.Camera | |
// target = THREE.Vector3 | |
constructor( camera, target, radius ) { | |
this.camera = camera | |
this.target = target || new THREE.Vector3() | |
this.targetBase = target || new THREE.Vector3() | |
this.radius = radius || this.camera.position.distanceTo( this.target ) || 1 | |
this._domEvents = document.getElementById( "three" ) | |
this._isDown = false | |
this.offset = new THREE.Vector3() | |
//Freeze camera | |
this._isFreeze = false; | |
this.cameraAddPosition = new THREE.Vector3(0,0,0); | |
this.speedMouse = 1 | |
this.speedMax = 1 | |
this.targetX = 0 | |
this.followTargetX = false | |
this.followTargetY = false | |
// this._canAutoRotate = false | |
// | |
// this.mouseElasticX = .1 | |
// this.mouseElasticY = .1 | |
this._xExplorationCurr = 0 | |
this._yExplorationCurr = 0 | |
this._xExplorationTo = 0 | |
this._yExplorationTo = 0 | |
this.friction = .968 | |
this.frictionVx = .0008 | |
this.frictionVy = .0006 | |
this.bouncing = true | |
this._posCurr = new THREE.Vector2() | |
this._posLast = new THREE.Vector2() | |
this._vx = 0 | |
this._vy = 0 | |
// this._phi = Math.PI * .5 | |
this._phi = Math.PI * .5 | |
this._theta = Math.PI * .5 | |
// this._theta = Math.PI * .25 | |
this.isPhiRestricted = false | |
this.minPhi = Math.PI / 8 | |
this.maxPhi = 1.7 | |
// this.maxPhi = 1.95 | |
this._binds = {} | |
this._binds.onDown = this._onDown.bind( this ) | |
this._binds.onMove = this._onMove.bind( this ) | |
this._binds.onUp = this._onUp.bind( this ) | |
// const fOrbitControl = dgui.addFolder( "CameraControls" ) | |
// fOrbitControl.add( this, "friction", .6, .968, .01 ).listen() | |
// fOrbitControl.add( this, "frictionVx", 0, .01, .00001 ).listen() | |
// fOrbitControl.add( this, "frictionVy", 0, .01, .00001 ).listen() | |
// fOrbitControl.add( this.offset, "x", -50, 50, .01 ).listen() | |
// fOrbitControl.add( this.offset, "y", -50, 50, .01 ).listen() | |
// fOrbitControl.add( this.offset, "z", -50, 50, .01 ).listen() | |
// fOrbitControl.close() | |
this.activate() | |
} | |
_onDown( e ) { | |
return | |
this._isDown = true | |
this._posCurr.set( e.x, e.y ) | |
this._posLast.set( e.x, e.y ) | |
interactions.on( this._domEvents, "up", this._binds.onUp ) | |
} | |
_onMove( e ) { | |
return | |
if( this._isDown ) { | |
this._posLast.copy( this._posCurr ) | |
this._posCurr.set( e.x, e.y ) | |
const diffX = ( this._posCurr.x - this._posLast.x ) * this.speedMouse | |
const diffY = ( this._posCurr.y - this._posLast.y ) * this.speedMouse | |
// this._vx -= diffX * .0008 | |
// this._vy += diffY * .0003 | |
this._vx -= diffX * this.frictionVx | |
this._vy += diffY * this.frictionVy | |
} | |
const swh = stage.width >> 1 | |
const shh = stage.height >> 1 | |
const dx = swh - e.x | |
const dy = shh - e.y | |
let xAdd = dx / swh | |
let yAdd = dy / shh | |
this._xExplorationTo = xAdd * .1 | |
this._yExplorationTo = yAdd * .1 | |
} | |
_onUp() { | |
this._isDown = false | |
interactions.off( this._domEvents, "up", this._binds.onUp ) | |
} | |
update() { | |
this._vx = Math.max( -this.speedMax, Math.min( this._vx, this.speedMax ) ) | |
this._vy = Math.max( -this.speedMax, Math.min( this._vy, this.speedMax ) ) | |
this._vx *= this.friction | |
this._vy *= this.friction | |
if( this.followTargetY ) { | |
this._phi += ( Math.PI * .5 - this._phi ) * .005 | |
} else { | |
this._phi -= this._vy | |
} | |
if( this.followTargetX ) { | |
this._theta += ( this.targetX - this._theta ) * .09 | |
} else { | |
this._theta -= this._vx | |
} | |
this._xExplorationCurr += ( this._xExplorationTo - this._xExplorationCurr ) * .09 | |
this._yExplorationCurr += ( this._yExplorationTo - this._yExplorationCurr ) * .09 | |
this._phi %= Math.PI * 2 | |
if( this._phi < 0 ) { | |
this._phi += Math.PI * 2 | |
} | |
this._theta %= Math.PI * 2 | |
if( this._theta < 0 ) { | |
this._theta += Math.PI * 2 | |
} | |
if( this.isPhiRestricted ) { | |
const targetPhi = Math.max( this.minPhi, Math.min( this.maxPhi, this._phi ) ) | |
this._phi += ( targetPhi-this._phi ) * .15 | |
} | |
this.camera.position.x = this.offset.x + this.target.x + this.radius * Math.sin( this._phi + this._yExplorationCurr ) * Math.cos( this._theta + this._xExplorationCurr ) | |
this.camera.position.y = this.offset.y + this.target.y + this.radius * Math.cos( this._phi + this._yExplorationCurr ) | |
this.camera.position.z = this.offset.z + this.target.z + this.radius * Math.sin( this._phi + this._yExplorationCurr ) * Math.sin( this._theta + this._xExplorationCurr ) | |
this.camera.lookAt( this.target ) | |
} | |
activate() { | |
interactions.on( this._domEvents, "down", this._binds.onDown ) | |
interactions.on( this._domEvents, "move", this._binds.onMove ) | |
} | |
} | |
module.exports = OrbitControls |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment