Browse Source

Focus will frame target in view. Object will only translate to focus on target, will not rotate. Fixed a bug where empty's couldn't be focused on.

Adam 7 years ago
parent
commit
65a7e2823e
1 changed files with 31 additions and 1 deletions
  1. 31 1
      examples/js/controls/EditorControls.js

+ 31 - 1
examples/js/controls/EditorControls.js

@@ -37,8 +37,36 @@ THREE.EditorControls = function ( object, domElement ) {
 
 	this.focus = function ( target ) {
 
+		if ( target === undefined || !target.isObject3D ) {
+			return;
+		}
+
 		var box = new THREE.Box3().setFromObject( target );
-		object.lookAt( center.copy( box.getCenter() ) );
+
+		var targetDistance;
+
+		if ( box.isEmpty() ) {
+
+			// Focusing on an empty such as a light.
+
+			target.getWorldPosition( center );
+			targetDistance = 0.5;
+
+		} else {
+
+			center.copy( box.getCenter() );
+			targetDistance = box.getBoundingSphere().radius * 6;
+
+		}
+
+		var forwards = object.getWorldDirection().normalize();
+
+		var targetDelta = forwards.multiplyScalar( -targetDistance );
+
+		var targetWorldPosition = targetDelta.add( center );
+
+		object.position.copy( targetWorldPosition );
+
 		scope.dispatchEvent( changeEvent );
 
 	};
@@ -199,6 +227,8 @@ THREE.EditorControls = function ( object, domElement ) {
 
 	// touch
 
+	var touch = new THREE.Vector3();
+
 	var touches = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
 	var prevTouches = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];