提交 3d94fc6b 编写于 作者: A alteredq

Refactored Shape.Utils.triangulateShape to use hashmaps instead of O( m * n ) loops.

This cut TextGeometry creation time almost by half.
上级 92465c4e
因为 它太大了无法显示 source diff 。你可以改为 查看blob
......@@ -117,9 +117,9 @@ e.curves):g.holes.push(e);h.push(g)}else{g=new THREE.Shape;a=0;for(b=f.length;a<
THREE.Shape.prototype.getPointsHoles=function(){var a,b=this.holes.length,c=[];for(a=0;a<b;a++)c[a]=this.holes[a].getPoints();return c};THREE.Shape.prototype.getSpacedPointsHoles=function(){var a,b=this.holes.length,c=[];for(a=0;a<b;a++)c[a]=this.holes[a].getSpacedPoints();return c};THREE.Shape.prototype.extractAllPoints=function(){return{shape:this.getPoints(),holes:this.getPointsHoles()}};THREE.Shape.prototype.extractAllSpacedPoints=function(){return{shape:this.getSpacedPoints(),holes:this.getSpacedPointsHoles()}};
THREE.Shape.Utils={removeHoles:function(a,b){var c=a.concat(),e=c.concat(),f,h,g,j,k,l,m,n,p,o,x=[];for(k=0;k<b.length;k++){l=b[k];e=e.concat(l);h=Number.POSITIVE_INFINITY;for(f=0;f<l.length;f++){p=l[f];o=[];for(n=0;n<c.length;n++)m=c[n],m=p.distanceToSquared(m),o.push(m),m<h&&(h=m,g=f,j=n)}f=j-1>=0?j-1:c.length-1;h=g-1>=0?g-1:l.length-1;var v=[l[g],c[j],c[f]];n=THREE.FontUtils.Triangulate.area(v);var w=[l[g],l[h],c[j]];p=THREE.FontUtils.Triangulate.area(w);o=j;m=g;j+=1;g+=-1;j<0&&(j+=c.length);j%=
c.length;g<0&&(g+=l.length);g%=l.length;f=j-1>=0?j-1:c.length-1;h=g-1>=0?g-1:l.length-1;v=[l[g],c[j],c[f]];v=THREE.FontUtils.Triangulate.area(v);w=[l[g],l[h],c[j]];w=THREE.FontUtils.Triangulate.area(w);n+p>v+w&&(j=o,g=m,j<0&&(j+=c.length),j%=c.length,g<0&&(g+=l.length),g%=l.length,f=j-1>=0?j-1:c.length-1,h=g-1>=0?g-1:l.length-1);n=c.slice(0,j);p=c.slice(j);o=l.slice(g);m=l.slice(0,g);h=[l[g],l[h],c[j]];x.push([l[g],c[j],c[f]]);x.push(h);c=n.concat(o).concat(m).concat(p)}return{shape:c,isolatedPts:x,
allpoints:e}},triangulateShape:function(a,b){var c=THREE.Shape.Utils.removeHoles(a,b),e=c.allpoints,f=c.isolatedPts,c=THREE.FontUtils.Triangulate(c.shape,!1),h,g,j,k;for(h=0;h<c.length;h++){k=c[h];for(g=0;g<3;g++)for(j=0;j<e.length;j++)e[j].equals(k[g])&&(k[g]=j)}for(h=0;h<f.length;h++){k=f[h];for(g=0;g<3;g++)for(j=0;j<e.length;j++)e[j].equals(k[g])&&(k[g]=j)}return c.concat(f)},isClockWise:function(a){return THREE.FontUtils.Triangulate.area(a)<0},b2p0:function(a,b){var c=1-a;return c*c*b},b2p1:function(a,
b){return 2*(1-a)*a*b},b2p2:function(a,b){return a*a*b},b2:function(a,b,c,e){return this.b2p0(a,b)+this.b2p1(a,c)+this.b2p2(a,e)},b3p0:function(a,b){var c=1-a;return c*c*c*b},b3p1:function(a,b){var c=1-a;return 3*c*c*a*b},b3p2:function(a,b){return 3*(1-a)*a*a*b},b3p3:function(a,b){return a*a*a*b},b3:function(a,b,c,e,f){return this.b3p0(a,b)+this.b3p1(a,c)+this.b3p2(a,e)+this.b3p3(a,f)}};THREE.TextPath=function(a,b){THREE.Path.call(this);this.parameters=b||{};this.set(a)};
THREE.TextPath.prototype.set=function(a,b){this.text=a;var b=b||this.parameters,c=b.curveSegments!==void 0?b.curveSegments:4,e=b.font!==void 0?b.font:"helvetiker",f=b.weight!==void 0?b.weight:"normal",h=b.style!==void 0?b.style:"normal";THREE.FontUtils.size=b.size!==void 0?b.size:100;THREE.FontUtils.divisions=c;THREE.FontUtils.face=e;THREE.FontUtils.weight=f;THREE.FontUtils.style=h};
allpoints:e}},triangulateShape:function(a,b){var c=THREE.Shape.Utils.removeHoles(a,b),e=c.allpoints,f=c.isolatedPts,c=THREE.FontUtils.Triangulate(c.shape,!1),h,g,j,k,l={};h=0;for(g=e.length;h<g;h++)k=e[h].x+":"+e[h].y,l[k]!==void 0&&console.log("Duplicate point",k),l[k]=h;h=0;for(g=c.length;h<g;h++){j=c[h];for(e=0;e<3;e++)k=j[e].x+":"+j[e].y,k=l[k],k!==void 0&&(j[e]=k)}h=0;for(g=f.length;h<g;h++){j=f[h];for(e=0;e<3;e++)k=j[e].x+":"+j[e].y,k=l[k],k!==void 0&&(j[e]=k)}return c.concat(f)},isClockWise:function(a){return THREE.FontUtils.Triangulate.area(a)<
0},b2p0:function(a,b){var c=1-a;return c*c*b},b2p1:function(a,b){return 2*(1-a)*a*b},b2p2:function(a,b){return a*a*b},b2:function(a,b,c,e){return this.b2p0(a,b)+this.b2p1(a,c)+this.b2p2(a,e)},b3p0:function(a,b){var c=1-a;return c*c*c*b},b3p1:function(a,b){var c=1-a;return 3*c*c*a*b},b3p2:function(a,b){return 3*(1-a)*a*a*b},b3p3:function(a,b){return a*a*a*b},b3:function(a,b,c,e,f){return this.b3p0(a,b)+this.b3p1(a,c)+this.b3p2(a,e)+this.b3p3(a,f)}};
THREE.TextPath=function(a,b){THREE.Path.call(this);this.parameters=b||{};this.set(a)};THREE.TextPath.prototype.set=function(a,b){this.text=a;var b=b||this.parameters,c=b.curveSegments!==void 0?b.curveSegments:4,e=b.font!==void 0?b.font:"helvetiker",f=b.weight!==void 0?b.weight:"normal",h=b.style!==void 0?b.style:"normal";THREE.FontUtils.size=b.size!==void 0?b.size:100;THREE.FontUtils.divisions=c;THREE.FontUtils.face=e;THREE.FontUtils.weight=f;THREE.FontUtils.style=h};
THREE.TextPath.prototype.toShapes=function(){for(var a=THREE.FontUtils.drawText(this.text).paths,b=[],c=0,e=a.length;c<e;c++)b=b.concat(a[c].toShapes());return b};
THREE.CubeGeometry=function(a,b,c,e,f,h,g,j,k){function l(a,c,b,g,j,k,l,n){var o,p,x=e||1,v=f||1,E=j/2,F=k/2,L=m.vertices.length;if(a=="x"&&c=="y"||a=="y"&&c=="x")o="z";else if(a=="x"&&c=="z"||a=="z"&&c=="x")o="y",v=h||1;else if(a=="z"&&c=="y"||a=="y"&&c=="z")o="x",x=h||1;var M=x+1,O=v+1;j/=x;var Q=k/v;for(p=0;p<O;p++)for(k=0;k<M;k++){var C=new THREE.Vector3;C[a]=(k*j-E)*b;C[c]=(p*Q-F)*g;C[o]=l;m.vertices.push(new THREE.Vertex(C))}for(p=0;p<v;p++)for(k=0;k<x;k++)m.faces.push(new THREE.Face4(k+M*p+
L,k+M*(p+1)+L,k+1+M*(p+1)+L,k+1+M*p+L,null,null,n)),m.faceVertexUvs[0].push([new THREE.UV(k/x,p/v),new THREE.UV(k/x,(p+1)/v),new THREE.UV((k+1)/x,(p+1)/v),new THREE.UV((k+1)/x,p/v)])}THREE.Geometry.call(this);var m=this,n=a/2,p=b/2,o=c/2,j=j?-1:1;if(g!==void 0)if(g instanceof Array)this.materials=g;else{this.materials=[];for(var x=0;x<6;x++)this.materials.push([g])}else this.materials=[];this.sides={px:!0,nx:!0,py:!0,ny:!0,pz:!0,nz:!0};if(k!=void 0)for(var v in k)this.sides[v]!=void 0&&(this.sides[v]=
......
......@@ -303,24 +303,45 @@ THREE.Shape.Utils = {
// To maintain reference to old shape, one must match coordinates, or offset the indices from original arrays. It's probably easier to do the first.
//console.log("triangles",triangles, triangles.length);
//console.log("allpoints",allpoints, allpoints.length);
//console.log( "triangles",triangles, triangles.length );
//console.log( "allpoints",allpoints, allpoints.length );
var v, f, i, face;
var i, il, f, face,
key, index,
allPointsMap = {},
isolatedPointsMap = {};
for ( v = 0; v < triangles.length; v ++ ) {
// prepare all points map
face = triangles[ v ];
for ( i = 0, il = allpoints.length; i < il; i ++ ) {
for ( f = 0; f < 3; f ++ ) { // For 3 pts in faces
key = allpoints[ i ].x + ":" + allpoints[ i ].y;
for ( i = 0; i < allpoints.length; i ++ ) { // Go through all points
if ( allPointsMap[ key ] !== undefined ) {
if ( allpoints[ i ].equals( face[ f ] ) ) { // If matches
console.log( "Duplicate point", key );
face[ f ] = i; // face now has reference to index
}
}
allPointsMap[ key ] = i;
}
// check all face vertices against all points map
for ( i = 0, il = triangles.length; i < il; i ++ ) {
face = triangles[ i ];
for ( f = 0; f < 3; f ++ ) {
key = face[ f ].x + ":" + face[ f ].y;
index = allPointsMap[ key ];
if ( index !== undefined ) {
face[ f ] = index;
}
......@@ -328,19 +349,21 @@ THREE.Shape.Utils = {
}
for ( v = 0; v < isolatedPts.length; v ++ ) {
// check isolated points vertices against all points map
face = isolatedPts[ v ];
for ( i = 0, il = isolatedPts.length; i < il; i ++ ) {
for ( f = 0; f < 3; f ++ ) { // For 3 pts in faces
face = isolatedPts[ i ];
for ( i = 0; i < allpoints.length; i ++ ) { // Go thru all points
for ( f = 0; f < 3; f ++ ) {
if ( allpoints[ i ].equals( face[ f ] ) ) { // If matches
key = face[ f ].x + ":" + face[ f ].y;
face[ f ] = i; // face now has reference to index
index = allPointsMap[ key ];
}
if ( index !== undefined ) {
face[ f ] = index;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册