Browse Source

More garbage collection friendly (on repeated calls) Geometry.computeBoundingBox() and Geometry.computeBoundingSphere()

alteredq 12 years ago
parent
commit
4073df5091
4 changed files with 26 additions and 640 deletions
  1. 0 631
      build/three.js
  2. 5 4
      build/three.min.js
  3. 20 2
      src/core/Geometry.js
  4. 1 3
      src/math/Sphere.js

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


+ 5 - 4
build/three.min.js

@@ -142,10 +142,11 @@ b;a++){if(!this.morphNormals[a]){this.morphNormals[a]={};this.morphNormals[a].fa
 this.morphTargets[a].vertices;f.computeFaceNormals();f.computeVertexNormals();c=0;for(d=this.faces.length;c<d;c++)e=this.faces[c],i=g.faceNormals[c],j=g.vertexNormals[c],i.copy(e.normal),e instanceof THREE.Face3?(j.a.copy(e.vertexNormals[0]),j.b.copy(e.vertexNormals[1]),j.c.copy(e.vertexNormals[2])):(j.a.copy(e.vertexNormals[0]),j.b.copy(e.vertexNormals[1]),j.c.copy(e.vertexNormals[2]),j.d.copy(e.vertexNormals[3]))}c=0;for(d=this.faces.length;c<d;c++)e=this.faces[c],e.normal=e.__originalFaceNormal,
 e.vertexNormals=e.__originalVertexNormals},computeTangents:function(){function a(a,b,c,d,e,f,v){h=a.vertices[b];i=a.vertices[c];j=a.vertices[d];m=g[e];p=g[f];n=g[v];s=i.x-h.x;r=j.x-h.x;l=i.y-h.y;t=j.y-h.y;u=i.z-h.z;E=j.z-h.z;A=p.x-m.x;q=n.x-m.x;F=p.y-m.y;z=n.y-m.y;x=1/(A*z-q*F);G.set((z*s-F*r)*x,(z*l-F*t)*x,(z*u-F*E)*x);M.set((A*r-q*s)*x,(A*t-q*l)*x,(A*E-q*u)*x);I[b].addSelf(G);I[c].addSelf(G);I[d].addSelf(G);C[b].addSelf(M);C[c].addSelf(M);C[d].addSelf(M)}var b,c,d,e,f,g,h,i,j,m,p,n,s,r,l,t,u,E,
 A,q,F,z,x,v,I=[],C=[],G=new THREE.Vector3,M=new THREE.Vector3,B=new THREE.Vector3,J=new THREE.Vector3,D=new THREE.Vector3;b=0;for(c=this.vertices.length;b<c;b++)I[b]=new THREE.Vector3,C[b]=new THREE.Vector3;b=0;for(c=this.faces.length;b<c;b++)f=this.faces[b],g=this.faceVertexUvs[0][b],f instanceof THREE.Face3?a(this,f.a,f.b,f.c,0,1,2):f instanceof THREE.Face4&&(a(this,f.a,f.b,f.d,0,1,3),a(this,f.b,f.c,f.d,1,2,3));var L=["a","b","c","d"];b=0;for(c=this.faces.length;b<c;b++){f=this.faces[b];for(d=0;d<
-f.vertexNormals.length;d++)D.copy(f.vertexNormals[d]),e=f[L[d]],v=I[e],B.copy(v),B.subSelf(D.multiplyScalar(D.dot(v))).normalize(),J.cross(f.vertexNormals[d],v),e=J.dot(C[e]),e=0>e?-1:1,f.vertexTangents[d]=new THREE.Vector4(B.x,B.y,B.z,e)}this.hasTangents=!0},computeLineDistances:function(){for(var a=0,b=this.vertices,c=0,d=b.length;c<d;c++)0<c&&(a+=b[c].distanceTo(b[c-1])),this.lineDistances[c]=a},computeBoundingBox:function(){this.boundingBox=(new THREE.Box3).setFromPoints(this.vertices)},computeBoundingSphere:function(){this.boundingSphere=
-(new THREE.Sphere).setFromCenterAndPoints(new THREE.Vector3,this.vertices)},mergeVertices:function(){var a={},b=[],c=[],d,e=Math.pow(10,4),f,g,h,i;f=0;for(g=this.vertices.length;f<g;f++)d=this.vertices[f],d=[Math.round(d.x*e),Math.round(d.y*e),Math.round(d.z*e)].join("_"),void 0===a[d]?(a[d]=f,b.push(this.vertices[f]),c[f]=b.length-1):c[f]=c[a[d]];f=0;for(g=this.faces.length;f<g;f++)if(a=this.faces[f],a instanceof THREE.Face3)a.a=c[a.a],a.b=c[a.b],a.c=c[a.c];else if(a instanceof THREE.Face4){a.a=
-c[a.a];a.b=c[a.b];a.c=c[a.c];a.d=c[a.d];d=[a.a,a.b,a.c,a.d];for(e=3;0<e;e--)if(d.indexOf(a["abcd"[e]])!==e){d.splice(e,1);this.faces[f]=new THREE.Face3(d[0],d[1],d[2],a.normal,a.color,a.materialIndex);d=0;for(h=this.faceVertexUvs.length;d<h;d++)(i=this.faceVertexUvs[d][f])&&i.splice(e,1);this.faces[f].vertexColors=a.vertexColors;break}}c=this.vertices.length-b.length;this.vertices=b;return c},clone:function(){for(var a=new THREE.Geometry,b=this.vertices,c=0,d=b.length;c<d;c++)a.vertices.push(b[c].clone());
-b=this.faces;c=0;for(d=b.length;c<d;c++)a.faces.push(b[c].clone());b=this.faceVertexUvs[0];c=0;for(d=b.length;c<d;c++){for(var e=b[c],f=[],g=0,h=e.length;g<h;g++)f.push(new THREE.Vector2(e[g].x,e[g].y));a.faceVertexUvs[0].push(f)}return a},deallocate:function(){delete THREE.GeometryLibrary[this.id]}};THREE.GeometryIdCount=0;THREE.GeometryLibrary={};THREE.BufferGeometry=function(){this.id=THREE.GeometryIdCount++;this.attributes={};this.dynamic=!1;this.offsets=[];this.boundingSphere=this.boundingBox=null;this.hasTangents=!1;this.morphTargets=[];THREE.GeometryLibrary[this.id]=this};
+f.vertexNormals.length;d++)D.copy(f.vertexNormals[d]),e=f[L[d]],v=I[e],B.copy(v),B.subSelf(D.multiplyScalar(D.dot(v))).normalize(),J.cross(f.vertexNormals[d],v),e=J.dot(C[e]),e=0>e?-1:1,f.vertexTangents[d]=new THREE.Vector4(B.x,B.y,B.z,e)}this.hasTangents=!0},computeLineDistances:function(){for(var a=0,b=this.vertices,c=0,d=b.length;c<d;c++)0<c&&(a+=b[c].distanceTo(b[c-1])),this.lineDistances[c]=a},computeBoundingBox:function(){this.boundingBox?this.boundingBox.setFromPoints(this.vertices):this.boundingBox=
+(new THREE.Box3).setFromPoints(this.vertices)},computeBoundingSphere:function(){this.boundingSphere?this.boundingSphere.setFromCenterAndPoints(this.boundingSphere.center,this.vertices):this.boundingSphere=(new THREE.Sphere).setFromCenterAndPoints(new THREE.Vector3,this.vertices)},mergeVertices:function(){var a={},b=[],c=[],d,e=Math.pow(10,4),f,g,h,i;f=0;for(g=this.vertices.length;f<g;f++)d=this.vertices[f],d=[Math.round(d.x*e),Math.round(d.y*e),Math.round(d.z*e)].join("_"),void 0===a[d]?(a[d]=f,b.push(this.vertices[f]),
+c[f]=b.length-1):c[f]=c[a[d]];f=0;for(g=this.faces.length;f<g;f++)if(a=this.faces[f],a instanceof THREE.Face3)a.a=c[a.a],a.b=c[a.b],a.c=c[a.c];else if(a instanceof THREE.Face4){a.a=c[a.a];a.b=c[a.b];a.c=c[a.c];a.d=c[a.d];d=[a.a,a.b,a.c,a.d];for(e=3;0<e;e--)if(d.indexOf(a["abcd"[e]])!==e){d.splice(e,1);this.faces[f]=new THREE.Face3(d[0],d[1],d[2],a.normal,a.color,a.materialIndex);d=0;for(h=this.faceVertexUvs.length;d<h;d++)(i=this.faceVertexUvs[d][f])&&i.splice(e,1);this.faces[f].vertexColors=a.vertexColors;
+break}}c=this.vertices.length-b.length;this.vertices=b;return c},clone:function(){for(var a=new THREE.Geometry,b=this.vertices,c=0,d=b.length;c<d;c++)a.vertices.push(b[c].clone());b=this.faces;c=0;for(d=b.length;c<d;c++)a.faces.push(b[c].clone());b=this.faceVertexUvs[0];c=0;for(d=b.length;c<d;c++){for(var e=b[c],f=[],g=0,h=e.length;g<h;g++)f.push(new THREE.Vector2(e[g].x,e[g].y));a.faceVertexUvs[0].push(f)}return a},deallocate:function(){delete THREE.GeometryLibrary[this.id]}};
+THREE.GeometryIdCount=0;THREE.GeometryLibrary={};THREE.BufferGeometry=function(){this.id=THREE.GeometryIdCount++;this.attributes={};this.dynamic=!1;this.offsets=[];this.boundingSphere=this.boundingBox=null;this.hasTangents=!1;this.morphTargets=[];THREE.GeometryLibrary[this.id]=this};
 THREE.BufferGeometry.prototype={constructor:THREE.BufferGeometry,applyMatrix:function(a){var b,c;this.attributes.position&&(b=this.attributes.position.array);this.attributes.normal&&(c=this.attributes.normal.array);void 0!==b&&(a.multiplyVector3Array(b),this.verticesNeedUpdate=!0);void 0!==c&&(b=new THREE.Matrix3,b.getInverse(a).transpose(),b.multiplyVector3Array(c),this.normalizeNormals(),this.normalsNeedUpdate=!0)},computeBoundingBox:function(){this.boundingBox||(this.boundingBox=new THREE.Box3);
 var a=this.attributes.position.array;if(a){var b=this.boundingBox,c,d,e;3<=a.length&&(b.min.x=b.max.x=a[0],b.min.y=b.max.y=a[1],b.min.z=b.max.z=a[2]);for(var f=3,g=a.length;f<g;f+=3)c=a[f],d=a[f+1],e=a[f+2],c<b.min.x?b.min.x=c:c>b.max.x&&(b.max.x=c),d<b.min.y?b.min.y=d:d>b.max.y&&(b.max.y=d),e<b.min.z?b.min.z=e:e>b.max.z&&(b.max.z=e)}if(void 0===a||0===a.length)this.boundingBox.min.set(0,0,0),this.boundingBox.max.set(0,0,0)},computeBoundingSphere:function(){this.boundingSphere||(this.boundingSphere=
 new THREE.Sphere);var a=this.attributes.position.array;if(a){for(var b,c=0,d,e,f=0,g=a.length;f<g;f+=3)b=a[f],d=a[f+1],e=a[f+2],b=b*b+d*d+e*e,b>c&&(c=b);this.boundingSphere.radius=Math.sqrt(c)}},computeVertexNormals:function(){if(this.attributes.position){var a,b,c,d;a=this.attributes.position.array.length;if(void 0===this.attributes.normal)this.attributes.normal={itemSize:3,array:new Float32Array(a),numItems:a};else{a=0;for(b=this.attributes.normal.array.length;a<b;a++)this.attributes.normal.array[a]=

+ 20 - 2
src/core/Geometry.js

@@ -577,12 +577,30 @@ THREE.Geometry.prototype = {
 
 	computeBoundingBox: function () {
 
-		this.boundingBox = new THREE.Box3().setFromPoints( this.vertices );
+		if ( ! this.boundingBox ) {
+
+			this.boundingBox = new THREE.Box3().setFromPoints( this.vertices );
+
+		} else {
+
+			this.boundingBox.setFromPoints( this.vertices );
+
+		}
+
 	},
 
 	computeBoundingSphere: function () {
 
-		this.boundingSphere = new THREE.Sphere().setFromCenterAndPoints( new THREE.Vector3(), this.vertices );
+		if ( ! this.boundingSphere ) {
+
+			this.boundingSphere = new THREE.Sphere().setFromCenterAndPoints( new THREE.Vector3(), this.vertices );
+
+		} else {
+
+			this.boundingSphere.setFromCenterAndPoints( this.boundingSphere.center, this.vertices );
+
+		}
+
 	},
 
 	/*

+ 1 - 3
src/math/Sphere.js

@@ -4,7 +4,6 @@
 
 THREE.Sphere = function ( center, radius ) {
 
-
 	if ( center === undefined && radius === undefined ) {
 
 		this.center = new THREE.Vector3();
@@ -16,7 +15,7 @@ THREE.Sphere = function ( center, radius ) {
 		this.radius = radius || 0;
 
 	}
-	
+
 };
 
 THREE.Sphere.prototype = {
@@ -29,7 +28,6 @@ THREE.Sphere.prototype = {
 		this.radius = radius;
 
 		return this;
-
 	},
 
 	setFromCenterAndPoints: function ( center, points ) {

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