Browse Source

add geometry normalization

Zhonghua Xi 10 years ago
parent
commit
b439904eb9
2 changed files with 28 additions and 0 deletions
  1. 11 0
      docs/api/core/Geometry.html
  2. 17 0
      src/core/Geometry.js

+ 11 - 0
docs/api/core/Geometry.html

@@ -185,6 +185,11 @@
 		Bakes matrix transform directly into vertex coordinates.
 		</div>
 
+		<h3>[method:null center]()</h3>
+		<div>
+		Center the geometry based on the bounding box.
+		</div>
+
 		<h3>[method:null computeFaceNormals]()</h3>
 		<div>
 		Computes face normals.
@@ -228,6 +233,12 @@
 		Checks for duplicate vertices using hashmap.<br />
 		Duplicated vertices are removed and faces' vertices are updated.
 		</div>
+
+		<h3>[method:null normalize]()</h3>
+		<div>
+		Normalize the geometry. <br />
+		Make the geometry centered and has a bounding sphere whose raidus equals to 1.0.
+		</div>
 		
 		<h3>[method:Geometry clone]()</h3>
 		<div>

+ 17 - 0
src/core/Geometry.js

@@ -224,6 +224,23 @@ THREE.Geometry.prototype = {
 
 	},
 
+	normalize: function() {
+		var COM = this.boundingSphere.center;
+		var R = this.boundingSphere.radius;
+
+		var s = (R === 0 ? 1 : 1.0 / R);
+
+		var m = new THREE.Matrix4().set(
+			s, 0, 0, -s*COM.x,
+			0, s, 0, -s*COM.y,
+			0, 0, s, -s*COM.z,
+			0, 0, 0, 1);
+
+		this.applyMatrix(m);
+
+		return this;
+	},
+
 	computeFaceNormals: function () {
 
 		var cb = new THREE.Vector3(), ab = new THREE.Vector3();