瀏覽代碼

Merge branch 'vector-methods' of https://github.com/twhittock/three.js into dev

Mr.doob 11 年之前
父節點
當前提交
9582770323
共有 7 個文件被更改,包括 247 次插入2 次删除
  1. 30 1
      docs/api/math/Vector2.html
  2. 29 0
      docs/api/math/Vector3.html
  3. 31 1
      docs/api/math/Vector4.html
  4. 39 0
      src/math/Vector2.js
  5. 44 0
      src/math/Vector3.js
  6. 48 0
      src/math/Vector4.js
  7. 26 0
      test/unit/math/Vector2.js

+ 30 - 1
docs/api/math/Vector2.html

@@ -140,7 +140,36 @@
 		max -- [page:Vector2] containing the max x and y values in the desired range
 		</div>
 		<div>
-		If this vector's x or y value is greater than the max vector's x or y value, it is replaced by the corresponding value. <br />  If this vector's x or y value is less than the min vector's x or y value, it is replace by the corresponding value.
+		If this vector's x or y value is greater than the max vector's x or y value, it is replaced by the corresponding value. <br />	If this vector's x or y value is less than the min vector's x or y value, it is replace by the corresponding value.
+		</div>
+
+		<h3>.clampScalar([page:Float min], [page:Float max]) [page:Vector2 this]</h3>
+		<div>
+		min -- [page:Float] the minimum value the components will be clamped to <br />
+		max -- [page:Float] the maximum value the components will be clamped to
+		</div>
+		<div>
+		If this vector's x or y values are greater than the max value, they are replaced by the max value. <br />  If this vector's x or y values are less than the min value, they are replace by the min value.
+		</div>
+
+		<h3>.floor() [page:Vector2]</h3>
+		<div>
+		The components of the vector are rounded downwards (towards negative infinity) to an integer value.
+		</div>
+
+		<h3>.ceil() [page:Vector2]</h3>
+		<div>
+		The components of the vector are rounded upwards (towards positive infinity) to an integer value.
+		</div>
+
+		<h3>.round() [page:Vector2]</h3>
+		<div>
+		The components of the vector are rounded towards the nearest integer value.
+		</div>
+
+		<h3>.roundToZero() [page:Vector2]</h3>
+		<div>
+		The components of the vector are rounded towards zero (up if negative, down if positive) to an integer value.
 		</div>
 
 		<h3>.lerp([page:Vector2 v], [page:Float alpha]) [page:Vector2 this]</h3>

+ 29 - 0
docs/api/math/Vector3.html

@@ -190,6 +190,35 @@
 		If this vector's x, y or z value is less than the min vector's x, y or z value, it is replace by the corresponding value.
 		</div>
 
+		<h3>.clampScalar([page:Float min], [page:Float max]) [page:Vector3 this]</h3>
+		<div>
+		min -- [page:Float] the minimum value the components will be clamped to <br />
+		max -- [page:Float] the maximum value the components will be clamped to
+		</div>
+		<div>
+		If this vector's x, y or z values are greater than the max value, they are replaced by the max value. <br />  If this vector's x, y or z values are less than the min value, they are replace by the min value.
+		</div>
+
+		<h3>.floor() [page:Vector3]</h3>
+		<div>
+		The components of the vector are rounded downwards (towards negative infinity) to an integer value.
+		</div>
+
+		<h3>.ceil() [page:Vector3]</h3>
+		<div>
+		The components of the vector are rounded upwards (towards positive infinity) to an integer value.
+		</div>
+
+		<h3>.round() [page:Vector3]</h3>
+		<div>
+		The components of the vector are rounded towards the nearest integer value.
+		</div>
+
+		<h3>.roundToZero() [page:Vector3]</h3>
+		<div>
+		The components of the vector are rounded towards zero (up if negative, down if positive) to an integer value.
+		</div>
+
 		<h3>.applyMatrix3([page:Matrix3 m]) [page:Vector3 this]</h3>
 		<div>
 		m -- [page:Matrix3]

+ 31 - 1
docs/api/math/Vector4.html

@@ -130,6 +130,36 @@
 		If this vector's x, y, z, or w value is less than the min vector's x, y, z, or w value, it is replace by the corresponding value.
 		</div>
 
