ThreeDOM.js 42.0 KB
Newer Older
M
Mr.doob 已提交
1
// ThreeDOM.js r36 - http://github.com/mrdoob/three.js
M
Mr.doob 已提交
2
var THREE=THREE||{};THREE.Color=function(a){this.setHex(a)};
3
THREE.Color.prototype={autoUpdate:!0,setRGB:function(a,b,c){this.r=a;this.g=b;this.b=c;if(this.autoUpdate){this.updateHex();this.updateStyleString()}},setHSV:function(a,b,c){var e,d,g,f,i,h;if(c==0)e=d=g=0;else{f=Math.floor(a*6);i=a*6-f;a=c*(1-b);h=c*(1-b*i);b=c*(1-b*(1-i));switch(f){case 1:e=h;d=c;g=a;break;case 2:e=a;d=c;g=b;break;case 3:e=a;d=h;g=c;break;case 4:e=b;d=a;g=c;break;case 5:e=c;d=a;g=h;break;case 6:case 0:e=c;d=b;g=a}}this.r=e;this.g=d;this.b=g;if(this.autoUpdate){this.updateHex();
4 5
this.updateStyleString()}},setHex:function(a){this.hex=~~a&16777215;if(this.autoUpdate){this.updateRGB();this.updateStyleString()}},updateHex:function(){this.hex=~~(this.r*255)<<16^~~(this.g*255)<<8^~~(this.b*255)},updateRGB:function(){this.r=(this.hex>>16&255)/255;this.g=(this.hex>>8&255)/255;this.b=(this.hex&255)/255},updateStyleString:function(){this.__styleString="rgb("+~~(this.r*255)+","+~~(this.g*255)+","+~~(this.b*255)+")"},clone:function(){return new THREE.Color(this.hex)}};
THREE.Vector2=function(a,b){this.set(a||0,b||0)};
M
Mr.doob 已提交
6
THREE.Vector2.prototype={set:function(a,b){this.x=a;this.y=b;return this},copy:function(a){this.set(a.x,a.y);return this},addSelf:function(a){this.set(this.x+a.x,this.y+a.y);return this},add:function(a,b){this.set(a.x+b.x,a.y+b.y);return this},subSelf:function(a){this.set(this.x-a.x,this.y-a.y);return this},sub:function(a,b){this.set(a.x-b.x,a.y-b.y);return this},multiplyScalar:function(a){this.set(this.x*a,this.y*a);return this},negate:function(){this.set(-this.x,-this.y);return this},unit:function(){this.multiplyScalar(1/
7
this.length());return this},length:function(){return Math.sqrt(this.lengthSq())},lengthSq:function(){return this.x*this.x+this.y*this.y},clone:function(){return new THREE.Vector2(this.x,this.y)}};THREE.Vector3=function(a,b,c){this.set(a||0,b||0,c||0)};
M
Mr.doob 已提交
8
THREE.Vector3.prototype={set:function(a,b,c){this.x=a;this.y=b;this.z=c;return this},copy:function(a){this.set(a.x,a.y,a.z);return this},add:function(a,b){this.set(a.x+b.x,a.y+b.y,a.z+b.z);return this},addSelf:function(a){this.set(this.x+a.x,this.y+a.y,this.z+a.z);return this},addScalar:function(a){this.set(this.x+a,this.y+a,this.z+a);return this},sub:function(a,b){this.set(a.x-b.x,a.y-b.y,a.z-b.z);return this},subSelf:function(a){this.set(this.x-a.x,this.y-a.y,this.z-a.z);return this},cross:function(a,
9
b){this.set(a.y*b.z-a.z*b.y,a.z*b.x-a.x*b.z,a.x*b.y-a.y*b.x);return this},crossSelf:function(a){var b=this.x,c=this.y,e=this.z;this.set(c*a.z-e*a.y,e*a.x-b*a.z,b*a.y-c*a.x);return this},multiply:function(a,b){this.set(a.x*b.x,a.y*b.y,a.z*b.z);return this},multiplySelf:function(a){this.set(this.x*a.x,this.y*a.y,this.z*a.z);return this},multiplyScalar:function(a){this.set(this.x*a,this.y*a,this.z*a);return this},divideSelf:function(a){this.set(this.x/a.x,this.y/a.y,this.z/a.z);return this},divideScalar:function(a){this.set(this.x/
M
Mr.doob 已提交
10
a,this.y/a,this.z/a);return this},negate:function(){this.set(-this.x,-this.y,-this.z);return this},dot:function(a){return this.x*a.x+this.y*a.y+this.z*a.z},distanceTo:function(a){return Math.sqrt(this.distanceToSquared(a))},distanceToSquared:function(a){var b=this.x-a.x,c=this.y-a.y;a=this.z-a.z;return b*b+c*c+a*a},length:function(){return Math.sqrt(this.lengthSq())},lengthSq:function(){return this.x*this.x+this.y*this.y+this.z*this.z},lengthManhattan:function(){return this.x+this.y+this.z},normalize:function(){var a=
11
this.length();a>0?this.multiplyScalar(1/a):this.set(0,0,0);return this},setPositionFromMatrix:function(a){this.x=a.n14;this.y=a.n24;this.z=a.n34},setRotationFromMatrix:function(a){this.y=Math.asin(a.n13);var b=Math.cos(this.y);if(Math.abs(b)>1.0E-5){this.x=Math.atan2(-a.n23/b,a.n33/b);this.z=Math.atan2(-a.n13/b,a.n11/b)}else{this.x=0;this.z=Math.atan2(a.n21,a.n22)}},setLength:function(a){return this.normalize().multiplyScalar(a)},isZero:function(){return Math.abs(this.x)<1.0E-4&&Math.abs(this.y)<
12 13
1.0E-4&&Math.abs(this.z)<1.0E-4},clone:function(){return new THREE.Vector3(this.x,this.y,this.z)}};THREE.Vector4=function(a,b,c,e){this.set(a||0,b||0,c||0,e||1)};
THREE.Vector4.prototype={set:function(a,b,c,e){this.x=a;this.y=b;this.z=c;this.w=e;return this},copy:function(a){this.set(a.x,a.y,a.z,a.w||1);return this},add:function(a,b){this.set(a.x+b.x,a.y+b.y,a.z+b.z,a.w+b.w);return this},addSelf:function(a){this.set(this.x+a.x,this.y+a.y,this.z+a.z,this.w+a.w);return this},sub:function(a,b){this.set(a.x-b.x,a.y-b.y,a.z-b.z,a.w-b.w);return this},subSelf:function(a){this.set(this.x-a.x,this.y-a.y,this.z-a.z,this.w-a.w);return this},multiplyScalar:function(a){this.set(this.x*
14
a,this.y*a,this.z*a,this.w*a);return this},divideScalar:function(a){this.set(this.x/a,this.y/a,this.z/a,this.w/a);return this},lerpSelf:function(a,b){this.set(this.x+(a.x-this.x)*b,this.y+(a.y-this.y)*b,this.z+(a.z-this.z)*b,this.w+(a.w-this.w)*b)},clone:function(){return new THREE.Vector4(this.x,this.y,this.z,this.w)}};THREE.Ray=function(a,b){this.origin=a||new THREE.Vector3;this.direction=b||new THREE.Vector3};
M
Mr.doob 已提交
15 16 17
THREE.Ray.prototype={intersectScene:function(a){var b,c,e=a.objects,d=[];a=0;for(b=e.length;a<b;a++){c=e[a];c instanceof THREE.Mesh&&(d=d.concat(this.intersectObject(c)))}d.sort(function(g,f){return g.distance-f.distance});return d},intersectObject:function(a){function b(t,r,C,G){G=G.clone().subSelf(r);C=C.clone().subSelf(r);var K=t.clone().subSelf(r);t=G.dot(G);r=G.dot(C);G=G.dot(K);var x=C.dot(C);C=C.dot(K);K=1/(t*x-r*r);x=(x*G-r*C)*K;t=(t*C-r*G)*K;return x>0&&t>0&&x+t<1}var c,e,d,g,f,i,h,j,m,l,
n,k=a.geometry,p=k.vertices,q=[];c=0;for(e=k.faces.length;c<e;c++){d=k.faces[c];l=this.origin.clone();n=this.direction.clone();h=a.matrixWorld;g=h.multiplyVector3(p[d.a].position.clone());f=h.multiplyVector3(p[d.b].position.clone());i=h.multiplyVector3(p[d.c].position.clone());h=d instanceof THREE.Face4?h.multiplyVector3(p[d.d].position.clone()):null;j=a.matrixRotationWorld.multiplyVector3(d.normal.clone());m=n.dot(j);if(m<0){j=j.dot((new THREE.Vector3).sub(g,l))/m;l=l.addSelf(n.multiplyScalar(j));
if(d instanceof THREE.Face3){if(b(l,g,f,i)){d={distance:this.origin.distanceTo(l),point:l,face:d,object:a};q.push(d)}}else if(d instanceof THREE.Face4&&(b(l,g,f,h)||b(l,f,i,h))){d={distance:this.origin.distanceTo(l),point:l,face:d,object:a};q.push(d)}}}return q}};
18 19
THREE.Rectangle=function(){function a(){g=e-b;f=d-c}var b,c,e,d,g,f,i=!0;this.getX=function(){return b};this.getY=function(){return c};this.getWidth=function(){return g};this.getHeight=function(){return f};this.getLeft=function(){return b};this.getTop=function(){return c};this.getRight=function(){return e};this.getBottom=function(){return d};this.set=function(h,j,m,l){i=!1;b=h;c=j;e=m;d=l;a()};this.addPoint=function(h,j){if(i){i=!1;b=h;c=j;e=h;d=j}else{b=b<h?b:h;c=c<j?c:j;e=e>h?e:h;d=d>j?d:j}a()};
this.add3Points=function(h,j,m,l,n,k){if(i){i=!1;b=h<m?h<n?h:n:m<n?m:n;c=j<l?j<k?j:k:l<k?l:k;e=h>m?h>n?h:n:m>n?m:n;d=j>l?j>k?j:k:l>k?l:k}else{b=h<m?h<n?h<b?h:b:n<b?n:b:m<n?m<b?m:b:n<b?n:b;c=j<l?j<k?j<c?j:c:k<c?k:c:l<k?l<c?l:c:k<c?k:c;e=h>m?h>n?h>e?h:e:n>e?n:e:m>n?m>e?m:e:n>e?n:e;d=j>l?j>k?j>d?j:d:k>d?k:d:l>k?l>d?l:d:k>d?k:d}a()};this.addRectangle=function(h){if(i){i=!1;b=h.getLeft();c=h.getTop();e=h.getRight();d=h.getBottom()}else{b=b<h.getLeft()?b:h.getLeft();c=c<h.getTop()?c:h.getTop();e=e>h.getRight()?
20
e:h.getRight();d=d>h.getBottom()?d:h.getBottom()}a()};this.inflate=function(h){b-=h;c-=h;e+=h;d+=h;a()};this.minSelf=function(h){b=b>h.getLeft()?b:h.getLeft();c=c>h.getTop()?c:h.getTop();e=e<h.getRight()?e:h.getRight();d=d<h.getBottom()?d:h.getBottom();a()};this.instersects=function(h){return Math.min(e,h.getRight())-Math.max(b,h.getLeft())>=0&&Math.min(d,h.getBottom())-Math.max(c,h.getTop())>=0};this.empty=function(){i=!0;d=e=c=b=0;a()};this.isEmpty=function(){return i}};
21
THREE.Matrix3=function(){this.m=[]};THREE.Matrix3.prototype={transpose:function(){var a,b=this.m;a=b[1];b[1]=b[3];b[3]=a;a=b[2];b[2]=b[6];b[6]=a;a=b[5];b[5]=b[7];b[7]=a;return this},transposeIntoArray:function(a){var b=this.m;a[0]=b[0];a[1]=b[3];a[2]=b[6];a[3]=b[1];a[4]=b[4];a[5]=b[7];a[6]=b[2];a[7]=b[5];a[8]=b[8];return this}};
M
Mr.doob 已提交
22 23
THREE.Matrix4=function(a,b,c,e,d,g,f,i,h,j,m,l,n,k,p,q){this.set(a||1,b||0,c||0,e||0,d||0,g||1,f||0,i||0,h||0,j||0,m||1,l||0,n||0,k||0,p||0,q||1);this.flat=Array(16);this.m33=new THREE.Matrix3};
THREE.Matrix4.prototype={set:function(a,b,c,e,d,g,f,i,h,j,m,l,n,k,p,q){this.n11=a;this.n12=b;this.n13=c;this.n14=e;this.n21=d;this.n22=g;this.n23=f;this.n24=i;this.n31=h;this.n32=j;this.n33=m;this.n34=l;this.n41=n;this.n42=k;this.n43=p;this.n44=q;return this},identity:function(){this.set(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1);return this},copy:function(a){this.set(a.n11,a.n12,a.n13,a.n14,a.n21,a.n22,a.n23,a.n24,a.n31,a.n32,a.n33,a.n34,a.n41,a.n42,a.n43,a.n44);return this},lookAt:function(a,b,c){var e=THREE.Matrix4.__v1,
24
d=THREE.Matrix4.__v2,g=THREE.Matrix4.__v3;g.sub(a,b).normalize();if(g.length()===0)g.z=1;e.cross(c,g).normalize();if(e.length()===0){g.x+=1.0E-4;e.cross(c,g).normalize()}d.cross(g,e).normalize();this.n11=e.x;this.n12=d.x;this.n13=g.x;this.n21=e.y;this.n22=d.y;this.n23=g.y;this.n31=e.z;this.n32=d.z;this.n33=g.z;return this},multiplyVector3:function(a){var b=a.x,c=a.y,e=a.z,d=1/(this.n41*b+this.n42*c+this.n43*e+this.n44);a.x=(this.n11*b+this.n12*c+this.n13*e+this.n14)*d;a.y=(this.n21*b+this.n22*c+this.n23*
25
e+this.n24)*d;a.z=(this.n31*b+this.n32*c+this.n33*e+this.n34)*d;return a},multiplyVector4:function(a){var b=a.x,c=a.y,e=a.z,d=a.w;a.x=this.n11*b+this.n12*c+this.n13*e+this.n14*d;a.y=this.n21*b+this.n22*c+this.n23*e+this.n24*d;a.z=this.n31*b+this.n32*c+this.n33*e+this.n34*d;a.w=this.n41*b+this.n42*c+this.n43*e+this.n44*d;return a},rotateAxis:function(a){var b=a.x,c=a.y,e=a.z;a.x=b*this.n11+c*this.n12+e*this.n13;a.y=b*this.n21+c*this.n22+e*this.n23;a.z=b*this.n31+c*this.n32+e*this.n33;a.normalize();
M
Mr.doob 已提交
26 27
return a},crossVector:function(a){var b=new THREE.Vector4;b.x=this.n11*a.x+this.n12*a.y+this.n13*a.z+this.n14*a.w;b.y=this.n21*a.x+this.n22*a.y+this.n23*a.z+this.n24*a.w;b.z=this.n31*a.x+this.n32*a.y+this.n33*a.z+this.n34*a.w;b.w=a.w?this.n41*a.x+this.n42*a.y+this.n43*a.z+this.n44*a.w:1;return b},multiply:function(a,b){var c=a.n11,e=a.n12,d=a.n13,g=a.n14,f=a.n21,i=a.n22,h=a.n23,j=a.n24,m=a.n31,l=a.n32,n=a.n33,k=a.n34,p=a.n41,q=a.n42,t=a.n43,r=a.n44,C=b.n11,G=b.n12,K=b.n13,x=b.n14,o=b.n21,Q=b.n22,
H=b.n23,N=b.n24,O=b.n31,u=b.n32,s=b.n33,I=b.n34;this.n11=c*C+e*o+d*O;this.n12=c*G+e*Q+d*u;this.n13=c*K+e*H+d*s;this.n14=c*x+e*N+d*I+g;this.n21=f*C+i*o+h*O;this.n22=f*G+i*Q+h*u;this.n23=f*K+i*H+h*s;this.n24=f*x+i*N+h*I+j;this.n31=m*C+l*o+n*O;this.n32=m*G+l*Q+n*u;this.n33=m*K+l*H+n*s;this.n34=m*x+l*N+n*I+k;this.n41=p*C+q*o+t*O;this.n42=p*G+q*Q+t*u;this.n43=p*K+q*H+t*s;this.n44=p*x+q*N+t*I+r;return this},multiplyToArray:function(a,b,c){this.multiply(a,b);c[0]=this.n11;c[1]=this.n21;c[2]=this.n31;c[3]=
M
Mr.doob 已提交
28
this.n41;c[4]=this.n12;c[5]=this.n22;c[6]=this.n32;c[7]=this.n42;c[8]=this.n13;c[9]=this.n23;c[10]=this.n33;c[11]=this.n43;c[12]=this.n14;c[13]=this.n24;c[14]=this.n34;c[15]=this.n44;return this},multiplySelf:function(a){this.multiply(this,a);return this},multiplyScalar:function(a){this.n11*=a;this.n12*=a;this.n13*=a;this.n14*=a;this.n21*=a;this.n22*=a;this.n23*=a;this.n24*=a;this.n31*=a;this.n32*=a;this.n33*=a;this.n34*=a;this.n41*=a;this.n42*=a;this.n43*=a;this.n44*=a;return this},determinant:function(){var a=
M
Mr.doob 已提交
29
this.n11,b=this.n12,c=this.n13,e=this.n14,d=this.n21,g=this.n22,f=this.n23,i=this.n24,h=this.n31,j=this.n32,m=this.n33,l=this.n34,n=this.n41,k=this.n42,p=this.n43,q=this.n44;return e*f*j*n-c*i*j*n-e*g*m*n+b*i*m*n+c*g*l*n-b*f*l*n-e*f*h*k+c*i*h*k+e*d*m*k-a*i*m*k-c*d*l*k+a*f*l*k+e*g*h*p-b*i*h*p-e*d*j*p+a*i*j*p+b*d*l*p-a*g*l*p-c*g*h*q+b*f*h*q+c*d*j*q-a*f*j*q-b*d*m*q+a*g*m*q},transpose:function(){var a;a=this.n21;this.n21=this.n12;this.n12=a;a=this.n31;this.n31=this.n13;this.n13=a;a=this.n32;this.n32=
M
Mr.doob 已提交
30 31 32
this.n23;this.n23=a;a=this.n41;this.n41=this.n14;this.n14=a;a=this.n42;this.n42=this.n24;this.n24=a;a=this.n43;this.n43=this.n34;this.n43=a;return this},clone:function(){var a=new THREE.Matrix4;a.n11=this.n11;a.n12=this.n12;a.n13=this.n13;a.n14=this.n14;a.n21=this.n21;a.n22=this.n22;a.n23=this.n23;a.n24=this.n24;a.n31=this.n31;a.n32=this.n32;a.n33=this.n33;a.n34=this.n34;a.n41=this.n41;a.n42=this.n42;a.n43=this.n43;a.n44=this.n44;return a},flatten:function(){this.flat[0]=this.n11;this.flat[1]=this.n21;
this.flat[2]=this.n31;this.flat[3]=this.n41;this.flat[4]=this.n12;this.flat[5]=this.n22;this.flat[6]=this.n32;this.flat[7]=this.n42;this.flat[8]=this.n13;this.flat[9]=this.n23;this.flat[10]=this.n33;this.flat[11]=this.n43;this.flat[12]=this.n14;this.flat[13]=this.n24;this.flat[14]=this.n34;this.flat[15]=this.n44;return this.flat},flattenToArray:function(a){a[0]=this.n11;a[1]=this.n21;a[2]=this.n31;a[3]=this.n41;a[4]=this.n12;a[5]=this.n22;a[6]=this.n32;a[7]=this.n42;a[8]=this.n13;a[9]=this.n23;a[10]=
this.n33;a[11]=this.n43;a[12]=this.n14;a[13]=this.n24;a[14]=this.n34;a[15]=this.n44;return a},flattenToArrayOffset:function(a,b){a[b]=this.n11;a[b+1]=this.n21;a[b+2]=this.n31;a[b+3]=this.n41;a[b+4]=this.n12;a[b+5]=this.n22;a[b+6]=this.n32;a[b+7]=this.n42;a[b+8]=this.n13;a[b+9]=this.n23;a[b+10]=this.n33;a[b+11]=this.n43;a[b+12]=this.n14;a[b+13]=this.n24;a[b+14]=this.n34;a[b+15]=this.n44;return a},setTranslation:function(a,b,c){this.set(1,0,0,a,0,1,0,b,0,0,1,c,0,0,0,1);return this},setScale:function(a,
33 34 35
b,c){this.set(a,0,0,0,0,b,0,0,0,0,c,0,0,0,0,1);return this},setRotationX:function(a){var b=Math.cos(a);a=Math.sin(a);this.set(1,0,0,0,0,b,-a,0,0,a,b,0,0,0,0,1);return this},setRotationY:function(a){var b=Math.cos(a);a=Math.sin(a);this.set(b,0,a,0,0,1,0,0,-a,0,b,0,0,0,0,1);return this},setRotationZ:function(a){var b=Math.cos(a);a=Math.sin(a);this.set(b,-a,0,0,a,b,0,0,0,0,1,0,0,0,0,1);return this},setRotationAxis:function(a,b){var c=Math.cos(b),e=Math.sin(b),d=1-c,g=a.x,f=a.y,i=a.z,h=d*g,j=d*f;this.set(h*
g+c,h*f-e*i,h*i+e*f,0,h*f+e*i,j*f+c,j*i-e*g,0,h*i-e*f,j*i+e*g,d*i*i+c,0,0,0,0,1);return this},setPosition:function(a){this.n14=a.x;this.n24=a.y;this.n34=a.z;return this},setRotationFromEuler:function(a){var b=a.x,c=a.y,e=a.z;a=Math.cos(b);b=Math.sin(b);var d=Math.cos(c);c=Math.sin(c);var g=Math.cos(e);e=Math.sin(e);var f=a*c,i=b*c;this.n11=d*g;this.n12=-d*e;this.n13=c;this.n21=i*g+a*e;this.n22=-i*e+a*g;this.n23=-b*d;this.n31=-f*g+b*e;this.n32=f*e+b*g;this.n33=a*d;return this},setRotationFromQuaternion:function(a){var b=
a.x,c=a.y,e=a.z,d=a.w,g=b+b,f=c+c,i=e+e;a=b*g;var h=b*f;b*=i;var j=c*f;c*=i;e*=i;g*=d;f*=d;d*=i;this.n11=1-(j+e);this.n12=h-d;this.n13=b+f;this.n21=h+d;this.n22=1-(a+e);this.n23=c-g;this.n31=b-f;this.n32=c+g;this.n33=1-(a+j);return this},scale:function(a){var b=a.x,c=a.y;a=a.z;this.n11*=b;this.n12*=c;this.n13*=a;this.n21*=b;this.n22*=c;this.n23*=a;this.n31*=b;this.n32*=c;this.n33*=a;this.n41*=b;this.n42*=c;this.n43*=a;return this},extractPosition:function(a){this.n14=a.n14;this.n24=a.n24;this.n34=
36
a.n34},extractRotation:function(a,b){var c=1/b.x,e=1/b.y,d=1/b.z;this.n11=a.n11*c;this.n21=a.n21*c;this.n31=a.n31*c;this.n12=a.n12*e;this.n22=a.n22*e;this.n32=a.n32*e;this.n13=a.n13*d;this.n23=a.n23*d;this.n33=a.n33*d}};
M
Mr.doob 已提交
37 38
THREE.Matrix4.makeInvert=function(a,b){var c=a.n11,e=a.n12,d=a.n13,g=a.n14,f=a.n21,i=a.n22,h=a.n23,j=a.n24,m=a.n31,l=a.n32,n=a.n33,k=a.n34,p=a.n41,q=a.n42,t=a.n43,r=a.n44;b===undefined&&(b=new THREE.Matrix4);b.n11=h*k*q-j*n*q+j*l*t-i*k*t-h*l*r+i*n*r;b.n12=g*n*q-d*k*q-g*l*t+e*k*t+d*l*r-e*n*r;b.n13=d*j*q-g*h*q+g*i*t-e*j*t-d*i*r+e*h*r;b.n14=g*h*l-d*j*l-g*i*n+e*j*n+d*i*k-e*h*k;b.n21=j*n*p-h*k*p-j*m*t+f*k*t+h*m*r-f*n*r;b.n22=d*k*p-g*n*p+g*m*t-c*k*t-d*m*r+c*n*r;b.n23=g*h*p-d*j*p-g*f*t+c*j*t+d*f*r-c*h*r;
b.n24=d*j*m-g*h*m+g*f*n-c*j*n-d*f*k+c*h*k;b.n31=i*k*p-j*l*p+j*m*q-f*k*q-i*m*r+f*l*r;b.n32=g*l*p-e*k*p-g*m*q+c*k*q+e*m*r-c*l*r;b.n33=d*j*p-g*i*p+g*f*q-c*j*q-e*f*r+c*i*r;b.n34=g*i*m-e*j*m-g*f*l+c*j*l+e*f*k-c*i*k;b.n41=h*l*p-i*n*p-h*m*q+f*n*q+i*m*t-f*l*t;b.n42=e*n*p-d*l*p+d*m*q-c*n*q-e*m*t+c*l*t;b.n43=d*i*p-e*h*p-d*f*q+c*h*q+e*f*t-c*i*t;b.n44=e*h*m-d*i*m+d*f*l-c*h*l-e*f*n+c*i*n;b.multiplyScalar(1/a.determinant());return b};
39 40 41
THREE.Matrix4.makeInvert3x3=function(a){var b=a.m33,c=b.m,e=a.n33*a.n22-a.n32*a.n23,d=-a.n33*a.n21+a.n31*a.n23,g=a.n32*a.n21-a.n31*a.n22,f=-a.n33*a.n12+a.n32*a.n13,i=a.n33*a.n11-a.n31*a.n13,h=-a.n32*a.n11+a.n31*a.n12,j=a.n23*a.n12-a.n22*a.n13,m=-a.n23*a.n11+a.n21*a.n13,l=a.n22*a.n11-a.n21*a.n12;a=a.n11*e+a.n21*f+a.n31*j;if(a==0)throw"matrix not invertible";a=1/a;c[0]=a*e;c[1]=a*d;c[2]=a*g;c[3]=a*f;c[4]=a*i;c[5]=a*h;c[6]=a*j;c[7]=a*m;c[8]=a*l;return b};
THREE.Matrix4.makeFrustum=function(a,b,c,e,d,g){var f;f=new THREE.Matrix4;f.n11=2*d/(b-a);f.n12=0;f.n13=(b+a)/(b-a);f.n14=0;f.n21=0;f.n22=2*d/(e-c);f.n23=(e+c)/(e-c);f.n24=0;f.n31=0;f.n32=0;f.n33=-(g+d)/(g-d);f.n34=-2*g*d/(g-d);f.n41=0;f.n42=0;f.n43=-1;f.n44=0;return f};THREE.Matrix4.makePerspective=function(a,b,c,e){var d;a=c*Math.tan(a*Math.PI/360);d=-a;return THREE.Matrix4.makeFrustum(d*b,a*b,d,a,c,e)};
THREE.Matrix4.makeOrtho=function(a,b,c,e,d,g){var f,i,h,j;f=new THREE.Matrix4;i=b-a;h=c-e;j=g-d;f.n11=2/i;f.n12=0;f.n13=0;f.n14=-((b+a)/i);f.n21=0;f.n22=2/h;f.n23=0;f.n24=-((c+e)/h);f.n31=0;f.n32=0;f.n33=-2/j;f.n34=-((g+d)/j);f.n41=0;f.n42=0;f.n43=0;f.n44=1;return f};THREE.Matrix4.__v1=new THREE.Vector3;THREE.Matrix4.__v2=new THREE.Vector3;THREE.Matrix4.__v3=new THREE.Vector3;
42
THREE.Object3D=function(){this.parent=undefined;this.children=[];this.up=new THREE.Vector3(0,1,0);this.position=new THREE.Vector3;this.rotation=new THREE.Vector3;this.scale=new THREE.Vector3(1,1,1);this.rotationAutoUpdate=!0;this.matrix=new THREE.Matrix4;this.matrixWorld=new THREE.Matrix4;this.matrixRotationWorld=new THREE.Matrix4;this.matrixAutoUpdate=!0;this.matrixWorldNeedsUpdate=!0;this.quaternion=new THREE.Quaternion;this.useQuaternion=!1;this.boundRadius=0;this.boundRadiusScale=1;this.visible=
M
Mr.doob 已提交
43 44 45
!0;this._vector=new THREE.Vector3};
THREE.Object3D.prototype={translate:function(a,b){this.matrix.rotateAxis(b);this.position.addSelf(b.multiplyScalar(a))},translateX:function(a){this.translate(a,this._vector.set(1,0,0))},translateY:function(a){this.translate(a,this._vector.set(0,1,0))},translateZ:function(a){this.translate(a,this._vector.set(0,0,1))},lookAt:function(a){this.matrix.lookAt(this.position,a,this.up);this.rotationAutoUpdate&&this.rotation.setRotationFromMatrix(this.matrix)},addChild:function(a){if(this.children.indexOf(a)===-1){a.parent!==
undefined&&a.parent.removeChild(a);a.parent=this;this.children.push(a);for(var b=this;b instanceof THREE.Scene===!1&&b!==undefined;)b=b.parent;b!==undefined&&b.addChildRecurse(a)}},removeChild:function(a){var b=this.children.indexOf(a);if(b!==-1){a.parent=undefined;this.children.splice(b,1)}},updateMatrix:function(){this.matrix.setPosition(this.position);this.useQuaternion?this.matrix.setRotationFromQuaternion(this.quaternion):this.matrix.setRotationFromEuler(this.rotation);if(this.scale.x!==1||this.scale.y!==
46 47
1||this.scale.z!==1){this.matrix.scale(this.scale);this.boundRadiusScale=Math.max(this.scale.x,Math.max(this.scale.y,this.scale.z))}this.matrixWorldNeedsUpdate=!0},update:function(a,b,c){this.matrixAutoUpdate&&this.updateMatrix();if(this.matrixWorldNeedsUpdate||b){a?this.matrixWorld.multiply(a,this.matrix):this.matrixWorld.copy(this.matrix);this.matrixRotationWorld.extractRotation(this.matrixWorld,this.scale);this.matrixWorldNeedsUpdate=!1;b=!0}a=0;for(var e=this.children.length;a<e;a++)this.children[a].update(this.matrixWorld,
b,c)}};THREE.Quaternion=function(a,b,c,e){this.set(a||0,b||0,c||0,e!==undefined?e:1)};
48 49 50
THREE.Quaternion.prototype={set:function(a,b,c,e){this.x=a;this.y=b;this.z=c;this.w=e;return this},setFromEuler:function(a){var b=0.5*Math.PI/360,c=a.x*b,e=a.y*b,d=a.z*b;a=Math.cos(e);e=Math.sin(e);b=Math.cos(-d);d=Math.sin(-d);var g=Math.cos(c);c=Math.sin(c);var f=a*b,i=e*d;this.w=f*g-i*c;this.x=f*c+i*g;this.y=e*b*g+a*d*c;this.z=a*d*g-e*b*c;return this},calculateW:function(){this.w=-Math.sqrt(Math.abs(1-this.x*this.x-this.y*this.y-this.z*this.z));return this},inverse:function(){this.x*=-1;this.y*=
-1;this.z*=-1;return this},length:function(){return Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z+this.w*this.w)},normalize:function(){var a=Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z+this.w*this.w);if(a==0)this.w=this.z=this.y=this.x=0;else{a=1/a;this.x*=a;this.y*=a;this.z*=a;this.w*=a}return this},multiplySelf:function(a){var b=this.x,c=this.y,e=this.z,d=this.w,g=a.x,f=a.y,i=a.z;a=a.w;this.x=b*a+d*g+c*i-e*f;this.y=c*a+d*f+e*g-b*i;this.z=e*a+d*i+b*f-c*g;this.w=d*a-b*g-c*f-e*i;return this},
multiplyVector3:function(a,b){b||(b=a);var c=a.x,e=a.y,d=a.z,g=this.x,f=this.y,i=this.z,h=this.w,j=h*c+f*d-i*e,m=h*e+i*c-g*d,l=h*d+g*e-f*c;c=-g*c-f*e-i*d;b.x=j*h+c*-g+m*-i-l*-f;b.y=m*h+c*-f+l*-g-j*-i;b.z=l*h+c*-i+j*-f-m*-g;return b}};
51
THREE.Quaternion.slerp=function(a,b,c,e){var d=a.w*b.w+a.x*b.x+a.y*b.y+a.z*b.z;if(Math.abs(d)>=1){c.w=a.w;c.x=a.x;c.y=a.y;c.z=a.z;return c}var g=Math.acos(d),f=Math.sqrt(1-d*d);if(Math.abs(f)<0.0010){c.w=0.5*(a.w+b.w);c.x=0.5*(a.x+b.x);c.y=0.5*(a.y+b.y);c.z=0.5*(a.z+b.z);return c}d=Math.sin((1-e)*g)/f;e=Math.sin(e*g)/f;c.w=a.w*d+b.w*e;c.x=a.x*d+b.x*e;c.y=a.y*d+b.y*e;c.z=a.z*d+b.z*e;return c};THREE.Vertex=function(a){this.position=a||new THREE.Vector3};
52 53 54 55 56 57
THREE.Face3=function(a,b,c,e,d,g){this.a=a;this.b=b;this.c=c;this.normal=e instanceof THREE.Vector3?e:new THREE.Vector3;this.vertexNormals=e instanceof Array?e:[];this.color=d instanceof THREE.Color?d:new THREE.Color;this.vertexColors=d instanceof Array?d:[];this.materials=g instanceof Array?g:[g];this.centroid=new THREE.Vector3};
THREE.Face4=function(a,b,c,e,d,g,f){this.a=a;this.b=b;this.c=c;this.d=e;this.normal=d instanceof THREE.Vector3?d:new THREE.Vector3;this.vertexNormals=d instanceof Array?d:[];this.color=g instanceof THREE.Color?g:new THREE.Color;this.vertexColors=g instanceof Array?g:[];this.materials=f instanceof Array?f:[f];this.centroid=new THREE.Vector3};THREE.UV=function(a,b){this.set(a||0,b||0)};THREE.UV.prototype={set:function(a,b){this.u=a;this.v=b;return this},copy:function(a){this.set(a.u,a.v);return this}};
THREE.AnimationHandler=function(){var a=[],b={},c={};c.update=function(d){for(var g=0;g<a.length;g++)a[g].update(d)};c.addToUpdate=function(d){a.indexOf(d)===-1&&a.push(d)};c.removeFromUpdate=function(d){d=a.indexOf(d);d!==-1&&a.splice(d,1)};c.add=function(d){b[d.name]!==undefined&&console.log("THREE.AnimationHandler.add: Warning! "+d.name+" already exists in library. Overwriting.");b[d.name]=d;if(d.initialized!==!0){for(var g=0;g<d.hierarchy.length;g++){for(var f=0;f<d.hierarchy[g].keys.length;f++){if(d.hierarchy[g].keys[f].time<
0)d.hierarchy[g].keys[f].time=0;if(d.hierarchy[g].keys[f].rot!==undefined&&!(d.hierarchy[g].keys[f].rot instanceof THREE.Quaternion)){var i=d.hierarchy[g].keys[f].rot;d.hierarchy[g].keys[f].rot=new THREE.Quaternion(i[0],i[1],i[2],i[3])}}if(d.hierarchy[g].keys[0].morphTargets!==undefined){i={};for(f=0;f<d.hierarchy[g].keys.length;f++)for(var h=0;h<d.hierarchy[g].keys[f].morphTargets.length;h++){var j=d.hierarchy[g].keys[f].morphTargets[h];i[j]=-1}d.hierarchy[g].usedMorphTargets=i;for(f=0;f<d.hierarchy[g].keys.length;f++){var m=
{};for(j in i){for(h=0;h<d.hierarchy[g].keys[f].morphTargets.length;h++)if(d.hierarchy[g].keys[f].morphTargets[h]===j){m[j]=d.hierarchy[g].keys[f].morphTargetsInfluences[h];break}h===d.hierarchy[g].keys[f].morphTargets.length&&(m[j]=0)}d.hierarchy[g].keys[f].morphTargetsInfluences=m}}for(f=1;f<d.hierarchy[g].keys.length;f++)if(d.hierarchy[g].keys[f].time===d.hierarchy[g].keys[f-1].time){d.hierarchy[g].keys.splice(f,1);f--}for(f=1;f<d.hierarchy[g].keys.length;f++)d.hierarchy[g].keys[f].index=f}f=parseInt(d.length*
d.fps,10);d.JIT={};d.JIT.hierarchy=[];for(g=0;g<d.hierarchy.length;g++)d.JIT.hierarchy.push(Array(f));d.initialized=!0}};c.get=function(d){if(typeof d==="string")if(b[d])return b[d];else{console.log("THREE.AnimationHandler.get: Couldn't find animation "+d);return null}};c.parse=function(d){var g=[];if(d instanceof THREE.SkinnedMesh)for(var f=0;f<d.bones.length;f++)g.push(d.bones[f]);else e(d,g);return g};var e=function(d,g){g.push(d);for(var f=0;f<d.children.length;f++)e(d.children[f],g)};c.LINEAR=
58 59
0;c.CATMULLROM=1;c.CATMULLROM_FORWARD=2;return c}();THREE.Animation=function(a,b,c,e){this.root=a;this.data=THREE.AnimationHandler.get(b);this.hierarchy=THREE.AnimationHandler.parse(a);this.currentTime=0;this.timeScale=1;this.isPlaying=!1;this.isPaused=!0;this.loop=!0;this.interpolationType=c!==undefined?c:THREE.AnimationHandler.LINEAR;this.JITCompile=e!==undefined?e:!0;this.points=[];this.target=new THREE.Vector3};
THREE.Animation.prototype.play=function(a,b){if(!this.isPlaying){this.isPlaying=!0;this.loop=a!==undefined?a:!0;this.currentTime=b!==undefined?b:0;var c,e=this.hierarchy.length,d;for(c=0;c<e;c++){d=this.hierarchy[c];if(this.interpolationType!==THREE.AnimationHandler.CATMULLROM_FORWARD)d.useQuaternion=!0;d.matrixAutoUpdate=!0;if(d.animationCache===undefined){d.animationCache={};d.animationCache.prevKey={pos:0,rot:0,scl:0};d.animationCache.nextKey={pos:0,rot:0,scl:0};d.animationCache.originalMatrix=
60
d instanceof THREE.Bone?d.skinMatrix:d.matrix}var g=d.animationCache.prevKey;d=d.animationCache.nextKey;g.pos=this.data.hierarchy[c].keys[0];g.rot=this.data.hierarchy[c].keys[0];g.scl=this.data.hierarchy[c].keys[0];d.pos=this.getNextKeyWith("pos",c,1);d.rot=this.getNextKeyWith("rot",c,1);d.scl=this.getNextKeyWith("scl",c,1)}this.update(0)}this.isPaused=!1;THREE.AnimationHandler.addToUpdate(this)};
61 62
THREE.Animation.prototype.pause=function(){this.isPaused?THREE.AnimationHandler.addToUpdate(this):THREE.AnimationHandler.removeFromUpdate(this);this.isPaused=!this.isPaused};
THREE.Animation.prototype.stop=function(){this.isPlaying=!1;this.isPaused=!1;THREE.AnimationHandler.removeFromUpdate(this);for(var a=0;a<this.hierarchy.length;a++)if(this.hierarchy[a].animationCache!==undefined){if(this.hierarchy[a]instanceof THREE.Bone)this.hierarchy[a].skinMatrix=this.hierarchy[a].animationCache.originalMatrix;else this.hierarchy[a].matrix=this.hierarchy[a].animationCache.originalMatrix;delete this.hierarchy[a].animationCache}};
M
Mr.doob 已提交
63 64
THREE.Animation.prototype.update=function(a){if(this.isPlaying){var b=["pos","rot","scl"],c,e,d,g,f,i,h,j,m=this.data.JIT.hierarchy,l,n;this.currentTime+=a*this.timeScale;n=this.currentTime;l=this.currentTime%=this.data.length;j=parseInt(Math.min(l*this.data.fps,this.data.length*this.data.fps),10);for(var k=0,p=this.hierarchy.length;k<p;k++){a=this.hierarchy[k];h=a.animationCache;if(this.JITCompile&&m[k][j]!==undefined)if(a instanceof THREE.Bone){a.skinMatrix=m[k][j];a.matrixAutoUpdate=!1;a.matrixWorldNeedsUpdate=
!1}else{a.matrix=m[k][j];a.matrixAutoUpdate=!1;a.matrixWorldNeedsUpdate=!0}else{if(this.JITCompile)if(a instanceof THREE.Bone)a.skinMatrix=a.animationCache.originalMatrix;else a.matrix=a.animationCache.originalMatrix;for(var q=0;q<3;q++){c=b[q];f=h.prevKey[c];i=h.nextKey[c];if(i.time<=n){if(l<n)if(this.loop){f=this.data.hierarchy[k].keys[0];for(i=this.getNextKeyWith(c,k,1);i.time<l;){f=i;i=this.getNextKeyWith(c,k,i.index+1)}}else{this.stop();return}else{do{f=i;i=this.getNextKeyWith(c,k,i.index+1)}while(i.time<
65 66 67 68 69
l)}h.prevKey[c]=f;h.nextKey[c]=i}a.matrixAutoUpdate=!0;a.matrixWorldNeedsUpdate=!0;e=(l-f.time)/(i.time-f.time);d=f[c];g=i[c];if(e<0||e>1){console.log("THREE.Animation.update: Warning! Scale out of bounds:"+e+" on bone "+k);e=e<0?0:1}if(c==="pos"){c=a.position;if(this.interpolationType===THREE.AnimationHandler.LINEAR){c.x=d[0]+(g[0]-d[0])*e;c.y=d[1]+(g[1]-d[1])*e;c.z=d[2]+(g[2]-d[2])*e}else if(this.interpolationType===THREE.AnimationHandler.CATMULLROM||this.interpolationType===THREE.AnimationHandler.CATMULLROM_FORWARD){this.points[0]=
this.getPrevKeyWith("pos",k,f.index-1).pos;this.points[1]=d;this.points[2]=g;this.points[3]=this.getNextKeyWith("pos",k,i.index+1).pos;e=e*0.33+0.33;d=this.interpolateCatmullRom(this.points,e);c.x=d[0];c.y=d[1];c.z=d[2];if(this.interpolationType===THREE.AnimationHandler.CATMULLROM_FORWARD){e=this.interpolateCatmullRom(this.points,e*1.01);this.target.set(e[0],e[1],e[2]);this.target.subSelf(c);this.target.y=0;this.target.normalize();e=Math.atan2(this.target.x,this.target.z);a.rotation.set(0,e,0)}}}else if(c===
"rot")THREE.Quaternion.slerp(d,g,a.quaternion,e);else if(c==="scl"){c=a.scale;c.x=d[0]+(g[0]-d[0])*e;c.y=d[1]+(g[1]-d[1])*e;c.z=d[2]+(g[2]-d[2])*e}}}}if(this.JITCompile&&m[0][j]===undefined){this.hierarchy[0].update(undefined,!0);for(k=0;k<this.hierarchy.length;k++)m[k][j]=this.hierarchy[k]instanceof THREE.Bone?this.hierarchy[k].skinMatrix.clone():this.hierarchy[k].matrix.clone()}}};
THREE.Animation.prototype.interpolateCatmullRom=function(a,b){var c=[],e=[],d,g,f,i,h,j;d=(a.length-1)*b;g=Math.floor(d);d-=g;c[0]=g==0?g:g-1;c[1]=g;c[2]=g>a.length-2?g:g+1;c[3]=g>a.length-3?g:g+2;g=a[c[0]];i=a[c[1]];h=a[c[2]];j=a[c[3]];c=d*d;f=d*c;e[0]=this.interpolate(g[0],i[0],h[0],j[0],d,c,f);e[1]=this.interpolate(g[1],i[1],h[1],j[1],d,c,f);e[2]=this.interpolate(g[2],i[2],h[2],j[2],d,c,f);return e};
THREE.Animation.prototype.interpolate=function(a,b,c,e,d,g,f){a=(c-a)*0.5;e=(e-b)*0.5;return(2*(b-c)+a+e)*f+(-3*(b-c)-2*a-e)*g+a*d+b};THREE.Animation.prototype.getNextKeyWith=function(a,b,c){var e=this.data.hierarchy[b].keys;if(this.interpolationType===THREE.AnimationHandler.CATMULLROM||this.interpolationType===THREE.AnimationHandler.CATMULLROM_FORWARD)c=c<e.length-1?c:e.length-1;else c%=e.length;for(;c<e.length;c++)if(e[c][a]!==undefined)return e[c];return this.data.hierarchy[b].keys[0]};
70 71 72
THREE.Animation.prototype.getPrevKeyWith=function(a,b,c){var e=this.data.hierarchy[b].keys;for(c=this.interpolationType===THREE.AnimationHandler.CATMULLROM||this.interpolationType===THREE.AnimationHandler.CATMULLROM_FORWARD?c>0?c:0:c>=0?c:c+e.length;c>=0;c--)if(e[c][a]!==undefined)return e[c];return this.data.hierarchy[b].keys[e.length-1]};
THREE.Camera=function(a,b,c,e,d){THREE.Object3D.call(this);this.fov=a||50;this.aspect=b||1;this.near=c||0.1;this.far=e||2E3;this.target=d||new THREE.Object3D;this.useTarget=!0;this.matrixWorldInverse=new THREE.Matrix4;this.projectionMatrix=null;this.updateProjectionMatrix()};THREE.Camera.prototype=new THREE.Object3D;THREE.Camera.prototype.constructor=THREE.Camera;THREE.Camera.prototype.supr=THREE.Object3D.prototype;
THREE.Camera.prototype.translate=function(a,b){this.matrix.rotateAxis(b);this.position.addSelf(b.multiplyScalar(a));this.target.position.addSelf(b.multiplyScalar(a))};THREE.Camera.prototype.updateProjectionMatrix=function(){this.projectionMatrix=THREE.Matrix4.makePerspective(this.fov,this.aspect,this.near,this.far)};THREE.Camera.prototype.updateMatrix=function(){this.update(undefined,!0)};
73 74
THREE.Camera.prototype.update=function(a,b,c){if(this.useTarget){this.matrix.lookAt(this.position,this.target.position,this.up);this.matrix.setPosition(this.position);a?this.matrixWorld.multiply(a,this.matrix):this.matrixWorld.copy(this.matrix);THREE.Matrix4.makeInvert(this.matrixWorld,this.matrixWorldInverse);b=!0}else{this.matrixAutoUpdate&&(b|=this.updateMatrix());if(b||this.matrixWorldNeedsUpdate){a?this.matrixWorld.multiply(a,this.matrix):this.matrixWorld.copy(this.matrix);this.matrixWorldNeedsUpdate=
!1;b=!0;THREE.Matrix4.makeInvert(this.matrixWorld,this.matrixWorldInverse)}}for(a=0;a<this.children.length;a++)this.children[a].update(this.matrixWorld,b,c)};THREE.ParticleDOMMaterial=function(a){this.id=THREE.MaterialCounter.value++;this.domElement=a};THREE.Particle=function(a){THREE.Object3D.call(this);this.materials=a instanceof Array?a:[a];this.matrixAutoUpdate=!1};THREE.Particle.prototype=new THREE.Object3D;THREE.Particle.prototype.constructor=THREE.Particle;
75
THREE.Bone=function(a){THREE.Object3D.call(this);this.skin=a;this.skinMatrix=new THREE.Matrix4;this.hasNoneBoneChildren=!1};THREE.Bone.prototype=new THREE.Object3D;THREE.Bone.prototype.constructor=THREE.Bone;THREE.Bone.prototype.supr=THREE.Object3D.prototype;
76
THREE.Bone.prototype.update=function(a,b,c){this.matrixAutoUpdate&&(b|=this.updateMatrix());if(b||this.matrixWorldNeedsUpdate){a?this.skinMatrix.multiply(a,this.matrix):this.skinMatrix.copy(this.matrix);this.matrixWorldNeedsUpdate=!1;b=!0}var e,d=this.children.length;if(this.hasNoneBoneChildren){this.matrixWorld.multiply(this.skin.matrixWorld,this.skinMatrix);for(e=0;e<d;e++){a=this.children[e];a instanceof THREE.Bone?a.update(this.skinMatrix,b,c):a.update(this.matrixWorld,!0,c)}}else for(e=0;e<d;e++)this.children[e].update(this.skinMatrix,
77
b,c)};THREE.Bone.prototype.addChild=function(a){if(this.children.indexOf(a)===-1){a.parent!==undefined&&a.parent.removeChild(a);a.parent=this;this.children.push(a);if(!(a instanceof THREE.Bone))this.hasNoneBoneChildren=!0}};
78 79
THREE.Sound=function(a,b,c,e){THREE.Object3D.call(this);this.isLoaded=!1;this.isAddedToDOM=!1;this.isPlaying=!1;this.duration=-1;this.radius=b!==undefined?Math.abs(b):100;this.volume=Math.min(1,Math.max(0,c!==undefined?c:1));this.domElement=document.createElement("audio");this.domElement.volume=0;this.domElement.pan=0;this.domElement.loop=e!==undefined?e:!0;this.sources=a instanceof Array?a:[a];var d;c=this.sources.length;for(a=0;a<c;a++){b=this.sources[a];b.toLowerCase();if(b.indexOf(".mp3")!==-1)d=
"audio/mpeg";else if(b.indexOf(".ogg")!==-1)d="audio/ogg";else b.indexOf(".wav")!==-1&&(d="audio/wav");if(this.domElement.canPlayType(d)){d=document.createElement("source");d.src=this.sources[a];this.domElement.THREESound=this;this.domElement.appendChild(d);this.domElement.addEventListener("canplay",this.onLoad,!0);this.domElement.load();break}}};THREE.Sound.prototype=new THREE.Object3D;THREE.Sound.prototype.constructor=THREE.Sound;THREE.Sound.prototype.supr=THREE.Object3D.prototype;
80 81
THREE.Sound.prototype.onLoad=function(){var a=this.THREESound;if(!a.isLoaded){this.removeEventListener("canplay",this.onLoad,!0);a.isLoaded=!0;a.duration=this.duration;a.isPlaying&&a.play()}};THREE.Sound.prototype.addToDOM=function(a){this.isAddedToDOM=!0;a.appendChild(this.domElement)};THREE.Sound.prototype.play=function(a){this.isPlaying=!0;if(this.isLoaded){this.domElement.play();if(a)this.domElement.currentTime=a%this.duration}};THREE.Sound.prototype.pause=function(){this.isPlaying=!1;this.domElement.pause()};
THREE.Sound.prototype.stop=function(){this.isPlaying=!1;this.domElement.pause();this.domElement.currentTime=0};THREE.Sound.prototype.calculateVolumeAndPan=function(a){a=a.length();this.domElement.volume=a<=this.radius?this.volume*(1-a/this.radius):0};
82
THREE.Sound.prototype.update=function(a,b,c){if(this.matrixAutoUpdate){this.matrix.setPosition(this.position);b=!0}if(b||this.matrixWorldNeedsUpdate){a?this.matrixWorld.multiply(a,this.matrix):this.matrixWorld.copy(this.matrix);this.matrixWorldNeedsUpdate=!1;b=!0}var e=this.children.length;for(a=0;a<e;a++)this.children[a].update(this.matrixWorld,b,c)};
83 84 85 86
THREE.Scene=function(){THREE.Object3D.call(this);this.matrixAutoUpdate=!1;this.fog=null;this.objects=[];this.lights=[];this.sounds=[];this.__objectsAdded=[];this.__objectsRemoved=[]};THREE.Scene.prototype=new THREE.Object3D;THREE.Scene.prototype.constructor=THREE.Scene;THREE.Scene.prototype.supr=THREE.Object3D.prototype;THREE.Scene.prototype.addChild=function(a){this.supr.addChild.call(this,a);this.addChildRecurse(a)};
THREE.Scene.prototype.addChildRecurse=function(a){if(a instanceof THREE.Light)this.lights.indexOf(a)===-1&&this.lights.push(a);else if(a instanceof THREE.Sound)this.sounds.indexOf(a)===-1&&this.sounds.push(a);else if(!(a instanceof THREE.Camera||a instanceof THREE.Bone)&&this.objects.indexOf(a)===-1){this.objects.push(a);this.__objectsAdded.push(a)}for(var b=0;b<a.children.length;b++)this.addChildRecurse(a.children[b])};
THREE.Scene.prototype.removeChild=function(a){this.supr.removeChild.call(this,a);this.removeChildRecurse(a)};THREE.Scene.prototype.removeChildRecurse=function(a){if(a instanceof THREE.Light){var b=this.lights.indexOf(a);b!==-1&&this.lights.splice(b,1)}else if(a instanceof THREE.Sound){b=this.sounds.indexOf(a);b!==-1&&this.sounds.splice(b,1)}else if(!(a instanceof THREE.Camera)){b=this.objects.indexOf(a);if(b!==-1){this.objects.splice(b,1);this.__objectsRemoved.push(a)}}for(b=0;b<a.children.length;b++)this.removeChildRecurse(a.children[b])};
THREE.Scene.prototype.addObject=THREE.Scene.prototype.addChild;THREE.Scene.prototype.removeObject=THREE.Scene.prototype.removeChild;THREE.Scene.prototype.addLight=THREE.Scene.prototype.addChild;THREE.Scene.prototype.removeLight=THREE.Scene.prototype.removeChild;
M
Mr.doob 已提交
87 88 89 90 91 92 93 94 95 96 97 98
THREE.Projector=function(){function a(){var u=h[i]=h[i]||new THREE.RenderableVertex;i++;return u}function b(u,s){return s.z-u.z}function c(u,s){var I=0,J=1,M=u.z+u.w,E=s.z+s.w,w=-u.z+u.w,z=-s.z+s.w;if(M>=0&&E>=0&&w>=0&&z>=0)return!0;else if(M<0&&E<0||w<0&&z<0)return!1;else{if(M<0)I=Math.max(I,M/(M-E));else E<0&&(J=Math.min(J,M/(M-E)));if(w<0)I=Math.max(I,w/(w-z));else z<0&&(J=Math.min(J,w/(w-z)));if(J<I)return!1;else{u.lerpSelf(s,I);s.lerpSelf(u,1-J);return!0}}}var e,d,g=[],f,i,h=[],j,m,l=[],n,k=
[],p,q,t=[],r,C,G=[],K=new THREE.Vector4,x=new THREE.Vector4,o=new THREE.Matrix4,Q=new THREE.Matrix4,H=[new THREE.Vector4,new THREE.Vector4,new THREE.Vector4,new THREE.Vector4,new THREE.Vector4,new THREE.Vector4],N=new THREE.Vector4,O=new THREE.Vector4;this.projectVector=function(u,s){o.multiply(s.projectionMatrix,s.matrixWorldInverse);o.multiplyVector3(u);return u};this.unprojectVector=function(u,s){o.multiply(s.matrixWorld,THREE.Matrix4.makeInvert(s.projectionMatrix));o.multiplyVector3(u);return u};
this.projectObjects=function(u,s,I){s=[];var J,M,E;d=0;M=u.objects;u=0;for(J=M.length;u<J;u++){E=M[u];var w;if(!(w=!E.visible))if(w=E instanceof THREE.Mesh){a:{w=void 0;for(var z=E.matrixWorld,A=-E.geometry.boundingSphere.radius*Math.max(E.scale.x,Math.max(E.scale.y,E.scale.z)),D=0;D<6;D++){w=H[D].x*z.n14+H[D].y*z.n24+H[D].z*z.n34+H[D].w;if(w<=A){w=!1;break a}}w=!0}w=!w}if(!w){w=g[d]=g[d]||new THREE.RenderableObject;d++;e=w;K.copy(E.position);o.multiplyVector3(K);e.object=E;e.z=K.z;s.push(e)}}I&&
s.sort(b);return s};this.projectScene=function(u,s,I){var J=[],M=s.near,E=s.far,w,z,A,D,v,F,B,L,P,y,R,U,W,X,S,V,T;C=q=n=m=0;s.matrixAutoUpdate&&s.updateMatrix();u.update(undefined,!1,s);o.multiply(s.projectionMatrix,s.matrixWorldInverse);H[0].set(o.n41-o.n11,o.n42-o.n12,o.n43-o.n13,o.n44-o.n14);H[1].set(o.n41+o.n11,o.n42+o.n12,o.n43+o.n13,o.n44+o.n14);H[2].set(o.n41+o.n21,o.n42+o.n22,o.n43+o.n23,o.n44+o.n24);H[3].set(o.n41-o.n21,o.n42-o.n22,o.n43-o.n23,o.n44-o.n24);H[4].set(o.n41-o.n31,o.n42-o.n32,
o.n43-o.n33,o.n44-o.n34);H[5].set(o.n41+o.n31,o.n42+o.n32,o.n43+o.n33,o.n44+o.n34);for(w=0;w<6;w++){P=H[w];P.divideScalar(Math.sqrt(P.x*P.x+P.y*P.y+P.z*P.z))}P=this.projectObjects(u,s,!0);u=0;for(w=P.length;u<w;u++){y=P[u].object;if(y.visible){R=y.matrixWorld;U=y.matrixRotationWorld;W=y.materials;X=y.overdraw;i=0;if(y instanceof THREE.Mesh){S=y.geometry;D=S.vertices;V=S.faces;S=S.faceVertexUvs;z=0;for(A=D.length;z<A;z++){f=a();f.positionWorld.copy(D[z].position);R.multiplyVector3(f.positionWorld);
f.positionScreen.copy(f.positionWorld);o.multiplyVector4(f.positionScreen);f.positionScreen.x/=f.positionScreen.w;f.positionScreen.y/=f.positionScreen.w;f.visible=f.positionScreen.z>M&&f.positionScreen.z<E}D=0;for(z=V.length;D<z;D++){A=V[D];if(A instanceof THREE.Face3){v=h[A.a];F=h[A.b];B=h[A.c];if(v.visible&&F.visible&&B.visible&&(y.doubleSided||y.flipSided!=(B.positionScreen.x-v.positionScreen.x)*(F.positionScreen.y-v.positionScreen.y)-(B.positionScreen.y-v.positionScreen.y)*(F.positionScreen.x-
v.positionScreen.x)<0)){L=l[m]=l[m]||new THREE.RenderableFace3;m++;j=L;j.v1.copy(v);j.v2.copy(F);j.v3.copy(B)}else continue}else if(A instanceof THREE.Face4){v=h[A.a];F=h[A.b];B=h[A.c];L=h[A.d];if(v.visible&&F.visible&&B.visible&&L.visible&&(y.doubleSided||y.flipSided!=((L.positionScreen.x-v.positionScreen.x)*(F.positionScreen.y-v.positionScreen.y)-(L.positionScreen.y-v.positionScreen.y)*(F.positionScreen.x-v.positionScreen.x)<0||(F.positionScreen.x-B.positionScreen.x)*(L.positionScreen.y-B.positionScreen.y)-
(F.positionScreen.y-B.positionScreen.y)*(L.positionScreen.x-B.positionScreen.x)<0))){T=k[n]=k[n]||new THREE.RenderableFace4;n++;j=T;j.v1.copy(v);j.v2.copy(F);j.v3.copy(B);j.v4.copy(L)}else continue}j.normalWorld.copy(A.normal);U.multiplyVector3(j.normalWorld);j.centroidWorld.copy(A.centroid);R.multiplyVector3(j.centroidWorld);j.centroidScreen.copy(j.centroidWorld);o.multiplyVector3(j.centroidScreen);B=A.vertexNormals;v=0;for(F=B.length;v<F;v++){L=j.vertexNormalsWorld[v];L.copy(B[v]);U.multiplyVector3(L)}v=
0;for(F=S.length;v<F;v++)if(T=S[v][D]){B=0;for(L=T.length;B<L;B++)j.uvs[v][B]=T[B]}j.meshMaterials=W;j.faceMaterials=A.materials;j.overdraw=X;j.z=j.centroidScreen.z;J.push(j)}}else if(y instanceof THREE.Line){Q.multiply(o,R);D=y.geometry.vertices;v=a();v.positionScreen.copy(D[0].position);Q.multiplyVector4(v.positionScreen);z=1;for(A=D.length;z<A;z++){v=a();v.positionScreen.copy(D[z].position);Q.multiplyVector4(v.positionScreen);F=h[i-2];N.copy(v.positionScreen);O.copy(F.positionScreen);if(c(N,O)){N.multiplyScalar(1/
N.w);O.multiplyScalar(1/O.w);R=t[q]=t[q]||new THREE.RenderableLine;q++;p=R;p.v1.positionScreen.copy(N);p.v2.positionScreen.copy(O);p.z=Math.max(N.z,O.z);p.materials=y.materials;J.push(p)}}}else if(y instanceof THREE.Particle){x.set(y.position.x,y.position.y,y.position.z,1);o.multiplyVector4(x);x.z/=x.w;if(x.z>0&&x.z<1){R=G[C]=G[C]||new THREE.RenderableParticle;C++;r=R;r.x=x.x/x.w;r.y=x.y/x.w;r.z=x.z;r.rotation=y.rotation.z;r.scale.x=y.scale.x*Math.abs(r.x-(x.x+s.projectionMatrix.n11)/(x.w+s.projectionMatrix.n14));
r.scale.y=y.scale.y*Math.abs(r.y-(x.y+s.projectionMatrix.n22)/(x.w+s.projectionMatrix.n24));r.materials=y.materials;J.push(r)}}}}I&&J.sort(b);return J}};
THREE.DOMRenderer=function(){THREE.Renderer.call(this);var a=null,b=new THREE.Projector,c,e,d,g;this.domElement=document.createElement("div");this.setSize=function(f,i){c=f;e=i;d=c/2;g=e/2};this.render=function(f,i){var h,j,m,l,n,k,p,q;a=b.projectScene(f,i);h=0;for(j=a.length;h<j;h++){n=a[h];if(n instanceof THREE.RenderableParticle){p=n.x*d+d;q=n.y*g+g;m=0;for(l=n.material.length;m<l;m++){k=n.material[m];if(k instanceof THREE.ParticleDOMMaterial){k=k.domElement;k.style.left=p+"px";k.style.top=q+"px"}}}}}};
99
THREE.SoundRenderer=function(){this.volume=1;this.domElement=document.createElement("div");this.domElement.id="THREESound";this.cameraPosition=new THREE.Vector3;this.soundPosition=new THREE.Vector3;this.render=function(a,b,c){c&&a.update(undefined,!1,b);c=a.sounds;var e,d=c.length;for(e=0;e<d;e++){a=c[e];this.soundPosition.set(a.matrixWorld.n14,a.matrixWorld.n24,a.matrixWorld.n34);this.soundPosition.subSelf(b.position);if(a.isPlaying&&a.isLoaded){a.isAddedToDOM||a.addToDOM(this.domElement);a.calculateVolumeAndPan(this.soundPosition)}}}};
M
Mr.doob 已提交
100
THREE.RenderableParticle=function(){this.rotation=this.z=this.y=this.x=null;this.scale=new THREE.Vector2;this.materials=null};