1
0
mirror of https://github.com/troisjs/trois.git synced 2024-11-24 04:12:02 +08:00
trois/build/trois.module.cdn.min.js

136 lines
52 KiB
JavaScript
Raw Normal View History

2021-04-29 00:07:58 +08:00
import{defineComponent as o,toRef as Fr,watch as l,inject as X,provide as Dr,onUnmounted as Ur,createApp as Hr}from"vue";import{Vector3 as Z,Raycaster as Wr,Plane as Xr,Vector2 as I,InstancedMesh as ue,WebGLRenderer as Kr,OrthographicCamera as Vr,PerspectiveCamera as qr,Scene as Zr,Color as se,Texture as Yr,Group as Jr,WebGLCubeRenderTarget as Qr,RGBFormat as es,LinearMipmapLinearFilter as we,CubeCamera as ts,Mesh as rs,BoxGeometry as Ce,CircleGeometry as ss,ConeGeometry as is,CylinderGeometry as ns,DodecahedronGeometry as as,IcosahedronGeometry as os,LatheGeometry as hs,OctahedronGeometry as ds,PlaneGeometry as Me,PolyhedronGeometry as ls,RingGeometry as us,SphereGeometry as cs,TetrahedronGeometry as ms,TorusGeometry as fs,TorusKnotGeometry as ps,TubeGeometry as ys,Curve as gs,CatmullRomCurve3 as Le,SpotLight as ce,DirectionalLight as me,AmbientLight as vs,HemisphereLight as bs,PointLight as Ss,RectAreaLight as Ps,FrontSide as xs,MeshBasicMaterial as Ne,MeshLambertMaterial as ws,TextureLoader as Y,MeshMatcapMaterial as Cs,MeshPhongMaterial as Ms,MeshStandardMaterial as Ls,MeshPhysicalMaterial as Ns,ShaderMaterial as fe,ShaderChunk as K,UniformsUtils as Ge,ShaderLib as Gs,MeshToonMaterial as _s,UVMapping as js,ClampToEdgeWrapping as _e,LinearFilter as Es,CubeReflectionMapping as ks,CubeTextureLoader as Rs,FontLoader as Os,TextGeometry as $s,DoubleSide as Is,SpriteMaterial as zs,Sprite as Ts}from"three";import{OrbitControls as As}from"three/examples/jsm/controls/OrbitControls.js";import{RectAreaLightUniformsLib as Bs}from"three/examples/jsm/lights/RectAreaLightUniformsLib.js";import{RectAreaLightHelper as Fs}from"three/examples/jsm/helpers/RectAreaLightHelper.js";import{GLTFLoader as Ds}from"three/examples/jsm/loaders/GLTFLoader.js";import{FBXLoader as Us}from"three/examples/jsm/loaders/FBXLoader.js";import{EffectComposer as Hs}from"three/examples/jsm/postprocessing/EffectComposer.js";import{RenderPass as Ws}from"three/examples/jsm/postprocessing/RenderPass.js";import{BokehPass as Xs}from"three/examples/jsm/postprocessing/BokehPass.js";import{FilmPass as Ks}from"three/examples/jsm/postprocessing/FilmPass.js";import{ShaderPass as ie}from"three/examples/jsm/postprocessing/ShaderPass.js";import{FXAAShader as Vs}from"three/examples/jsm/shaders/FXAAShader.js";import{HalftonePass as qs}from"three/examples/jsm/postprocessing/HalftonePass.js";import{SMAAPass as Zs}from"three/examples/jsm/postprocessing/SMAAPass.js";import{SSAOPass as Ys}from"three/examples/jsm/postprocessing/SSAOPass.js";import{UnrealBloomPass as Js}from"three/examples/jsm/postprocessing/UnrealBloomPass.js";function Qs(e){const{camera:t,resetPosition:r=new Z(0,0,0)}=e,s=new Wr,n=r.clone(),a=new Xr(new Z(0,0,1),0);return{position:n,updatePosition:P=>{s.setFromCamera(P,t),t.getWorldDirection(a.normal),s.ray.intersectPlane(a,n)},intersect:(P,R)=>(s.setFromCamera(P,t),s.intersectObjects(R))}}function je(e){const{camera:t,domElement:r,intersectObjects:s,touch:n=!0,resetOnEnd:a=!1,resetPosition:i=new I(0,0),resetPositionV3:c=new Z(0,0,0),onEnter:P=()=>{},onMove:R=()=>{},onLeave:f=()=>{},onClick:T=()=>{},onIntersectEnter:W=()=>{},onIntersectOver:x=()=>{},onIntersectMove:F=()=>{},onIntersectLeave:D=()=>{},onIntersectClick:G=()=>{}}=e,m=i.clone(),p=new I(0,0),h=Qs({camera:t}),d=h.position,w={position:m,positionN:p,positionV3:d,intersectObjects:s,listeners:!1,addListeners:Ar,removeListeners:Br,intersect:be};return w;function C(){m.copy(i),d.copy(c)}function de(y){let O,$;y instanceof TouchEvent&&y.touches&&y.touches.length>0?(O=y.touches[0].clientX,$=y.touches[0].clientY):(O=y.clientX,$=y.clientY);const g=r.getBoundingClientRect();m.x=O-g.left,m.y=$-g.top,p.x=m.x/g.width*2-1,p.y=-(m.y/g.height)*2+1,h.updatePosition(p)}function be(){if(s.length){const y=h.intersect(p,s),O=[...s],$=[];y.forEach(g=>{var A,_,M;const{object:S}=g,{component:L}=S.userData;if(S instanceof ue){if($.indexOf(S)!==-1)return;$.push(S)}if(!S.userData.over){S.userData.over=!0;const le={type:"pointerover",over:!0,component:L,intersect:g},xe={...le,type:"pointerenter"};x(le),W(xe),(A=L.onPointerOver)==nul
varying vec2 vUv;
void main(){
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0);
}
`,ai=`
varying vec2 vUv;
void main() {
gl_FragColor = vec4(vUv.x, vUv.y, 0., 1.0);
}
`;var Kt=o({extends:j,props:{uniforms:{type:Object,default:()=>({})},vertexShader:{type:String,default:ni},fragmentShader:{type:String,default:ai}},methods:{createMaterial(){const e=new fe({uniforms:this.uniforms,vertexShader:this.vertexShader,fragmentShader:this.fragmentShader});return["vertexShader","fragmentShader"].forEach(r=>{l(()=>this[r],s=>{this.setProp(r,s,!0)})}),e},addWatchers(){}},__hmrId:"ShaderMaterial"});function oi(e,t,r){return e.split(t).join(r)}const hi=K.meshphong_frag.slice(0,K.meshphong_frag.indexOf("void main() {")),di=K.meshphong_frag.slice(K.meshphong_frag.indexOf("void main() {")),li={uniforms:Ge.merge([Gs.phong.uniforms,{thicknessColor:{value:new se(16777215)},thicknessDistortion:{value:.1},thicknessAmbient:{value:0},thicknessAttenuation:{value:.1},thicknessPower:{value:2},thicknessScale:{value:10}}]),vertexShader:`
#define USE_UV
${K.meshphong_vert}
`,fragmentShader:`
#define USE_UV
#define SUBSURFACE
${hi}
uniform float thicknessPower;
uniform float thicknessScale;
uniform float thicknessDistortion;
uniform float thicknessAmbient;
uniform float thicknessAttenuation;
uniform vec3 thicknessColor;
void RE_Direct_Scattering(const in IncidentLight directLight, const in vec2 uv, const in GeometricContext geometry, inout ReflectedLight reflectedLight) {
#ifdef USE_COLOR
vec3 thickness = vColor * thicknessColor;
#else
vec3 thickness = thicknessColor;
#endif
vec3 scatteringHalf = normalize(directLight.direction + (geometry.normal * thicknessDistortion));
float scatteringDot = pow(saturate(dot(geometry.viewDir, -scatteringHalf)), thicknessPower) * thicknessScale;
vec3 scatteringIllu = (scatteringDot + thicknessAmbient) * thickness;
reflectedLight.directDiffuse += scatteringIllu * thicknessAttenuation * directLight.color;
}
`+di.replace("#include <lights_fragment_begin>",oi(K.lights_fragment_begin,"RE_Direct( directLight, geometry, material, reflectedLight );",`
RE_Direct( directLight, geometry, material, reflectedLight );
#if defined( SUBSURFACE ) && defined( USE_UV )
RE_Direct_Scattering(directLight, vUv, geometry, reflectedLight);
#endif
`))},Vt={color:{type:[String,Number],default:"#ffffff"},thicknessColor:{type:[String,Number],default:"#ffffff"},thicknessDistortion:{type:Number,default:.4},thicknessAmbient:{type:Number,default:.01},thicknessAttenuation:{type:Number,default:.7},thicknessPower:{type:Number,default:2},thicknessScale:{type:Number,default:4}};var qt=o({extends:j,props:Vt,methods:{createMaterial(){const e=li,t=Ge.clone(e.uniforms);return Object.keys(Vt).forEach(s=>{const n=this[s];let a=s,i=n;["color","thicknessColor"].includes(s)&&(s==="color"&&(a="diffuse"),i=new se(n)),t[a].value=i}),new fe({...e,uniforms:t,lights:!0,transparent:this.transparent,vertexColors:this.vertexColors})}},__hmrId:"SubSurfaceMaterial"}),Zt=o({extends:j,props:{...E},methods:{createMaterial(){const e=new _s(z(this.$props));return U(this,Object.keys(E),e),e}},__hmrId:"ToonMaterial"}),ve=o({inject:{material:oe},props:{name:{type:String,default:"map"},uniform:String,src:String,onLoad:Function,onProgress:Function,onError:Function,mapping:{type:Number,default:js},wrapS:{type:Number,default:_e},wrapT:{type:Number,default:_e},magFilter:{type:Number,default:Es},minFilter:{type:Number,default:we},repeat:{type:Object,default:()=>({x:1,y:1})},rotation:{type:Number,default:0},center:{type:Object,default:()=>({x:0,y:0})}},setup(){return{}},created(){this.refreshTexture(),l(()=>this.src,this.refreshTexture)},unmounted(){var e,t;(e=this.material)==null||e.setTexture(null,this.name),(t=this.texture)==null||t.dispose()},methods:{createTexture(){if(!this.src)return;const e=new Y().load(this.src,this.onLoaded,this.onProgress,this.onError);return["mapping","wrapS","wrapT","magFilter","minFilter","repeat","rotation","center"].forEach(r=>{u(this,r,e)}),e},refreshTexture(){this.texture=this.createTexture(),this.texture&&this.material&&(this.material.setTexture(this.texture,this.name),this.material.material instanceof fe&&this.uniform&&(this.material.uniforms[this.uniform]={value:this.texture}))},onLoaded(e){var t;(t=this.onLoad)==null||t.call(this,e)}},render(){return[]}}),Yt=o({extends:ve,props:{path:{type:String,required:!0},urls:{type:Array,default:()=>["px.jpg","nx.jpg","py.jpg","ny.jpg","pz.jpg","nz.jpg"]},mapping:{type:Number,default:ks}},created(){l(()=>this.path,this.refreshTexture),l(()=>this.urls,this.refreshTexture)},methods:{createTexture(){return new Rs().setPath(this.path).load(this.urls,this.onLoaded,this.onProgress,this.onError)}}}),Jt=v("Box",Fe,De),Qt=v("Circle",He,We),er=v("Cone",Ke,Ve),tr=v("Cylinder",Ze,Ye),rr=v("Dodecahedron",Qe,et),sr=v("Icosahedron",rt,st),ir=v("Lathe",nt,at),nr=v("Octahedron",ht,dt),ar=v("Plane",ut,ct),or=v("Polyhedron",ft,pt),hr=v("Ring",gt,vt),dr=v("Sphere",St,Pt),lr=v("Tetrahedron",wt,Ct);const ui={text:{type:String,required:!0,default:"Text"},fontSrc:{type:String,required:!0},size:{type:Number,default:80},height:{type:Number,default:5},depth:{type:Number,default:1},curveSegments:{type:Number,default:12},bevelEnabled:{type:Boolean,default:!1},bevelThickness:{type:Number,default:10},bevelSize:{type:Number,default:8},bevelOffset:{type:Number,default:0},bevelSegments:{type:Number,default:5},align:{type:[Boolean,String],default:!1}};var ur=o({extends:H,props:ui,setup(){return{}},created(){if(!this.fontSrc){console.error('Missing required prop: "font-src"');return}["text","size","height","curveSegments","bevelEnabled","bevelThickness","bevelSize","bevelOffset","bevelSegments","align"].forEach(r=>{l(()=>this[r],()=>{this.font&&this.refreshGeometry()})});const t=new Os;this.loading=!0,t.load(this.fontSrc,r=>{this.loading=!1,this.font=r,this.createGeometry(),this.initMesh()})},methods:{createGeometry(){this.geometry=new $s(this.text,{font:this.font,size:this.size,height:this.height,depth:this.depth,curveSegments:this.curveSegments,bevelEnabled:this.bevelEnabled,bevelThickness:this.bevelThickness,bevelSize:this.bevelSize,bevelOffset:this.bevelOffset,bevelSegments:this.bevelSegments}),this.align==="center"&&this.geometry.center()}}}),cr=v("Torus",Lt,Nt),mr=v("TorusKnot",_t,jt),fr=o({extends:H,props:ye,created(){this.createGeometry(),this.addGeometr
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`,fragmentShader:`
varying vec2 vUv;
void main() {
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}
`},Rr={uniforms:{tDiffuse:{value:null},blurRadius:{value:0},gradientRadius:{value:0},start:{value:new I},end:{value:new I},delta:{value:new I},texSize:{value:new I}},vertexShader:kr.vertexShader,fragmentShader:`
uniform sampler2D tDiffuse;
uniform float blurRadius;
uniform float gradientRadius;
uniform vec2 start;
uniform vec2 end;
uniform vec2 delta;
uniform vec2 texSize;
varying vec2 vUv;
float random(vec3 scale, float seed) {
/* use the fragment position for a different seed per-pixel */
return fract(sin(dot(gl_FragCoord.xyz + seed, scale)) * 43758.5453 + seed);
}
void main() {
vec4 color = vec4(0.0);
float total = 0.0;
/* randomize the lookup values to hide the fixed number of samples */
float offset = random(vec3(12.9898, 78.233, 151.7182), 0.0);
vec2 normal = normalize(vec2(start.y - end.y, end.x - start.x));
float radius = smoothstep(0.0, 1.0, abs(dot(vUv * texSize - start, normal)) / gradientRadius) * blurRadius;
for (float t = -30.0; t <= 30.0; t++) {
float percent = (t + offset - 0.5) / 30.0;
float weight = 1.0 - abs(percent);
vec4 texel = texture2D(tDiffuse, vUv + delta / texSize * percent * radius);
// vec4 texel2 = texture2D(tDiffuse, vUv + vec2(-delta.y, delta.x) / texSize * percent * radius);
/* switch to pre-multiplied alpha to correctly blur transparent images */
texel.rgb *= texel.a;
// texel2.rgb *= texel2.a;
color += texel * weight;
total += 2.0 * weight;
}
gl_FragColor = color / total;
/* switch back from pre-multiplied alpha */
gl_FragColor.rgb /= gl_FragColor.a + 0.00001;
}
`};const ci={blurRadius:{type:Number,default:10},gradientRadius:{type:Number,default:100},start:{type:Object,default:()=>({x:0,y:100})},end:{type:Object,default:()=>({x:10,y:100})}};var Or=o({extends:k,props:ci,setup(){return{uniforms1:{},uniforms2:{}}},created(){if(!this.composer)return;this.pass1=new ie(Rr),this.pass2=new ie(Rr);const e=this.uniforms1=this.pass1.uniforms,t=this.uniforms2=this.pass2.uniforms;t.blurRadius=e.blurRadius,t.gradientRadius=e.gradientRadius,t.start=e.start,t.end=e.end,t.texSize=e.texSize,u(this,"blurRadius",e.blurRadius,"value"),u(this,"gradientRadius",e.gradientRadius,"value"),this.updateFocusLine(),["start","end"].forEach(r=>{l(()=>this[r],this.updateFocusLine,{deep:!0})}),this.pass1.setSize=(r,s)=>{e.texSize.value.set(r,s)},this.initEffectPass(this.pass1),this.composer.addPass(this.pass2)},unmounted(){this.composer&&this.pass2&&this.composer.removePass(this.pass2)},methods:{updateFocusLine(){this.uniforms1.start.value.copy(this.start),this.uniforms1.end.value.copy(this.end);const e=new I().copy(this.end).sub(this.start).normalize();this.uniforms1.delta.value.copy(e),this.uniforms2.delta.value.set(-e.y,e.x)}},__hmrId:"TiltShiftPass"});const $r={strength:{type:Number,default:1.5},radius:{type:Number,default:0},threshold:{type:Number,default:0}};var Ir=o({extends:k,props:$r,created(){if(!this.renderer)return;const e=new I(this.renderer.size.width,this.renderer.size.height),t=new Js(e,this.strength,this.radius,this.threshold);Object.keys($r).forEach(r=>{l(()=>this[r],s=>{t.uniforms[r].value=s})}),this.initEffectPass(t)},__hmrId:"UnrealBloomPass"}),mi={uniforms:{tDiffuse:{value:null},center:{value:new I(.5,.5)},strength:{value:0}},vertexShader:kr.vertexShader,fragmentShader:`
uniform sampler2D tDiffuse;
uniform vec2 center;
uniform float strength;
varying vec2 vUv;
float random(vec3 scale, float seed) {
/* use the fragment position for a different seed per-pixel */
return fract(sin(dot(gl_FragCoord.xyz + seed, scale)) * 43758.5453 + seed);
}
void main() {
vec4 color = vec4(0.0);
float total = 0.0;
vec2 toCenter = center - vUv;
/* randomize the lookup values to hide the fixed number of samples */
float offset = random(vec3(12.9898, 78.233, 151.7182), 0.0);
for (float t = 0.0; t <= 40.0; t++) {
float percent = (t + offset) / 40.0;
float weight = 4.0 * (percent - percent * percent);
vec4 texel = texture2D(tDiffuse, vUv + toCenter * percent * strength);
/* switch to pre-multiplied alpha to correctly blur transparent images */
texel.rgb *= texel.a;
color += texel * weight;
total += weight;
}
gl_FragColor = color / total;
/* switch back from pre-multiplied alpha */
gl_FragColor.rgb /= gl_FragColor.a + 0.00001;
}
`},zr=o({extends:k,props:{center:{type:Object,default:()=>({x:.5,y:.5})},strength:{type:Number,default:.5}},created(){const e=new ie(mi);u(this,"center",e.uniforms.center,"value"),u(this,"strength",e.uniforms.strength,"value"),this.initEffectPass(e)},__hmrId:"ZoomBlurPass"}),fi=Object.freeze({__proto__:null,Renderer:Ee,RendererInjectionKey:N,OrthographicCamera:$e,PerspectiveCamera:ne,Camera:ne,Group:ze,Scene:Ie,SceneInjectionKey:ae,Object3D:B,Raycaster:Te,CubeCamera:Ae,BoxGeometry:Ue,CircleGeometry:Xe,ConeGeometry:qe,CylinderGeometry:Je,DodecahedronGeometry:tt,IcosahedronGeometry:it,LatheGeometry:ot,OctahedronGeometry:lt,PlaneGeometry:mt,PolyhedronGeometry:yt,RingGeometry:bt,SphereGeometry:xt,TetrahedronGeometry:Mt,TorusGeometry:Gt,TorusKnotGeometry:Et,TubeGeometry:Rt,AmbientLight:$t,DirectionalLight:It,HemisphereLight:zt,PointLight:Tt,RectAreaLight:At,SpotLight:Bt,Material:j,MaterialInjectionKey:oe,BasicMaterial:Ft,LambertMaterial:Dt,MatcapMaterial:Ut,PhongMaterial:Ht,PhysicalMaterial:Xt,ShaderMaterial:Kt,StandardMaterial:ge,SubSurfaceMaterial:qt,ToonMaterial:Zt,Texture:ve,CubeTexture:Yt,Mesh:H,MeshInjectionKey:Q,Box:Jt,Circle:Qt,Cone:er,Cylinder:tr,Dodecahedron:rr,Icosahedron:sr,Lathe:ir,Octahedron:nr,Plane:ar,Polyhedron:or,Ring:hr,Sphere:dr,Tetrahedron:lr,Text:ur,Torus:cr,TorusKnot:mr,Tube:fr,Image:pr,InstancedMesh:yr,Sprite:gr,GLTFModel:br,FBXModel:Sr,EffectComposer:Pr,ComposerInjectionKey:he,RenderPass:xr,BokehPass:Cr,FilmPass:Lr,FXAAPass:Nr,HalftonePass:_r,SMAAPass:jr,SSAOPass:Er,TiltShiftPass:Or,UnrealBloomPass:Ir,ZoomBlurPass:zr,setFromProp:V,bindProps:U,bindProp:u,propsValues:z,lerp:ke,limit:Re,getMatcapUrl:pe});const Tr={install(e){["Camera","OrthographicCamera","PerspectiveCamera","Raycaster","Renderer","Scene","Group","AmbientLight","DirectionalLight","HemisphereLight","PointLight","RectAreaLight","SpotLight","BasicMaterial","LambertMaterial","MatcapMaterial","PhongMaterial","PhysicalMaterial","ShaderMaterial","StandardMaterial","SubSurfaceMaterial","ToonMaterial","Texture","CubeTexture","Mesh","Box","BoxGeometry","Circle","CircleGeometry","Cone","ConeGeometry","Cylinder","CylinderGeometry","Dodecahedron","DodecahedronGeometry","Icosahedron","IcosahedronGeometry","Lathe","LatheGeometry","Octahedron","OctahedronGeometry","Plane","PlaneGeometry","Polyhedron","PolyhedronGeometry","Ring","RingGeometry","Sphere","SphereGeometry","Tetrahedron","TetrahedronGeometry","Text","Torus","TorusGeometry","TorusKnot","TorusKnotGeometry","Tube","TubeGeometry","Image","InstancedMesh","Sprite","FBXModel","GLTFModel","BokehPass","EffectComposer","FilmPass","FXAAPass","HalftonePass","RenderPass","SAOPass","SMAAPass","SSAOPass","TiltShiftPass","UnrealBloomPass","ZoomBlurPass","GLTFViewer"].forEach(r=>{e.component(r,fi[r])})}};function pi(e){return Hr(e).use(Tr)}function yi(){const e={loader:new Y,count:0,textures:[],loadProgress:0,loadTextures:t,dispose:s};return e;function t(n,a){e.count=n.length,e.textures.splice(0),e.loadProgress=0,Promise.all(n.map(r)).then(a)}function r(n,a){return new Promise(i=>{e.loader.load(n.src,c=>{e.loadProgress+=1/e.count,e.textures[a]=c,i(c)})})}function s(){e.textures.forEach(n=>n.dispose())}}export{$t as AmbientLight,Ft as BasicMaterial,Cr as BokehPass,Jt as Box,Ue as BoxGeometry,ne as Camera,Qt as Circle,Xe as CircleGeometry,he as ComposerInjectionKey,er as Cone,qe as ConeGeometry,Ae as CubeCamera,Yt as CubeTexture,tr as Cylinder,Je as CylinderGeometry,It as DirectionalLight,rr as Dodecahedron,tt as DodecahedronGeometry,Pr as EffectComposer,Sr as FBXModel,Nr as FXAAPass,Lr as FilmPass,br as GLTFModel,ze as Group,_r as HalftonePass,zt as HemisphereLight,sr as Icosahedron,it as IcosahedronGeometry,pr as Image,yr as InstancedMesh,Dt as LambertMaterial,ir as Lathe,ot as LatheGeometry,Ut as MatcapMaterial,j as Material,oe as MaterialInjectionKey,H as Mesh,Q as MeshInjectionKey,B as Object3D,nr as Octahedron,lt as OctahedronGeometry,$e as OrthographicCamera,ne as PerspectiveCamera,Ht as PhongMaterial,Xt as PhysicalMaterial,ar as Plane,mt as PlaneGeometry,Tt as PointLight,or as Polyhedron,yt as Polyhe
2021-02-26 05:55:44 +08:00
//# sourceMappingURL=trois.module.cdn.min.js.map