diff --git a/.gitignore b/.gitignore index 9caeef9..ad54838 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ __pycache__/ *.webm node_modules/ build/ +devbox.* \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..6f3a291 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "liveServer.settings.port": 5501 +} \ No newline at end of file diff --git a/experiences/bus-bunching/config.json b/experiences/bus-bunching/config.json new file mode 100644 index 0000000..88886be --- /dev/null +++ b/experiences/bus-bunching/config.json @@ -0,0 +1,10 @@ +{ + "id": "bus-bunching", + "type": "web", + "title": "Bus Bunching", + "description": "Bus traffic simulation", + "action_hints": ["change the number of buses", "change the number of stops", "pause a bus on your phone"], + "layout": "full", + "lifetime": 180, + "queueable": true +} diff --git a/experiences/bus-bunching/controls/lib/index.js b/experiences/bus-bunching/controls/lib/index.js new file mode 100644 index 0000000..9c7bd25 --- /dev/null +++ b/experiences/bus-bunching/controls/lib/index.js @@ -0,0 +1,61 @@ +/** @jsxImportSource @emotion/react */ +import { css } from "@emotion/react" +import React, { useCallback } from "react" +import { useMessaging } from "@footron/controls-client" +import { Slider } from "@material-ui/core" + +const containerStyle = css` + padding: 16px; + overflow-x: hidden; + + p { + margin: 0 0 16px; + } +` + +const ControlsComponent = () => { + const { sendMessage } = useMessaging() + + const updateBusCount = useCallback( + async (event, value) => { + await sendMessage({ type: "busCount", value: value }) + }, + [sendMessage] + ) + + const updateStopCount = useCallback( + async (event, value) => { + await sendMessage({ type: "stopCount", value: value }) + }, + [sendMessage] + ) + + return ( +
+ Change the number of buses! +
++ Change the number of bus stops! +
+0&&(t.defines=this.defines),t.vertexShader=this.vertexShader,t.fragmentShader=this.fragmentShader,t.lights=this.lights,t.clipping=this.clipping;const i={};for(const n in this.extensions)this.extensions[n]===!0&&(i[n]=!0);return Object.keys(i).length>0&&(t.extensions=i),t}}let Cp=class extends tn{constructor(){super(),this.isCamera=!0,this.type="Camera",this.matrixWorldInverse=new oi,this.projectionMatrix=new oi,this.projectionMatrixInverse=new oi,this.coordinateSystem=Zn}copy(e,t){return super.copy(e,t),this.matrixWorldInverse.copy(e.matrixWorldInverse),this.projectionMatrix.copy(e.projectionMatrix),this.projectionMatrixInverse.copy(e.projectionMatrixInverse),this.coordinateSystem=e.coordinateSystem,this}getWorldDirection(e){return super.getWorldDirection(e).negate()}updateMatrixWorld(e){super.updateMatrixWorld(e),this.matrixWorldInverse.copy(this.matrixWorld).invert()}updateWorldMatrix(e,t){super.updateWorldMatrix(e,t),this.matrixWorldInverse.copy(this.matrixWorld).invert()}clone(){return new this.constructor().copy(this)}};const ls=new M,md=new Qe,gd=new Qe;let Zi=class extends Cp{constructor(e=50,t=1,i=.1,n=2e3){super(),this.isPerspectiveCamera=!0,this.type="PerspectiveCamera",this.fov=e,this.zoom=1,this.near=i,this.far=n,this.focus=10,this.aspect=t,this.view=null,this.filmGauge=35,this.filmOffset=0,this.updateProjectionMatrix()}copy(e,t){return super.copy(e,t),this.fov=e.fov,this.zoom=e.zoom,this.near=e.near,this.far=e.far,this.focus=e.focus,this.aspect=e.aspect,this.view=e.view===null?null:Object.assign({},e.view),this.filmGauge=e.filmGauge,this.filmOffset=e.filmOffset,this}setFocalLength(e){const t=.5*this.getFilmHeight()/e;this.fov=zh*2*Math.atan(t),this.updateProjectionMatrix()}getFocalLength(){const e=Math.tan(tc*.5*this.fov);return .5*this.getFilmHeight()/e}getEffectiveFOV(){return zh*2*Math.atan(Math.tan(tc*.5*this.fov)/this.zoom)}getFilmWidth(){return this.filmGauge*Math.min(this.aspect,1)}getFilmHeight(){return this.filmGauge/Math.max(this.aspect,1)}getViewBounds(e,t,i){ls.set(-1,-1,.5).applyMatrix4(this.projectionMatrixInverse),t.set(ls.x,ls.y).multiplyScalar(-e/ls.z),ls.set(1,1,.5).applyMatrix4(this.projectionMatrixInverse),i.set(ls.x,ls.y).multiplyScalar(-e/ls.z)}getViewSize(e,t){return this.getViewBounds(e,md,gd),t.subVectors(gd,md)}setViewOffset(e,t,i,n,s,o){this.aspect=e/t,this.view===null&&(this.view={enabled:!0,fullWidth:1,fullHeight:1,offsetX:0,offsetY:0,width:1,height:1}),this.view.enabled=!0,this.view.fullWidth=e,this.view.fullHeight=t,this.view.offsetX=i,this.view.offsetY=n,this.view.width=s,this.view.height=o,this.updateProjectionMatrix()}clearViewOffset(){this.view!==null&&(this.view.enabled=!1),this.updateProjectionMatrix()}updateProjectionMatrix(){const e=this.near;let t=e*Math.tan(tc*.5*this.fov)/this.zoom,i=2*t,n=this.aspect*i,s=-.5*n;const o=this.view;if(this.view!==null&&this.view.enabled){const l=o.fullWidth,h=o.fullHeight;s+=o.offsetX*n/l,t-=o.offsetY*i/h,n*=o.width/l,i*=o.height/h}const a=this.filmOffset;a!==0&&(s+=e*a/this.getFilmWidth()),this.projectionMatrix.makePerspective(s,s+n,t,t-i,e,this.far,this.coordinateSystem),this.projectionMatrixInverse.copy(this.projectionMatrix).invert()}toJSON(e){const t=super.toJSON(e);return t.object.fov=this.fov,t.object.zoom=this.zoom,t.object.near=this.near,t.object.far=this.far,t.object.focus=this.focus,t.object.aspect=this.aspect,this.view!==null&&(t.object.view=Object.assign({},this.view)),t.object.filmGauge=this.filmGauge,t.object.filmOffset=this.filmOffset,t}};const Mr=-90,Sr=1;class i0 extends tn{constructor(e,t,i){super(),this.type="CubeCamera",this.renderTarget=i,this.coordinateSystem=null,this.activeMipmapLevel=0;const n=new Zi(Mr,Sr,e,t);n.layers=this.layers,this.add(n);const s=new Zi(Mr,Sr,e,t);s.layers=this.layers,this.add(s);const o=new Zi(Mr,Sr,e,t);o.layers=this.layers,this.add(o);const a=new Zi(Mr,Sr,e,t);a.layers=this.layers,this.add(a);const l=new Zi(Mr,Sr,e,t);l.layers=this.layers,this.add(l);const h=new Zi(Mr,Sr,e,t);h.layers=this.layers,this.add(h)}updateCoordinateSystem(){const e=this.coordinateSystem,t=this.children.concat(),[i,n,s,o,a,l]=t;for(const h of t)this.remove(h);if(e===Zn)i.up.set(0,1,0),i.lookAt(1,0,0),n.up.set(0,1,0),n.lookAt(-1,0,0),s.up.set(0,0,-1),s.lookAt(0,1,0),o.up.set(0,0,1),o.lookAt(0,-1,0),a.up.set(0,1,0),a.lookAt(0,0,1),l.up.set(0,1,0),l.lookAt(0,0,-1);else if(e===Ll)i.up.set(0,-1,0),i.lookAt(-1,0,0),n.up.set(0,-1,0),n.lookAt(1,0,0),s.up.set(0,0,1),s.lookAt(0,1,0),o.up.set(0,0,-1),o.lookAt(0,-1,0),a.up.set(0,-1,0),a.lookAt(0,0,1),l.up.set(0,-1,0),l.lookAt(0,0,-1);else throw new Error("THREE.CubeCamera.updateCoordinateSystem(): Invalid coordinate system: "+e);for(const h of t)this.add(h),h.updateMatrixWorld()}update(e,t){this.parent===null&&this.updateMatrixWorld();const{renderTarget:i,activeMipmapLevel:n}=this;this.coordinateSystem!==e.coordinateSystem&&(this.coordinateSystem=e.coordinateSystem,this.updateCoordinateSystem());const[s,o,a,l,h,u]=this.children,d=e.getRenderTarget(),f=e.getActiveCubeFace(),m=e.getActiveMipmapLevel(),y=e.xr.enabled;e.xr.enabled=!1;const w=i.texture.generateMipmaps;i.texture.generateMipmaps=!1,e.setRenderTarget(i,0,n),e.render(t,s),e.setRenderTarget(i,1,n),e.render(t,o),e.setRenderTarget(i,2,n),e.render(t,a),e.setRenderTarget(i,3,n),e.render(t,l),e.setRenderTarget(i,4,n),e.render(t,h),i.texture.generateMipmaps=w,e.setRenderTarget(i,5,n),e.render(t,u),e.setRenderTarget(d,f,m),e.xr.enabled=y,i.texture.needsPMREMUpdate=!0}}class Lp extends Pn{constructor(e,t,i,n,s,o,a,l,h,u){e=e!==void 0?e:[],t=t!==void 0?t:Kr,super(e,t,i,n,s,o,a,l,h,u),this.isCubeTexture=!0,this.flipY=!1}get images(){return this.image}set images(e){this.image=e}}class n0 extends Js{constructor(e=1,t={}){super(e,e,t),this.isWebGLCubeRenderTarget=!0;const i={width:e,height:e,depth:1},n=[i,i,i,i,i,i];this.texture=new Lp(n,t.mapping,t.wrapS,t.wrapT,t.magFilter,t.minFilter,t.format,t.type,t.anisotropy,t.colorSpace),this.texture.isRenderTargetTexture=!0,this.texture.generateMipmaps=t.generateMipmaps!==void 0?t.generateMipmaps:!1,this.texture.minFilter=t.minFilter!==void 0?t.minFilter:mn}fromEquirectangularTexture(e,t){this.texture.type=t.type,this.texture.colorSpace=t.colorSpace,this.texture.generateMipmaps=t.generateMipmaps,this.texture.minFilter=t.minFilter,this.texture.magFilter=t.magFilter;const i={uniforms:{tEquirect:{value:null}},vertexShader:` + + varying vec3 vWorldDirection; + + vec3 transformDirection( in vec3 dir, in mat4 matrix ) { + + return normalize( ( matrix * vec4( dir, 0.0 ) ).xyz ); + + } + + void main() { + + vWorldDirection = transformDirection( position, modelMatrix ); + + #include+ #include + + } + `,fragmentShader:` + + uniform sampler2D tEquirect; + + varying vec3 vWorldDirection; + + #include + + void main() { + + vec3 direction = normalize( vWorldDirection ); + + vec2 sampleUV = equirectUv( direction ); + + gl_FragColor = texture2D( tEquirect, sampleUV ); + + } + `},n=new Qs(5,5,5),s=new Ss({name:"CubemapFromEquirect",uniforms:eo(i.uniforms),vertexShader:i.vertexShader,fragmentShader:i.fragmentShader,side:Ii,blending:_s});s.uniforms.tEquirect.value=t;const o=new Xi(n,s),a=t.minFilter;return t.minFilter===js&&(t.minFilter=mn),new i0(1,10,this).update(e,o),t.minFilter=a,o.geometry.dispose(),o.material.dispose(),this}clear(e,t,i,n){const s=e.getRenderTarget();for(let o=0;o<6;o++)e.setRenderTarget(this,o),e.clear(t,i,n);e.setRenderTarget(s)}}const vc=new M,s0=new M,r0=new gt;let Hs=class{constructor(e=new M(1,0,0),t=0){this.isPlane=!0,this.normal=e,this.constant=t}set(e,t){return this.normal.copy(e),this.constant=t,this}setComponents(e,t,i,n){return this.normal.set(e,t,i),this.constant=n,this}setFromNormalAndCoplanarPoint(e,t){return this.normal.copy(e),this.constant=-t.dot(this.normal),this}setFromCoplanarPoints(e,t,i){const n=vc.subVectors(i,t).cross(s0.subVectors(e,t)).normalize();return this.setFromNormalAndCoplanarPoint(n,e),this}copy(e){return this.normal.copy(e.normal),this.constant=e.constant,this}normalize(){const e=1/this.normal.length();return this.normal.multiplyScalar(e),this.constant*=e,this}negate(){return this.constant*=-1,this.normal.negate(),this}distanceToPoint(e){return this.normal.dot(e)+this.constant}distanceToSphere(e){return this.distanceToPoint(e.center)-e.radius}projectPoint(e,t){return t.copy(e).addScaledVector(this.normal,-this.distanceToPoint(e))}intersectLine(e,t){const i=e.delta(vc),n=this.normal.dot(i);if(n===0)return this.distanceToPoint(e.start)===0?t.copy(e.start):null;const s=-(e.start.dot(this.normal)+this.constant)/n;return s<0||s>1?null:t.copy(e.start).addScaledVector(i,s)}intersectsLine(e){const t=this.distanceToPoint(e.start),i=this.distanceToPoint(e.end);return t<0&&i>0||i<0&&t>0}intersectsBox(e){return e.intersectsPlane(this)}intersectsSphere(e){return e.intersectsPlane(this)}coplanarPoint(e){return e.copy(this.normal).multiplyScalar(-this.constant)}applyMatrix4(e,t){const i=t||r0.getNormalMatrix(e),n=this.coplanarPoint(vc).applyMatrix4(e),s=this.normal.applyMatrix3(i).normalize();return this.constant=-n.dot(s),this}translate(e){return this.constant-=e.dot(this.normal),this}equals(e){return e.normal.equals(this.normal)&&e.constant===this.constant}clone(){return new this.constructor().copy(this)}};const Ds=new lu,ka=new M;let cu=class{constructor(e=new Hs,t=new Hs,i=new Hs,n=new Hs,s=new Hs,o=new Hs){this.planes=[e,t,i,n,s,o]}set(e,t,i,n,s,o){const a=this.planes;return a[0].copy(e),a[1].copy(t),a[2].copy(i),a[3].copy(n),a[4].copy(s),a[5].copy(o),this}copy(e){const t=this.planes;for(let i=0;i<6;i++)t[i].copy(e.planes[i]);return this}setFromProjectionMatrix(e,t=Zn){const i=this.planes,n=e.elements,s=n[0],o=n[1],a=n[2],l=n[3],h=n[4],u=n[5],d=n[6],f=n[7],m=n[8],y=n[9],w=n[10],x=n[11],v=n[12],D=n[13],T=n[14],C=n[15];if(i[0].setComponents(l-s,f-h,x-m,C-v).normalize(),i[1].setComponents(l+s,f+h,x+m,C+v).normalize(),i[2].setComponents(l+o,f+u,x+y,C+D).normalize(),i[3].setComponents(l-o,f-u,x-y,C-D).normalize(),i[4].setComponents(l-a,f-d,x-w,C-T).normalize(),t===Zn)i[5].setComponents(l+a,f+d,x+w,C+T).normalize();else if(t===Ll)i[5].setComponents(a,d,w,T).normalize();else throw new Error("THREE.Frustum.setFromProjectionMatrix(): Invalid coordinate system: "+t);return this}intersectsObject(e){if(e.boundingSphere!==void 0)e.boundingSphere===null&&e.computeBoundingSphere(),Ds.copy(e.boundingSphere).applyMatrix4(e.matrixWorld);else{const t=e.geometry;t.boundingSphere===null&&t.computeBoundingSphere(),Ds.copy(t.boundingSphere).applyMatrix4(e.matrixWorld)}return this.intersectsSphere(Ds)}intersectsSprite(e){return Ds.center.set(0,0,0),Ds.radius=.7071067811865476,Ds.applyMatrix4(e.matrixWorld),this.intersectsSphere(Ds)}intersectsSphere(e){const t=this.planes,i=e.center,n=-e.radius;for(let s=0;s<6;s++)if(t[s].distanceToPoint(i) 0?e.max.x:e.min.x,ka.y=n.normal.y>0?e.max.y:e.min.y,ka.z=n.normal.z>0?e.max.z:e.min.z,n.distanceToPoint(ka)<0)return!1}return!0}containsPoint(e){const t=this.planes;for(let i=0;i<6;i++)if(t[i].distanceToPoint(e)<0)return!1;return!0}clone(){return new this.constructor().copy(this)}};function Pp(){let r=null,e=!1,t=null,i=null;function n(s,o){t(s,o),i=r.requestAnimationFrame(n)}return{start:function(){e!==!0&&t!==null&&(i=r.requestAnimationFrame(n),e=!0)},stop:function(){r.cancelAnimationFrame(i),e=!1},setAnimationLoop:function(s){t=s},setContext:function(s){r=s}}}function o0(r){const e=new WeakMap;function t(a,l){const h=a.array,u=a.usage,d=h.byteLength,f=r.createBuffer();r.bindBuffer(l,f),r.bufferData(l,h,u),a.onUploadCallback();let m;if(h instanceof Float32Array)m=r.FLOAT;else if(h instanceof Uint16Array)a.isFloat16BufferAttribute?m=r.HALF_FLOAT:m=r.UNSIGNED_SHORT;else if(h instanceof Int16Array)m=r.SHORT;else if(h instanceof Uint32Array)m=r.UNSIGNED_INT;else if(h instanceof Int32Array)m=r.INT;else if(h instanceof Int8Array)m=r.BYTE;else if(h instanceof Uint8Array)m=r.UNSIGNED_BYTE;else if(h instanceof Uint8ClampedArray)m=r.UNSIGNED_BYTE;else throw new Error("THREE.WebGLAttributes: Unsupported buffer data format: "+h);return{buffer:f,type:m,bytesPerElement:h.BYTES_PER_ELEMENT,version:a.version,size:d}}function i(a,l,h){const u=l.array,d=l._updateRange,f=l.updateRanges;if(r.bindBuffer(h,a),d.count===-1&&f.length===0&&r.bufferSubData(h,0,u),f.length!==0){for(let m=0,y=f.length;m outsideIOR when thinFilmThickness -> 0.0 + float iridescenceIOR = mix( outsideIOR, eta2, smoothstep( 0.0, 0.03, thinFilmThickness ) ); + // Evaluate the cosTheta on the base layer (Snell law) + float sinTheta2Sq = pow2( outsideIOR / iridescenceIOR ) * ( 1.0 - pow2( cosTheta1 ) ); + + // Handle TIR: + float cosTheta2Sq = 1.0 - sinTheta2Sq; + if ( cosTheta2Sq < 0.0 ) { + + return vec3( 1.0 ); + + } + + float cosTheta2 = sqrt( cosTheta2Sq ); + + // First interface + float R0 = IorToFresnel0( iridescenceIOR, outsideIOR ); + float R12 = F_Schlick( R0, 1.0, cosTheta1 ); + float T121 = 1.0 - R12; + float phi12 = 0.0; + if ( iridescenceIOR < outsideIOR ) phi12 = PI; + float phi21 = PI - phi12; + + // Second interface + vec3 baseIOR = Fresnel0ToIor( clamp( baseF0, 0.0, 0.9999 ) ); // guard against 1.0 + vec3 R1 = IorToFresnel0( baseIOR, iridescenceIOR ); + vec3 R23 = F_Schlick( R1, 1.0, cosTheta2 ); + vec3 phi23 = vec3( 0.0 ); + if ( baseIOR[ 0 ] < iridescenceIOR ) phi23[ 0 ] = PI; + if ( baseIOR[ 1 ] < iridescenceIOR ) phi23[ 1 ] = PI; + if ( baseIOR[ 2 ] < iridescenceIOR ) phi23[ 2 ] = PI; + + // Phase shift + float OPD = 2.0 * iridescenceIOR * thinFilmThickness * cosTheta2; + vec3 phi = vec3( phi21 ) + phi23; + + // Compound terms + vec3 R123 = clamp( R12 * R23, 1e-5, 0.9999 ); + vec3 r123 = sqrt( R123 ); + vec3 Rs = pow2( T121 ) * R23 / ( vec3( 1.0 ) - R123 ); + + // Reflectance term for m = 0 (DC term amplitude) + vec3 C0 = R12 + Rs; + I = C0; + + // Reflectance term for m > 0 (pairs of diracs) + vec3 Cm = Rs - T121; + for ( int m = 1; m <= 2; ++ m ) { + + Cm *= r123; + vec3 Sm = 2.0 * evalSensitivity( float( m ) * OPD, float( m ) * phi ); + I += Cm * Sm; + + } + + // Since out of gamut colors might be produced, negative color values are clamped to 0. + return max( I, vec3( 0.0 ) ); + + } + +#endif + +`,M0=` +#ifdef USE_BUMPMAP + + uniform sampler2D bumpMap; + uniform float bumpScale; + + // Bump Mapping Unparametrized Surfaces on the GPU by Morten S. Mikkelsen + // https://mmikk.github.io/papers3d/mm_sfgrad_bump.pdf + + // Evaluate the derivative of the height w.r.t. screen-space using forward differencing (listing 2) + + vec2 dHdxy_fwd() { + + vec2 dSTdx = dFdx( vBumpMapUv ); + vec2 dSTdy = dFdy( vBumpMapUv ); + + float Hll = bumpScale * texture2D( bumpMap, vBumpMapUv ).x; + float dBx = bumpScale * texture2D( bumpMap, vBumpMapUv + dSTdx ).x - Hll; + float dBy = bumpScale * texture2D( bumpMap, vBumpMapUv + dSTdy ).x - Hll; + + return vec2( dBx, dBy ); + + } + + vec3 perturbNormalArb( vec3 surf_pos, vec3 surf_norm, vec2 dHdxy, float faceDirection ) { + + // normalize is done to ensure that the bump map looks the same regardless of the texture's scale + vec3 vSigmaX = normalize( dFdx( surf_pos.xyz ) ); + vec3 vSigmaY = normalize( dFdy( surf_pos.xyz ) ); + vec3 vN = surf_norm; // normalized + + vec3 R1 = cross( vSigmaY, vN ); + vec3 R2 = cross( vN, vSigmaX ); + + float fDet = dot( vSigmaX, R1 ) * faceDirection; + + vec3 vGrad = sign( fDet ) * ( dHdxy.x * R1 + dHdxy.y * R2 ); + return normalize( abs( fDet ) * surf_norm - vGrad ); + + } + +#endif +`,S0=` +#if NUM_CLIPPING_PLANES > 0 + + vec4 plane; + + #ifdef ALPHA_TO_COVERAGE + + float distanceToPlane, distanceGradient; + float clipOpacity = 1.0; + + #pragma unroll_loop_start + for ( int i = 0; i < UNION_CLIPPING_PLANES; i ++ ) { + + plane = clippingPlanes[ i ]; + distanceToPlane = - dot( vClipPosition, plane.xyz ) + plane.w; + distanceGradient = fwidth( distanceToPlane ) / 2.0; + clipOpacity *= smoothstep( - distanceGradient, distanceGradient, distanceToPlane ); + + if ( clipOpacity == 0.0 ) discard; + + } + #pragma unroll_loop_end + + #if UNION_CLIPPING_PLANES < NUM_CLIPPING_PLANES + + float unionClipOpacity = 1.0; + + #pragma unroll_loop_start + for ( int i = UNION_CLIPPING_PLANES; i < NUM_CLIPPING_PLANES; i ++ ) { + + plane = clippingPlanes[ i ]; + distanceToPlane = - dot( vClipPosition, plane.xyz ) + plane.w; + distanceGradient = fwidth( distanceToPlane ) / 2.0; + unionClipOpacity *= 1.0 - smoothstep( - distanceGradient, distanceGradient, distanceToPlane ); + + } + #pragma unroll_loop_end + + clipOpacity *= 1.0 - unionClipOpacity; + + #endif + + diffuseColor.a *= clipOpacity; + + if ( diffuseColor.a == 0.0 ) discard; + + #else + + #pragma unroll_loop_start + for ( int i = 0; i < UNION_CLIPPING_PLANES; i ++ ) { + + plane = clippingPlanes[ i ]; + if ( dot( vClipPosition, plane.xyz ) > plane.w ) discard; + + } + #pragma unroll_loop_end + + #if UNION_CLIPPING_PLANES < NUM_CLIPPING_PLANES + + bool clipped = true; + + #pragma unroll_loop_start + for ( int i = UNION_CLIPPING_PLANES; i < NUM_CLIPPING_PLANES; i ++ ) { + + plane = clippingPlanes[ i ]; + clipped = ( dot( vClipPosition, plane.xyz ) > plane.w ) && clipped; + + } + #pragma unroll_loop_end + + if ( clipped ) discard; + + #endif + + #endif + +#endif +`,w0=` +#if NUM_CLIPPING_PLANES > 0 + + varying vec3 vClipPosition; + + uniform vec4 clippingPlanes[ NUM_CLIPPING_PLANES ]; + +#endif +`,b0=` +#if NUM_CLIPPING_PLANES > 0 + + varying vec3 vClipPosition; + +#endif +`,T0=` +#if NUM_CLIPPING_PLANES > 0 + + vClipPosition = - mvPosition.xyz; + +#endif +`,E0=` +#if defined( USE_COLOR_ALPHA ) + + diffuseColor *= vColor; + +#elif defined( USE_COLOR ) + + diffuseColor.rgb *= vColor; + +#endif +`,A0=` +#if defined( USE_COLOR_ALPHA ) + + varying vec4 vColor; + +#elif defined( USE_COLOR ) + + varying vec3 vColor; + +#endif +`,R0=` +#if defined( USE_COLOR_ALPHA ) + + varying vec4 vColor; + +#elif defined( USE_COLOR ) || defined( USE_INSTANCING_COLOR ) || defined( USE_BATCHING_COLOR ) + + varying vec3 vColor; + +#endif +`,C0=` +#if defined( USE_COLOR_ALPHA ) + + vColor = vec4( 1.0 ); + +#elif defined( USE_COLOR ) || defined( USE_INSTANCING_COLOR ) || defined( USE_BATCHING_COLOR ) + + vColor = vec3( 1.0 ); + +#endif + +#ifdef USE_COLOR + + vColor *= color; + +#endif + +#ifdef USE_INSTANCING_COLOR + + vColor.xyz *= instanceColor.xyz; + +#endif + +#ifdef USE_BATCHING_COLOR + + vec3 batchingColor = getBatchingColor( getIndirectIndex( gl_DrawID ) ); + + vColor.xyz *= batchingColor.xyz; + +#endif +`,L0=` +#define PI 3.141592653589793 +#define PI2 6.283185307179586 +#define PI_HALF 1.5707963267948966 +#define RECIPROCAL_PI 0.3183098861837907 +#define RECIPROCAL_PI2 0.15915494309189535 +#define EPSILON 1e-6 + +#ifndef saturate +// may have defined saturate() already +#define saturate( a ) clamp( a, 0.0, 1.0 ) +#endif +#define whiteComplement( a ) ( 1.0 - saturate( a ) ) + +float pow2( const in float x ) { return x*x; } +vec3 pow2( const in vec3 x ) { return x*x; } +float pow3( const in float x ) { return x*x*x; } +float pow4( const in float x ) { float x2 = x*x; return x2*x2; } +float max3( const in vec3 v ) { return max( max( v.x, v.y ), v.z ); } +float average( const in vec3 v ) { return dot( v, vec3( 0.3333333 ) ); } + +// expects values in the range of [0,1]x[0,1], returns values in the [0,1] range. +// do not collapse into a single function per: http://byteblacksmith.com/improvements-to-the-canonical-one-liner-glsl-rand-for-opengl-es-2-0/ +highp float rand( const in vec2 uv ) { + + const highp float a = 12.9898, b = 78.233, c = 43758.5453; + highp float dt = dot( uv.xy, vec2( a,b ) ), sn = mod( dt, PI ); + + return fract( sin( sn ) * c ); + +} + +#ifdef HIGH_PRECISION + float precisionSafeLength( vec3 v ) { return length( v ); } +#else + float precisionSafeLength( vec3 v ) { + float maxComponent = max3( abs( v ) ); + return length( v / maxComponent ) * maxComponent; + } +#endif + +struct IncidentLight { + vec3 color; + vec3 direction; + bool visible; +}; + +struct ReflectedLight { + vec3 directDiffuse; + vec3 directSpecular; + vec3 indirectDiffuse; + vec3 indirectSpecular; +}; + +#ifdef USE_ALPHAHASH + + varying vec3 vPosition; + +#endif + +vec3 transformDirection( in vec3 dir, in mat4 matrix ) { + + return normalize( ( matrix * vec4( dir, 0.0 ) ).xyz ); + +} + +vec3 inverseTransformDirection( in vec3 dir, in mat4 matrix ) { + + // dir can be either a direction vector or a normal vector + // upper-left 3x3 of matrix is assumed to be orthogonal + + return normalize( ( vec4( dir, 0.0 ) * matrix ).xyz ); + +} + +mat3 transposeMat3( const in mat3 m ) { + + mat3 tmp; + + tmp[ 0 ] = vec3( m[ 0 ].x, m[ 1 ].x, m[ 2 ].x ); + tmp[ 1 ] = vec3( m[ 0 ].y, m[ 1 ].y, m[ 2 ].y ); + tmp[ 2 ] = vec3( m[ 0 ].z, m[ 1 ].z, m[ 2 ].z ); + + return tmp; + +} + +float luminance( const in vec3 rgb ) { + + // assumes rgb is in linear color space with sRGB primaries and D65 white point + + const vec3 weights = vec3( 0.2126729, 0.7151522, 0.0721750 ); + + return dot( weights, rgb ); + +} + +bool isPerspectiveMatrix( mat4 m ) { + + return m[ 2 ][ 3 ] == - 1.0; + +} + +vec2 equirectUv( in vec3 dir ) { + + // dir is assumed to be unit length + + float u = atan( dir.z, dir.x ) * RECIPROCAL_PI2 + 0.5; + + float v = asin( clamp( dir.y, - 1.0, 1.0 ) ) * RECIPROCAL_PI + 0.5; + + return vec2( u, v ); + +} + +vec3 BRDF_Lambert( const in vec3 diffuseColor ) { + + return RECIPROCAL_PI * diffuseColor; + +} // validated + +vec3 F_Schlick( const in vec3 f0, const in float f90, const in float dotVH ) { + + // Original approximation by Christophe Schlick '94 + // float fresnel = pow( 1.0 - dotVH, 5.0 ); + + // Optimized variant (presented by Epic at SIGGRAPH '13) + // https://cdn2.unrealengine.com/Resources/files/2013SiggraphPresentationsNotes-26915738.pdf + float fresnel = exp2( ( - 5.55473 * dotVH - 6.98316 ) * dotVH ); + + return f0 * ( 1.0 - fresnel ) + ( f90 * fresnel ); + +} // validated + +float F_Schlick( const in float f0, const in float f90, const in float dotVH ) { + + // Original approximation by Christophe Schlick '94 + // float fresnel = pow( 1.0 - dotVH, 5.0 ); + + // Optimized variant (presented by Epic at SIGGRAPH '13) + // https://cdn2.unrealengine.com/Resources/files/2013SiggraphPresentationsNotes-26915738.pdf + float fresnel = exp2( ( - 5.55473 * dotVH - 6.98316 ) * dotVH ); + + return f0 * ( 1.0 - fresnel ) + ( f90 * fresnel ); + +} // validated +`,P0=` +#ifdef ENVMAP_TYPE_CUBE_UV + + #define cubeUV_minMipLevel 4.0 + #define cubeUV_minTileSize 16.0 + + // These shader functions convert between the UV coordinates of a single face of + // a cubemap, the 0-5 integer index of a cube face, and the direction vector for + // sampling a textureCube (not generally normalized ). + + float getFace( vec3 direction ) { + + vec3 absDirection = abs( direction ); + + float face = - 1.0; + + if ( absDirection.x > absDirection.z ) { + + if ( absDirection.x > absDirection.y ) + + face = direction.x > 0.0 ? 0.0 : 3.0; + + else + + face = direction.y > 0.0 ? 1.0 : 4.0; + + } else { + + if ( absDirection.z > absDirection.y ) + + face = direction.z > 0.0 ? 2.0 : 5.0; + + else + + face = direction.y > 0.0 ? 1.0 : 4.0; + + } + + return face; + + } + + // RH coordinate system; PMREM face-indexing convention + vec2 getUV( vec3 direction, float face ) { + + vec2 uv; + + if ( face == 0.0 ) { + + uv = vec2( direction.z, direction.y ) / abs( direction.x ); // pos x + + } else if ( face == 1.0 ) { + + uv = vec2( - direction.x, - direction.z ) / abs( direction.y ); // pos y + + } else if ( face == 2.0 ) { + + uv = vec2( - direction.x, direction.y ) / abs( direction.z ); // pos z + + } else if ( face == 3.0 ) { + + uv = vec2( - direction.z, direction.y ) / abs( direction.x ); // neg x + + } else if ( face == 4.0 ) { + + uv = vec2( - direction.x, direction.z ) / abs( direction.y ); // neg y + + } else { + + uv = vec2( direction.x, direction.y ) / abs( direction.z ); // neg z + + } + + return 0.5 * ( uv + 1.0 ); + + } + + vec3 bilinearCubeUV( sampler2D envMap, vec3 direction, float mipInt ) { + + float face = getFace( direction ); + + float filterInt = max( cubeUV_minMipLevel - mipInt, 0.0 ); + + mipInt = max( mipInt, cubeUV_minMipLevel ); + + float faceSize = exp2( mipInt ); + + highp vec2 uv = getUV( direction, face ) * ( faceSize - 2.0 ) + 1.0; // #25071 + + if ( face > 2.0 ) { + + uv.y += faceSize; + + face -= 3.0; + + } + + uv.x += face * faceSize; + + uv.x += filterInt * 3.0 * cubeUV_minTileSize; + + uv.y += 4.0 * ( exp2( CUBEUV_MAX_MIP ) - faceSize ); + + uv.x *= CUBEUV_TEXEL_WIDTH; + uv.y *= CUBEUV_TEXEL_HEIGHT; + + #ifdef texture2DGradEXT + + return texture2DGradEXT( envMap, uv, vec2( 0.0 ), vec2( 0.0 ) ).rgb; // disable anisotropic filtering + + #else + + return texture2D( envMap, uv ).rgb; + + #endif + + } + + // These defines must match with PMREMGenerator + + #define cubeUV_r0 1.0 + #define cubeUV_m0 - 2.0 + #define cubeUV_r1 0.8 + #define cubeUV_m1 - 1.0 + #define cubeUV_r4 0.4 + #define cubeUV_m4 2.0 + #define cubeUV_r5 0.305 + #define cubeUV_m5 3.0 + #define cubeUV_r6 0.21 + #define cubeUV_m6 4.0 + + float roughnessToMip( float roughness ) { + + float mip = 0.0; + + if ( roughness >= cubeUV_r1 ) { + + mip = ( cubeUV_r0 - roughness ) * ( cubeUV_m1 - cubeUV_m0 ) / ( cubeUV_r0 - cubeUV_r1 ) + cubeUV_m0; + + } else if ( roughness >= cubeUV_r4 ) { + + mip = ( cubeUV_r1 - roughness ) * ( cubeUV_m4 - cubeUV_m1 ) / ( cubeUV_r1 - cubeUV_r4 ) + cubeUV_m1; + + } else if ( roughness >= cubeUV_r5 ) { + + mip = ( cubeUV_r4 - roughness ) * ( cubeUV_m5 - cubeUV_m4 ) / ( cubeUV_r4 - cubeUV_r5 ) + cubeUV_m4; + + } else if ( roughness >= cubeUV_r6 ) { + + mip = ( cubeUV_r5 - roughness ) * ( cubeUV_m6 - cubeUV_m5 ) / ( cubeUV_r5 - cubeUV_r6 ) + cubeUV_m5; + + } else { + + mip = - 2.0 * log2( 1.16 * roughness ); // 1.16 = 1.79^0.25 + } + + return mip; + + } + + vec4 textureCubeUV( sampler2D envMap, vec3 sampleDir, float roughness ) { + + float mip = clamp( roughnessToMip( roughness ), cubeUV_m0, CUBEUV_MAX_MIP ); + + float mipF = fract( mip ); + + float mipInt = floor( mip ); + + vec3 color0 = bilinearCubeUV( envMap, sampleDir, mipInt ); + + if ( mipF == 0.0 ) { + + return vec4( color0, 1.0 ); + + } else { + + vec3 color1 = bilinearCubeUV( envMap, sampleDir, mipInt + 1.0 ); + + return vec4( mix( color0, color1, mipF ), 1.0 ); + + } + + } + +#endif +`,I0=` + +vec3 transformedNormal = objectNormal; +#ifdef USE_TANGENT + + vec3 transformedTangent = objectTangent; + +#endif + +#ifdef USE_BATCHING + + // this is in lieu of a per-instance normal-matrix + // shear transforms in the instance matrix are not supported + + mat3 bm = mat3( batchingMatrix ); + transformedNormal /= vec3( dot( bm[ 0 ], bm[ 0 ] ), dot( bm[ 1 ], bm[ 1 ] ), dot( bm[ 2 ], bm[ 2 ] ) ); + transformedNormal = bm * transformedNormal; + + #ifdef USE_TANGENT + + transformedTangent = bm * transformedTangent; + + #endif + +#endif + +#ifdef USE_INSTANCING + + // this is in lieu of a per-instance normal-matrix + // shear transforms in the instance matrix are not supported + + mat3 im = mat3( instanceMatrix ); + transformedNormal /= vec3( dot( im[ 0 ], im[ 0 ] ), dot( im[ 1 ], im[ 1 ] ), dot( im[ 2 ], im[ 2 ] ) ); + transformedNormal = im * transformedNormal; + + #ifdef USE_TANGENT + + transformedTangent = im * transformedTangent; + + #endif + +#endif + +transformedNormal = normalMatrix * transformedNormal; + +#ifdef FLIP_SIDED + + transformedNormal = - transformedNormal; + +#endif + +#ifdef USE_TANGENT + + transformedTangent = ( modelViewMatrix * vec4( transformedTangent, 0.0 ) ).xyz; + + #ifdef FLIP_SIDED + + transformedTangent = - transformedTangent; + + #endif + +#endif +`,D0=` +#ifdef USE_DISPLACEMENTMAP + + uniform sampler2D displacementMap; + uniform float displacementScale; + uniform float displacementBias; + +#endif +`,N0=` +#ifdef USE_DISPLACEMENTMAP + + transformed += normalize( objectNormal ) * ( texture2D( displacementMap, vDisplacementMapUv ).x * displacementScale + displacementBias ); + +#endif +`,U0=` +#ifdef USE_EMISSIVEMAP + + vec4 emissiveColor = texture2D( emissiveMap, vEmissiveMapUv ); + + totalEmissiveRadiance *= emissiveColor.rgb; + +#endif +`,F0=` +#ifdef USE_EMISSIVEMAP + + uniform sampler2D emissiveMap; + +#endif +`,O0=` +gl_FragColor = linearToOutputTexel( gl_FragColor ); +`,B0=` + +// http://www.russellcottrell.com/photo/matrixCalculator.htm + +// Linear sRGB => XYZ => Linear Display P3 +const mat3 LINEAR_SRGB_TO_LINEAR_DISPLAY_P3 = mat3( + vec3( 0.8224621, 0.177538, 0.0 ), + vec3( 0.0331941, 0.9668058, 0.0 ), + vec3( 0.0170827, 0.0723974, 0.9105199 ) +); + +// Linear Display P3 => XYZ => Linear sRGB +const mat3 LINEAR_DISPLAY_P3_TO_LINEAR_SRGB = mat3( + vec3( 1.2249401, - 0.2249404, 0.0 ), + vec3( - 0.0420569, 1.0420571, 0.0 ), + vec3( - 0.0196376, - 0.0786361, 1.0982735 ) +); + +vec4 LinearSRGBToLinearDisplayP3( in vec4 value ) { + return vec4( value.rgb * LINEAR_SRGB_TO_LINEAR_DISPLAY_P3, value.a ); +} + +vec4 LinearDisplayP3ToLinearSRGB( in vec4 value ) { + return vec4( value.rgb * LINEAR_DISPLAY_P3_TO_LINEAR_SRGB, value.a ); +} + +vec4 LinearTransferOETF( in vec4 value ) { + return value; +} + +vec4 sRGBTransferOETF( in vec4 value ) { + return vec4( mix( pow( value.rgb, vec3( 0.41666 ) ) * 1.055 - vec3( 0.055 ), value.rgb * 12.92, vec3( lessThanEqual( value.rgb, vec3( 0.0031308 ) ) ) ), value.a ); +} + +// @deprecated, r156 +vec4 LinearToLinear( in vec4 value ) { + return value; +} + +// @deprecated, r156 +vec4 LinearTosRGB( in vec4 value ) { + return sRGBTransferOETF( value ); +} +`,z0=` +#ifdef USE_ENVMAP + + #ifdef ENV_WORLDPOS + + vec3 cameraToFrag; + + if ( isOrthographic ) { + + cameraToFrag = normalize( vec3( - viewMatrix[ 0 ][ 2 ], - viewMatrix[ 1 ][ 2 ], - viewMatrix[ 2 ][ 2 ] ) ); + + } else { + + cameraToFrag = normalize( vWorldPosition - cameraPosition ); + + } + + // Transforming Normal Vectors with the Inverse Transformation + vec3 worldNormal = inverseTransformDirection( normal, viewMatrix ); + + #ifdef ENVMAP_MODE_REFLECTION + + vec3 reflectVec = reflect( cameraToFrag, worldNormal ); + + #else + + vec3 reflectVec = refract( cameraToFrag, worldNormal, refractionRatio ); + + #endif + + #else + + vec3 reflectVec = vReflect; + + #endif + + #ifdef ENVMAP_TYPE_CUBE + + vec4 envColor = textureCube( envMap, envMapRotation * vec3( flipEnvMap * reflectVec.x, reflectVec.yz ) ); + + #else + + vec4 envColor = vec4( 0.0 ); + + #endif + + #ifdef ENVMAP_BLENDING_MULTIPLY + + outgoingLight = mix( outgoingLight, outgoingLight * envColor.xyz, specularStrength * reflectivity ); + + #elif defined( ENVMAP_BLENDING_MIX ) + + outgoingLight = mix( outgoingLight, envColor.xyz, specularStrength * reflectivity ); + + #elif defined( ENVMAP_BLENDING_ADD ) + + outgoingLight += envColor.xyz * specularStrength * reflectivity; + + #endif + +#endif +`,k0=` +#ifdef USE_ENVMAP + + uniform float envMapIntensity; + uniform float flipEnvMap; + uniform mat3 envMapRotation; + + #ifdef ENVMAP_TYPE_CUBE + uniform samplerCube envMap; + #else + uniform sampler2D envMap; + #endif + +#endif +`,H0=` +#ifdef USE_ENVMAP + + uniform float reflectivity; + + #if defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( PHONG ) || defined( LAMBERT ) + + #define ENV_WORLDPOS + + #endif + + #ifdef ENV_WORLDPOS + + varying vec3 vWorldPosition; + uniform float refractionRatio; + #else + varying vec3 vReflect; + #endif + +#endif +`,V0=` +#ifdef USE_ENVMAP + + #if defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( PHONG ) || defined( LAMBERT ) + + #define ENV_WORLDPOS + + #endif + + #ifdef ENV_WORLDPOS + + varying vec3 vWorldPosition; + + #else + + varying vec3 vReflect; + uniform float refractionRatio; + + #endif + +#endif +`,G0=` +#ifdef USE_ENVMAP + + #ifdef ENV_WORLDPOS + + vWorldPosition = worldPosition.xyz; + + #else + + vec3 cameraToVertex; + + if ( isOrthographic ) { + + cameraToVertex = normalize( vec3( - viewMatrix[ 0 ][ 2 ], - viewMatrix[ 1 ][ 2 ], - viewMatrix[ 2 ][ 2 ] ) ); + + } else { + + cameraToVertex = normalize( worldPosition.xyz - cameraPosition ); + + } + + vec3 worldNormal = inverseTransformDirection( transformedNormal, viewMatrix ); + + #ifdef ENVMAP_MODE_REFLECTION + + vReflect = reflect( cameraToVertex, worldNormal ); + + #else + + vReflect = refract( cameraToVertex, worldNormal, refractionRatio ); + + #endif + + #endif + +#endif +`,W0=` +#ifdef USE_FOG + + vFogDepth = - mvPosition.z; + +#endif +`,X0=` +#ifdef USE_FOG + + varying float vFogDepth; + +#endif +`,q0=` +#ifdef USE_FOG + + #ifdef FOG_EXP2 + + float fogFactor = 1.0 - exp( - fogDensity * fogDensity * vFogDepth * vFogDepth ); + + #else + + float fogFactor = smoothstep( fogNear, fogFar, vFogDepth ); + + #endif + + gl_FragColor.rgb = mix( gl_FragColor.rgb, fogColor, fogFactor ); + +#endif +`,j0=` +#ifdef USE_FOG + + uniform vec3 fogColor; + varying float vFogDepth; + + #ifdef FOG_EXP2 + + uniform float fogDensity; + + #else + + uniform float fogNear; + uniform float fogFar; + + #endif + +#endif +`,$0=` + +#ifdef USE_GRADIENTMAP + + uniform sampler2D gradientMap; + +#endif + +vec3 getGradientIrradiance( vec3 normal, vec3 lightDirection ) { + + // dotNL will be from -1.0 to 1.0 + float dotNL = dot( normal, lightDirection ); + vec2 coord = vec2( dotNL * 0.5 + 0.5, 0.0 ); + + #ifdef USE_GRADIENTMAP + + return vec3( texture2D( gradientMap, coord ).r ); + + #else + + vec2 fw = fwidth( coord ) * 0.5; + return mix( vec3( 0.7 ), vec3( 1.0 ), smoothstep( 0.7 - fw.x, 0.7 + fw.x, coord.x ) ); + + #endif + +} +`,Y0=` +#ifdef USE_LIGHTMAP + + uniform sampler2D lightMap; + uniform float lightMapIntensity; + +#endif +`,K0=` +LambertMaterial material; +material.diffuseColor = diffuseColor.rgb; +material.specularStrength = specularStrength; +`,J0=` +varying vec3 vViewPosition; + +struct LambertMaterial { + + vec3 diffuseColor; + float specularStrength; + +}; + +void RE_Direct_Lambert( const in IncidentLight directLight, const in vec3 geometryPosition, const in vec3 geometryNormal, const in vec3 geometryViewDir, const in vec3 geometryClearcoatNormal, const in LambertMaterial material, inout ReflectedLight reflectedLight ) { + + float dotNL = saturate( dot( geometryNormal, directLight.direction ) ); + vec3 irradiance = dotNL * directLight.color; + + reflectedLight.directDiffuse += irradiance * BRDF_Lambert( material.diffuseColor ); + +} + +void RE_IndirectDiffuse_Lambert( const in vec3 irradiance, const in vec3 geometryPosition, const in vec3 geometryNormal, const in vec3 geometryViewDir, const in vec3 geometryClearcoatNormal, const in LambertMaterial material, inout ReflectedLight reflectedLight ) { + + reflectedLight.indirectDiffuse += irradiance * BRDF_Lambert( material.diffuseColor ); + +} + +#define RE_Direct RE_Direct_Lambert +#define RE_IndirectDiffuse RE_IndirectDiffuse_Lambert +`,Z0=` +uniform bool receiveShadow; +uniform vec3 ambientLightColor; + +#if defined( USE_LIGHT_PROBES ) + + uniform vec3 lightProbe[ 9 ]; + +#endif + +// get the irradiance (radiance convolved with cosine lobe) at the point 'normal' on the unit sphere +// source: https://graphics.stanford.edu/papers/envmap/envmap.pdf +vec3 shGetIrradianceAt( in vec3 normal, in vec3 shCoefficients[ 9 ] ) { + + // normal is assumed to have unit length + + float x = normal.x, y = normal.y, z = normal.z; + + // band 0 + vec3 result = shCoefficients[ 0 ] * 0.886227; + + // band 1 + result += shCoefficients[ 1 ] * 2.0 * 0.511664 * y; + result += shCoefficients[ 2 ] * 2.0 * 0.511664 * z; + result += shCoefficients[ 3 ] * 2.0 * 0.511664 * x; + + // band 2 + result += shCoefficients[ 4 ] * 2.0 * 0.429043 * x * y; + result += shCoefficients[ 5 ] * 2.0 * 0.429043 * y * z; + result += shCoefficients[ 6 ] * ( 0.743125 * z * z - 0.247708 ); + result += shCoefficients[ 7 ] * 2.0 * 0.429043 * x * z; + result += shCoefficients[ 8 ] * 0.429043 * ( x * x - y * y ); + + return result; + +} + +vec3 getLightProbeIrradiance( const in vec3 lightProbe[ 9 ], const in vec3 normal ) { + + vec3 worldNormal = inverseTransformDirection( normal, viewMatrix ); + + vec3 irradiance = shGetIrradianceAt( worldNormal, lightProbe ); + + return irradiance; + +} + +vec3 getAmbientLightIrradiance( const in vec3 ambientLightColor ) { + + vec3 irradiance = ambientLightColor; + + return irradiance; + +} + +float getDistanceAttenuation( const in float lightDistance, const in float cutoffDistance, const in float decayExponent ) { + + // based upon Frostbite 3 Moving to Physically-based Rendering + // page 32, equation 26: E[window1] + // https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf + float distanceFalloff = 1.0 / max( pow( lightDistance, decayExponent ), 0.01 ); + + if ( cutoffDistance > 0.0 ) { + + distanceFalloff *= pow2( saturate( 1.0 - pow4( lightDistance / cutoffDistance ) ) ); + + } + + return distanceFalloff; + +} + +float getSpotAttenuation( const in float coneCosine, const in float penumbraCosine, const in float angleCosine ) { + + return smoothstep( coneCosine, penumbraCosine, angleCosine ); + +} + +#if NUM_DIR_LIGHTS > 0 + + struct DirectionalLight { + vec3 direction; + vec3 color; + }; + + uniform DirectionalLight directionalLights[ NUM_DIR_LIGHTS ]; + + void getDirectionalLightInfo( const in DirectionalLight directionalLight, out IncidentLight light ) { + + light.color = directionalLight.color; + light.direction = directionalLight.direction; + light.visible = true; + + } + +#endif + + +#if NUM_POINT_LIGHTS > 0 + + struct PointLight { + vec3 position; + vec3 color; + float distance; + float decay; + }; + + uniform PointLight pointLights[ NUM_POINT_LIGHTS ]; + + // light is an out parameter as having it as a return value caused compiler errors on some devices + void getPointLightInfo( const in PointLight pointLight, const in vec3 geometryPosition, out IncidentLight light ) { + + vec3 lVector = pointLight.position - geometryPosition; + + light.direction = normalize( lVector ); + + float lightDistance = length( lVector ); + + light.color = pointLight.color; + light.color *= getDistanceAttenuation( lightDistance, pointLight.distance, pointLight.decay ); + light.visible = ( light.color != vec3( 0.0 ) ); + + } + +#endif + + +#if NUM_SPOT_LIGHTS > 0 + + struct SpotLight { + vec3 position; + vec3 direction; + vec3 color; + float distance; + float decay; + float coneCos; + float penumbraCos; + }; + + uniform SpotLight spotLights[ NUM_SPOT_LIGHTS ]; + + // light is an out parameter as having it as a return value caused compiler errors on some devices + void getSpotLightInfo( const in SpotLight spotLight, const in vec3 geometryPosition, out IncidentLight light ) { + + vec3 lVector = spotLight.position - geometryPosition; + + light.direction = normalize( lVector ); + + float angleCos = dot( light.direction, spotLight.direction ); + + float spotAttenuation = getSpotAttenuation( spotLight.coneCos, spotLight.penumbraCos, angleCos ); + + if ( spotAttenuation > 0.0 ) { + + float lightDistance = length( lVector ); + + light.color = spotLight.color * spotAttenuation; + light.color *= getDistanceAttenuation( lightDistance, spotLight.distance, spotLight.decay ); + light.visible = ( light.color != vec3( 0.0 ) ); + + } else { + + light.color = vec3( 0.0 ); + light.visible = false; + + } + + } + +#endif + + +#if NUM_RECT_AREA_LIGHTS > 0 + + struct RectAreaLight { + vec3 color; + vec3 position; + vec3 halfWidth; + vec3 halfHeight; + }; + + // Pre-computed values of LinearTransformedCosine approximation of BRDF + // BRDF approximation Texture is 64x64 + uniform sampler2D ltc_1; // RGBA Float + uniform sampler2D ltc_2; // RGBA Float + + uniform RectAreaLight rectAreaLights[ NUM_RECT_AREA_LIGHTS ]; + +#endif + + +#if NUM_HEMI_LIGHTS > 0 + + struct HemisphereLight { + vec3 direction; + vec3 skyColor; + vec3 groundColor; + }; + + uniform HemisphereLight hemisphereLights[ NUM_HEMI_LIGHTS ]; + + vec3 getHemisphereLightIrradiance( const in HemisphereLight hemiLight, const in vec3 normal ) { + + float dotNL = dot( normal, hemiLight.direction ); + float hemiDiffuseWeight = 0.5 * dotNL + 0.5; + + vec3 irradiance = mix( hemiLight.groundColor, hemiLight.skyColor, hemiDiffuseWeight ); + + return irradiance; + + } + +#endif +`,Q0=` +#ifdef USE_ENVMAP + + vec3 getIBLIrradiance( const in vec3 normal ) { + + #ifdef ENVMAP_TYPE_CUBE_UV + + vec3 worldNormal = inverseTransformDirection( normal, viewMatrix ); + + vec4 envMapColor = textureCubeUV( envMap, envMapRotation * worldNormal, 1.0 ); + + return PI * envMapColor.rgb * envMapIntensity; + + #else + + return vec3( 0.0 ); + + #endif + + } + + vec3 getIBLRadiance( const in vec3 viewDir, const in vec3 normal, const in float roughness ) { + + #ifdef ENVMAP_TYPE_CUBE_UV + + vec3 reflectVec = reflect( - viewDir, normal ); + + // Mixing the reflection with the normal is more accurate and keeps rough objects from gathering light from behind their tangent plane. + reflectVec = normalize( mix( reflectVec, normal, roughness * roughness) ); + + reflectVec = inverseTransformDirection( reflectVec, viewMatrix ); + + vec4 envMapColor = textureCubeUV( envMap, envMapRotation * reflectVec, roughness ); + + return envMapColor.rgb * envMapIntensity; + + #else + + return vec3( 0.0 ); + + #endif + + } + + #ifdef USE_ANISOTROPY + + vec3 getIBLAnisotropyRadiance( const in vec3 viewDir, const in vec3 normal, const in float roughness, const in vec3 bitangent, const in float anisotropy ) { + + #ifdef ENVMAP_TYPE_CUBE_UV + + // https://google.github.io/filament/Filament.md.html#lighting/imagebasedlights/anisotropy + vec3 bentNormal = cross( bitangent, viewDir ); + bentNormal = normalize( cross( bentNormal, bitangent ) ); + bentNormal = normalize( mix( bentNormal, normal, pow2( pow2( 1.0 - anisotropy * ( 1.0 - roughness ) ) ) ) ); + + return getIBLRadiance( viewDir, bentNormal, roughness ); + + #else + + return vec3( 0.0 ); + + #endif + + } + + #endif + +#endif +`,ex=` +ToonMaterial material; +material.diffuseColor = diffuseColor.rgb; +`,tx=` +varying vec3 vViewPosition; + +struct ToonMaterial { + + vec3 diffuseColor; + +}; + +void RE_Direct_Toon( const in IncidentLight directLight, const in vec3 geometryPosition, const in vec3 geometryNormal, const in vec3 geometryViewDir, const in vec3 geometryClearcoatNormal, const in ToonMaterial material, inout ReflectedLight reflectedLight ) { + + vec3 irradiance = getGradientIrradiance( geometryNormal, directLight.direction ) * directLight.color; + + reflectedLight.directDiffuse += irradiance * BRDF_Lambert( material.diffuseColor ); + +} + +void RE_IndirectDiffuse_Toon( const in vec3 irradiance, const in vec3 geometryPosition, const in vec3 geometryNormal, const in vec3 geometryViewDir, const in vec3 geometryClearcoatNormal, const in ToonMaterial material, inout ReflectedLight reflectedLight ) { + + reflectedLight.indirectDiffuse += irradiance * BRDF_Lambert( material.diffuseColor ); + +} + +#define RE_Direct RE_Direct_Toon +#define RE_IndirectDiffuse RE_IndirectDiffuse_Toon +`,ix=` +BlinnPhongMaterial material; +material.diffuseColor = diffuseColor.rgb; +material.specularColor = specular; +material.specularShininess = shininess; +material.specularStrength = specularStrength; +`,nx=` +varying vec3 vViewPosition; + +struct BlinnPhongMaterial { + + vec3 diffuseColor; + vec3 specularColor; + float specularShininess; + float specularStrength; + +}; + +void RE_Direct_BlinnPhong( const in IncidentLight directLight, const in vec3 geometryPosition, const in vec3 geometryNormal, const in vec3 geometryViewDir, const in vec3 geometryClearcoatNormal, const in BlinnPhongMaterial material, inout ReflectedLight reflectedLight ) { + + float dotNL = saturate( dot( geometryNormal, directLight.direction ) ); + vec3 irradiance = dotNL * directLight.color; + + reflectedLight.directDiffuse += irradiance * BRDF_Lambert( material.diffuseColor ); + + reflectedLight.directSpecular += irradiance * BRDF_BlinnPhong( directLight.direction, geometryViewDir, geometryNormal, material.specularColor, material.specularShininess ) * material.specularStrength; + +} + +void RE_IndirectDiffuse_BlinnPhong( const in vec3 irradiance, const in vec3 geometryPosition, const in vec3 geometryNormal, const in vec3 geometryViewDir, const in vec3 geometryClearcoatNormal, const in BlinnPhongMaterial material, inout ReflectedLight reflectedLight ) { + + reflectedLight.indirectDiffuse += irradiance * BRDF_Lambert( material.diffuseColor ); + +} + +#define RE_Direct RE_Direct_BlinnPhong +#define RE_IndirectDiffuse RE_IndirectDiffuse_BlinnPhong +`,sx=` +PhysicalMaterial material; +material.diffuseColor = diffuseColor.rgb * ( 1.0 - metalnessFactor ); + +vec3 dxy = max( abs( dFdx( nonPerturbedNormal ) ), abs( dFdy( nonPerturbedNormal ) ) ); +float geometryRoughness = max( max( dxy.x, dxy.y ), dxy.z ); + +material.roughness = max( roughnessFactor, 0.0525 );// 0.0525 corresponds to the base mip of a 256 cubemap. +material.roughness += geometryRoughness; +material.roughness = min( material.roughness, 1.0 ); + +#ifdef IOR + + material.ior = ior; + + #ifdef USE_SPECULAR + + float specularIntensityFactor = specularIntensity; + vec3 specularColorFactor = specularColor; + + #ifdef USE_SPECULAR_COLORMAP + + specularColorFactor *= texture2D( specularColorMap, vSpecularColorMapUv ).rgb; + + #endif + + #ifdef USE_SPECULAR_INTENSITYMAP + + specularIntensityFactor *= texture2D( specularIntensityMap, vSpecularIntensityMapUv ).a; + + #endif + + material.specularF90 = mix( specularIntensityFactor, 1.0, metalnessFactor ); + + #else + + float specularIntensityFactor = 1.0; + vec3 specularColorFactor = vec3( 1.0 ); + material.specularF90 = 1.0; + + #endif + + material.specularColor = mix( min( pow2( ( material.ior - 1.0 ) / ( material.ior + 1.0 ) ) * specularColorFactor, vec3( 1.0 ) ) * specularIntensityFactor, diffuseColor.rgb, metalnessFactor ); + +#else + + material.specularColor = mix( vec3( 0.04 ), diffuseColor.rgb, metalnessFactor ); + material.specularF90 = 1.0; + +#endif + +#ifdef USE_CLEARCOAT + + material.clearcoat = clearcoat; + material.clearcoatRoughness = clearcoatRoughness; + material.clearcoatF0 = vec3( 0.04 ); + material.clearcoatF90 = 1.0; + + #ifdef USE_CLEARCOATMAP + + material.clearcoat *= texture2D( clearcoatMap, vClearcoatMapUv ).x; + + #endif + + #ifdef USE_CLEARCOAT_ROUGHNESSMAP + + material.clearcoatRoughness *= texture2D( clearcoatRoughnessMap, vClearcoatRoughnessMapUv ).y; + + #endif + + material.clearcoat = saturate( material.clearcoat ); // Burley clearcoat model + material.clearcoatRoughness = max( material.clearcoatRoughness, 0.0525 ); + material.clearcoatRoughness += geometryRoughness; + material.clearcoatRoughness = min( material.clearcoatRoughness, 1.0 ); + +#endif + +#ifdef USE_DISPERSION + + material.dispersion = dispersion; + +#endif + +#ifdef USE_IRIDESCENCE + + material.iridescence = iridescence; + material.iridescenceIOR = iridescenceIOR; + + #ifdef USE_IRIDESCENCEMAP + + material.iridescence *= texture2D( iridescenceMap, vIridescenceMapUv ).r; + + #endif + + #ifdef USE_IRIDESCENCE_THICKNESSMAP + + material.iridescenceThickness = (iridescenceThicknessMaximum - iridescenceThicknessMinimum) * texture2D( iridescenceThicknessMap, vIridescenceThicknessMapUv ).g + iridescenceThicknessMinimum; + + #else + + material.iridescenceThickness = iridescenceThicknessMaximum; + + #endif + +#endif + +#ifdef USE_SHEEN + + material.sheenColor = sheenColor; + + #ifdef USE_SHEEN_COLORMAP + + material.sheenColor *= texture2D( sheenColorMap, vSheenColorMapUv ).rgb; + + #endif + + material.sheenRoughness = clamp( sheenRoughness, 0.07, 1.0 ); + + #ifdef USE_SHEEN_ROUGHNESSMAP + + material.sheenRoughness *= texture2D( sheenRoughnessMap, vSheenRoughnessMapUv ).a; + + #endif + +#endif + +#ifdef USE_ANISOTROPY + + #ifdef USE_ANISOTROPYMAP + + mat2 anisotropyMat = mat2( anisotropyVector.x, anisotropyVector.y, - anisotropyVector.y, anisotropyVector.x ); + vec3 anisotropyPolar = texture2D( anisotropyMap, vAnisotropyMapUv ).rgb; + vec2 anisotropyV = anisotropyMat * normalize( 2.0 * anisotropyPolar.rg - vec2( 1.0 ) ) * anisotropyPolar.b; + + #else + + vec2 anisotropyV = anisotropyVector; + + #endif + + material.anisotropy = length( anisotropyV ); + + if( material.anisotropy == 0.0 ) { + anisotropyV = vec2( 1.0, 0.0 ); + } else { + anisotropyV /= material.anisotropy; + material.anisotropy = saturate( material.anisotropy ); + } + + // Roughness along the anisotropy bitangent is the material roughness, while the tangent roughness increases with anisotropy. + material.alphaT = mix( pow2( material.roughness ), 1.0, pow2( material.anisotropy ) ); + + material.anisotropyT = tbn[ 0 ] * anisotropyV.x + tbn[ 1 ] * anisotropyV.y; + material.anisotropyB = tbn[ 1 ] * anisotropyV.x - tbn[ 0 ] * anisotropyV.y; + +#endif +`,rx=` + +struct PhysicalMaterial { + + vec3 diffuseColor; + float roughness; + vec3 specularColor; + float specularF90; + float dispersion; + + #ifdef USE_CLEARCOAT + float clearcoat; + float clearcoatRoughness; + vec3 clearcoatF0; + float clearcoatF90; + #endif + + #ifdef USE_IRIDESCENCE + float iridescence; + float iridescenceIOR; + float iridescenceThickness; + vec3 iridescenceFresnel; + vec3 iridescenceF0; + #endif + + #ifdef USE_SHEEN + vec3 sheenColor; + float sheenRoughness; + #endif + + #ifdef IOR + float ior; + #endif + + #ifdef USE_TRANSMISSION + float transmission; + float transmissionAlpha; + float thickness; + float attenuationDistance; + vec3 attenuationColor; + #endif + + #ifdef USE_ANISOTROPY + float anisotropy; + float alphaT; + vec3 anisotropyT; + vec3 anisotropyB; + #endif + +}; + +// temporary +vec3 clearcoatSpecularDirect = vec3( 0.0 ); +vec3 clearcoatSpecularIndirect = vec3( 0.0 ); +vec3 sheenSpecularDirect = vec3( 0.0 ); +vec3 sheenSpecularIndirect = vec3(0.0 ); + +vec3 Schlick_to_F0( const in vec3 f, const in float f90, const in float dotVH ) { + float x = clamp( 1.0 - dotVH, 0.0, 1.0 ); + float x2 = x * x; + float x5 = clamp( x * x2 * x2, 0.0, 0.9999 ); + + return ( f - vec3( f90 ) * x5 ) / ( 1.0 - x5 ); +} + +// Moving Frostbite to Physically Based Rendering 3.0 - page 12, listing 2 +// https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf +float V_GGX_SmithCorrelated( const in float alpha, const in float dotNL, const in float dotNV ) { + + float a2 = pow2( alpha ); + + float gv = dotNL * sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNV ) ); + float gl = dotNV * sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNL ) ); + + return 0.5 / max( gv + gl, EPSILON ); + +} + +// Microfacet Models for Refraction through Rough Surfaces - equation (33) +// http://graphicrants.blogspot.com/2013/08/specular-brdf-reference.html +// alpha is "roughness squared" in Disney’s reparameterization +float D_GGX( const in float alpha, const in float dotNH ) { + + float a2 = pow2( alpha ); + + float denom = pow2( dotNH ) * ( a2 - 1.0 ) + 1.0; // avoid alpha = 0 with dotNH = 1 + + return RECIPROCAL_PI * a2 / pow2( denom ); + +} + +// https://google.github.io/filament/Filament.md.html#materialsystem/anisotropicmodel/anisotropicspecularbrdf +#ifdef USE_ANISOTROPY + + float V_GGX_SmithCorrelated_Anisotropic( const in float alphaT, const in float alphaB, const in float dotTV, const in float dotBV, const in float dotTL, const in float dotBL, const in float dotNV, const in float dotNL ) { + + float gv = dotNL * length( vec3( alphaT * dotTV, alphaB * dotBV, dotNV ) ); + float gl = dotNV * length( vec3( alphaT * dotTL, alphaB * dotBL, dotNL ) ); + float v = 0.5 / ( gv + gl ); + + return saturate(v); + + } + + float D_GGX_Anisotropic( const in float alphaT, const in float alphaB, const in float dotNH, const in float dotTH, const in float dotBH ) { + + float a2 = alphaT * alphaB; + highp vec3 v = vec3( alphaB * dotTH, alphaT * dotBH, a2 * dotNH ); + highp float v2 = dot( v, v ); + float w2 = a2 / v2; + + return RECIPROCAL_PI * a2 * pow2 ( w2 ); + + } + +#endif + +#ifdef USE_CLEARCOAT + + // GGX Distribution, Schlick Fresnel, GGX_SmithCorrelated Visibility + vec3 BRDF_GGX_Clearcoat( const in vec3 lightDir, const in vec3 viewDir, const in vec3 normal, const in PhysicalMaterial material) { + + vec3 f0 = material.clearcoatF0; + float f90 = material.clearcoatF90; + float roughness = material.clearcoatRoughness; + + float alpha = pow2( roughness ); // UE4's roughness + + vec3 halfDir = normalize( lightDir + viewDir ); + + float dotNL = saturate( dot( normal, lightDir ) ); + float dotNV = saturate( dot( normal, viewDir ) ); + float dotNH = saturate( dot( normal, halfDir ) ); + float dotVH = saturate( dot( viewDir, halfDir ) ); + + vec3 F = F_Schlick( f0, f90, dotVH ); + + float V = V_GGX_SmithCorrelated( alpha, dotNL, dotNV ); + + float D = D_GGX( alpha, dotNH ); + + return F * ( V * D ); + + } + +#endif + +vec3 BRDF_GGX( const in vec3 lightDir, const in vec3 viewDir, const in vec3 normal, const in PhysicalMaterial material ) { + + vec3 f0 = material.specularColor; + float f90 = material.specularF90; + float roughness = material.roughness; + + float alpha = pow2( roughness ); // UE4's roughness + + vec3 halfDir = normalize( lightDir + viewDir ); + + float dotNL = saturate( dot( normal, lightDir ) ); + float dotNV = saturate( dot( normal, viewDir ) ); + float dotNH = saturate( dot( normal, halfDir ) ); + float dotVH = saturate( dot( viewDir, halfDir ) ); + + vec3 F = F_Schlick( f0, f90, dotVH ); + + #ifdef USE_IRIDESCENCE + + F = mix( F, material.iridescenceFresnel, material.iridescence ); + + #endif + + #ifdef USE_ANISOTROPY + + float dotTL = dot( material.anisotropyT, lightDir ); + float dotTV = dot( material.anisotropyT, viewDir ); + float dotTH = dot( material.anisotropyT, halfDir ); + float dotBL = dot( material.anisotropyB, lightDir ); + float dotBV = dot( material.anisotropyB, viewDir ); + float dotBH = dot( material.anisotropyB, halfDir ); + + float V = V_GGX_SmithCorrelated_Anisotropic( material.alphaT, alpha, dotTV, dotBV, dotTL, dotBL, dotNV, dotNL ); + + float D = D_GGX_Anisotropic( material.alphaT, alpha, dotNH, dotTH, dotBH ); + + #else + + float V = V_GGX_SmithCorrelated( alpha, dotNL, dotNV ); + + float D = D_GGX( alpha, dotNH ); + + #endif + + return F * ( V * D ); + +} + +// Rect Area Light + +// Real-Time Polygonal-Light Shading with Linearly Transformed Cosines +// by Eric Heitz, Jonathan Dupuy, Stephen Hill and David Neubelt +// code: https://github.com/selfshadow/ltc_code/ + +vec2 LTC_Uv( const in vec3 N, const in vec3 V, const in float roughness ) { + + const float LUT_SIZE = 64.0; + const float LUT_SCALE = ( LUT_SIZE - 1.0 ) / LUT_SIZE; + const float LUT_BIAS = 0.5 / LUT_SIZE; + + float dotNV = saturate( dot( N, V ) ); + + // texture parameterized by sqrt( GGX alpha ) and sqrt( 1 - cos( theta ) ) + vec2 uv = vec2( roughness, sqrt( 1.0 - dotNV ) ); + + uv = uv * LUT_SCALE + LUT_BIAS; + + return uv; + +} + +float LTC_ClippedSphereFormFactor( const in vec3 f ) { + + // Real-Time Area Lighting: a Journey from Research to Production (p.102) + // An approximation of the form factor of a horizon-clipped rectangle. + + float l = length( f ); + + return max( ( l * l + f.z ) / ( l + 1.0 ), 0.0 ); + +} + +vec3 LTC_EdgeVectorFormFactor( const in vec3 v1, const in vec3 v2 ) { + + float x = dot( v1, v2 ); + + float y = abs( x ); + + // rational polynomial approximation to theta / sin( theta ) / 2PI + float a = 0.8543985 + ( 0.4965155 + 0.0145206 * y ) * y; + float b = 3.4175940 + ( 4.1616724 + y ) * y; + float v = a / b; + + float theta_sintheta = ( x > 0.0 ) ? v : 0.5 * inversesqrt( max( 1.0 - x * x, 1e-7 ) ) - v; + + return cross( v1, v2 ) * theta_sintheta; + +} + +vec3 LTC_Evaluate( const in vec3 N, const in vec3 V, const in vec3 P, const in mat3 mInv, const in vec3 rectCoords[ 4 ] ) { + + // bail if point is on back side of plane of light + // assumes ccw winding order of light vertices + vec3 v1 = rectCoords[ 1 ] - rectCoords[ 0 ]; + vec3 v2 = rectCoords[ 3 ] - rectCoords[ 0 ]; + vec3 lightNormal = cross( v1, v2 ); + + if( dot( lightNormal, P - rectCoords[ 0 ] ) < 0.0 ) return vec3( 0.0 ); + + // construct orthonormal basis around N + vec3 T1, T2; + T1 = normalize( V - N * dot( V, N ) ); + T2 = - cross( N, T1 ); // negated from paper; possibly due to a different handedness of world coordinate system + + // compute transform + mat3 mat = mInv * transposeMat3( mat3( T1, T2, N ) ); + + // transform rect + vec3 coords[ 4 ]; + coords[ 0 ] = mat * ( rectCoords[ 0 ] - P ); + coords[ 1 ] = mat * ( rectCoords[ 1 ] - P ); + coords[ 2 ] = mat * ( rectCoords[ 2 ] - P ); + coords[ 3 ] = mat * ( rectCoords[ 3 ] - P ); + + // project rect onto sphere + coords[ 0 ] = normalize( coords[ 0 ] ); + coords[ 1 ] = normalize( coords[ 1 ] ); + coords[ 2 ] = normalize( coords[ 2 ] ); + coords[ 3 ] = normalize( coords[ 3 ] ); + + // calculate vector form factor + vec3 vectorFormFactor = vec3( 0.0 ); + vectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 0 ], coords[ 1 ] ); + vectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 1 ], coords[ 2 ] ); + vectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 2 ], coords[ 3 ] ); + vectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 3 ], coords[ 0 ] ); + + // adjust for horizon clipping + float result = LTC_ClippedSphereFormFactor( vectorFormFactor ); + +/* + // alternate method of adjusting for horizon clipping (see referece) + // refactoring required + float len = length( vectorFormFactor ); + float z = vectorFormFactor.z / len; + + const float LUT_SIZE = 64.0; + const float LUT_SCALE = ( LUT_SIZE - 1.0 ) / LUT_SIZE; + const float LUT_BIAS = 0.5 / LUT_SIZE; + + // tabulated horizon-clipped sphere, apparently... + vec2 uv = vec2( z * 0.5 + 0.5, len ); + uv = uv * LUT_SCALE + LUT_BIAS; + + float scale = texture2D( ltc_2, uv ).w; + + float result = len * scale; +*/ + + return vec3( result ); + +} + +// End Rect Area Light + +#if defined( USE_SHEEN ) + +// https://github.com/google/filament/blob/master/shaders/src/brdf.fs +float D_Charlie( float roughness, float dotNH ) { + + float alpha = pow2( roughness ); + + // Estevez and Kulla 2017, "Production Friendly Microfacet Sheen BRDF" + float invAlpha = 1.0 / alpha; + float cos2h = dotNH * dotNH; + float sin2h = max( 1.0 - cos2h, 0.0078125 ); // 2^(-14/2), so sin2h^2 > 0 in fp16 + + return ( 2.0 + invAlpha ) * pow( sin2h, invAlpha * 0.5 ) / ( 2.0 * PI ); + +} + +// https://github.com/google/filament/blob/master/shaders/src/brdf.fs +float V_Neubelt( float dotNV, float dotNL ) { + + // Neubelt and Pettineo 2013, "Crafting a Next-gen Material Pipeline for The Order: 1886" + return saturate( 1.0 / ( 4.0 * ( dotNL + dotNV - dotNL * dotNV ) ) ); + +} + +vec3 BRDF_Sheen( const in vec3 lightDir, const in vec3 viewDir, const in vec3 normal, vec3 sheenColor, const in float sheenRoughness ) { + + vec3 halfDir = normalize( lightDir + viewDir ); + + float dotNL = saturate( dot( normal, lightDir ) ); + float dotNV = saturate( dot( normal, viewDir ) ); + float dotNH = saturate( dot( normal, halfDir ) ); + + float D = D_Charlie( sheenRoughness, dotNH ); + float V = V_Neubelt( dotNV, dotNL ); + + return sheenColor * ( D * V ); + +} + +#endif + +// This is a curve-fit approxmation to the "Charlie sheen" BRDF integrated over the hemisphere from +// Estevez and Kulla 2017, "Production Friendly Microfacet Sheen BRDF". The analysis can be found +// in the Sheen section of https://drive.google.com/file/d/1T0D1VSyR4AllqIJTQAraEIzjlb5h4FKH/view?usp=sharing +float IBLSheenBRDF( const in vec3 normal, const in vec3 viewDir, const in float roughness ) { + + float dotNV = saturate( dot( normal, viewDir ) ); + + float r2 = roughness * roughness; + + float a = roughness < 0.25 ? -339.2 * r2 + 161.4 * roughness - 25.9 : -8.48 * r2 + 14.3 * roughness - 9.95; + + float b = roughness < 0.25 ? 44.0 * r2 - 23.7 * roughness + 3.26 : 1.97 * r2 - 3.27 * roughness + 0.72; + + float DG = exp( a * dotNV + b ) + ( roughness < 0.25 ? 0.0 : 0.1 * ( roughness - 0.25 ) ); + + return saturate( DG * RECIPROCAL_PI ); + +} + +// Analytical approximation of the DFG LUT, one half of the +// split-sum approximation used in indirect specular lighting. +// via 'environmentBRDF' from "Physically Based Shading on Mobile" +// https://www.unrealengine.com/blog/physically-based-shading-on-mobile +vec2 DFGApprox( const in vec3 normal, const in vec3 viewDir, const in float roughness ) { + + float dotNV = saturate( dot( normal, viewDir ) ); + + const vec4 c0 = vec4( - 1, - 0.0275, - 0.572, 0.022 ); + + const vec4 c1 = vec4( 1, 0.0425, 1.04, - 0.04 ); + + vec4 r = roughness * c0 + c1; + + float a004 = min( r.x * r.x, exp2( - 9.28 * dotNV ) ) * r.x + r.y; + + vec2 fab = vec2( - 1.04, 1.04 ) * a004 + r.zw; + + return fab; + +} + +vec3 EnvironmentBRDF( const in vec3 normal, const in vec3 viewDir, const in vec3 specularColor, const in float specularF90, const in float roughness ) { + + vec2 fab = DFGApprox( normal, viewDir, roughness ); + + return specularColor * fab.x + specularF90 * fab.y; + +} + +// Fdez-Agüera's "Multiple-Scattering Microfacet Model for Real-Time Image Based Lighting" +// Approximates multiscattering in order to preserve energy. +// http://www.jcgt.org/published/0008/01/03/ +#ifdef USE_IRIDESCENCE +void computeMultiscatteringIridescence( const in vec3 normal, const in vec3 viewDir, const in vec3 specularColor, const in float specularF90, const in float iridescence, const in vec3 iridescenceF0, const in float roughness, inout vec3 singleScatter, inout vec3 multiScatter ) { +#else +void computeMultiscattering( const in vec3 normal, const in vec3 viewDir, const in vec3 specularColor, const in float specularF90, const in float roughness, inout vec3 singleScatter, inout vec3 multiScatter ) { +#endif + + vec2 fab = DFGApprox( normal, viewDir, roughness ); + + #ifdef USE_IRIDESCENCE + + vec3 Fr = mix( specularColor, iridescenceF0, iridescence ); + + #else + + vec3 Fr = specularColor; + + #endif + + vec3 FssEss = Fr * fab.x + specularF90 * fab.y; + + float Ess = fab.x + fab.y; + float Ems = 1.0 - Ess; + + vec3 Favg = Fr + ( 1.0 - Fr ) * 0.047619; // 1/21 + vec3 Fms = FssEss * Favg / ( 1.0 - Ems * Favg ); + + singleScatter += FssEss; + multiScatter += Fms * Ems; + +} + +#if NUM_RECT_AREA_LIGHTS > 0 + + void RE_Direct_RectArea_Physical( const in RectAreaLight rectAreaLight, const in vec3 geometryPosition, const in vec3 geometryNormal, const in vec3 geometryViewDir, const in vec3 geometryClearcoatNormal, const in PhysicalMaterial material, inout ReflectedLight reflectedLight ) { + + vec3 normal = geometryNormal; + vec3 viewDir = geometryViewDir; + vec3 position = geometryPosition; + vec3 lightPos = rectAreaLight.position; + vec3 halfWidth = rectAreaLight.halfWidth; + vec3 halfHeight = rectAreaLight.halfHeight; + vec3 lightColor = rectAreaLight.color; + float roughness = material.roughness; + + vec3 rectCoords[ 4 ]; + rectCoords[ 0 ] = lightPos + halfWidth - halfHeight; // counterclockwise; light shines in local neg z direction + rectCoords[ 1 ] = lightPos - halfWidth - halfHeight; + rectCoords[ 2 ] = lightPos - halfWidth + halfHeight; + rectCoords[ 3 ] = lightPos + halfWidth + halfHeight; + + vec2 uv = LTC_Uv( normal, viewDir, roughness ); + + vec4 t1 = texture2D( ltc_1, uv ); + vec4 t2 = texture2D( ltc_2, uv ); + + mat3 mInv = mat3( + vec3( t1.x, 0, t1.y ), + vec3( 0, 1, 0 ), + vec3( t1.z, 0, t1.w ) + ); + + // LTC Fresnel Approximation by Stephen Hill + // http://blog.selfshadow.com/publications/s2016-advances/s2016_ltc_fresnel.pdf + vec3 fresnel = ( material.specularColor * t2.x + ( vec3( 1.0 ) - material.specularColor ) * t2.y ); + + reflectedLight.directSpecular += lightColor * fresnel * LTC_Evaluate( normal, viewDir, position, mInv, rectCoords ); + + reflectedLight.directDiffuse += lightColor * material.diffuseColor * LTC_Evaluate( normal, viewDir, position, mat3( 1.0 ), rectCoords ); + + } + +#endif + +void RE_Direct_Physical( const in IncidentLight directLight, const in vec3 geometryPosition, const in vec3 geometryNormal, const in vec3 geometryViewDir, const in vec3 geometryClearcoatNormal, const in PhysicalMaterial material, inout ReflectedLight reflectedLight ) { + + float dotNL = saturate( dot( geometryNormal, directLight.direction ) ); + + vec3 irradiance = dotNL * directLight.color; + + #ifdef USE_CLEARCOAT + + float dotNLcc = saturate( dot( geometryClearcoatNormal, directLight.direction ) ); + + vec3 ccIrradiance = dotNLcc * directLight.color; + + clearcoatSpecularDirect += ccIrradiance * BRDF_GGX_Clearcoat( directLight.direction, geometryViewDir, geometryClearcoatNormal, material ); + + #endif + + #ifdef USE_SHEEN + + sheenSpecularDirect += irradiance * BRDF_Sheen( directLight.direction, geometryViewDir, geometryNormal, material.sheenColor, material.sheenRoughness ); + + #endif + + reflectedLight.directSpecular += irradiance * BRDF_GGX( directLight.direction, geometryViewDir, geometryNormal, material ); + + reflectedLight.directDiffuse += irradiance * BRDF_Lambert( material.diffuseColor ); +} + +void RE_IndirectDiffuse_Physical( const in vec3 irradiance, const in vec3 geometryPosition, const in vec3 geometryNormal, const in vec3 geometryViewDir, const in vec3 geometryClearcoatNormal, const in PhysicalMaterial material, inout ReflectedLight reflectedLight ) { + + reflectedLight.indirectDiffuse += irradiance * BRDF_Lambert( material.diffuseColor ); + +} + +void RE_IndirectSpecular_Physical( const in vec3 radiance, const in vec3 irradiance, const in vec3 clearcoatRadiance, const in vec3 geometryPosition, const in vec3 geometryNormal, const in vec3 geometryViewDir, const in vec3 geometryClearcoatNormal, const in PhysicalMaterial material, inout ReflectedLight reflectedLight) { + + #ifdef USE_CLEARCOAT + + clearcoatSpecularIndirect += clearcoatRadiance * EnvironmentBRDF( geometryClearcoatNormal, geometryViewDir, material.clearcoatF0, material.clearcoatF90, material.clearcoatRoughness ); + + #endif + + #ifdef USE_SHEEN + + sheenSpecularIndirect += irradiance * material.sheenColor * IBLSheenBRDF( geometryNormal, geometryViewDir, material.sheenRoughness ); + + #endif + + // Both indirect specular and indirect diffuse light accumulate here + + vec3 singleScattering = vec3( 0.0 ); + vec3 multiScattering = vec3( 0.0 ); + vec3 cosineWeightedIrradiance = irradiance * RECIPROCAL_PI; + + #ifdef USE_IRIDESCENCE + + computeMultiscatteringIridescence( geometryNormal, geometryViewDir, material.specularColor, material.specularF90, material.iridescence, material.iridescenceFresnel, material.roughness, singleScattering, multiScattering ); + + #else + + computeMultiscattering( geometryNormal, geometryViewDir, material.specularColor, material.specularF90, material.roughness, singleScattering, multiScattering ); + + #endif + + vec3 totalScattering = singleScattering + multiScattering; + vec3 diffuse = material.diffuseColor * ( 1.0 - max( max( totalScattering.r, totalScattering.g ), totalScattering.b ) ); + + reflectedLight.indirectSpecular += radiance * singleScattering; + reflectedLight.indirectSpecular += multiScattering * cosineWeightedIrradiance; + + reflectedLight.indirectDiffuse += diffuse * cosineWeightedIrradiance; + +} + +#define RE_Direct RE_Direct_Physical +#define RE_Direct_RectArea RE_Direct_RectArea_Physical +#define RE_IndirectDiffuse RE_IndirectDiffuse_Physical +#define RE_IndirectSpecular RE_IndirectSpecular_Physical + +// ref: https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf +float computeSpecularOcclusion( const in float dotNV, const in float ambientOcclusion, const in float roughness ) { + + return saturate( pow( dotNV + ambientOcclusion, exp2( - 16.0 * roughness - 1.0 ) ) - 1.0 + ambientOcclusion ); + +} +`,ox=` +/** + * This is a template that can be used to light a material, it uses pluggable + * RenderEquations (RE)for specific lighting scenarios. + * + * Instructions for use: + * - Ensure that both RE_Direct, RE_IndirectDiffuse and RE_IndirectSpecular are defined + * - Create a material parameter that is to be passed as the third parameter to your lighting functions. + * + * TODO: + * - Add area light support. + * - Add sphere light support. + * - Add diffuse light probe (irradiance cubemap) support. + */ + +vec3 geometryPosition = - vViewPosition; +vec3 geometryNormal = normal; +vec3 geometryViewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( vViewPosition ); + +vec3 geometryClearcoatNormal = vec3( 0.0 ); + +#ifdef USE_CLEARCOAT + + geometryClearcoatNormal = clearcoatNormal; + +#endif + +#ifdef USE_IRIDESCENCE + + float dotNVi = saturate( dot( normal, geometryViewDir ) ); + + if ( material.iridescenceThickness == 0.0 ) { + + material.iridescence = 0.0; + + } else { + + material.iridescence = saturate( material.iridescence ); + + } + + if ( material.iridescence > 0.0 ) { + + material.iridescenceFresnel = evalIridescence( 1.0, material.iridescenceIOR, dotNVi, material.iridescenceThickness, material.specularColor ); + + // Iridescence F0 approximation + material.iridescenceF0 = Schlick_to_F0( material.iridescenceFresnel, 1.0, dotNVi ); + + } + +#endif + +IncidentLight directLight; + +#if ( NUM_POINT_LIGHTS > 0 ) && defined( RE_Direct ) + + PointLight pointLight; + #if defined( USE_SHADOWMAP ) && NUM_POINT_LIGHT_SHADOWS > 0 + PointLightShadow pointLightShadow; + #endif + + #pragma unroll_loop_start + for ( int i = 0; i < NUM_POINT_LIGHTS; i ++ ) { + + pointLight = pointLights[ i ]; + + getPointLightInfo( pointLight, geometryPosition, directLight ); + + #if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_POINT_LIGHT_SHADOWS ) + pointLightShadow = pointLightShadows[ i ]; + directLight.color *= ( directLight.visible && receiveShadow ) ? getPointShadow( pointShadowMap[ i ], pointLightShadow.shadowMapSize, pointLightShadow.shadowIntensity, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ i ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0; + #endif + + RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight ); + + } + #pragma unroll_loop_end + +#endif + +#if ( NUM_SPOT_LIGHTS > 0 ) && defined( RE_Direct ) + + SpotLight spotLight; + vec4 spotColor; + vec3 spotLightCoord; + bool inSpotLightMap; + + #if defined( USE_SHADOWMAP ) && NUM_SPOT_LIGHT_SHADOWS > 0 + SpotLightShadow spotLightShadow; + #endif + + #pragma unroll_loop_start + for ( int i = 0; i < NUM_SPOT_LIGHTS; i ++ ) { + + spotLight = spotLights[ i ]; + + getSpotLightInfo( spotLight, geometryPosition, directLight ); + + // spot lights are ordered [shadows with maps, shadows without maps, maps without shadows, none] + #if ( UNROLLED_LOOP_INDEX < NUM_SPOT_LIGHT_SHADOWS_WITH_MAPS ) + #define SPOT_LIGHT_MAP_INDEX UNROLLED_LOOP_INDEX + #elif ( UNROLLED_LOOP_INDEX < NUM_SPOT_LIGHT_SHADOWS ) + #define SPOT_LIGHT_MAP_INDEX NUM_SPOT_LIGHT_MAPS + #else + #define SPOT_LIGHT_MAP_INDEX ( UNROLLED_LOOP_INDEX - NUM_SPOT_LIGHT_SHADOWS + NUM_SPOT_LIGHT_SHADOWS_WITH_MAPS ) + #endif + + #if ( SPOT_LIGHT_MAP_INDEX < NUM_SPOT_LIGHT_MAPS ) + spotLightCoord = vSpotLightCoord[ i ].xyz / vSpotLightCoord[ i ].w; + inSpotLightMap = all( lessThan( abs( spotLightCoord * 2. - 1. ), vec3( 1.0 ) ) ); + spotColor = texture2D( spotLightMap[ SPOT_LIGHT_MAP_INDEX ], spotLightCoord.xy ); + directLight.color = inSpotLightMap ? directLight.color * spotColor.rgb : directLight.color; + #endif + + #undef SPOT_LIGHT_MAP_INDEX + + #if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_SPOT_LIGHT_SHADOWS ) + spotLightShadow = spotLightShadows[ i ]; + directLight.color *= ( directLight.visible && receiveShadow ) ? getShadow( spotShadowMap[ i ], spotLightShadow.shadowMapSize, spotLightShadow.shadowIntensity, spotLightShadow.shadowBias, spotLightShadow.shadowRadius, vSpotLightCoord[ i ] ) : 1.0; + #endif + + RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight ); + + } + #pragma unroll_loop_end + +#endif + +#if ( NUM_DIR_LIGHTS > 0 ) && defined( RE_Direct ) + + DirectionalLight directionalLight; + #if defined( USE_SHADOWMAP ) && NUM_DIR_LIGHT_SHADOWS > 0 + DirectionalLightShadow directionalLightShadow; + #endif + + #pragma unroll_loop_start + for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) { + + directionalLight = directionalLights[ i ]; + + getDirectionalLightInfo( directionalLight, directLight ); + + #if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS ) + directionalLightShadow = directionalLightShadows[ i ]; + directLight.color *= ( directLight.visible && receiveShadow ) ? getShadow( directionalShadowMap[ i ], directionalLightShadow.shadowMapSize, directionalLightShadow.shadowIntensity, directionalLightShadow.shadowBias, directionalLightShadow.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0; + #endif + + RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight ); + + } + #pragma unroll_loop_end + +#endif + +#if ( NUM_RECT_AREA_LIGHTS > 0 ) && defined( RE_Direct_RectArea ) + + RectAreaLight rectAreaLight; + + #pragma unroll_loop_start + for ( int i = 0; i < NUM_RECT_AREA_LIGHTS; i ++ ) { + + rectAreaLight = rectAreaLights[ i ]; + RE_Direct_RectArea( rectAreaLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight ); + + } + #pragma unroll_loop_end + +#endif + +#if defined( RE_IndirectDiffuse ) + + vec3 iblIrradiance = vec3( 0.0 ); + + vec3 irradiance = getAmbientLightIrradiance( ambientLightColor ); + + #if defined( USE_LIGHT_PROBES ) + + irradiance += getLightProbeIrradiance( lightProbe, geometryNormal ); + + #endif + + #if ( NUM_HEMI_LIGHTS > 0 ) + + #pragma unroll_loop_start + for ( int i = 0; i < NUM_HEMI_LIGHTS; i ++ ) { + + irradiance += getHemisphereLightIrradiance( hemisphereLights[ i ], geometryNormal ); + + } + #pragma unroll_loop_end + + #endif + +#endif + +#if defined( RE_IndirectSpecular ) + + vec3 radiance = vec3( 0.0 ); + vec3 clearcoatRadiance = vec3( 0.0 ); + +#endif +`,ax=` +#if defined( RE_IndirectDiffuse ) + + #ifdef USE_LIGHTMAP + + vec4 lightMapTexel = texture2D( lightMap, vLightMapUv ); + vec3 lightMapIrradiance = lightMapTexel.rgb * lightMapIntensity; + + irradiance += lightMapIrradiance; + + #endif + + #if defined( USE_ENVMAP ) && defined( STANDARD ) && defined( ENVMAP_TYPE_CUBE_UV ) + + iblIrradiance += getIBLIrradiance( geometryNormal ); + + #endif + +#endif + +#if defined( USE_ENVMAP ) && defined( RE_IndirectSpecular ) + + #ifdef USE_ANISOTROPY + + radiance += getIBLAnisotropyRadiance( geometryViewDir, geometryNormal, material.roughness, material.anisotropyB, material.anisotropy ); + + #else + + radiance += getIBLRadiance( geometryViewDir, geometryNormal, material.roughness ); + + #endif + + #ifdef USE_CLEARCOAT + + clearcoatRadiance += getIBLRadiance( geometryViewDir, geometryClearcoatNormal, material.clearcoatRoughness ); + + #endif + +#endif +`,lx=` +#if defined( RE_IndirectDiffuse ) + + RE_IndirectDiffuse( irradiance, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight ); + +#endif + +#if defined( RE_IndirectSpecular ) + + RE_IndirectSpecular( radiance, iblIrradiance, clearcoatRadiance, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight ); + +#endif +`,cx=` +#if defined( USE_LOGDEPTHBUF ) + + // Doing a strict comparison with == 1.0 can cause noise artifacts + // on some platforms. See issue #17623. + gl_FragDepth = vIsPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5; + +#endif +`,hx=` +#if defined( USE_LOGDEPTHBUF ) + + uniform float logDepthBufFC; + varying float vFragDepth; + varying float vIsPerspective; + +#endif +`,ux=` +#ifdef USE_LOGDEPTHBUF + + varying float vFragDepth; + varying float vIsPerspective; + +#endif +`,dx=` +#ifdef USE_LOGDEPTHBUF + + vFragDepth = 1.0 + gl_Position.w; + vIsPerspective = float( isPerspectiveMatrix( projectionMatrix ) ); + +#endif +`,fx=` +#ifdef USE_MAP + + vec4 sampledDiffuseColor = texture2D( map, vMapUv ); + + #ifdef DECODE_VIDEO_TEXTURE + + // use inline sRGB decode until browsers properly support SRGB8_ALPHA8 with video textures (#26516) + + sampledDiffuseColor = vec4( mix( pow( sampledDiffuseColor.rgb * 0.9478672986 + vec3( 0.0521327014 ), vec3( 2.4 ) ), sampledDiffuseColor.rgb * 0.0773993808, vec3( lessThanEqual( sampledDiffuseColor.rgb, vec3( 0.04045 ) ) ) ), sampledDiffuseColor.w ); + + #endif + + diffuseColor *= sampledDiffuseColor; + +#endif +`,px=` +#ifdef USE_MAP + + uniform sampler2D map; + +#endif +`,mx=` +#if defined( USE_MAP ) || defined( USE_ALPHAMAP ) + + #if defined( USE_POINTS_UV ) + + vec2 uv = vUv; + + #else + + vec2 uv = ( uvTransform * vec3( gl_PointCoord.x, 1.0 - gl_PointCoord.y, 1 ) ).xy; + + #endif + +#endif + +#ifdef USE_MAP + + diffuseColor *= texture2D( map, uv ); + +#endif + +#ifdef USE_ALPHAMAP + + diffuseColor.a *= texture2D( alphaMap, uv ).g; + +#endif +`,gx=` +#if defined( USE_POINTS_UV ) + + varying vec2 vUv; + +#else + + #if defined( USE_MAP ) || defined( USE_ALPHAMAP ) + + uniform mat3 uvTransform; + + #endif + +#endif + +#ifdef USE_MAP + + uniform sampler2D map; + +#endif + +#ifdef USE_ALPHAMAP + + uniform sampler2D alphaMap; + +#endif +`,xx=` +float metalnessFactor = metalness; + +#ifdef USE_METALNESSMAP + + vec4 texelMetalness = texture2D( metalnessMap, vMetalnessMapUv ); + + // reads channel B, compatible with a combined OcclusionRoughnessMetallic (RGB) texture + metalnessFactor *= texelMetalness.b; + +#endif +`,_x=` +#ifdef USE_METALNESSMAP + + uniform sampler2D metalnessMap; + +#endif +`,yx=` +#ifdef USE_INSTANCING_MORPH + + float morphTargetInfluences[ MORPHTARGETS_COUNT ]; + + float morphTargetBaseInfluence = texelFetch( morphTexture, ivec2( 0, gl_InstanceID ), 0 ).r; + + for ( int i = 0; i < MORPHTARGETS_COUNT; i ++ ) { + + morphTargetInfluences[i] = texelFetch( morphTexture, ivec2( i + 1, gl_InstanceID ), 0 ).r; + + } +#endif +`,vx=` +#if defined( USE_MORPHCOLORS ) + + // morphTargetBaseInfluence is set based on BufferGeometry.morphTargetsRelative value: + // When morphTargetsRelative is false, this is set to 1 - sum(influences); this results in normal = sum((target - base) * influence) + // When morphTargetsRelative is true, this is set to 1; as a result, all morph targets are simply added to the base after weighting + vColor *= morphTargetBaseInfluence; + + for ( int i = 0; i < MORPHTARGETS_COUNT; i ++ ) { + + #if defined( USE_COLOR_ALPHA ) + + if ( morphTargetInfluences[ i ] != 0.0 ) vColor += getMorph( gl_VertexID, i, 2 ) * morphTargetInfluences[ i ]; + + #elif defined( USE_COLOR ) + + if ( morphTargetInfluences[ i ] != 0.0 ) vColor += getMorph( gl_VertexID, i, 2 ).rgb * morphTargetInfluences[ i ]; + + #endif + + } + +#endif +`,Mx=` +#ifdef USE_MORPHNORMALS + + // morphTargetBaseInfluence is set based on BufferGeometry.morphTargetsRelative value: + // When morphTargetsRelative is false, this is set to 1 - sum(influences); this results in normal = sum((target - base) * influence) + // When morphTargetsRelative is true, this is set to 1; as a result, all morph targets are simply added to the base after weighting + objectNormal *= morphTargetBaseInfluence; + + for ( int i = 0; i < MORPHTARGETS_COUNT; i ++ ) { + + if ( morphTargetInfluences[ i ] != 0.0 ) objectNormal += getMorph( gl_VertexID, i, 1 ).xyz * morphTargetInfluences[ i ]; + + } + +#endif +`,Sx=` +#ifdef USE_MORPHTARGETS + + #ifndef USE_INSTANCING_MORPH + + uniform float morphTargetBaseInfluence; + uniform float morphTargetInfluences[ MORPHTARGETS_COUNT ]; + + #endif + + uniform sampler2DArray morphTargetsTexture; + uniform ivec2 morphTargetsTextureSize; + + vec4 getMorph( const in int vertexIndex, const in int morphTargetIndex, const in int offset ) { + + int texelIndex = vertexIndex * MORPHTARGETS_TEXTURE_STRIDE + offset; + int y = texelIndex / morphTargetsTextureSize.x; + int x = texelIndex - y * morphTargetsTextureSize.x; + + ivec3 morphUV = ivec3( x, y, morphTargetIndex ); + return texelFetch( morphTargetsTexture, morphUV, 0 ); + + } + +#endif +`,wx=` +#ifdef USE_MORPHTARGETS + + // morphTargetBaseInfluence is set based on BufferGeometry.morphTargetsRelative value: + // When morphTargetsRelative is false, this is set to 1 - sum(influences); this results in position = sum((target - base) * influence) + // When morphTargetsRelative is true, this is set to 1; as a result, all morph targets are simply added to the base after weighting + transformed *= morphTargetBaseInfluence; + + for ( int i = 0; i < MORPHTARGETS_COUNT; i ++ ) { + + if ( morphTargetInfluences[ i ] != 0.0 ) transformed += getMorph( gl_VertexID, i, 0 ).xyz * morphTargetInfluences[ i ]; + + } + +#endif +`,bx=` +float faceDirection = gl_FrontFacing ? 1.0 : - 1.0; + +#ifdef FLAT_SHADED + + vec3 fdx = dFdx( vViewPosition ); + vec3 fdy = dFdy( vViewPosition ); + vec3 normal = normalize( cross( fdx, fdy ) ); + +#else + + vec3 normal = normalize( vNormal ); + + #ifdef DOUBLE_SIDED + + normal *= faceDirection; + + #endif + +#endif + +#if defined( USE_NORMALMAP_TANGENTSPACE ) || defined( USE_CLEARCOAT_NORMALMAP ) || defined( USE_ANISOTROPY ) + + #ifdef USE_TANGENT + + mat3 tbn = mat3( normalize( vTangent ), normalize( vBitangent ), normal ); + + #else + + mat3 tbn = getTangentFrame( - vViewPosition, normal, + #if defined( USE_NORMALMAP ) + vNormalMapUv + #elif defined( USE_CLEARCOAT_NORMALMAP ) + vClearcoatNormalMapUv + #else + vUv + #endif + ); + + #endif + + #if defined( DOUBLE_SIDED ) && ! defined( FLAT_SHADED ) + + tbn[0] *= faceDirection; + tbn[1] *= faceDirection; + + #endif + +#endif + +#ifdef USE_CLEARCOAT_NORMALMAP + + #ifdef USE_TANGENT + + mat3 tbn2 = mat3( normalize( vTangent ), normalize( vBitangent ), normal ); + + #else + + mat3 tbn2 = getTangentFrame( - vViewPosition, normal, vClearcoatNormalMapUv ); + + #endif + + #if defined( DOUBLE_SIDED ) && ! defined( FLAT_SHADED ) + + tbn2[0] *= faceDirection; + tbn2[1] *= faceDirection; + + #endif + +#endif + +// non perturbed normal for clearcoat among others + +vec3 nonPerturbedNormal = normal; + +`,Tx=` + +#ifdef USE_NORMALMAP_OBJECTSPACE + + normal = texture2D( normalMap, vNormalMapUv ).xyz * 2.0 - 1.0; // overrides both flatShading and attribute normals + + #ifdef FLIP_SIDED + + normal = - normal; + + #endif + + #ifdef DOUBLE_SIDED + + normal = normal * faceDirection; + + #endif + + normal = normalize( normalMatrix * normal ); + +#elif defined( USE_NORMALMAP_TANGENTSPACE ) + + vec3 mapN = texture2D( normalMap, vNormalMapUv ).xyz * 2.0 - 1.0; + mapN.xy *= normalScale; + + normal = normalize( tbn * mapN ); + +#elif defined( USE_BUMPMAP ) + + normal = perturbNormalArb( - vViewPosition, normal, dHdxy_fwd(), faceDirection ); + +#endif +`,Ex=` +#ifndef FLAT_SHADED + + varying vec3 vNormal; + + #ifdef USE_TANGENT + + varying vec3 vTangent; + varying vec3 vBitangent; + + #endif + +#endif +`,Ax=` +#ifndef FLAT_SHADED + + varying vec3 vNormal; + + #ifdef USE_TANGENT + + varying vec3 vTangent; + varying vec3 vBitangent; + + #endif + +#endif +`,Rx=` +#ifndef FLAT_SHADED // normal is computed with derivatives when FLAT_SHADED + + vNormal = normalize( transformedNormal ); + + #ifdef USE_TANGENT + + vTangent = normalize( transformedTangent ); + vBitangent = normalize( cross( vNormal, vTangent ) * tangent.w ); + + #endif + +#endif +`,Cx=` +#ifdef USE_NORMALMAP + + uniform sampler2D normalMap; + uniform vec2 normalScale; + +#endif + +#ifdef USE_NORMALMAP_OBJECTSPACE + + uniform mat3 normalMatrix; + +#endif + +#if ! defined ( USE_TANGENT ) && ( defined ( USE_NORMALMAP_TANGENTSPACE ) || defined ( USE_CLEARCOAT_NORMALMAP ) || defined( USE_ANISOTROPY ) ) + + // Normal Mapping Without Precomputed Tangents + // http://www.thetenthplanet.de/archives/1180 + + mat3 getTangentFrame( vec3 eye_pos, vec3 surf_norm, vec2 uv ) { + + vec3 q0 = dFdx( eye_pos.xyz ); + vec3 q1 = dFdy( eye_pos.xyz ); + vec2 st0 = dFdx( uv.st ); + vec2 st1 = dFdy( uv.st ); + + vec3 N = surf_norm; // normalized + + vec3 q1perp = cross( q1, N ); + vec3 q0perp = cross( N, q0 ); + + vec3 T = q1perp * st0.x + q0perp * st1.x; + vec3 B = q1perp * st0.y + q0perp * st1.y; + + float det = max( dot( T, T ), dot( B, B ) ); + float scale = ( det == 0.0 ) ? 0.0 : inversesqrt( det ); + + return mat3( T * scale, B * scale, N ); + + } + +#endif +`,Lx=` +#ifdef USE_CLEARCOAT + + vec3 clearcoatNormal = nonPerturbedNormal; + +#endif +`,Px=` +#ifdef USE_CLEARCOAT_NORMALMAP + + vec3 clearcoatMapN = texture2D( clearcoatNormalMap, vClearcoatNormalMapUv ).xyz * 2.0 - 1.0; + clearcoatMapN.xy *= clearcoatNormalScale; + + clearcoatNormal = normalize( tbn2 * clearcoatMapN ); + +#endif +`,Ix=` + +#ifdef USE_CLEARCOATMAP + + uniform sampler2D clearcoatMap; + +#endif + +#ifdef USE_CLEARCOAT_NORMALMAP + + uniform sampler2D clearcoatNormalMap; + uniform vec2 clearcoatNormalScale; + +#endif + +#ifdef USE_CLEARCOAT_ROUGHNESSMAP + + uniform sampler2D clearcoatRoughnessMap; + +#endif +`,Dx=` + +#ifdef USE_IRIDESCENCEMAP + + uniform sampler2D iridescenceMap; + +#endif + +#ifdef USE_IRIDESCENCE_THICKNESSMAP + + uniform sampler2D iridescenceThicknessMap; + +#endif +`,Nx=` +#ifdef OPAQUE +diffuseColor.a = 1.0; +#endif + +#ifdef USE_TRANSMISSION +diffuseColor.a *= material.transmissionAlpha; +#endif + +gl_FragColor = vec4( outgoingLight, diffuseColor.a ); +`,Ux=` +vec3 packNormalToRGB( const in vec3 normal ) { + return normalize( normal ) * 0.5 + 0.5; +} + +vec3 unpackRGBToNormal( const in vec3 rgb ) { + return 2.0 * rgb.xyz - 1.0; +} + +const float PackUpscale = 256. / 255.; // fraction -> 0..1 (including 1) +const float UnpackDownscale = 255. / 256.; // 0..1 -> fraction (excluding 1) + +const vec3 PackFactors = vec3( 256. * 256. * 256., 256. * 256., 256. ); +const vec4 UnpackFactors = UnpackDownscale / vec4( PackFactors, 1. ); + +const float ShiftRight8 = 1. / 256.; + +vec4 packDepthToRGBA( const in float v ) { + vec4 r = vec4( fract( v * PackFactors ), v ); + r.yzw -= r.xyz * ShiftRight8; // tidy overflow + return r * PackUpscale; +} + +float unpackRGBAToDepth( const in vec4 v ) { + return dot( v, UnpackFactors ); +} + +vec2 packDepthToRG( in highp float v ) { + return packDepthToRGBA( v ).yx; +} + +float unpackRGToDepth( const in highp vec2 v ) { + return unpackRGBAToDepth( vec4( v.xy, 0.0, 0.0 ) ); +} + +vec4 pack2HalfToRGBA( vec2 v ) { + vec4 r = vec4( v.x, fract( v.x * 255.0 ), v.y, fract( v.y * 255.0 ) ); + return vec4( r.x - r.y / 255.0, r.y, r.z - r.w / 255.0, r.w ); +} + +vec2 unpackRGBATo2Half( vec4 v ) { + return vec2( v.x + ( v.y / 255.0 ), v.z + ( v.w / 255.0 ) ); +} + +// NOTE: viewZ, the z-coordinate in camera space, is negative for points in front of the camera + +float viewZToOrthographicDepth( const in float viewZ, const in float near, const in float far ) { + // -near maps to 0; -far maps to 1 + return ( viewZ + near ) / ( near - far ); +} + +float orthographicDepthToViewZ( const in float depth, const in float near, const in float far ) { + // maps orthographic depth in [ 0, 1 ] to viewZ + return depth * ( near - far ) - near; +} + +// NOTE: https://twitter.com/gonnavis/status/1377183786949959682 + +float viewZToPerspectiveDepth( const in float viewZ, const in float near, const in float far ) { + // -near maps to 0; -far maps to 1 + return ( ( near + viewZ ) * far ) / ( ( far - near ) * viewZ ); +} + +float perspectiveDepthToViewZ( const in float depth, const in float near, const in float far ) { + // maps perspective depth in [ 0, 1 ] to viewZ + return ( near * far ) / ( ( far - near ) * depth - far ); +} +`,Fx=` +#ifdef PREMULTIPLIED_ALPHA + + // Get get normal blending with premultipled, use with CustomBlending, OneFactor, OneMinusSrcAlphaFactor, AddEquation. + gl_FragColor.rgb *= gl_FragColor.a; + +#endif +`,Ox=` +vec4 mvPosition = vec4( transformed, 1.0 ); + +#ifdef USE_BATCHING + + mvPosition = batchingMatrix * mvPosition; + +#endif + +#ifdef USE_INSTANCING + + mvPosition = instanceMatrix * mvPosition; + +#endif + +mvPosition = modelViewMatrix * mvPosition; + +gl_Position = projectionMatrix * mvPosition; +`,Bx=` +#ifdef DITHERING + + gl_FragColor.rgb = dithering( gl_FragColor.rgb ); + +#endif +`,zx=` +#ifdef DITHERING + + // based on https://www.shadertoy.com/view/MslGR8 + vec3 dithering( vec3 color ) { + //Calculate grid position + float grid_position = rand( gl_FragCoord.xy ); + + //Shift the individual colors differently, thus making it even harder to see the dithering pattern + vec3 dither_shift_RGB = vec3( 0.25 / 255.0, -0.25 / 255.0, 0.25 / 255.0 ); + + //modify shift according to grid position. + dither_shift_RGB = mix( 2.0 * dither_shift_RGB, -2.0 * dither_shift_RGB, grid_position ); + + //shift the color by dither_shift + return color + dither_shift_RGB; + } + +#endif +`,kx=` +float roughnessFactor = roughness; + +#ifdef USE_ROUGHNESSMAP + + vec4 texelRoughness = texture2D( roughnessMap, vRoughnessMapUv ); + + // reads channel G, compatible with a combined OcclusionRoughnessMetallic (RGB) texture + roughnessFactor *= texelRoughness.g; + +#endif +`,Hx=` +#ifdef USE_ROUGHNESSMAP + + uniform sampler2D roughnessMap; + +#endif +`,Vx=` +#if NUM_SPOT_LIGHT_COORDS > 0 + + varying vec4 vSpotLightCoord[ NUM_SPOT_LIGHT_COORDS ]; + +#endif + +#if NUM_SPOT_LIGHT_MAPS > 0 + + uniform sampler2D spotLightMap[ NUM_SPOT_LIGHT_MAPS ]; + +#endif + +#ifdef USE_SHADOWMAP + + #if NUM_DIR_LIGHT_SHADOWS > 0 + + uniform sampler2D directionalShadowMap[ NUM_DIR_LIGHT_SHADOWS ]; + varying vec4 vDirectionalShadowCoord[ NUM_DIR_LIGHT_SHADOWS ]; + + struct DirectionalLightShadow { + float shadowIntensity; + float shadowBias; + float shadowNormalBias; + float shadowRadius; + vec2 shadowMapSize; + }; + + uniform DirectionalLightShadow directionalLightShadows[ NUM_DIR_LIGHT_SHADOWS ]; + + #endif + + #if NUM_SPOT_LIGHT_SHADOWS > 0 + + uniform sampler2D spotShadowMap[ NUM_SPOT_LIGHT_SHADOWS ]; + + struct SpotLightShadow { + float shadowIntensity; + float shadowBias; + float shadowNormalBias; + float shadowRadius; + vec2 shadowMapSize; + }; + + uniform SpotLightShadow spotLightShadows[ NUM_SPOT_LIGHT_SHADOWS ]; + + #endif + + #if NUM_POINT_LIGHT_SHADOWS > 0 + + uniform sampler2D pointShadowMap[ NUM_POINT_LIGHT_SHADOWS ]; + varying vec4 vPointShadowCoord[ NUM_POINT_LIGHT_SHADOWS ]; + + struct PointLightShadow { + float shadowIntensity; + float shadowBias; + float shadowNormalBias; + float shadowRadius; + vec2 shadowMapSize; + float shadowCameraNear; + float shadowCameraFar; + }; + + uniform PointLightShadow pointLightShadows[ NUM_POINT_LIGHT_SHADOWS ]; + + #endif + + /* + #if NUM_RECT_AREA_LIGHTS > 0 + + // TODO (abelnation): create uniforms for area light shadows + + #endif + */ + + float texture2DCompare( sampler2D depths, vec2 uv, float compare ) { + + return step( compare, unpackRGBAToDepth( texture2D( depths, uv ) ) ); + + } + + vec2 texture2DDistribution( sampler2D shadow, vec2 uv ) { + + return unpackRGBATo2Half( texture2D( shadow, uv ) ); + + } + + float VSMShadow (sampler2D shadow, vec2 uv, float compare ){ + + float occlusion = 1.0; + + vec2 distribution = texture2DDistribution( shadow, uv ); + + float hard_shadow = step( compare , distribution.x ); // Hard Shadow + + if (hard_shadow != 1.0 ) { + + float distance = compare - distribution.x ; + float variance = max( 0.00000, distribution.y * distribution.y ); + float softness_probability = variance / (variance + distance * distance ); // Chebeyshevs inequality + softness_probability = clamp( ( softness_probability - 0.3 ) / ( 0.95 - 0.3 ), 0.0, 1.0 ); // 0.3 reduces light bleed + occlusion = clamp( max( hard_shadow, softness_probability ), 0.0, 1.0 ); + + } + return occlusion; + + } + + float getShadow( sampler2D shadowMap, vec2 shadowMapSize, float shadowIntensity, float shadowBias, float shadowRadius, vec4 shadowCoord ) { + + float shadow = 1.0; + + shadowCoord.xyz /= shadowCoord.w; + shadowCoord.z += shadowBias; + + bool inFrustum = shadowCoord.x >= 0.0 && shadowCoord.x <= 1.0 && shadowCoord.y >= 0.0 && shadowCoord.y <= 1.0; + bool frustumTest = inFrustum && shadowCoord.z <= 1.0; + + if ( frustumTest ) { + + #if defined( SHADOWMAP_TYPE_PCF ) + + vec2 texelSize = vec2( 1.0 ) / shadowMapSize; + + float dx0 = - texelSize.x * shadowRadius; + float dy0 = - texelSize.y * shadowRadius; + float dx1 = + texelSize.x * shadowRadius; + float dy1 = + texelSize.y * shadowRadius; + float dx2 = dx0 / 2.0; + float dy2 = dy0 / 2.0; + float dx3 = dx1 / 2.0; + float dy3 = dy1 / 2.0; + + shadow = ( + texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx0, dy0 ), shadowCoord.z ) + + texture2DCompare( shadowMap, shadowCoord.xy + vec2( 0.0, dy0 ), shadowCoord.z ) + + texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx1, dy0 ), shadowCoord.z ) + + texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx2, dy2 ), shadowCoord.z ) + + texture2DCompare( shadowMap, shadowCoord.xy + vec2( 0.0, dy2 ), shadowCoord.z ) + + texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx3, dy2 ), shadowCoord.z ) + + texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx0, 0.0 ), shadowCoord.z ) + + texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx2, 0.0 ), shadowCoord.z ) + + texture2DCompare( shadowMap, shadowCoord.xy, shadowCoord.z ) + + texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx3, 0.0 ), shadowCoord.z ) + + texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx1, 0.0 ), shadowCoord.z ) + + texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx2, dy3 ), shadowCoord.z ) + + texture2DCompare( shadowMap, shadowCoord.xy + vec2( 0.0, dy3 ), shadowCoord.z ) + + texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx3, dy3 ), shadowCoord.z ) + + texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx0, dy1 ), shadowCoord.z ) + + texture2DCompare( shadowMap, shadowCoord.xy + vec2( 0.0, dy1 ), shadowCoord.z ) + + texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx1, dy1 ), shadowCoord.z ) + ) * ( 1.0 / 17.0 ); + + #elif defined( SHADOWMAP_TYPE_PCF_SOFT ) + + vec2 texelSize = vec2( 1.0 ) / shadowMapSize; + float dx = texelSize.x; + float dy = texelSize.y; + + vec2 uv = shadowCoord.xy; + vec2 f = fract( uv * shadowMapSize + 0.5 ); + uv -= f * texelSize; + + shadow = ( + texture2DCompare( shadowMap, uv, shadowCoord.z ) + + texture2DCompare( shadowMap, uv + vec2( dx, 0.0 ), shadowCoord.z ) + + texture2DCompare( shadowMap, uv + vec2( 0.0, dy ), shadowCoord.z ) + + texture2DCompare( shadowMap, uv + texelSize, shadowCoord.z ) + + mix( texture2DCompare( shadowMap, uv + vec2( -dx, 0.0 ), shadowCoord.z ), + texture2DCompare( shadowMap, uv + vec2( 2.0 * dx, 0.0 ), shadowCoord.z ), + f.x ) + + mix( texture2DCompare( shadowMap, uv + vec2( -dx, dy ), shadowCoord.z ), + texture2DCompare( shadowMap, uv + vec2( 2.0 * dx, dy ), shadowCoord.z ), + f.x ) + + mix( texture2DCompare( shadowMap, uv + vec2( 0.0, -dy ), shadowCoord.z ), + texture2DCompare( shadowMap, uv + vec2( 0.0, 2.0 * dy ), shadowCoord.z ), + f.y ) + + mix( texture2DCompare( shadowMap, uv + vec2( dx, -dy ), shadowCoord.z ), + texture2DCompare( shadowMap, uv + vec2( dx, 2.0 * dy ), shadowCoord.z ), + f.y ) + + mix( mix( texture2DCompare( shadowMap, uv + vec2( -dx, -dy ), shadowCoord.z ), + texture2DCompare( shadowMap, uv + vec2( 2.0 * dx, -dy ), shadowCoord.z ), + f.x ), + mix( texture2DCompare( shadowMap, uv + vec2( -dx, 2.0 * dy ), shadowCoord.z ), + texture2DCompare( shadowMap, uv + vec2( 2.0 * dx, 2.0 * dy ), shadowCoord.z ), + f.x ), + f.y ) + ) * ( 1.0 / 9.0 ); + + #elif defined( SHADOWMAP_TYPE_VSM ) + + shadow = VSMShadow( shadowMap, shadowCoord.xy, shadowCoord.z ); + + #else // no percentage-closer filtering: + + shadow = texture2DCompare( shadowMap, shadowCoord.xy, shadowCoord.z ); + + #endif + + } + + return mix( 1.0, shadow, shadowIntensity ); + + } + + // cubeToUV() maps a 3D direction vector suitable for cube texture mapping to a 2D + // vector suitable for 2D texture mapping. This code uses the following layout for the + // 2D texture: + // + // xzXZ + // y Y + // + // Y - Positive y direction + // y - Negative y direction + // X - Positive x direction + // x - Negative x direction + // Z - Positive z direction + // z - Negative z direction + // + // Source and test bed: + // https://gist.github.com/tschw/da10c43c467ce8afd0c4 + + vec2 cubeToUV( vec3 v, float texelSizeY ) { + + // Number of texels to avoid at the edge of each square + + vec3 absV = abs( v ); + + // Intersect unit cube + + float scaleToCube = 1.0 / max( absV.x, max( absV.y, absV.z ) ); + absV *= scaleToCube; + + // Apply scale to avoid seams + + // two texels less per square (one texel will do for NEAREST) + v *= scaleToCube * ( 1.0 - 2.0 * texelSizeY ); + + // Unwrap + + // space: -1 ... 1 range for each square + // + // #X## dim := ( 4 , 2 ) + // # # center := ( 1 , 1 ) + + vec2 planar = v.xy; + + float almostATexel = 1.5 * texelSizeY; + float almostOne = 1.0 - almostATexel; + + if ( absV.z >= almostOne ) { + + if ( v.z > 0.0 ) + planar.x = 4.0 - v.x; + + } else if ( absV.x >= almostOne ) { + + float signX = sign( v.x ); + planar.x = v.z * signX + 2.0 * signX; + + } else if ( absV.y >= almostOne ) { + + float signY = sign( v.y ); + planar.x = v.x + 2.0 * signY + 2.0; + planar.y = v.z * signY - 2.0; + + } + + // Transform to UV space + + // scale := 0.5 / dim + // translate := ( center + 0.5 ) / dim + return vec2( 0.125, 0.25 ) * planar + vec2( 0.375, 0.75 ); + + } + + float getPointShadow( sampler2D shadowMap, vec2 shadowMapSize, float shadowIntensity, float shadowBias, float shadowRadius, vec4 shadowCoord, float shadowCameraNear, float shadowCameraFar ) { + + float shadow = 1.0; + + // for point lights, the uniform @vShadowCoord is re-purposed to hold + // the vector from the light to the world-space position of the fragment. + vec3 lightToPosition = shadowCoord.xyz; + + float lightToPositionLength = length( lightToPosition ); + + if ( lightToPositionLength - shadowCameraFar <= 0.0 && lightToPositionLength - shadowCameraNear >= 0.0 ) { + + // dp = normalized distance from light to fragment position + float dp = ( lightToPositionLength - shadowCameraNear ) / ( shadowCameraFar - shadowCameraNear ); // need to clamp? + dp += shadowBias; + + // bd3D = base direction 3D + vec3 bd3D = normalize( lightToPosition ); + + vec2 texelSize = vec2( 1.0 ) / ( shadowMapSize * vec2( 4.0, 2.0 ) ); + + #if defined( SHADOWMAP_TYPE_PCF ) || defined( SHADOWMAP_TYPE_PCF_SOFT ) || defined( SHADOWMAP_TYPE_VSM ) + + vec2 offset = vec2( - 1, 1 ) * shadowRadius * texelSize.y; + + shadow = ( + texture2DCompare( shadowMap, cubeToUV( bd3D + offset.xyy, texelSize.y ), dp ) + + texture2DCompare( shadowMap, cubeToUV( bd3D + offset.yyy, texelSize.y ), dp ) + + texture2DCompare( shadowMap, cubeToUV( bd3D + offset.xyx, texelSize.y ), dp ) + + texture2DCompare( shadowMap, cubeToUV( bd3D + offset.yyx, texelSize.y ), dp ) + + texture2DCompare( shadowMap, cubeToUV( bd3D, texelSize.y ), dp ) + + texture2DCompare( shadowMap, cubeToUV( bd3D + offset.xxy, texelSize.y ), dp ) + + texture2DCompare( shadowMap, cubeToUV( bd3D + offset.yxy, texelSize.y ), dp ) + + texture2DCompare( shadowMap, cubeToUV( bd3D + offset.xxx, texelSize.y ), dp ) + + texture2DCompare( shadowMap, cubeToUV( bd3D + offset.yxx, texelSize.y ), dp ) + ) * ( 1.0 / 9.0 ); + + #else // no percentage-closer filtering + + shadow = texture2DCompare( shadowMap, cubeToUV( bd3D, texelSize.y ), dp ); + + #endif + + } + + return mix( 1.0, shadow, shadowIntensity ); + + } + +#endif +`,Gx=` + +#if NUM_SPOT_LIGHT_COORDS > 0 + + uniform mat4 spotLightMatrix[ NUM_SPOT_LIGHT_COORDS ]; + varying vec4 vSpotLightCoord[ NUM_SPOT_LIGHT_COORDS ]; + +#endif + +#ifdef USE_SHADOWMAP + + #if NUM_DIR_LIGHT_SHADOWS > 0 + + uniform mat4 directionalShadowMatrix[ NUM_DIR_LIGHT_SHADOWS ]; + varying vec4 vDirectionalShadowCoord[ NUM_DIR_LIGHT_SHADOWS ]; + + struct DirectionalLightShadow { + float shadowIntensity; + float shadowBias; + float shadowNormalBias; + float shadowRadius; + vec2 shadowMapSize; + }; + + uniform DirectionalLightShadow directionalLightShadows[ NUM_DIR_LIGHT_SHADOWS ]; + + #endif + + #if NUM_SPOT_LIGHT_SHADOWS > 0 + + struct SpotLightShadow { + float shadowIntensity; + float shadowBias; + float shadowNormalBias; + float shadowRadius; + vec2 shadowMapSize; + }; + + uniform SpotLightShadow spotLightShadows[ NUM_SPOT_LIGHT_SHADOWS ]; + + #endif + + #if NUM_POINT_LIGHT_SHADOWS > 0 + + uniform mat4 pointShadowMatrix[ NUM_POINT_LIGHT_SHADOWS ]; + varying vec4 vPointShadowCoord[ NUM_POINT_LIGHT_SHADOWS ]; + + struct PointLightShadow { + float shadowIntensity; + float shadowBias; + float shadowNormalBias; + float shadowRadius; + vec2 shadowMapSize; + float shadowCameraNear; + float shadowCameraFar; + }; + + uniform PointLightShadow pointLightShadows[ NUM_POINT_LIGHT_SHADOWS ]; + + #endif + + /* + #if NUM_RECT_AREA_LIGHTS > 0 + + // TODO (abelnation): uniforms for area light shadows + + #endif + */ + +#endif +`,Wx=` + +#if ( defined( USE_SHADOWMAP ) && ( NUM_DIR_LIGHT_SHADOWS > 0 || NUM_POINT_LIGHT_SHADOWS > 0 ) ) || ( NUM_SPOT_LIGHT_COORDS > 0 ) + + // Offsetting the position used for querying occlusion along the world normal can be used to reduce shadow acne. + vec3 shadowWorldNormal = inverseTransformDirection( transformedNormal, viewMatrix ); + vec4 shadowWorldPosition; + +#endif + +#if defined( USE_SHADOWMAP ) + + #if NUM_DIR_LIGHT_SHADOWS > 0 + + #pragma unroll_loop_start + for ( int i = 0; i < NUM_DIR_LIGHT_SHADOWS; i ++ ) { + + shadowWorldPosition = worldPosition + vec4( shadowWorldNormal * directionalLightShadows[ i ].shadowNormalBias, 0 ); + vDirectionalShadowCoord[ i ] = directionalShadowMatrix[ i ] * shadowWorldPosition; + + } + #pragma unroll_loop_end + + #endif + + #if NUM_POINT_LIGHT_SHADOWS > 0 + + #pragma unroll_loop_start + for ( int i = 0; i < NUM_POINT_LIGHT_SHADOWS; i ++ ) { + + shadowWorldPosition = worldPosition + vec4( shadowWorldNormal * pointLightShadows[ i ].shadowNormalBias, 0 ); + vPointShadowCoord[ i ] = pointShadowMatrix[ i ] * shadowWorldPosition; + + } + #pragma unroll_loop_end + + #endif + + /* + #if NUM_RECT_AREA_LIGHTS > 0 + + // TODO (abelnation): update vAreaShadowCoord with area light info + + #endif + */ + +#endif + +// spot lights can be evaluated without active shadow mapping (when SpotLight.map is used) + +#if NUM_SPOT_LIGHT_COORDS > 0 + + #pragma unroll_loop_start + for ( int i = 0; i < NUM_SPOT_LIGHT_COORDS; i ++ ) { + + shadowWorldPosition = worldPosition; + #if ( defined( USE_SHADOWMAP ) && UNROLLED_LOOP_INDEX < NUM_SPOT_LIGHT_SHADOWS ) + shadowWorldPosition.xyz += shadowWorldNormal * spotLightShadows[ i ].shadowNormalBias; + #endif + vSpotLightCoord[ i ] = spotLightMatrix[ i ] * shadowWorldPosition; + + } + #pragma unroll_loop_end + +#endif + + +`,Xx=` +float getShadowMask() { + + float shadow = 1.0; + + #ifdef USE_SHADOWMAP + + #if NUM_DIR_LIGHT_SHADOWS > 0 + + DirectionalLightShadow directionalLight; + + #pragma unroll_loop_start + for ( int i = 0; i < NUM_DIR_LIGHT_SHADOWS; i ++ ) { + + directionalLight = directionalLightShadows[ i ]; + shadow *= receiveShadow ? getShadow( directionalShadowMap[ i ], directionalLight.shadowMapSize, directionalLight.shadowIntensity, directionalLight.shadowBias, directionalLight.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0; + + } + #pragma unroll_loop_end + + #endif + + #if NUM_SPOT_LIGHT_SHADOWS > 0 + + SpotLightShadow spotLight; + + #pragma unroll_loop_start + for ( int i = 0; i < NUM_SPOT_LIGHT_SHADOWS; i ++ ) { + + spotLight = spotLightShadows[ i ]; + shadow *= receiveShadow ? getShadow( spotShadowMap[ i ], spotLight.shadowMapSize, spotLight.shadowIntensity, spotLight.shadowBias, spotLight.shadowRadius, vSpotLightCoord[ i ] ) : 1.0; + + } + #pragma unroll_loop_end + + #endif + + #if NUM_POINT_LIGHT_SHADOWS > 0 + + PointLightShadow pointLight; + + #pragma unroll_loop_start + for ( int i = 0; i < NUM_POINT_LIGHT_SHADOWS; i ++ ) { + + pointLight = pointLightShadows[ i ]; + shadow *= receiveShadow ? getPointShadow( pointShadowMap[ i ], pointLight.shadowMapSize, pointLight.shadowIntensity, pointLight.shadowBias, pointLight.shadowRadius, vPointShadowCoord[ i ], pointLight.shadowCameraNear, pointLight.shadowCameraFar ) : 1.0; + + } + #pragma unroll_loop_end + + #endif + + /* + #if NUM_RECT_AREA_LIGHTS > 0 + + // TODO (abelnation): update shadow for Area light + + #endif + */ + + #endif + + return shadow; + +} +`,qx=` +#ifdef USE_SKINNING + + mat4 boneMatX = getBoneMatrix( skinIndex.x ); + mat4 boneMatY = getBoneMatrix( skinIndex.y ); + mat4 boneMatZ = getBoneMatrix( skinIndex.z ); + mat4 boneMatW = getBoneMatrix( skinIndex.w ); + +#endif +`,jx=` +#ifdef USE_SKINNING + + uniform mat4 bindMatrix; + uniform mat4 bindMatrixInverse; + + uniform highp sampler2D boneTexture; + + mat4 getBoneMatrix( const in float i ) { + + int size = textureSize( boneTexture, 0 ).x; + int j = int( i ) * 4; + int x = j % size; + int y = j / size; + vec4 v1 = texelFetch( boneTexture, ivec2( x, y ), 0 ); + vec4 v2 = texelFetch( boneTexture, ivec2( x + 1, y ), 0 ); + vec4 v3 = texelFetch( boneTexture, ivec2( x + 2, y ), 0 ); + vec4 v4 = texelFetch( boneTexture, ivec2( x + 3, y ), 0 ); + + return mat4( v1, v2, v3, v4 ); + + } + +#endif +`,$x=` +#ifdef USE_SKINNING + + vec4 skinVertex = bindMatrix * vec4( transformed, 1.0 ); + + vec4 skinned = vec4( 0.0 ); + skinned += boneMatX * skinVertex * skinWeight.x; + skinned += boneMatY * skinVertex * skinWeight.y; + skinned += boneMatZ * skinVertex * skinWeight.z; + skinned += boneMatW * skinVertex * skinWeight.w; + + transformed = ( bindMatrixInverse * skinned ).xyz; + +#endif +`,Yx=` +#ifdef USE_SKINNING + + mat4 skinMatrix = mat4( 0.0 ); + skinMatrix += skinWeight.x * boneMatX; + skinMatrix += skinWeight.y * boneMatY; + skinMatrix += skinWeight.z * boneMatZ; + skinMatrix += skinWeight.w * boneMatW; + skinMatrix = bindMatrixInverse * skinMatrix * bindMatrix; + + objectNormal = vec4( skinMatrix * vec4( objectNormal, 0.0 ) ).xyz; + + #ifdef USE_TANGENT + + objectTangent = vec4( skinMatrix * vec4( objectTangent, 0.0 ) ).xyz; + + #endif + +#endif +`,Kx=` +float specularStrength; + +#ifdef USE_SPECULARMAP + + vec4 texelSpecular = texture2D( specularMap, vSpecularMapUv ); + specularStrength = texelSpecular.r; + +#else + + specularStrength = 1.0; + +#endif +`,Jx=` +#ifdef USE_SPECULARMAP + + uniform sampler2D specularMap; + +#endif +`,Zx=` +#if defined( TONE_MAPPING ) + + gl_FragColor.rgb = toneMapping( gl_FragColor.rgb ); + +#endif +`,Qx=` +#ifndef saturate +// may have defined saturate() already +#define saturate( a ) clamp( a, 0.0, 1.0 ) +#endif + +uniform float toneMappingExposure; + +// exposure only +vec3 LinearToneMapping( vec3 color ) { + + return saturate( toneMappingExposure * color ); + +} + +// source: https://www.cs.utah.edu/docs/techreports/2002/pdf/UUCS-02-001.pdf +vec3 ReinhardToneMapping( vec3 color ) { + + color *= toneMappingExposure; + return saturate( color / ( vec3( 1.0 ) + color ) ); + +} + +// source: http://filmicworlds.com/blog/filmic-tonemapping-operators/ +vec3 OptimizedCineonToneMapping( vec3 color ) { + + // optimized filmic operator by Jim Hejl and Richard Burgess-Dawson + color *= toneMappingExposure; + color = max( vec3( 0.0 ), color - 0.004 ); + return pow( ( color * ( 6.2 * color + 0.5 ) ) / ( color * ( 6.2 * color + 1.7 ) + 0.06 ), vec3( 2.2 ) ); + +} + +// source: https://github.com/selfshadow/ltc_code/blob/master/webgl/shaders/ltc/ltc_blit.fs +vec3 RRTAndODTFit( vec3 v ) { + + vec3 a = v * ( v + 0.0245786 ) - 0.000090537; + vec3 b = v * ( 0.983729 * v + 0.4329510 ) + 0.238081; + return a / b; + +} + +// this implementation of ACES is modified to accommodate a brighter viewing environment. +// the scale factor of 1/0.6 is subjective. see discussion in #19621. + +vec3 ACESFilmicToneMapping( vec3 color ) { + + // sRGB => XYZ => D65_2_D60 => AP1 => RRT_SAT + const mat3 ACESInputMat = mat3( + vec3( 0.59719, 0.07600, 0.02840 ), // transposed from source + vec3( 0.35458, 0.90834, 0.13383 ), + vec3( 0.04823, 0.01566, 0.83777 ) + ); + + // ODT_SAT => XYZ => D60_2_D65 => sRGB + const mat3 ACESOutputMat = mat3( + vec3( 1.60475, -0.10208, -0.00327 ), // transposed from source + vec3( -0.53108, 1.10813, -0.07276 ), + vec3( -0.07367, -0.00605, 1.07602 ) + ); + + color *= toneMappingExposure / 0.6; + + color = ACESInputMat * color; + + // Apply RRT and ODT + color = RRTAndODTFit( color ); + + color = ACESOutputMat * color; + + // Clamp to [0, 1] + return saturate( color ); + +} + +// Matrices for rec 2020 <> rec 709 color space conversion +// matrix provided in row-major order so it has been transposed +// https://www.itu.int/pub/R-REP-BT.2407-2017 +const mat3 LINEAR_REC2020_TO_LINEAR_SRGB = mat3( + vec3( 1.6605, - 0.1246, - 0.0182 ), + vec3( - 0.5876, 1.1329, - 0.1006 ), + vec3( - 0.0728, - 0.0083, 1.1187 ) +); + +const mat3 LINEAR_SRGB_TO_LINEAR_REC2020 = mat3( + vec3( 0.6274, 0.0691, 0.0164 ), + vec3( 0.3293, 0.9195, 0.0880 ), + vec3( 0.0433, 0.0113, 0.8956 ) +); + +// https://iolite-engine.com/blog_posts/minimal_agx_implementation +// Mean error^2: 3.6705141e-06 +vec3 agxDefaultContrastApprox( vec3 x ) { + + vec3 x2 = x * x; + vec3 x4 = x2 * x2; + + return + 15.5 * x4 * x2 + - 40.14 * x4 * x + + 31.96 * x4 + - 6.868 * x2 * x + + 0.4298 * x2 + + 0.1191 * x + - 0.00232; + +} + +// AgX Tone Mapping implementation based on Filament, which in turn is based +// on Blender's implementation using rec 2020 primaries +// https://github.com/google/filament/pull/7236 +// Inputs and outputs are encoded as Linear-sRGB. + +vec3 AgXToneMapping( vec3 color ) { + + // AgX constants + const mat3 AgXInsetMatrix = mat3( + vec3( 0.856627153315983, 0.137318972929847, 0.11189821299995 ), + vec3( 0.0951212405381588, 0.761241990602591, 0.0767994186031903 ), + vec3( 0.0482516061458583, 0.101439036467562, 0.811302368396859 ) + ); + + // explicit AgXOutsetMatrix generated from Filaments AgXOutsetMatrixInv + const mat3 AgXOutsetMatrix = mat3( + vec3( 1.1271005818144368, - 0.1413297634984383, - 0.14132976349843826 ), + vec3( - 0.11060664309660323, 1.157823702216272, - 0.11060664309660294 ), + vec3( - 0.016493938717834573, - 0.016493938717834257, 1.2519364065950405 ) + ); + + // LOG2_MIN = -10.0 + // LOG2_MAX = +6.5 + // MIDDLE_GRAY = 0.18 + const float AgxMinEv = - 12.47393; // log2( pow( 2, LOG2_MIN ) * MIDDLE_GRAY ) + const float AgxMaxEv = 4.026069; // log2( pow( 2, LOG2_MAX ) * MIDDLE_GRAY ) + + color *= toneMappingExposure; + + color = LINEAR_SRGB_TO_LINEAR_REC2020 * color; + + color = AgXInsetMatrix * color; + + // Log2 encoding + color = max( color, 1e-10 ); // avoid 0 or negative numbers for log2 + color = log2( color ); + color = ( color - AgxMinEv ) / ( AgxMaxEv - AgxMinEv ); + + color = clamp( color, 0.0, 1.0 ); + + // Apply sigmoid + color = agxDefaultContrastApprox( color ); + + // Apply AgX look + // v = agxLook(v, look); + + color = AgXOutsetMatrix * color; + + // Linearize + color = pow( max( vec3( 0.0 ), color ), vec3( 2.2 ) ); + + color = LINEAR_REC2020_TO_LINEAR_SRGB * color; + + // Gamut mapping. Simple clamp for now. + color = clamp( color, 0.0, 1.0 ); + + return color; + +} + +// https://modelviewer.dev/examples/tone-mapping + +vec3 NeutralToneMapping( vec3 color ) { + + const float StartCompression = 0.8 - 0.04; + const float Desaturation = 0.15; + + color *= toneMappingExposure; + + float x = min( color.r, min( color.g, color.b ) ); + + float offset = x < 0.08 ? x - 6.25 * x * x : 0.04; + + color -= offset; + + float peak = max( color.r, max( color.g, color.b ) ); + + if ( peak < StartCompression ) return color; + + float d = 1. - StartCompression; + + float newPeak = 1. - d * d / ( peak + d - StartCompression ); + + color *= newPeak / peak; + + float g = 1. - 1. / ( Desaturation * ( peak - newPeak ) + 1. ); + + return mix( color, vec3( newPeak ), g ); + +} + +vec3 CustomToneMapping( vec3 color ) { return color; } +`,e_=` +#ifdef USE_TRANSMISSION + + material.transmission = transmission; + material.transmissionAlpha = 1.0; + material.thickness = thickness; + material.attenuationDistance = attenuationDistance; + material.attenuationColor = attenuationColor; + + #ifdef USE_TRANSMISSIONMAP + + material.transmission *= texture2D( transmissionMap, vTransmissionMapUv ).r; + + #endif + + #ifdef USE_THICKNESSMAP + + material.thickness *= texture2D( thicknessMap, vThicknessMapUv ).g; + + #endif + + vec3 pos = vWorldPosition; + vec3 v = normalize( cameraPosition - pos ); + vec3 n = inverseTransformDirection( normal, viewMatrix ); + + vec4 transmitted = getIBLVolumeRefraction( + n, v, material.roughness, material.diffuseColor, material.specularColor, material.specularF90, + pos, modelMatrix, viewMatrix, projectionMatrix, material.dispersion, material.ior, material.thickness, + material.attenuationColor, material.attenuationDistance ); + + material.transmissionAlpha = mix( material.transmissionAlpha, transmitted.a, material.transmission ); + + totalDiffuse = mix( totalDiffuse, transmitted.rgb, material.transmission ); + +#endif +`,t_=` +#ifdef USE_TRANSMISSION + + // Transmission code is based on glTF-Sampler-Viewer + // https://github.com/KhronosGroup/glTF-Sample-Viewer + + uniform float transmission; + uniform float thickness; + uniform float attenuationDistance; + uniform vec3 attenuationColor; + + #ifdef USE_TRANSMISSIONMAP + + uniform sampler2D transmissionMap; + + #endif + + #ifdef USE_THICKNESSMAP + + uniform sampler2D thicknessMap; + + #endif + + uniform vec2 transmissionSamplerSize; + uniform sampler2D transmissionSamplerMap; + + uniform mat4 modelMatrix; + uniform mat4 projectionMatrix; + + varying vec3 vWorldPosition; + + // Mipped Bicubic Texture Filtering by N8 + // https://www.shadertoy.com/view/Dl2SDW + + float w0( float a ) { + + return ( 1.0 / 6.0 ) * ( a * ( a * ( - a + 3.0 ) - 3.0 ) + 1.0 ); + + } + + float w1( float a ) { + + return ( 1.0 / 6.0 ) * ( a * a * ( 3.0 * a - 6.0 ) + 4.0 ); + + } + + float w2( float a ){ + + return ( 1.0 / 6.0 ) * ( a * ( a * ( - 3.0 * a + 3.0 ) + 3.0 ) + 1.0 ); + + } + + float w3( float a ) { + + return ( 1.0 / 6.0 ) * ( a * a * a ); + + } + + // g0 and g1 are the two amplitude functions + float g0( float a ) { + + return w0( a ) + w1( a ); + + } + + float g1( float a ) { + + return w2( a ) + w3( a ); + + } + + // h0 and h1 are the two offset functions + float h0( float a ) { + + return - 1.0 + w1( a ) / ( w0( a ) + w1( a ) ); + + } + + float h1( float a ) { + + return 1.0 + w3( a ) / ( w2( a ) + w3( a ) ); + + } + + vec4 bicubic( sampler2D tex, vec2 uv, vec4 texelSize, float lod ) { + + uv = uv * texelSize.zw + 0.5; + + vec2 iuv = floor( uv ); + vec2 fuv = fract( uv ); + + float g0x = g0( fuv.x ); + float g1x = g1( fuv.x ); + float h0x = h0( fuv.x ); + float h1x = h1( fuv.x ); + float h0y = h0( fuv.y ); + float h1y = h1( fuv.y ); + + vec2 p0 = ( vec2( iuv.x + h0x, iuv.y + h0y ) - 0.5 ) * texelSize.xy; + vec2 p1 = ( vec2( iuv.x + h1x, iuv.y + h0y ) - 0.5 ) * texelSize.xy; + vec2 p2 = ( vec2( iuv.x + h0x, iuv.y + h1y ) - 0.5 ) * texelSize.xy; + vec2 p3 = ( vec2( iuv.x + h1x, iuv.y + h1y ) - 0.5 ) * texelSize.xy; + + return g0( fuv.y ) * ( g0x * textureLod( tex, p0, lod ) + g1x * textureLod( tex, p1, lod ) ) + + g1( fuv.y ) * ( g0x * textureLod( tex, p2, lod ) + g1x * textureLod( tex, p3, lod ) ); + + } + + vec4 textureBicubic( sampler2D sampler, vec2 uv, float lod ) { + + vec2 fLodSize = vec2( textureSize( sampler, int( lod ) ) ); + vec2 cLodSize = vec2( textureSize( sampler, int( lod + 1.0 ) ) ); + vec2 fLodSizeInv = 1.0 / fLodSize; + vec2 cLodSizeInv = 1.0 / cLodSize; + vec4 fSample = bicubic( sampler, uv, vec4( fLodSizeInv, fLodSize ), floor( lod ) ); + vec4 cSample = bicubic( sampler, uv, vec4( cLodSizeInv, cLodSize ), ceil( lod ) ); + return mix( fSample, cSample, fract( lod ) ); + + } + + vec3 getVolumeTransmissionRay( const in vec3 n, const in vec3 v, const in float thickness, const in float ior, const in mat4 modelMatrix ) { + + // Direction of refracted light. + vec3 refractionVector = refract( - v, normalize( n ), 1.0 / ior ); + + // Compute rotation-independant scaling of the model matrix. + vec3 modelScale; + modelScale.x = length( vec3( modelMatrix[ 0 ].xyz ) ); + modelScale.y = length( vec3( modelMatrix[ 1 ].xyz ) ); + modelScale.z = length( vec3( modelMatrix[ 2 ].xyz ) ); + + // The thickness is specified in local space. + return normalize( refractionVector ) * thickness * modelScale; + + } + + float applyIorToRoughness( const in float roughness, const in float ior ) { + + // Scale roughness with IOR so that an IOR of 1.0 results in no microfacet refraction and + // an IOR of 1.5 results in the default amount of microfacet refraction. + return roughness * clamp( ior * 2.0 - 2.0, 0.0, 1.0 ); + + } + + vec4 getTransmissionSample( const in vec2 fragCoord, const in float roughness, const in float ior ) { + + float lod = log2( transmissionSamplerSize.x ) * applyIorToRoughness( roughness, ior ); + return textureBicubic( transmissionSamplerMap, fragCoord.xy, lod ); + + } + + vec3 volumeAttenuation( const in float transmissionDistance, const in vec3 attenuationColor, const in float attenuationDistance ) { + + if ( isinf( attenuationDistance ) ) { + + // Attenuation distance is +∞, i.e. the transmitted color is not attenuated at all. + return vec3( 1.0 ); + + } else { + + // Compute light attenuation using Beer's law. + vec3 attenuationCoefficient = -log( attenuationColor ) / attenuationDistance; + vec3 transmittance = exp( - attenuationCoefficient * transmissionDistance ); // Beer's law + return transmittance; + + } + + } + + vec4 getIBLVolumeRefraction( const in vec3 n, const in vec3 v, const in float roughness, const in vec3 diffuseColor, + const in vec3 specularColor, const in float specularF90, const in vec3 position, const in mat4 modelMatrix, + const in mat4 viewMatrix, const in mat4 projMatrix, const in float dispersion, const in float ior, const in float thickness, + const in vec3 attenuationColor, const in float attenuationDistance ) { + + vec4 transmittedLight; + vec3 transmittance; + + #ifdef USE_DISPERSION + + float halfSpread = ( ior - 1.0 ) * 0.025 * dispersion; + vec3 iors = vec3( ior - halfSpread, ior, ior + halfSpread ); + + for ( int i = 0; i < 3; i ++ ) { + + vec3 transmissionRay = getVolumeTransmissionRay( n, v, thickness, iors[ i ], modelMatrix ); + vec3 refractedRayExit = position + transmissionRay; + + // Project refracted vector on the framebuffer, while mapping to normalized device coordinates. + vec4 ndcPos = projMatrix * viewMatrix * vec4( refractedRayExit, 1.0 ); + vec2 refractionCoords = ndcPos.xy / ndcPos.w; + refractionCoords += 1.0; + refractionCoords /= 2.0; + + // Sample framebuffer to get pixel the refracted ray hits. + vec4 transmissionSample = getTransmissionSample( refractionCoords, roughness, iors[ i ] ); + transmittedLight[ i ] = transmissionSample[ i ]; + transmittedLight.a += transmissionSample.a; + + transmittance[ i ] = diffuseColor[ i ] * volumeAttenuation( length( transmissionRay ), attenuationColor, attenuationDistance )[ i ]; + + } + + transmittedLight.a /= 3.0; + + #else + + vec3 transmissionRay = getVolumeTransmissionRay( n, v, thickness, ior, modelMatrix ); + vec3 refractedRayExit = position + transmissionRay; + + // Project refracted vector on the framebuffer, while mapping to normalized device coordinates. + vec4 ndcPos = projMatrix * viewMatrix * vec4( refractedRayExit, 1.0 ); + vec2 refractionCoords = ndcPos.xy / ndcPos.w; + refractionCoords += 1.0; + refractionCoords /= 2.0; + + // Sample framebuffer to get pixel the refracted ray hits. + transmittedLight = getTransmissionSample( refractionCoords, roughness, ior ); + transmittance = diffuseColor * volumeAttenuation( length( transmissionRay ), attenuationColor, attenuationDistance ); + + #endif + + vec3 attenuatedColor = transmittance * transmittedLight.rgb; + + // Get the specular component. + vec3 F = EnvironmentBRDF( n, v, specularColor, specularF90, roughness ); + + // As less light is transmitted, the opacity should be increased. This simple approximation does a decent job + // of modulating a CSS background, and has no effect when the buffer is opaque, due to a solid object or clear color. + float transmittanceFactor = ( transmittance.r + transmittance.g + transmittance.b ) / 3.0; + + return vec4( ( 1.0 - F ) * attenuatedColor, 1.0 - ( 1.0 - transmittedLight.a ) * transmittanceFactor ); + + } +#endif +`,i_=` +#if defined( USE_UV ) || defined( USE_ANISOTROPY ) + + varying vec2 vUv; + +#endif +#ifdef USE_MAP + + varying vec2 vMapUv; + +#endif +#ifdef USE_ALPHAMAP + + varying vec2 vAlphaMapUv; + +#endif +#ifdef USE_LIGHTMAP + + varying vec2 vLightMapUv; + +#endif +#ifdef USE_AOMAP + + varying vec2 vAoMapUv; + +#endif +#ifdef USE_BUMPMAP + + varying vec2 vBumpMapUv; + +#endif +#ifdef USE_NORMALMAP + + varying vec2 vNormalMapUv; + +#endif +#ifdef USE_EMISSIVEMAP + + varying vec2 vEmissiveMapUv; + +#endif +#ifdef USE_METALNESSMAP + + varying vec2 vMetalnessMapUv; + +#endif +#ifdef USE_ROUGHNESSMAP + + varying vec2 vRoughnessMapUv; + +#endif +#ifdef USE_ANISOTROPYMAP + + varying vec2 vAnisotropyMapUv; + +#endif +#ifdef USE_CLEARCOATMAP + + varying vec2 vClearcoatMapUv; + +#endif +#ifdef USE_CLEARCOAT_NORMALMAP + + varying vec2 vClearcoatNormalMapUv; + +#endif +#ifdef USE_CLEARCOAT_ROUGHNESSMAP + + varying vec2 vClearcoatRoughnessMapUv; + +#endif +#ifdef USE_IRIDESCENCEMAP + + varying vec2 vIridescenceMapUv; + +#endif +#ifdef USE_IRIDESCENCE_THICKNESSMAP + + varying vec2 vIridescenceThicknessMapUv; + +#endif +#ifdef USE_SHEEN_COLORMAP + + varying vec2 vSheenColorMapUv; + +#endif +#ifdef USE_SHEEN_ROUGHNESSMAP + + varying vec2 vSheenRoughnessMapUv; + +#endif +#ifdef USE_SPECULARMAP + + varying vec2 vSpecularMapUv; + +#endif +#ifdef USE_SPECULAR_COLORMAP + + varying vec2 vSpecularColorMapUv; + +#endif +#ifdef USE_SPECULAR_INTENSITYMAP + + varying vec2 vSpecularIntensityMapUv; + +#endif +#ifdef USE_TRANSMISSIONMAP + + uniform mat3 transmissionMapTransform; + varying vec2 vTransmissionMapUv; + +#endif +#ifdef USE_THICKNESSMAP + + uniform mat3 thicknessMapTransform; + varying vec2 vThicknessMapUv; + +#endif +`,n_=` +#if defined( USE_UV ) || defined( USE_ANISOTROPY ) + + varying vec2 vUv; + +#endif +#ifdef USE_MAP + + uniform mat3 mapTransform; + varying vec2 vMapUv; + +#endif +#ifdef USE_ALPHAMAP + + uniform mat3 alphaMapTransform; + varying vec2 vAlphaMapUv; + +#endif +#ifdef USE_LIGHTMAP + + uniform mat3 lightMapTransform; + varying vec2 vLightMapUv; + +#endif +#ifdef USE_AOMAP + + uniform mat3 aoMapTransform; + varying vec2 vAoMapUv; + +#endif +#ifdef USE_BUMPMAP + + uniform mat3 bumpMapTransform; + varying vec2 vBumpMapUv; + +#endif +#ifdef USE_NORMALMAP + + uniform mat3 normalMapTransform; + varying vec2 vNormalMapUv; + +#endif +#ifdef USE_DISPLACEMENTMAP + + uniform mat3 displacementMapTransform; + varying vec2 vDisplacementMapUv; + +#endif +#ifdef USE_EMISSIVEMAP + + uniform mat3 emissiveMapTransform; + varying vec2 vEmissiveMapUv; + +#endif +#ifdef USE_METALNESSMAP + + uniform mat3 metalnessMapTransform; + varying vec2 vMetalnessMapUv; + +#endif +#ifdef USE_ROUGHNESSMAP + + uniform mat3 roughnessMapTransform; + varying vec2 vRoughnessMapUv; + +#endif +#ifdef USE_ANISOTROPYMAP + + uniform mat3 anisotropyMapTransform; + varying vec2 vAnisotropyMapUv; + +#endif +#ifdef USE_CLEARCOATMAP + + uniform mat3 clearcoatMapTransform; + varying vec2 vClearcoatMapUv; + +#endif +#ifdef USE_CLEARCOAT_NORMALMAP + + uniform mat3 clearcoatNormalMapTransform; + varying vec2 vClearcoatNormalMapUv; + +#endif +#ifdef USE_CLEARCOAT_ROUGHNESSMAP + + uniform mat3 clearcoatRoughnessMapTransform; + varying vec2 vClearcoatRoughnessMapUv; + +#endif +#ifdef USE_SHEEN_COLORMAP + + uniform mat3 sheenColorMapTransform; + varying vec2 vSheenColorMapUv; + +#endif +#ifdef USE_SHEEN_ROUGHNESSMAP + + uniform mat3 sheenRoughnessMapTransform; + varying vec2 vSheenRoughnessMapUv; + +#endif +#ifdef USE_IRIDESCENCEMAP + + uniform mat3 iridescenceMapTransform; + varying vec2 vIridescenceMapUv; + +#endif +#ifdef USE_IRIDESCENCE_THICKNESSMAP + + uniform mat3 iridescenceThicknessMapTransform; + varying vec2 vIridescenceThicknessMapUv; + +#endif +#ifdef USE_SPECULARMAP + + uniform mat3 specularMapTransform; + varying vec2 vSpecularMapUv; + +#endif +#ifdef USE_SPECULAR_COLORMAP + + uniform mat3 specularColorMapTransform; + varying vec2 vSpecularColorMapUv; + +#endif +#ifdef USE_SPECULAR_INTENSITYMAP + + uniform mat3 specularIntensityMapTransform; + varying vec2 vSpecularIntensityMapUv; + +#endif +#ifdef USE_TRANSMISSIONMAP + + uniform mat3 transmissionMapTransform; + varying vec2 vTransmissionMapUv; + +#endif +#ifdef USE_THICKNESSMAP + + uniform mat3 thicknessMapTransform; + varying vec2 vThicknessMapUv; + +#endif +`,s_=` +#if defined( USE_UV ) || defined( USE_ANISOTROPY ) + + vUv = vec3( uv, 1 ).xy; + +#endif +#ifdef USE_MAP + + vMapUv = ( mapTransform * vec3( MAP_UV, 1 ) ).xy; + +#endif +#ifdef USE_ALPHAMAP + + vAlphaMapUv = ( alphaMapTransform * vec3( ALPHAMAP_UV, 1 ) ).xy; + +#endif +#ifdef USE_LIGHTMAP + + vLightMapUv = ( lightMapTransform * vec3( LIGHTMAP_UV, 1 ) ).xy; + +#endif +#ifdef USE_AOMAP + + vAoMapUv = ( aoMapTransform * vec3( AOMAP_UV, 1 ) ).xy; + +#endif +#ifdef USE_BUMPMAP + + vBumpMapUv = ( bumpMapTransform * vec3( BUMPMAP_UV, 1 ) ).xy; + +#endif +#ifdef USE_NORMALMAP + + vNormalMapUv = ( normalMapTransform * vec3( NORMALMAP_UV, 1 ) ).xy; + +#endif +#ifdef USE_DISPLACEMENTMAP + + vDisplacementMapUv = ( displacementMapTransform * vec3( DISPLACEMENTMAP_UV, 1 ) ).xy; + +#endif +#ifdef USE_EMISSIVEMAP + + vEmissiveMapUv = ( emissiveMapTransform * vec3( EMISSIVEMAP_UV, 1 ) ).xy; + +#endif +#ifdef USE_METALNESSMAP + + vMetalnessMapUv = ( metalnessMapTransform * vec3( METALNESSMAP_UV, 1 ) ).xy; + +#endif +#ifdef USE_ROUGHNESSMAP + + vRoughnessMapUv = ( roughnessMapTransform * vec3( ROUGHNESSMAP_UV, 1 ) ).xy; + +#endif +#ifdef USE_ANISOTROPYMAP + + vAnisotropyMapUv = ( anisotropyMapTransform * vec3( ANISOTROPYMAP_UV, 1 ) ).xy; + +#endif +#ifdef USE_CLEARCOATMAP + + vClearcoatMapUv = ( clearcoatMapTransform * vec3( CLEARCOATMAP_UV, 1 ) ).xy; + +#endif +#ifdef USE_CLEARCOAT_NORMALMAP + + vClearcoatNormalMapUv = ( clearcoatNormalMapTransform * vec3( CLEARCOAT_NORMALMAP_UV, 1 ) ).xy; + +#endif +#ifdef USE_CLEARCOAT_ROUGHNESSMAP + + vClearcoatRoughnessMapUv = ( clearcoatRoughnessMapTransform * vec3( CLEARCOAT_ROUGHNESSMAP_UV, 1 ) ).xy; + +#endif +#ifdef USE_IRIDESCENCEMAP + + vIridescenceMapUv = ( iridescenceMapTransform * vec3( IRIDESCENCEMAP_UV, 1 ) ).xy; + +#endif +#ifdef USE_IRIDESCENCE_THICKNESSMAP + + vIridescenceThicknessMapUv = ( iridescenceThicknessMapTransform * vec3( IRIDESCENCE_THICKNESSMAP_UV, 1 ) ).xy; + +#endif +#ifdef USE_SHEEN_COLORMAP + + vSheenColorMapUv = ( sheenColorMapTransform * vec3( SHEEN_COLORMAP_UV, 1 ) ).xy; + +#endif +#ifdef USE_SHEEN_ROUGHNESSMAP + + vSheenRoughnessMapUv = ( sheenRoughnessMapTransform * vec3( SHEEN_ROUGHNESSMAP_UV, 1 ) ).xy; + +#endif +#ifdef USE_SPECULARMAP + + vSpecularMapUv = ( specularMapTransform * vec3( SPECULARMAP_UV, 1 ) ).xy; + +#endif +#ifdef USE_SPECULAR_COLORMAP + + vSpecularColorMapUv = ( specularColorMapTransform * vec3( SPECULAR_COLORMAP_UV, 1 ) ).xy; + +#endif +#ifdef USE_SPECULAR_INTENSITYMAP + + vSpecularIntensityMapUv = ( specularIntensityMapTransform * vec3( SPECULAR_INTENSITYMAP_UV, 1 ) ).xy; + +#endif +#ifdef USE_TRANSMISSIONMAP + + vTransmissionMapUv = ( transmissionMapTransform * vec3( TRANSMISSIONMAP_UV, 1 ) ).xy; + +#endif +#ifdef USE_THICKNESSMAP + + vThicknessMapUv = ( thicknessMapTransform * vec3( THICKNESSMAP_UV, 1 ) ).xy; + +#endif +`,r_=` +#if defined( USE_ENVMAP ) || defined( DISTANCE ) || defined ( USE_SHADOWMAP ) || defined ( USE_TRANSMISSION ) || NUM_SPOT_LIGHT_COORDS > 0 + + vec4 worldPosition = vec4( transformed, 1.0 ); + + #ifdef USE_BATCHING + + worldPosition = batchingMatrix * worldPosition; + + #endif + + #ifdef USE_INSTANCING + + worldPosition = instanceMatrix * worldPosition; + + #endif + + worldPosition = modelMatrix * worldPosition; + +#endif +`,o_=` +varying vec2 vUv; +uniform mat3 uvTransform; + +void main() { + + vUv = ( uvTransform * vec3( uv, 1 ) ).xy; + + gl_Position = vec4( position.xy, 1.0, 1.0 ); + +} +`,a_=` +uniform sampler2D t2D; +uniform float backgroundIntensity; + +varying vec2 vUv; + +void main() { + + vec4 texColor = texture2D( t2D, vUv ); + + #ifdef DECODE_VIDEO_TEXTURE + + // use inline sRGB decode until browsers properly support SRGB8_APLHA8 with video textures + + texColor = vec4( mix( pow( texColor.rgb * 0.9478672986 + vec3( 0.0521327014 ), vec3( 2.4 ) ), texColor.rgb * 0.0773993808, vec3( lessThanEqual( texColor.rgb, vec3( 0.04045 ) ) ) ), texColor.w ); + + #endif + + texColor.rgb *= backgroundIntensity; + + gl_FragColor = texColor; + + #include + #include + +} +`,l_=` +varying vec3 vWorldDirection; + +#include + +void main() { + + vWorldDirection = transformDirection( position, modelMatrix ); + + #include + #include + + gl_Position.z = gl_Position.w; // set z to camera.far + +} +`,c_=` + +#ifdef ENVMAP_TYPE_CUBE + + uniform samplerCube envMap; + +#elif defined( ENVMAP_TYPE_CUBE_UV ) + + uniform sampler2D envMap; + +#endif + +uniform float flipEnvMap; +uniform float backgroundBlurriness; +uniform float backgroundIntensity; +uniform mat3 backgroundRotation; + +varying vec3 vWorldDirection; + +#include + +void main() { + + #ifdef ENVMAP_TYPE_CUBE + + vec4 texColor = textureCube( envMap, backgroundRotation * vec3( flipEnvMap * vWorldDirection.x, vWorldDirection.yz ) ); + + #elif defined( ENVMAP_TYPE_CUBE_UV ) + + vec4 texColor = textureCubeUV( envMap, backgroundRotation * vWorldDirection, backgroundBlurriness ); + + #else + + vec4 texColor = vec4( 0.0, 0.0, 0.0, 1.0 ); + + #endif + + texColor.rgb *= backgroundIntensity; + + gl_FragColor = texColor; + + #include + #include + +} +`,h_=` +varying vec3 vWorldDirection; + +#include + +void main() { + + vWorldDirection = transformDirection( position, modelMatrix ); + + #include + #include + + gl_Position.z = gl_Position.w; // set z to camera.far + +} +`,u_=` +uniform samplerCube tCube; +uniform float tFlip; +uniform float opacity; + +varying vec3 vWorldDirection; + +void main() { + + vec4 texColor = textureCube( tCube, vec3( tFlip * vWorldDirection.x, vWorldDirection.yz ) ); + + gl_FragColor = texColor; + gl_FragColor.a *= opacity; + + #include + #include + +} +`,d_=` +#include +#include +#include +#include +#include +#include +#include +#include + +// This is used for computing an equivalent of gl_FragCoord.z that is as high precision as possible. +// Some platforms compute gl_FragCoord at a lower precision which makes the manually computed value better for +// depth-based postprocessing effects. Reproduced on iPad with A10 processor / iPadOS 13.3.1. +varying vec2 vHighPrecisionZW; + +void main() { + + #include + + #include + #include + + #include + + #ifdef USE_DISPLACEMENTMAP + + #include + #include + #include + + #endif + + #include + #include + #include + #include + #include + #include + #include + + vHighPrecisionZW = gl_Position.zw; + +} +`,f_=` +#if DEPTH_PACKING == 3200 + + uniform float opacity; + +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +varying vec2 vHighPrecisionZW; + +void main() { + + vec4 diffuseColor = vec4( 1.0 ); + #include + + #if DEPTH_PACKING == 3200 + + diffuseColor.a = opacity; + + #endif + + #include + #include + #include + #include + + #include + + // Higher precision equivalent of gl_FragCoord.z. This assumes depthRange has been left to its default values. + float fragCoordZ = 0.5 * vHighPrecisionZW[0] / vHighPrecisionZW[1] + 0.5; + + #if DEPTH_PACKING == 3200 + + gl_FragColor = vec4( vec3( 1.0 - fragCoordZ ), opacity ); + + #elif DEPTH_PACKING == 3201 + + gl_FragColor = packDepthToRGBA( fragCoordZ ); + + #endif + +} +`,p_=` +#define DISTANCE + +varying vec3 vWorldPosition; + +#include +#include +#include +#include +#include +#include +#include + +void main() { + + #include + + #include + #include + + #include + + #ifdef USE_DISPLACEMENTMAP + + #include + #include + #include + + #endif + + #include + #include + #include + #include + #include + #include + #include + + vWorldPosition = worldPosition.xyz; + +} +`,m_=` +#define DISTANCE + +uniform vec3 referencePosition; +uniform float nearDistance; +uniform float farDistance; +varying vec3 vWorldPosition; + +#include +#include +#include +#include +#include +#include +#include +#include + +void main () { + + vec4 diffuseColor = vec4( 1.0 ); + #include + + #include + #include + #include + #include + + float dist = length( vWorldPosition - referencePosition ); + dist = ( dist - nearDistance ) / ( farDistance - nearDistance ); + dist = saturate( dist ); // clamp to [ 0, 1 ] + + gl_FragColor = packDepthToRGBA( dist ); + +} +`,g_=` +varying vec3 vWorldDirection; + +#include + +void main() { + + vWorldDirection = transformDirection( position, modelMatrix ); + + #include + #include + +} +`,x_=` +uniform sampler2D tEquirect; + +varying vec3 vWorldDirection; + +#include + +void main() { + + vec3 direction = normalize( vWorldDirection ); + + vec2 sampleUV = equirectUv( direction ); + + gl_FragColor = texture2D( tEquirect, sampleUV ); + + #include + #include + +} +`,__=` +uniform float scale; +attribute float lineDistance; + +varying float vLineDistance; + +#include +#include +#include +#include +#include +#include +#include + +void main() { + + vLineDistance = scale * lineDistance; + + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + +} +`,y_=` +uniform vec3 diffuse; +uniform float opacity; + +uniform float dashSize; +uniform float totalSize; + +varying float vLineDistance; + +#include +#include +#include +#include +#include +#include +#include + +void main() { + + vec4 diffuseColor = vec4( diffuse, opacity ); + #include + + if ( mod( vLineDistance, totalSize ) > dashSize ) { + + discard; + + } + + vec3 outgoingLight = vec3( 0.0 ); + + #include + #include + #include + + outgoingLight = diffuseColor.rgb; // simple shader + + #include + #include + #include + #include + #include + +} +`,v_=` +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void main() { + + #include + #include + #include + #include + #include + + #if defined ( USE_ENVMAP ) || defined ( USE_SKINNING ) + + #include + #include + #include + #include + #include + + #endif + + #include + #include + #include + #include + #include + #include + + #include + #include + #include + +} +`,M_=` +uniform vec3 diffuse; +uniform float opacity; + +#ifndef FLAT_SHADED + + varying vec3 vNormal; + +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void main() { + + vec4 diffuseColor = vec4( diffuse, opacity ); + #include + + #include + #include + #include + #include + #include + #include + #include + + ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) ); + + // accumulation (baked indirect lighting only) + #ifdef USE_LIGHTMAP + + vec4 lightMapTexel = texture2D( lightMap, vLightMapUv ); + reflectedLight.indirectDiffuse += lightMapTexel.rgb * lightMapIntensity * RECIPROCAL_PI; + + #else + + reflectedLight.indirectDiffuse += vec3( 1.0 ); + + #endif + + // modulation + #include + + reflectedLight.indirectDiffuse *= diffuseColor.rgb; + + vec3 outgoingLight = reflectedLight.indirectDiffuse; + + #include + + #include + #include + #include + #include + #include + #include + +} +`,S_=` +#define LAMBERT + +varying vec3 vViewPosition; + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void main() { + + #include + #include + #include + #include + #include + + #include + #include + #include + #include + #include + #include + + #include + #include + #include + #include + #include + #include + #include + + vViewPosition = - mvPosition.xyz; + + #include + #include + #include + #include + +} +`,w_=` +#define LAMBERT + +uniform vec3 diffuse; +uniform vec3 emissive; +uniform float opacity; + +#include