UNPKG

flexy-bend

Version:

A Three.js library that bends BufferGeometry along Bezier curves

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