Skip to content

Instantly share code, notes, and snippets.

@gero3
Created August 17, 2012 07:23
Show Gist options
  • Save gero3/3376707 to your computer and use it in GitHub Desktop.
Save gero3/3376707 to your computer and use it in GitHub Desktop.
test for ray.js
THREE.Ray=function(L,M,N,O){this.origin=L||new THREE.Vector3;this.direction=M||new THREE.Vector3;this.near=N||0;this.far=O||Infinity;var q=new THREE.Vector3,p=new THREE.Vector3,r=new THREE.Vector3,v=new THREE.Vector3,s=new THREE.Vector3,D=new THREE.Vector3,t=new THREE.Vector3,u=new THREE.Vector3,E=new THREE.Vector3,w=new THREE.Vector3,m=new THREE.Vector3,x=new THREE.Matrix4,G=function(a,c){return a.distance-c.distance},g=new THREE.Vector3,h=new THREE.Vector3,j=new THREE.Vector3,H,I,c,J=function(a,
F,b){g.sub(b,a);H=g.dot(F);I=h.add(a,j.copy(F).multiplyScalar(H));return c=b.distanceTo(I)},n,i,o,k,y,z,A,B,C=function(a,c,b,f){g.sub(f,c);h.sub(b,c);j.sub(a,c);n=g.dot(g);i=g.dot(h);o=g.dot(j);k=h.dot(h);y=h.dot(j);z=1/(n*k-i*i);A=(k*o-i*y)*z;B=(n*y-i*o)*z;return 0<=A&&0<=B&&1>A+B},K=1E-4;this.setPrecision=function(a){K=a};this.intersectObject=function(a,g){var b,f=[];if(!0===g)for(var d=0,h=a.children.length;d<h;d++)Array.prototype.push.apply(f,this.intersectObject(a.children[d],g));if(a instanceof
THREE.Particle){c=J(this.origin,this.direction,a.matrixWorld.getPosition());if(c>a.scale.x)return[];b={distance:c,point:a.position,face:null,object:a};f.push(b)}else if(a instanceof THREE.Mesh){d=THREE.Frustum.__v1.set(a.matrixWorld.getColumnX().length(),a.matrixWorld.getColumnY().length(),a.matrixWorld.getColumnZ().length());d=a.geometry.boundingSphere.radius*Math.max(d.x,Math.max(d.y,d.z));c=J(this.origin,this.direction,a.matrixWorld.getPosition());if(c>d)return f;var e,i,j=a.geometry,l=j.vertices,
n,o,k;n=a.geometry.materials;o=a.material instanceof THREE.MeshFaceMaterial;a.matrixRotationWorld.extractRotation(a.matrixWorld);s.copy(this.origin);D.copy(this.direction);x.getInverse(a.matrixWorld);t.copy(s);x.multiplyVector3(t);u.copy(D);x.rotateAxis(u).normalize();d=0;for(h=j.faces.length;d<h;d++)if(b=j.faces[d],e=!0===o?n[b.materialIndex]:a.material,void 0!==e&&(k=e.side,E.sub(b.centroid,t),w.copy(b.normal),e=u.dot(w),!(Math.abs(e)<K)&&(i=w.dot(E)/e,!(0>i)&&(k===THREE.DoubleSide||(k===THREE.FrontSide?
0>e:0<e)))))if(m.add(t,u.multiplyScalar(i)),b instanceof THREE.Face3)q.copy(l[b.a]),p.copy(l[b.b]),r.copy(l[b.c]),C(m,q,p,r)&&(e=a.matrixWorld.multiplyVector3(m.clone()),c=s.distanceTo(e),!(c<this.near)&&!(c>this.far)&&(b={distance:c,point:e,face:b,faceIndex:d,object:a},f.push(b)));else if(b instanceof THREE.Face4&&(q.copy(l[b.a]),p.copy(l[b.b]),r.copy(l[b.c]),v.copy(l[b.d]),C(m,q,p,v)||C(m,p,r,v)))e=a.matrixWorld.multiplyVector3(m.clone()),c=s.distanceTo(e),!(c<this.near)&&!(c>this.far)&&(b={distance:c,
point:e,face:b,faceIndex:d,object:a},f.push(b))}f.sort(G);return f};this.intersectObjects=function(a,c){for(var b=[],f=0,d=a.length;f<d;f++)Array.prototype.push.apply(b,this.intersectObject(a[f],c));b.sort(G);return b}};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment