UNPKG

flexy-bend

Version:

A Three.js library that bends BufferGeometry along Bezier curves

2 lines (1 loc) 4.54 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function Q(o,c){const t=c.getAttribute("position"),e=new o.Box3;e.center=new o.Vector3;for(let s=0;s<t.count;s++){const n=new o.Vector3;n.fromBufferAttribute(t,s),e.expandByPoint(n),e.center.add(n)}return e.center.divideScalar(t.count),e}const F=function({THREE:o,curve:c,quaternion:t,orientation:e,bufferGeometry:s,axis:n,mode:i="fit",orbit:m=0,scene:p}){const f=Q(o,s),r=s.attributes.position.array,l=n==="x"?new o.Vector3(0,0,1):new o.Vector3(1,0,0),a=e||l.applyQuaternion(t).normalize().multiplyScalar(1e6),x=f.min[n],z=f.max[n]-x,g=i==="preserve"||i==="tile"?W(c):0,M=i==="preserve"?.5-z/g/2+m:0;for(let y=0;y<r.length;y+=3){const w=r[y],V=r[y+1],P=r[y+2],B=n==="x"?w:n==="y"?V:P;let u;if(i==="fit"?u=(B-x)/z:i==="preserve"?(u=M+(B-x)/g,u=Math.max(0,Math.min(1,u))):(u=m+(B-x)/g,u=(u%1+1)%1),n==="x"){const d=c.getPointAt(u),A=c.getTangent(u),h=a.clone().cross(A).normalize(),b=new o.Quaternion().setFromAxisAngle(A,Math.atan2(P,V));h.applyQuaternion(b);const C=h.clone().setLength(Math.hypot(V,P));d.add(C),r[y]=d.x,r[y+1]=d.y,r[y+2]=d.z}else if(n==="z"){const d=c.getPointAt(u),A=c.getTangent(u),h=a.clone().cross(A).normalize(),b=new o.Quaternion().setFromAxisAngle(A,Math.atan2(V,w)+Math.PI/2);h.applyQuaternion(b);const C=h.clone().setLength(Math.hypot(w,V));d.add(C),r[y]=d.x,r[y+1]=d.y,r[y+2]=d.z}else if(n==="y"){const d=c.getPointAt(u),A=c.getTangent(u),h=a.clone().cross(A).normalize(),b=new o.Quaternion().setFromAxisAngle(A,Math.atan2(w,P));h.applyQuaternion(b);const C=h.clone().setLength(Math.hypot(w,P));d.add(C),r[y]=d.x,r[y+1]=d.y,r[y+2]=d.z}}s.attributes.position.needsUpdate=!0},S=function({THREE:o,surface:c,castingRectangular:t,resolution:e,scene:s}){const n={};t.direction.normalize();const i=L(o,t.A,t.D,e),m=L(o,t.B,t.C,e);for(let p=0;p<=e;p++)L(o,i[p],m[p],e).forEach(r=>{const a=new o.Raycaster(r,t.direction).intersectObject(c);a.length>0&&(n[v(r.x,r.y,r.z,e)]={normal:{x:a[0].face.normal.x,y:a[0].face.normal.y,z:a[0].face.normal.z},point:{x:a[0].point.x,y:a[0].point.y,z:a[0].point.z}})});return{data:n,castingRectangular:{A:{x:t.A.x,y:t.A.y,z:t.A.z},B:{x:t.B.x,y:t.B.y,z:t.B.z},C:{x:t.C.x,y:t.C.y,z:t.C.z},D:{x:t.D.x,y:t.D.y,z:t.D.z},direction:{x:t.direction.x,y:t.direction.y,z:t.direction.z}},resolution:e}},D=function({THREE:o,pointToFaceNormalMap:c,obj:t,scene:e}){const s=c.castingRectangular,n=new o.Vector3(s.A.x,s.A.y,s.A.z),i=new o.Vector3(s.B.x,s.B.y,s.B.z),m=new o.Vector3(s.C.x,s.C.y,s.C.z),p=new o.Vector3().subVectors(i,n),f=new o.Vector3().subVectors(m,n),r=new o.Vector3().crossVectors(p,f).normalize(),l=new o.Plane().setFromNormalAndCoplanarPoint(r,n),a=t.matrixWorld.clone().invert(),x=t.geometry.attributes.position.array;for(let z=0;z<x.length;z+=3){const g=x[z],M=x[z+1],y=x[z+2],w=new o.Vector3(g,M,y).applyMatrix4(t.matrixWorld),V=w.clone().sub(n).dot(l.normal),P=l.normal.clone().multiplyScalar(V/l.normal.lengthSq()),B=w.clone().sub(P),u=v(B.x,B.y,B.z,c.resolution),d=c.data[u],A=d?d.point.y:0,h=new o.Vector3(B.x,A,B.z);h.applyMatrix4(a),x[z]=h.x,x[z+1]=h.y,x[z+2]=h.z}t.geometry.attributes.position.needsUpdate=!0},N=function({THREE:o,bufferGeometry:c,axis:t="x",shearAxis:e="z",amplitude:s,sigma:n}){const i=Q(o,c),m=(i.max[t]+i.min[t])/2,p=(i.max[e]-i.min[e])/2,f=i.max[t]-i.min[t],r=n!==void 0?n:f/4,l=c.attributes.position.array;for(let a=0;a<l.length;a+=3){const x=l[a],z=l[a+1],g=l[a+2],M=t==="x"?x:t==="y"?z:g,y=e==="x"?x:e==="y"?z:g,w=M-m,V=Math.exp(-(w*w)/(2*r*r)),P=p!==0?s*V*(y/p):0;e==="x"?l[a]+=P:e==="y"?l[a+1]+=P:l[a+2]+=P}c.attributes.position.needsUpdate=!0},U=function({THREE:o,bufferGeometry:c,axis:t="y",predicate:e,fn:s}){const n=c.attributes.position.array;for(let i=0;i<n.length;i+=3){const m=n[i],p=n[i+1],f=n[i+2];if(!e(m,p,f))continue;let r,l;t==="x"?(r=p,l=f):t==="y"?(r=m,l=f):(r=m,l=p);const a=s(r,l);t==="x"?n[i]+=a:t==="y"?n[i+1]+=a:n[i+2]+=a}c.attributes.position.needsUpdate=!0};function v(o,c,t,e){function s(r,l){return Math.round(r/l)*l}function n(r){return r==="-0.0"?"0.0":r}const i=1/e,m=n(s(o,i).toFixed(1)),p=n(s(c,i).toFixed(1)),f=n(s(t,i).toFixed(1));return`${m}^${p}^${f}`}function L(o,c,t,e){const s=[];for(let n=0;n<=e;n++){const i=new o.Vector3(c.x+(t.x-c.x)*(n/e),c.y+(t.y-c.y)*(n/e),c.z+(t.z-c.z)*(n/e));s.push(i)}return s}function W(o){let t=0,e=o.getPointAt(0);for(let s=1;s<=100;s++){const n=o.getPointAt(s/100);t+=n.distanceTo(e),e=n}return t}exports.bend=F;exports.getPointToFaceNormalMap=S;exports.shear=N;exports.wave=U;exports.wrap=D;