浏览代码

incorporate feedback.

Ben Houston 9 年之前
父节点
当前提交
16d79724f1
共有 4 个文件被更改,包括 18 次插入21 次删除
  1. 2 2
      examples/js/controls/EditorControls.js
  2. 3 3
      examples/js/controls/OrbitControls.js
  3. 2 16
      src/math/Spherical.js
  4. 11 0
      src/math/Vector3.js

+ 2 - 2
examples/js/controls/EditorControls.js

@@ -87,14 +87,14 @@ THREE.EditorControls = function ( object, domElement ) {
 
 		vector.copy( object.position ).sub( center );
 
-		spherical.fromVector3( vector );
+		spherical.setFromVector3( vector );
 
 		spherical.theta += delta.x;
 		spherical.phi += delta.y;
 
 		spherical.makeSafe();
 
-		vector = spherical.toVector3( vector );
+		vector.setFromSpherical( spherical );
 
 		object.position.copy( center ).add( vector );
 

+ 3 - 3
examples/js/controls/OrbitControls.js

@@ -133,7 +133,7 @@ THREE.OrbitControls = function ( object, domElement ) {
 			offset.applyQuaternion( quat );
 
 			// angle from z-axis around y-axis
-			spherical.fromVector3( offset );
+			spherical.setFromVector3( offset );
 
 			if ( scope.autoRotate && state === STATE.NONE ) {
 
@@ -161,8 +161,8 @@ THREE.OrbitControls = function ( object, domElement ) {
 			// move target to panned location
 			scope.target.add( panOffset );
 
-			offset = spherical.toVector3( offset );
-
+			offset.setFromSpherical( spherical );
+			
 			// rotate offset back to "camera-up-vector-is-up" space
 			offset.applyQuaternion( quatInverse );
 

+ 2 - 16
src/math/Spherical.js

@@ -55,7 +55,7 @@ THREE.Spherical.prototype = {
 
 	},
 
-  fromVector3: function( vec3 ) {
+  setFromVector3: function( vec3 ) {
 
 		this.radius = vec3.length();
 		if( this.radius === 0 ) {
@@ -64,25 +64,11 @@ THREE.Spherical.prototype = {
 		}
 		else {
     	this.theta = Math.atan2( vec3.x, vec3.z ); // equator angle around y-up axis
-    	this.phi = Math.acos( vec3.y / this.radius ); // polar angle
+    	this.phi = Math.acos( THREE.Math.clamp( vec3.y / this.radius, -1, 1 ) ); // polar angle
 		}
 
     return this;
 
   },
 
-  toVector3: function( optionalTarget ) {
-
-    var result = optionalTarget || new THREE.Vector3();
-		var sinPhiRadius = Math.sin( this.phi ) * this.radius;
-    result.set(
-      sinPhiRadius * Math.sin( this.theta ),
-      Math.cos( this.phi ) * this.radius,
-      sinPhiRadius * Math.cos( this.theta )
-    );
-
-    return result;
-
-  }
-
 };

+ 11 - 0
src/math/Vector3.js

@@ -693,6 +693,17 @@ THREE.Vector3.prototype = {
 
 	},
 
+	setFromSpherical: function( s ) {
+
+		var sinPhiRadius = Math.sin( s.phi ) * s.radius;
+		this.x = sinPhiRadius * Math.sin( s.theta );
+		this.y = Math.cos( s.phi ) * s.radius;
+		this.z = sinPhiRadius * Math.cos( s.theta );
+
+		return this;
+
+	},
+
 	setFromMatrixPosition: function ( m ) {
 
 		this.x = m.elements[ 12 ];