+		<h3>.clampScalar([page:Float min], [page:Float max]) [page:Vector4 this]</h3>
+		<div>
+		min -- [page:Float] the minimum value the components will be clamped to <br />
+		max -- [page:Float] the maximum value the components will be clamped to
+		</div>
+		<div>
+		If this vector's x, y, z or w values are greater than the max value, they are replaced by the max value. <br />
+		If this vector's x, y, z or w values are less than the min value, they are replace by the min value.
+		</div>
+
+		<h3>.floor() [page:Vector4]</h3>
+		<div>
+		The components of the vector are rounded downwards (towards negative infinity) to an integer value.
+		</div>
+
+		<h3>.ceil() [page:Vector4]</h3>
+		<div>
+		The components of the vector are rounded upwards (towards positive infinity) to an integer value.
+		</div>
+
+		<h3>.round() [page:Vector4]</h3>
+		<div>
+		The components of the vector are rounded towards the nearest integer value.
+		</div>
+
+		<h3>.roundToZero() [page:Vector4]</h3>
+		<div>
+		The components of the vector are rounded towards zero (up if negative, down if positive) to an integer value.
+		</div>
+
 		<h3>.applyMatrix4([page:Matrix4 m]) [page:Vector4 this]</h3>
 		<div>
 		m -- [page:Matrix4]
@@ -210,7 +240,7 @@
 		value -- [page:Float]
 		</div>
 		<div>
-		Sets the value of the vector component  x, y, or z by an index.<br/><br/>
+		Sets the value of the vector component	x, y, or z by an index.<br/><br/>
 		
 		Index 0: x<br/>
 		Index 1: y<br/>

+ 39 - 0
src/math/Vector2.js

@@ -224,7 +224,46 @@ THREE.Vector2.prototype = {
 		}
 
 		return this;
+	},
+
+	clampScalar: ( function () {
+		var min, max;
+
+		return function ( minVal, maxVal ) {
+			if ( !min || !max ) {
+				min = new THREE.Vector2();
+				max = new THREE.Vector2();
+			}
+
+			min.set(minVal, minVal);
+			max.set(maxVal, maxVal);
+			return this.clamp(min, max);
+
+		};
+	} )(),
+
+	floor: function() {
+		this.x = Math.floor(this.x);
+		this.y = Math.floor(this.y);
+		return this;
+	},
 
