浏览代码

Add Vector methods clampScalar, floor, ceil, round, roundToZero

Tom Whittock 11 年之前
父节点
当前提交
3a58a5804c
共有 6 个文件被更改,包括 243 次插入3 次删除
  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. 41 1
      src/math/Vector2.js
  5. 51 0
      src/math/Vector3.js
  6. 61 0
      src/math/Vector4.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/>

+ 41 - 1
src/math/Vector2.js

@@ -224,9 +224,49 @@ THREE.Vector2.prototype = {
 		}
 
 		return this;
-
 	},
 
+    clampScalar: function ( minVal, maxVal ) {
+
+        if ( this.x < minVal ) {
+            this.x = minVal;
+        } else if ( this.x > maxVal ) {
+            this.x = maxVal;
+        }
+
+        if ( this.y < minVal ) {
+            this.y = minVal;
+        } else if ( this.y > maxVal ) {
+            this.y = maxVal;
+        }
+
+        return this;
+    },
+
+    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() {
 
 		return this.multiplyScalar( - 1 );

+ 51 - 0
src/math/Vector3.js

@@ -404,6 +404,57 @@ THREE.Vector3.prototype = {
 
 	},
 
+    clampScalar: function ( minVal, maxVal ) {
+
+        if ( this.x < minVal ) {
+            this.x = minVal;
+        } else if ( this.x > maxVal ) {
+            this.x = maxVal;
+        }
+
+        if ( this.y < minVal ) {
+            this.y = minVal;
+        } else if ( this.y > maxVal ) {
+            this.y = maxVal;
+        }
+
+        if ( this.z < minVal ) {
+            this.z = minVal;
+        } else if ( this.z > maxVal ) {
+            this.z = maxVal;
+        }
+
+        return this;
+    },
+
+    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 );

+ 61 - 0
src/math/Vector4.js

@@ -483,6 +483,67 @@ THREE.Vector4.prototype = {
 
 	},
 
+    clampScalar: function ( minVal, maxVal ) {
+
+        if ( this.x < minVal ) {
+            this.x = minVal;
+        } else if ( this.x > maxVal ) {
+            this.x = maxVal;
+        }
+
+        if ( this.y < minVal ) {
+            this.y = minVal;
+        } else if ( this.y > maxVal ) {
+            this.y = maxVal;
+        }
+
+        if ( this.z < minVal ) {
+            this.z = minVal;
+        } else if ( this.z > maxVal ) {
+            this.z = maxVal;
+        }
+
+        if ( this.w < minVal ) {
+            this.w = minVal;
+        } else if ( this.w > maxVal ) {
+            this.w = maxVal;
+        }
+
+        return this;
+    },
+
+    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 );