浏览代码

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 年之前
父节点
当前提交
65a7e2823e
共有 1 个文件被更改,包括 31 次插入1 次删除
  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() ];