Browse Source

Vector3.multiply -> multiplyScalar
Vector3.cross -> crossSelf
added Vector3.multiplySelf and others (kile)

Mr.doob 15 years ago
parent
commit
47ba30790a
6 changed files with 76 additions and 12 deletions
  1. 0 0
      build/three.js
  2. 43 0
      src/core/Geometry.js
  3. 2 2
      src/core/Matrix4.js
  4. 1 1
      src/core/Vector2.js
  5. 29 8
      src/core/Vector3.js
  6. 1 1
      src/core/Vertex.js

File diff suppressed because it is too large
+ 0 - 0
build/three.js


+ 43 - 0
src/core/Geometry.js

@@ -1,4 +1,5 @@
 /**
+ * @author kile / http://kile.stravaganza.org/
  * @author mr.doob / http://mrdoob.com/
  */
 
@@ -7,4 +8,46 @@ THREE.Geometry = function () {
 	this.vertices = [];
 	this.faces = [];
 
+	this.computeNormals = function () {
+	
+		var v, f, vA, vB, vC, cb, ab, normal;
+	
+		for(v = 0; v < this.vertices.length; v++) {
+		
+			this.vertices[v].normal.set(0,0,0);
+		}
+	
+		for(f = 0; f < this.faces.length; f++) {
+		
+			vA = this.vertices[this.faces[f].a],
+			vB = this.vertices[this.faces[f].b],
+			vC = this.vertices[this.faces[f].c],
+			
+			cb = new THREE.Vector3(),
+			ab = new THREE.Vector3(),
+			normal = new THREE.Vector3();
+			
+			cb.sub(vC.position,vB.position);
+			ab.sub(vA.position,vB.position);
+			cb.cross(ab);
+			
+			if (!cb.isZero()) {
+			
+				cb.normalize();
+				
+			}
+				
+			this.faces[f].normal=cb;
+
+			vA.normal.addSelf(normal);
+			vB.normal.addSelf(normal);
+			vC.normal.addSelf(normal);
+			
+			if (this.faces[f] instanceof THREE.Face4) {
+			
+				this.vertices[this.faces[f].d].normal.addSelf(normal);
+				
+			}
+		}
+	}
 }

+ 2 - 2
src/core/Matrix4.js

@@ -30,11 +30,11 @@ THREE.Matrix4 = function () {
 		z.normalize();
 
 		x.copy(z);
-		x.cross(up);
+		x.crossSelf(up);
 		x.normalize();
 
 		y.copy(x);
-		y.cross(z);
+		y.crossSelf(z);
 		y.normalize();
 		y.negate(); //
 

+ 1 - 1
src/core/Vector2.js

@@ -37,7 +37,7 @@ THREE.Vector2 = function (x, y) {
 		this.y = v1.y - v2.y;
 	}
 	
-	this.multiply = function (s) {
+	this.multiplyScalar = function (s) {
 	
 		this.x *= s;
 		this.y *= s;

+ 29 - 8
src/core/Vector3.js

@@ -1,4 +1,5 @@
 /**
+ * @author kile / http://kile.stravaganza.org/
  * @author mr.doob / http://mrdoob.com/
  */
 
@@ -28,6 +29,13 @@ THREE.Vector3 = function (x, y, z) {
 		this.y += v.y;
 		this.z += v.z;
 	}
+	
+	this.addScalar = function (s) {
+	
+		this.x += s;
+		this.y += s;
+		this.z += s;
+	}	
 
 	this.sub = function(v1, v2) {
 	
@@ -43,7 +51,7 @@ THREE.Vector3 = function (x, y, z) {
 		this.z -= v.z;
 	}
 	
-	this.cross = function (v) {
+	this.crossSelf = function (v) {
 	
 		var tx = this.x, ty = this.y, tz = this.z;
 		
@@ -52,13 +60,25 @@ THREE.Vector3 = function (x, y, z) {
 		this.z = tx * v.y - ty * v.x;
 	}
 	
-	this.multiply = function (s) {
+	this.multiplySelf = function (v) {
+	
+		this.x *= v.x;
+		this.y *= v.y;
+		this.z *= v.z;
+	}	
+	
+	this.multiplyScalar = function (s) {
 	
 		this.x *= s;
 		this.y *= s;
 		this.z *= s;
 	}
 	
+	this.dot = function (v) {
+	
+		return this.x * v.x + this.y * v.y + this.z * v.z;
+	}
+	
 	this.distanceTo = function (v) {
 	
 		var dx = this.x - v.x, dy = this.y - v.y, dz = this.z - v.z;
@@ -92,22 +112,23 @@ THREE.Vector3 = function (x, y, z) {
 	
 		if (this.length() > 0) {
 		
-			this.multiply(1 / this.length());
+			this.multiplyScalar(1 / this.length());
 			
 		} else {
 		
-			this.multiply(0);
+			this.multiplyScalar(0);
 		}
 	}
 	
-	this.dot = function (v) {
+	this.isZero = function () {
 	
-		return this.x * v.x + this.y * v.y + this.z * v.z;
-	}
+		var almostZero = 0.0001;
+		return (Math.abs(this.x) < almostZero) && (Math.abs(this.y) < almostZero) && (Math.abs(this.z) < almostZero);
+	}	
 	
 	this.clone = function () {
 	
-		return new Vector3(this.x, this.y, this.z);
+		return new THREE.Vector3(this.x, this.y, this.z);
 	}
 	
 	this.toString = function () {

+ 1 - 1
src/core/Vertex.js

@@ -12,6 +12,6 @@ THREE.Vertex = function (position, normal) {
 
 	this.toString = function () {
 	
-		return 'THREE.Vertex ( ' + this.position + ', ' + this.normal + ' )';
+		return 'THREE.Vertex ( position: ' + this.position + ', normal: ' + this.normal + ' )';
 	}
 }

Some files were not shown because too many files changed in this diff