+	ceil: function() {
+		this.x = Math.ceil(this.x);
+		this.y = Math.ceil(this.y);
+		return this;
+	},
+
+	round: function() {
+		this.x = Math.round(this.x);
+		this.y = Math.round(this.y);
+		return this;
+	},
+
+	roundToZero: function() {
+		this.x = (this.x < 0) ? Math.ceil(this.x) : Math.floor(this.x);
+		this.y = (this.y < 0) ? Math.ceil(this.y) : Math.floor(this.y);
+		return this;
 	},
 
 	negate: function() {

+ 44 - 0
src/math/Vector3.js

@@ -404,6 +404,50 @@ THREE.Vector3.prototype = {
 
 	},
 
+	clampScalar: ( function () {
+		var min, max;
+
+		return function ( minVal, maxVal ) {
+			if ( !min || !max ) {
+				min = new THREE.Vector3();
+				max = new THREE.Vector3();
+			}
+
+			min.set(minVal, minVal, minVal);
+			max.set(maxVal, maxVal, maxVal);
+			return this.clamp(min, max);
+
+		};
+	} )(),
+
+	floor: function() {
+		this.x = Math.floor(this.x);
+		this.y = Math.floor(this.y);
+		this.z = Math.floor(this.z);
+		return this;
+	},
+
+	ceil: function() {
+		this.x = Math.ceil(this.x);
+		this.y = Math.ceil(this.y);
+		this.z = Math.ceil(this.z);
+		return this;
+	},
+
+	round: function() {
+		this.x = Math.round(this.x);
+		this.y = Math.round(this.y);
+		this.z = Math.round(this.z);
+		return this;
+	},
+
+	roundToZero: function() {
+		this.x = (this.x < 0) ? Math.ceil(this.x) : Math.floor(this.x);
+		this.y = (this.y < 0) ? Math.ceil(this.y) : Math.floor(this.y);
+		this.z = (this.z < 0) ? Math.ceil(this.z) : Math.floor(this.z);
+		return this;
+	},
+
 	negate: function () {
 
 		return this.multiplyScalar( - 1 );

+ 48 - 0
src/math/Vector4.js

@@ -483,6 +483,54 @@ THREE.Vector4.prototype = {
 
 	},
 
+	clampScalar: ( function () {
+		var min, max;
+
+		return function ( minVal, maxVal ) {
+			if ( !min || !max ) {
+				min = new THREE.Vector4();
+				max = new THREE.Vector4();
+			}
+
+			min.set(minVal, minVal, minVal, minVal);
+			max.set(maxVal, maxVal, maxVal, maxVal);
+			return this.clamp(min, max);
+
+		};
+	} )(),
+
+    floor: function() {
+        this.x = Math.floor(this.x);
+        this.y = Math.floor(this.y);
+        this.z = Math.floor(this.z);
+        this.w = Math.floor(this.w);
+        return this;
+    },
+
+    ceil: function() {
+        this.x = Math.ceil(this.x);
+        this.y = Math.ceil(this.y);
+        this.z = Math.ceil(this.z);
+        this.w = Math.ceil(this.w);
+        return this;
+    },
+
+    round: function() {
+        this.x = Math.round(this.x);
+        this.y = Math.round(this.y);
+        this.z = Math.round(this.z);
+        this.w = Math.round(this.w);
+        return this;
+    },
+
+    roundToZero: function() {
+        this.x = (this.x < 0) ? Math.ceil(this.x) : Math.floor(this.x);
+        this.y = (this.y < 0) ? Math.ceil(this.y) : Math.floor(this.y);
+        this.z = (this.z < 0) ? Math.ceil(this.z) : Math.floor(this.z);
+        this.w = (this.w < 0) ? Math.ceil(this.w) : Math.floor(this.w);
+        return this;
+    },
+
 	negate: function() {
 
 		return this.multiplyScalar( -1 );

+ 26 - 0
test/unit/math/Vector2.js

@@ -124,6 +124,32 @@ test( "min/max/clamp", function() {
 	c.clamp( b, a );
 	ok( c.x == -x, "Passed!" );
 	ok( c.y == y, "Passed!" );
+
+	c.set(-2*x, 2*x);
+	c.clampScalar( -x, x );
+	equal( c.x, -x, "scalar clamp x" );
+	equal( c.y,  x, "scalar clamp y" );
+});
+
+test( "rounding", function() {
+	deepEqual( new THREE.Vector2( -0.1, 0.1 ).floor(), new THREE.Vector2( -1, 0 ), "floor .1" );
+	deepEqual( new THREE.Vector2( -0.5, 0.5 ).floor(), new THREE.Vector2( -1, 0 ), "floor .5" );
+	deepEqual( new THREE.Vector2( -0.9, 0.9 ).floor(), new THREE.Vector2( -1, 0 ), "floor .9" );
+
+	deepEqual( new THREE.Vector2( -0.1, 0.1 ).ceil(), new THREE.Vector2( 0, 1 ), "ceil .1" );
+	deepEqual( new THREE.Vector2( -0.5, 0.5 ).ceil(), new THREE.Vector2( 0, 1 ), "ceil .5" );
+	deepEqual( new THREE.Vector2( -0.9, 0.9 ).ceil(), new THREE.Vector2( 0, 1 ), "ceil .9" );
+
+	deepEqual( new THREE.Vector2( -0.1, 0.1 ).round(), new THREE.Vector2( 0, 0 ), "round .1" );
+	deepEqual( new THREE.Vector2( -0.5, 0.5 ).round(), new THREE.Vector2( 0, 1 ), "round .5" );
+	deepEqual( new THREE.Vector2( -0.9, 0.9 ).round(), new THREE.Vector2( -1, 1 ), "round .9" );
+
+	deepEqual( new THREE.Vector2( -0.1, 0.1 ).roundToZero(), new THREE.Vector2( 0, 0 ), "roundToZero .1" );
+	deepEqual( new THREE.Vector2( -0.5, 0.5 ).roundToZero(), new THREE.Vector2( 0, 0 ), "roundToZero .5" );
+	deepEqual( new THREE.Vector2( -0.9, 0.9 ).roundToZero(), new THREE.Vector2( 0, 0 ), "roundToZero .9" );
+	deepEqual( new THREE.Vector2( -1.1, 1.1 ).roundToZero(), new THREE.Vector2( -1, 1 ), "roundToZero 1.1" );
+	deepEqual( new THREE.Vector2( -1.5, 1.5 ).roundToZero(), new THREE.Vector2( -1, 1 ), "roundToZero 1.5" );
+	deepEqual( new THREE.Vector2( -1.9, 1.9 ).roundToZero(), new THREE.Vector2( -1, 1 ), "roundToZero 1.9" );
 });
 
 test( "negate", function() {