提交 503f08fa 编写于 作者: A alteredq

Brought back old TrackballControls. Sorry @mrdoob :/

At least till we don't have this solved somehow better I think we should keep the old one. New one makes everything using it feeling broken, it's distracting.

Moved new event-driven `TrackballControls` into `TrackballControlsExperimental`. For the moment used just in "misc_camera_trackball.html" example.
上级 b6e3d573
因为 它太大了无法显示 source diff 。你可以改为 查看blob
此差异已折叠。
......@@ -38,6 +38,7 @@
</div>
<script src="../build/Three.js"></script>
<script src="../src/extras/controls/TrackballControlsExperimental.js"></script>
<script src="js/Detector.js"></script>
......
......@@ -261,6 +261,8 @@
light6.position.x = Math.cos( time * 0.7 ) * d;
light6.position.z = Math.cos( time * 0.5 ) * d;
controls.update();
renderer.render( scene, camera );
}
......
......@@ -44,7 +44,8 @@
<script src="js/ctm/CTMLoader.js"></script>
<script src="js/Detector.js"></script>
<script src="js/Stats.js"></script>
<script>
if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
......@@ -53,7 +54,7 @@
var SCREEN_HEIGHT = window.innerHeight;
var FLOOR = -250;
var container;
var container, stats;
var camera, scene, controls;
var renderer;
......@@ -71,6 +72,7 @@
var windowHalfY = window.innerHeight / 2;
init();
animate();
function init() {
......@@ -89,7 +91,7 @@
controls = new THREE.TrackballControls( camera );
controls.dynamicDampingFactor = 0.25;
controls.addEventListener( 'update', render );
// SKYBOX
......@@ -103,7 +105,7 @@
r + "pz.png", r + "nz.png" ];
textureCube = THREE.ImageUtils.loadTextureCube( urls, new THREE.UVMapping(), render );
textureCube = THREE.ImageUtils.loadTextureCube( urls );
var shader = THREE.ShaderUtils.lib[ "cube" ];
shader.uniforms[ "tCube" ].texture = textureCube;
......@@ -155,6 +157,18 @@
renderer.gammaOutput = true;
renderer.physicallyBasedShading = true;
// STATS
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '5px';
stats.domElement.style.zIndex = 100;
container.appendChild( stats.domElement );
stats.domElement.children[ 0 ].children[ 0 ].style.color = "#aaa";
stats.domElement.children[ 0 ].style.background = "transparent";
stats.domElement.children[ 0 ].children[ 1 ].style.display = "none";
// EVENTS
window.addEventListener( 'resize', onWindowResize, false );
......@@ -290,8 +304,6 @@
mesh.scale.set( s, s, s );
mesh.doubleSided = true;
scene.add( mesh );
render();
}
......@@ -310,8 +322,6 @@
cameraCube.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
cameraCube.updateProjectionMatrix();
render();
}
......@@ -324,8 +334,19 @@
//
function animate() {
requestAnimationFrame( animate );
render();
stats.update();
}
function render() {
controls.update();
cameraCube.rotation.copy( camera.rotation );
renderer.clear();
......
......@@ -383,6 +383,7 @@
var delta = clock.getDelta();
controls.update();
character.update( delta );
renderer.render( scene, camera );
......
......@@ -550,6 +550,7 @@
var delta = 1000 * clock.getDelta();
TWEEN.update();
controls.update();
if ( morph ) morph.updateAnimation( delta );
......
......@@ -738,6 +738,8 @@
}
controls.update();
//renderer.render( scene, camera );
composer.render( 0.1 );
......
......@@ -287,6 +287,8 @@
);
meshMoon.rotation.y -= angle;
controls.update();
renderer.clear();
renderer.render( scene, camera );
......
......@@ -4,8 +4,6 @@
THREE.TrackballControls = function ( object, domElement ) {
THREE.EventTarget.call( this );
var _this = this,
STATE = { NONE : -1, ROTATE : 0, ZOOM : 1, PAN : 2 };
......@@ -208,6 +206,37 @@ THREE.TrackballControls = function ( object, domElement ) {
};
this.update = function() {
_eye.copy( _this.object.position ).subSelf( this.target );
if ( !_this.noRotate ) {
_this.rotateCamera();
}
if ( !_this.noZoom ) {
_this.zoomCamera();
}
if ( !_this.noPan ) {
_this.panCamera();
}
_this.object.position.add( _this.target, _eye );
_this.checkDistances();
_this.object.lookAt( _this.target );
};
// listeners
function keydown( event ) {
......@@ -313,8 +342,6 @@ THREE.TrackballControls = function ( object, domElement ) {
}
update();
};
function mouseup( event ) {
......@@ -328,40 +355,6 @@ THREE.TrackballControls = function ( object, domElement ) {
};
//
function update() {
_eye.copy( _this.object.position ).subSelf( _this.target );
if ( !_this.noRotate ) {
_this.rotateCamera();
}
if ( !_this.noZoom ) {
_this.zoomCamera();
}
if ( !_this.noPan ) {
_this.panCamera();
}
_this.object.position.add( _this.target, _eye );
_this.checkDistances();
_this.object.lookAt( _this.target );
_this.dispatchEvent( { type: 'update' } );
};
this.domElement.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false );
this.domElement.addEventListener( 'mousemove', mousemove, false );
......@@ -371,7 +364,4 @@ THREE.TrackballControls = function ( object, domElement ) {
window.addEventListener( 'keydown', keydown, false );
window.addEventListener( 'keyup', keyup, false );
// TODO: Clean up the code so this isn't needed.
update();
};
};
\ No newline at end of file
/**
* @author Eberhard Graether / http://egraether.com/
*/
THREE.TrackballControls = function ( object, domElement ) {
THREE.EventTarget.call( this );
var _this = this,
STATE = { NONE : -1, ROTATE : 0, ZOOM : 1, PAN : 2 };
this.object = object;
this.domElement = ( domElement !== undefined ) ? domElement : document;
// API
this.enabled = true;
this.screen = { width: window.innerWidth, height: window.innerHeight, offsetLeft: 0, offsetTop: 0 };
this.radius = ( this.screen.width + this.screen.height ) / 4;
this.rotateSpeed = 1.0;
this.zoomSpeed = 1.2;
this.panSpeed = 0.3;
this.noRotate = false;
this.noZoom = false;
this.noPan = false;
this.staticMoving = false;
this.dynamicDampingFactor = 0.2;
this.minDistance = 0;
this.maxDistance = Infinity;
this.keys = [ 65 /*A*/, 83 /*S*/, 68 /*D*/ ];
// internals
this.target = new THREE.Vector3( 0, 0, 0 );
var _keyPressed = false,
_state = STATE.NONE,
_eye = new THREE.Vector3(),
_rotateStart = new THREE.Vector3(),
_rotateEnd = new THREE.Vector3(),
_zoomStart = new THREE.Vector2(),
_zoomEnd = new THREE.Vector2(),
_panStart = new THREE.Vector2(),
_panEnd = new THREE.Vector2();
// methods
this.handleEvent = function ( event ) {
if ( typeof this[ event.type ] == 'function' ) {
this[ event.type ]( event );
}
};
this.getMouseOnScreen = function( clientX, clientY ) {
return new THREE.Vector2(
( clientX - _this.screen.offsetLeft ) / _this.radius * 0.5,
( clientY - _this.screen.offsetTop ) / _this.radius * 0.5
);
};
this.getMouseProjectionOnBall = function( clientX, clientY ) {
var mouseOnBall = new THREE.Vector3(
( clientX - _this.screen.width * 0.5 - _this.screen.offsetLeft ) / _this.radius,
( _this.screen.height * 0.5 + _this.screen.offsetTop - clientY ) / _this.radius,
0.0
);
var length = mouseOnBall.length();
if ( length > 1.0 ) {
mouseOnBall.normalize();
} else {
mouseOnBall.z = Math.sqrt( 1.0 - length * length );
}
_eye.copy( _this.object.position ).subSelf( _this.target );
var projection = _this.object.up.clone().setLength( mouseOnBall.y );
projection.addSelf( _this.object.up.clone().crossSelf( _eye ).setLength( mouseOnBall.x ) );
projection.addSelf( _eye.setLength( mouseOnBall.z ) );
return projection;
};
this.rotateCamera = function() {
var angle = Math.acos( _rotateStart.dot( _rotateEnd ) / _rotateStart.length() / _rotateEnd.length() );
if ( angle ) {
var axis = ( new THREE.Vector3() ).cross( _rotateStart, _rotateEnd ).normalize(),
quaternion = new THREE.Quaternion();
angle *= _this.rotateSpeed;
quaternion.setFromAxisAngle( axis, -angle );
quaternion.multiplyVector3( _eye );
quaternion.multiplyVector3( _this.object.up );
quaternion.multiplyVector3( _rotateEnd );
if ( _this.staticMoving ) {
_rotateStart = _rotateEnd;
} else {
quaternion.setFromAxisAngle( axis, angle * ( _this.dynamicDampingFactor - 1.0 ) );
quaternion.multiplyVector3( _rotateStart );
}
}
};
this.zoomCamera = function() {
var factor = 1.0 + ( _zoomEnd.y - _zoomStart.y ) * _this.zoomSpeed;
if ( factor !== 1.0 && factor > 0.0 ) {
_eye.multiplyScalar( factor );
if ( _this.staticMoving ) {
_zoomStart = _zoomEnd;
} else {
_zoomStart.y += ( _zoomEnd.y - _zoomStart.y ) * this.dynamicDampingFactor;
}
}
};
this.panCamera = function() {
var mouseChange = _panEnd.clone().subSelf( _panStart );
if ( mouseChange.lengthSq() ) {
mouseChange.multiplyScalar( _eye.length() * _this.panSpeed );
var pan = _eye.clone().crossSelf( _this.object.up ).setLength( mouseChange.x );
pan.addSelf( _this.object.up.clone().setLength( mouseChange.y ) );
_this.object.position.addSelf( pan );
_this.target.addSelf( pan );
if ( _this.staticMoving ) {
_panStart = _panEnd;
} else {
_panStart.addSelf( mouseChange.sub( _panEnd, _panStart ).multiplyScalar( _this.dynamicDampingFactor ) );
}
}
};
this.checkDistances = function() {
if ( !_this.noZoom || !_this.noPan ) {
if ( _this.object.position.lengthSq() > _this.maxDistance * _this.maxDistance ) {
_this.object.position.setLength( _this.maxDistance );
}
if ( _eye.lengthSq() < _this.minDistance * _this.minDistance ) {
_this.object.position.add( _this.target, _eye.setLength( _this.minDistance ) );
}
}
};
// listeners
function keydown( event ) {
if ( ! _this.enabled ) return;
if ( _state !== STATE.NONE ) {
return;
} else if ( event.keyCode === _this.keys[ STATE.ROTATE ] && !_this.noRotate ) {
_state = STATE.ROTATE;
} else if ( event.keyCode === _this.keys[ STATE.ZOOM ] && !_this.noZoom ) {
_state = STATE.ZOOM;
} else if ( event.keyCode === _this.keys[ STATE.PAN ] && !_this.noPan ) {
_state = STATE.PAN;
}
if ( _state !== STATE.NONE ) {
_keyPressed = true;
}
};
function keyup( event ) {
if ( ! _this.enabled ) return;
if ( _state !== STATE.NONE ) {
_state = STATE.NONE;
}
};
function mousedown( event ) {
if ( ! _this.enabled ) return;
event.preventDefault();
event.stopPropagation();
if ( _state === STATE.NONE ) {
_state = event.button;
if ( _state === STATE.ROTATE && !_this.noRotate ) {
_rotateStart = _rotateEnd = _this.getMouseProjectionOnBall( event.clientX, event.clientY );
} else if ( _state === STATE.ZOOM && !_this.noZoom ) {
_zoomStart = _zoomEnd = _this.getMouseOnScreen( event.clientX, event.clientY );
} else if ( !this.noPan ) {
_panStart = _panEnd = _this.getMouseOnScreen( event.clientX, event.clientY );
}
}
};
function mousemove( event ) {
if ( ! _this.enabled ) return;
if ( _keyPressed ) {
_rotateStart = _rotateEnd = _this.getMouseProjectionOnBall( event.clientX, event.clientY );
_zoomStart = _zoomEnd = _this.getMouseOnScreen( event.clientX, event.clientY );
_panStart = _panEnd = _this.getMouseOnScreen( event.clientX, event.clientY );
_keyPressed = false;
}
if ( _state === STATE.NONE ) {
return;
} else if ( _state === STATE.ROTATE && !_this.noRotate ) {
_rotateEnd = _this.getMouseProjectionOnBall( event.clientX, event.clientY );
} else if ( _state === STATE.ZOOM && !_this.noZoom ) {
_zoomEnd = _this.getMouseOnScreen( event.clientX, event.clientY );
} else if ( _state === STATE.PAN && !_this.noPan ) {
_panEnd = _this.getMouseOnScreen( event.clientX, event.clientY );
}
update();
};
function mouseup( event ) {
if ( ! _this.enabled ) return;
event.preventDefault();
event.stopPropagation();
_state = STATE.NONE;
};
//
function update() {
_eye.copy( _this.object.position ).subSelf( _this.target );
if ( !_this.noRotate ) {
_this.rotateCamera();
}
if ( !_this.noZoom ) {
_this.zoomCamera();
}
if ( !_this.noPan ) {
_this.panCamera();
}
_this.object.position.add( _this.target, _eye );
_this.checkDistances();
_this.object.lookAt( _this.target );
_this.dispatchEvent( { type: 'update' } );
};
this.domElement.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false );
this.domElement.addEventListener( 'mousemove', mousemove, false );
this.domElement.addEventListener( 'mousedown', mousedown, false );
this.domElement.addEventListener( 'mouseup', mouseup, false );
window.addEventListener( 'keydown', keydown, false );
window.addEventListener( 'keyup', keyup, false );
// TODO: Clean up the code so this isn't needed.
update();
};
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册