瀏覽代碼

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 年之前
父節點
當前提交
f56fef8d97
共有 1 個文件被更改,包括 29 次插入1 次删除
  1. 29 1
      examples/js/controls/EditorControls.js

+ 29 - 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 );
 
 	};