/* proctree.js Copyright (c) 2012, Paul Brunt All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of tree.js nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PAUL BRUNT BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ (function(window){ var Tree=function(data){ for(var i in data){ if(this.properties[i]!==undefined){ this.properties[i]=data[i]; } } this.properties.rseed=this.properties.seed; this.root=new Branch([0,this.properties.trunkLength,0]); this.root.length=this.properties.initalBranchLength; this.verts=[]; this.faces=[]; this.normals=[]; this.UV=[]; this.vertsTwig=[]; this.normalsTwig=[]; this.facesTwig=[]; this.uvsTwig=[]; this.root.split(null,null,this.properties); this.createForks(); this.createTwigs(); this.doFaces(); this.calcNormals(); }; Tree.prototype.properties={ clumpMax:0.8, clumpMin:0.5, lengthFalloffFactor:0.85, lengthFalloffPower:1, branchFactor:2.0, radiusFalloffRate:0.6, climbRate:1.5, trunkKink:0.00, maxRadius:0.25, treeSteps:2, taperRate:0.95, twistRate:13, segments:6, levels:3, sweepAmount:0, initalBranchLength:0.85, trunkLength:2.5, dropAmount: 0.0, growAmount: 0.0, vMultiplier:0.2, twigScale:2.0, seed:10, rseed:10, random:function(a){ if(!a) a=this.rseed++; return Math.abs(Math.cos(a+a*a)); } }; Tree.prototype.calcNormals=function(){ var normals=this.normals; var faces=this.faces; var verts=this.verts; var allNormals=[]; for(var i=0;i0) angle=2*Math.PI-angle; var segOffset=Math.round((angle/Math.PI/2*segments)); for(var i=0;imatch0){ match0=l; segOffset0=segments-i; } d=normalize(subVec(verts[branch.child1.ring0[i]],branch.child1.head)); l=dot(d,v2); if(segOffset1==undefined || l>match1){ match1=l; segOffset1=segments-i; } } var UVScale=this.properties.maxRadius/branch.radius; for(i=0;ibranch.length) radius=branch.length; var verts=this.verts; var segments=this.properties.segments; var segmentAngle=Math.PI*2/segments; if(!branch.parent){ //create the root of the tree branch.root=[]; var axis=[0,1,0]; for(var i=0;i0.5) adj=scaleVec(adj,-1); var clump=(clumpmax-clumpmin)*r+clumpmin var newdir=normalize(addVec(scaleVec(adj,1-clump),scaleVec(dir,clump))); var newdir2=this.mirrorBranch(newdir,dir,properties); if(r>0.5){ var tmp=newdir; newdir=newdir2; newdir2=tmp; } if(steps>0){ var angle=steps/properties.treeSteps*2*Math.PI*properties.twistRate; newdir2=normalize([Math.sin(angle),r,Math.cos(angle)]); } var growAmount=level*level/(properties.levels*properties.levels)*properties.growAmount; var dropAmount=rLevel*properties.dropAmount var sweepAmount=rLevel*properties.sweepAmount; newdir=normalize(addVec(newdir,[sweepAmount,dropAmount+growAmount,0])); newdir2=normalize(addVec(newdir2,[sweepAmount,dropAmount+growAmount,0])); var head0=addVec(so,scaleVec(newdir,this.length)); var head1=addVec(so,scaleVec(newdir2,this.length)); this.child0=new Branch(head0,this); this.child1=new Branch(head1,this); this.child0.length=Math.pow(this.length,properties.lengthFalloffPower)*properties.lengthFalloffFactor; this.child1.length=Math.pow(this.length,properties.lengthFalloffPower)*properties.lengthFalloffFactor; if(level>0){ if(steps>0){ this.child0.head=addVec(this.head,[(r-0.5)*2*properties.trunkKink,properties.climbRate,(r-0.5)*2*properties.trunkKink]); this.child0.type="trunk"; this.child0.length=this.length*properties.taperRate; this.child0.split(level,steps-1,properties,l1+1,l2); }else{ this.child0.split(level-1,0,properties,l1+1,l2); } this.child1.split(level-1,0,properties,l1,l2+1); } }; var dot=function(v1,v2){ return v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2]; }; var cross=function(v1,v2){ return [v1[1]*v2[2]-v1[2]*v2[1],v1[2]*v2[0]-v1[0]*v2[2],v1[0]*v2[1]-v1[1]*v2[0]]; }; var length=function(v){ return Math.sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]); }; var normalize=function(v){ var l=length(v); return scaleVec(v,1/l); }; var scaleVec=function(v,s){ return [v[0]*s,v[1]*s,v[2]*s]; }; var subVec=function(v1,v2){ return [v1[0]-v2[0],v1[1]-v2[1],v1[2]-v2[2]]; }; var addVec=function(v1,v2){ return [v1[0]+v2[0],v1[1]+v2[1],v1[2]+v2[2]]; }; var vecAxisAngle=function(vec,axis,angle){ //v cos(T) + (axis x v) * sin(T) + axis*(axis . v)(1-cos(T) var cosr=Math.cos(angle); var sinr=Math.sin(angle); return addVec(addVec(scaleVec(vec,cosr),scaleVec(cross(axis,vec),sinr)),scaleVec(axis,dot(axis,vec)*(1-cosr))); }; var scaleInDirection=function(vector,direction,scale){ var currentMag=dot(vector,direction); var change=scaleVec(direction,currentMag*scale-currentMag); return addVec(vector,change); }; Tree.flattenArray=function(input){ var retArray=[]; for(var i=0;i