Shader error fog height

So i have this shader code thst works in other projects but for some reason won’t work anymore ever since i added some terrain and keeps throwing an error what is causing this?
i have next to 0 experience with shaders so forgive my ignorance

shader:


 


THREE.ShaderChunk.fog_pars_vertex += `
#ifdef USE_FOG
  varying vec3 WorldPosition;
#endif
`;

THREE.ShaderChunk.fog_vertex += `
#ifdef USE_FOG
  WorldPosition = worldPosition.xyz;
#endif
`;

// fragment shader
THREE.ShaderChunk.fog_pars_fragment += `
#ifdef USE_FOG
  varying vec3 WorldPosition;

  float fogHeight = 60.0;
#endif
`;

const FOG_APPLIED_LINE = 'gl_FragColor.rgb = mix( gl_FragColor.rgb, fogColor, fogFactor );';
THREE.ShaderChunk.fog_fragment = THREE.ShaderChunk.fog_fragment.replace(FOG_APPLIED_LINE, `
  float fogFactor2 = smoothstep( fogHeight, 0.0, vWorldPosition.y );
  float fogFactor3 = smoothstep( fogHeight, 0.0, cameraPosition.y );

  fogFactor = fogFactor * max(fogFactor2, fogFactor3);

  ${FOG_APPLIED_LINE}
`);

THREE.ShaderLib.sprite.vertexShader = THREE.ShaderLib.sprite.vertexShader.replace('#include <fog_vertex>', `
  vec4 worldPosition = mvPosition;
  #include <fog_vertex>
`);

THREE.WebGLProgram: shader error: 0 35715 false gl.getProgramInfoLog invalid shaders THREE.WebGLShader: gl.getShaderInfoLog() vertex
ERROR: 0:431: ‘worldPosition’ : undeclared identifier
ERROR: 0:431: ‘xyz’ : field selection requires structure, vector, or interface block on left hand side
ERROR: 0:431: ‘=’ : dimension mismatch
ERROR: 0:431: ‘assign’ : cannot convert from ‘const highp float’ to ‘out highp 3-component vector of float’

and also:

THREE.WebGLProgram: shader error: 1282 35715 false gl.getProgramInfoLog invalid shaders THREE.WebGLShader: gl.getShaderInfoLog() vertex
ERROR: 0:430: ‘worldPosition’ : undeclared identifier
ERROR: 0:430: ‘xyz’ : field selection requires structure, vector, or interface block on left hand side
ERROR: 0:430: ‘=’ : dimension mismatch
ERROR: 0:430: ‘assign’ : cannot convert from ‘const highp float’ to ‘out highp 3-component vector of float’

any ideas? it works in other projects
r127

I can only say that the worldPosition variable only exists in the vertex shader when certain conditions are true.

Instead of relying on worldPosition, try it with:

    vec4 wp = modelMatrix * vec4( transformed, 1.0 );
    WorldPosition = wp.xyz;
1 Like

Apparently the setting true on both cast shadow and receive shadow creates this error…?

Your solution seems to work on laptop with ni error but on chrome iso i get this:

THREE.WebGLProgram: shader error: 0 35715 false gl.getProgramInfoLog invalid shaders THREE.WebGLShader: gl.getShaderInfoLog() fragment
0:434: S0032: no default precision defined for variable 'float[4]'1: #version 300 es
2: #define varying in
3: out highp vec4 pc_fragColor;
4: #define gl_FragColor pc_fragColor
5: #define gl_FragDepthEXT gl_FragDepth
6: #define texture2D texture
7: #define textureCube texture
8: #define texture2DProj textureProj
9: #define texture2DLodEXT textureLod
10: #define texture2DProjLodEXT textureProjLod
11: #define textureCubeLodEXT textureLod
12: #define texture2DGradEXT textureGrad
13: #define texture2DProjGradEXT textureProjGrad
14: #define textureCubeGradEXT textureGrad
15: precision highp float;
16: precision highp int;
17: #define HIGH_PRECISION
18: #define SHADER_NAME MeshStandardMaterial
19: #define STANDARD
20: #define GAMMA_FACTOR 2
21: #define USE_FOG
22: #define FOG_EXP2
23: #define USE_ENVMAP
24: #define ENVMAP_TYPE_CUBE
25: #define ENVMAP_MODE_REFLECTION
26: #define ENVMAP_BLENDING_NONE
27: #define USE_COLOR
28: #define FLIP_SIDED
29: #define USE_SHADOWMAP
30: #define SHADOWMAP_TYPE_PCF_SOFT
31: #define TEXTURE_LOD_EXT
32: uniform mat4 viewMatrix;
33: uniform vec3 cameraPosition;
34: uniform bool isOrthographic;
35: #define TONE_MAPPING
36: #ifndef saturate
37: #define saturate(a) clamp( a, 0.0, 1.0 )
38: #endif
39: uniform float toneMappingExposure;
40: vec3 LinearToneMapping( vec3 color ) {
41: return toneMappingExposure * color;
42: }
43: vec3 ReinhardToneMapping( vec3 color ) {
44: color = toneMappingExposure;
45: return saturate( color / ( vec3( 1.0 ) + color ) );
46: }
47: vec3 OptimizedCineonToneMapping( vec3 color ) {
48: color = toneMappingExposure;
49: color = max( vec3( 0.0 ), color - 0.004 );
50: return pow( ( color * ( 6.2 * color + 0.5 ) ) / ( color * ( 6.2 * color + 1.7 ) + 0.06 ), vec3( 2.2 ) );
51: }
52: vec3 RRTAndODTFit( vec3 v ) {
53: vec3 a = v * ( v + 0.0245786 ) - 0.000090537;
54: vec3 b = v * ( 0.983729 * v + 0.4329510 ) + 0.238081;
55: return a / b;
56: }
57: vec3 ACESFilmicToneMapping( vec3 color ) {
58: const mat3 ACESInputMat = mat3(
59: vec3( 0.59719, 0.07600, 0.02840 ), vec3( 0.35458, 0.90834, 0.13383 ),
60: vec3( 0.04823, 0.01566, 0.83777 )
61: );
62: const mat3 ACESOutputMat = mat3(
63: vec3( 1.60475, -0.10208, -0.00327 ), vec3( -0.53108, 1.10813, -0.07276 ),
64: vec3( -0.07367, -0.00605, 1.07602 )
65: );
66: color = toneMappingExposure / 0.6;
67: color = ACESInputMat * color;
68: color = RRTAndODTFit( color );
69: color = ACESOutputMat * color;
70: return saturate( color );
71: }
72: vec3 CustomToneMapping( vec3 color ) { return color; }
73: vec3 toneMapping( vec3 color ) { return LinearToneMapping( color ); }
74:
75: vec4 LinearToLinear( in vec4 value ) {
76: return value;
77: }
78: vec4 GammaToLinear( in vec4 value, in float gammaFactor ) {
79: return vec4( pow( value.rgb, vec3( gammaFactor ) ), value.a );
80: }
81: vec4 LinearToGamma( in vec4 value, in float gammaFactor ) {
82: return vec4( pow( value.rgb, vec3( 1.0 / gammaFactor ) ), value.a );
83: }
84: vec4 sRGBToLinear( in vec4 value ) {
85: return vec4( mix( pow( value.rgb * 0.9478672986 + vec3( 0.0521327014 ), vec3( 2.4 ) ), value.rgb * 0.0773993808, vec3( lessThanEqual( value.rgb, vec3( 0.04045 ) ) ) ), value.a );
86: }
87: vec4 LinearTosRGB( in vec4 value ) {
88: 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 );
89: }
90: vec4 RGBEToLinear( in vec4 value ) {
91: return vec4( value.rgb * exp2( value.a * 255.0 - 128.0 ), 1.0 );
92: }
93: vec4 LinearToRGBE( in vec4 value ) {
94: float maxComponent = max( max( value.r, value.g ), value.b );
95: float fExp = clamp( ceil( log2( maxComponent ) ), -128.0, 127.0 );
96: return vec4( value.rgb / exp2( fExp ), ( fExp + 128.0 ) / 255.0 );
97: }
98: vec4 RGBMToLinear( in vec4 value, in float maxRange ) {
99: return vec4( value.rgb * value.a * maxRange, 1.0 );
100: }
101: vec4 LinearToRGBM( in vec4 value, in float maxRange ) {
102: float maxRGB = max( value.r, max( value.g, value.b ) );
103: float M = clamp( maxRGB / maxRange, 0.0, 1.0 );
104: M = ceil( M * 255.0 ) / 255.0;
105: return vec4( value.rgb / ( M * maxRange ), M );
106: }
107: vec4 RGBDToLinear( in vec4 value, in float maxRange ) {
108: return vec4( value.rgb * ( ( maxRange / 255.0 ) / value.a ), 1.0 );
109: }
110: vec4 LinearToRGBD( in vec4 value, in float maxRange ) {
111: float maxRGB = max( value.r, max( value.g, value.b ) );
112: float D = max( maxRange / maxRGB, 1.0 );
113: D = clamp( floor( D ) / 255.0, 0.0, 1.0 );
114: return vec4( value.rgb * ( D * ( 255.0 / maxRange ) ), D );
115: }
116: const mat3 cLogLuvM = mat3( 0.2209, 0.3390, 0.4184, 0.1138, 0.6780, 0.7319, 0.0102, 0.1130, 0.2969 );
117: vec4 LinearToLogLuv( in vec4 value ) {
118: vec3 Xp_Y_XYZp = cLogLuvM * value.rgb;
119: Xp_Y_XYZp = max( Xp_Y_XYZp, vec3( 1e-6, 1e-6, 1e-6 ) );
120: vec4 vResult;
121: vResult.xy = Xp_Y_XYZp.xy / Xp_Y_XYZp.z;
122: float Le = 2.0 * log2(Xp_Y_XYZp.y) + 127.0;
123: vResult.w = fract( Le );
124: vResult.z = ( Le - ( floor( vResult.w * 255.0 ) ) / 255.0 ) / 255.0;
125: return vResult;
126: }
127: const mat3 cLogLuvInverseM = mat3( 6.0014, -2.7008, -1.7996, -1.3320, 3.1029, -5.7721, 0.3008, -1.0882, 5.6268 );
128: vec4 LogLuvToLinear( in vec4 value ) {
129: float Le = value.z * 255.0 + value.w;
130: vec3 Xp_Y_XYZp;
131: Xp_Y_XYZp.y = exp2( ( Le - 127.0 ) / 2.0 );
132: Xp_Y_XYZp.z = Xp_Y_XYZp.y / value.y;
133: Xp_Y_XYZp.x = value.x * Xp_Y_XYZp.z;
134: vec3 vRGB = cLogLuvInverseM * Xp_Y_XYZp.rgb;
135: return vec4( max( vRGB, 0.0 ), 1.0 );
136: }
137: vec4 envMapTexelToLinear( vec4 value ) { return RGBEToLinear( value ); }
138: vec4 linearToOutputTexel( vec4 value ) { return LinearTosRGB( value ); }
139:
140:
141:
142: precision mediump sampler2DArray;
143:
144: uniform sampler2DArray TRIPLANAR_normalMap;
145: uniform sampler2DArray TRIPLANAR_diffuseMap;
146: uniform sampler2D TRIPLANAR_noiseMap;
147:
148: in vec3 vCoords;
149: in vec4 vWeights1;
150: in vec4 vWeights2;
151:
152:
153: const float _TRI_SCALE = 10.0;
154:
155: float sum( vec3 v ) { return v.x+v.y+v.z; }
156:
157: vec4 hash4( vec2 p ) {
158: return fract(
159: sin(vec4(1.0+dot(p,vec2(37.0,17.0)),
160: 2.0+dot(p,vec2(11.0,47.0)),
161: 3.0+dot(p,vec2(41.0,29.0)),
162: 4.0+dot(p,vec2(23.0,31.0))))103.0);
163: }
164:
165: vec4 _TerrainBlend_4(vec4 samples[4]) {
166: float depth = 0.2;
167: float ma = max(
168: samples[0].w,
169: max(
170: samples[1].w,
171: max(samples[2].w, samples[3].w))) - depth;
172:
173: float b1 = max(samples[0].w - ma, 0.0);
174: float b2 = max(samples[1].w - ma, 0.0);
175: float b3 = max(samples[2].w - ma, 0.0);
176: float b4 = max(samples[3].w - ma, 0.0);
177:
178: vec4 numer = (
179: samples[0] * b1 + samples[1] * b2 +
180: samples[2] * b3 + samples[3] * b4);
181: float denom = (b1 + b2 + b3 + b4);
182: return numer / denom;
183: }
184:
185: vec4 _TerrainBlend_4_lerp(vec4 samples[4]) {
186: return (
187: samples[0] * samples[0].w + samples[1] * samples[1].w +
188: samples[2] * samples[2].w + samples[3] * samples[3].w);
189: }
190:
191: // Lifted from Shader - Shadertoy BETA
192: vec4 texture_UV(in sampler2DArray srcTexture, in vec3 x) {
193: float k = texture(TRIPLANAR_noiseMap, 0.0025
x.xy).x; // cheap (cache friendly) lookup
194: float l = k
8.0;
195: float f = fract(l);
196:
197: float ia = floor(l+0.5); // suslik’s method (see comments)
198: float ib = floor(l);
199: f = min(f, 1.0-f)2.0;
200:
201: vec2 offa = sin(vec2(3.0,7.0)ia); // can replace with any other hash
202: vec2 offb = sin(vec2(3.0,7.0)ib); // can replace with any other hash
203:
204: vec4 cola = texture(srcTexture, vec3(x.xy + offa, x.z));
205: vec4 colb = texture(srcTexture, vec3(x.xy + offb, x.z));
206:
207: return mix(cola, colb, smoothstep(0.2,0.8,f-0.1
sum(cola.xyz-colb.xyz)));
208: }
209:
210: vec4 _Triplanar_UV(vec3 pos, vec3 normal, float texSlice, sampler2DArray tex) {
211: vec4 dx = texture_UV(tex, vec3(pos.zy / _TRI_SCALE, texSlice));
212: vec4 dy = texture_UV(tex, vec3(pos.xz / _TRI_SCALE, texSlice));
213: vec4 dz = texture_UV(tex, vec3(pos.xy / _TRI_SCALE, texSlice));
214:
215: vec3 weights = abs(normal.xyz);
216: weights = weights / (weights.x + weights.y + weights.z);
217:
218: return dx * weights.x + dy * weights.y + dz * weights.z;
219: }
220:
221: vec4 _TriplanarN_UV(vec3 pos, vec3 normal, float texSlice, sampler2DArray tex) {
222: // Tangent Reconstruction
223: // Triplanar uvs
224: vec2 uvX = pos.zy; // x facing plane
225: vec2 uvY = pos.xz; // y facing plane
226: vec2 uvZ = pos.xy; // z facing plane
227: // Tangent space normal maps
228: vec3 tx = texture_UV(tex, vec3(uvX / _TRI_SCALE, texSlice)).xyz * vec3(2,2,2) - vec3(1,1,1);
229: vec3 ty = texture_UV(tex, vec3(uvY / _TRI_SCALE, texSlice)).xyz * vec3(2,2,2) - vec3(1,1,1);
230: vec3 tz = texture_UV(tex, vec3(uvZ / _TRI_SCALE, texSlice)).xyz * vec3(2,2,2) - vec3(1,1,1);
231:
232: vec3 weights = abs(normal.xyz);
233: weights = weights / (weights.x + weights.y + weights.z);
234:
235: // Get the sign (-1 or 1) of the surface normal
236: vec3 axis = sign(normal);
237: // Construct tangent to world matrices for each axis
238: vec3 tangentX = normalize(cross(normal, vec3(0.0, axis.x, 0.0)));
239: vec3 bitangentX = normalize(cross(tangentX, normal)) * axis.x;
240: mat3 tbnX = mat3(tangentX, bitangentX, normal);
241:
242: vec3 tangentY = normalize(cross(normal, vec3(0.0, 0.0, axis.y)));
243: vec3 bitangentY = normalize(cross(tangentY, normal)) * axis.y;
244: mat3 tbnY = mat3(tangentY, bitangentY, normal);
245:
246: vec3 tangentZ = normalize(cross(normal, vec3(0.0, -axis.z, 0.0)));
247: vec3 bitangentZ = normalize(-cross(tangentZ, normal)) * axis.z;
248: mat3 tbnZ = mat3(tangentZ, bitangentZ, normal);
249:
250: // Apply tangent to world matrix and triblend
251: // Using clamp() because the cross products may be NANs
252: vec3 worldNormal = normalize(
253: clamp(tbnX * tx, -1.0, 1.0) * weights.x +
254: clamp(tbnY * ty, -1.0, 1.0) * weights.y +
255: clamp(tbnZ * tz, -1.0, 1.0) * weights.z
256: );
257: return vec4(worldNormal, 0.0);
258: }
259:
260: vec4 _Triplanar(vec3 pos, vec3 normal, float texSlice, sampler2DArray tex) {
261: vec4 dx = texture(tex, vec3(pos.zy / _TRI_SCALE, texSlice));
262: vec4 dy = texture(tex, vec3(pos.xz / _TRI_SCALE, texSlice));
263: vec4 dz = texture(tex, vec3(pos.xy / _TRI_SCALE, texSlice));
264:
265: vec3 weights = abs(normal.xyz);
266: weights = weights / (weights.x + weights.y + weights.z);
267:
268: return dx * weights.x + dy * weights.y + dz * weights.z;
269: }
270:
271: vec4 _TriplanarN(vec3 pos, vec3 normal, float texSlice, sampler2DArray tex) {
272: vec2 uvx = pos.zy;
273: vec2 uvy = pos.xz;
274: vec2 uvz = pos.xy;
275: vec3 tx = texture(tex, vec3(uvx / _TRI_SCALE, texSlice)).xyz * vec3(2,2,2) - vec3(1,1,1);
276: vec3 ty = texture(tex, vec3(uvy / _TRI_SCALE, texSlice)).xyz * vec3(2,2,2) - vec3(1,1,1);
277: vec3 tz = texture(tex, vec3(uvz / _TRI_SCALE, texSlice)).xyz * vec3(2,2,2) - vec3(1,1,1);
278:
279: vec3 weights = abs(normal.xyz);
280: weights = weights;
281: weights = weights / (weights.x + weights.y + weights.z);
282:
283: vec3 axis = sign(normal);
284: vec3 tangentX = normalize(cross(normal, vec3(0.0, axis.x, 0.0)));
285: vec3 bitangentX = normalize(cross(tangentX, normal)) * axis.x;
286: mat3 tbnX = mat3(tangentX, bitangentX, normal);
287:
288: vec3 tangentY = normalize(cross(normal, vec3(0.0, 0.0, axis.y)));
289: vec3 bitangentY = normalize(cross(tangentY, normal)) * axis.y;
290: mat3 tbnY = mat3(tangentY, bitangentY, normal);
291:
292: vec3 tangentZ = normalize(cross(normal, vec3(0.0, -axis.z, 0.0)));
293: vec3 bitangentZ = normalize(-cross(tangentZ, normal)) * axis.z;
294: mat3 tbnZ = mat3(tangentZ, bitangentZ, normal);
295:
296: vec3 worldNormal = normalize(
297: clamp(tbnX * tx, -1.0, 1.0) * weights.x +
298: clamp(tbnY * ty, -1.0, 1.0) * weights.y +
299: clamp(tbnZ * tz, -1.0, 1.0) * weights.z);
300: return vec4(worldNormal, 0.0);
301: }
302:
303: #define STANDARD
304: #ifdef PHYSICAL
305: #define REFLECTIVITY
306: #define CLEARCOAT
307: #define TRANSMISSION
308: #endif
309: uniform vec3 diffuse;
310: uniform vec3 emissive;
311: uniform float roughness;
312: uniform float metalness;
313: uniform float opacity;
314: #ifdef TRANSMISSION
315: uniform float transmission;
316: #endif
317: #ifdef REFLECTIVITY
318: uniform float reflectivity;
319: #endif
320: #ifdef CLEARCOAT
321: uniform float clearcoat;
322: uniform float clearcoatRoughness;
323: #endif
324: #ifdef USE_SHEEN
325: uniform vec3 sheen;
326: #endif
327: varying vec3 vViewPosition;
328: #ifndef FLAT_SHADED
329: varying vec3 vNormal;
330: #ifdef USE_TANGENT
331: varying vec3 vTangent;
332: varying vec3 vBitangent;
333: #endif
334: #endif
335: #define PI 3.141592653589793
336: #define PI2 6.283185307179586
337: #define PI_HALF 1.5707963267948966
338: #define RECIPROCAL_PI 0.3183098861837907
339: #define RECIPROCAL_PI2 0.15915494309189535
340: #define EPSILON 1e-6
341: #ifndef saturate
342: #define saturate(a) clamp( a, 0.0, 1.0 )
343: #endif
344: #define whiteComplement(a) ( 1.0 - saturate( a ) )
345: float pow2( const in float x ) { return x
x; }
346: float pow3( const in float x ) { return x
x
x; }
347: float pow4( const in float x ) { float x2 = x
x; return x2
x2; }
348: float average( const in vec3 color ) { return dot( color, vec3( 0.3333 ) ); }
349: highp float rand( const in vec2 uv ) {
350: const highp float a = 12.9898, b = 78.233, c = 43758.5453;
351: highp float dt = dot( uv.xy, vec2( a,b ) ), sn = mod( dt, PI );
352: return fract(sin(sn) * c);
353: }
354: #ifdef HIGH_PRECISION
355: float precisionSafeLength( vec3 v ) { return length( v ); }
356: #else
357: float max3( vec3 v ) { return max( max( v.x, v.y ), v.z ); }
358: float precisionSafeLength( vec3 v ) {
359: float maxComponent = max3( abs( v ) );
360: return length( v / maxComponent ) * maxComponent;
361: }
362: #endif
363: struct IncidentLight {
364: vec3 color;
365: vec3 direction;
366: bool visible;
367: };
368: struct ReflectedLight {
369: vec3 directDiffuse;
370: vec3 directSpecular;
371: vec3 indirectDiffuse;
372: vec3 indirectSpecular;
373: };
374: struct GeometricContext {
375: vec3 position;
376: vec3 normal;
377: vec3 viewDir;
378: #ifdef CLEARCOAT
379: vec3 clearcoatNormal;
380: #endif
381: };
382: vec3 transformDirection( in vec3 dir, in mat4 matrix ) {
383: return normalize( ( matrix * vec4( dir, 0.0 ) ).xyz );
384: }
385: vec3 inverseTransformDirection( in vec3 dir, in mat4 matrix ) {
386: return normalize( ( vec4( dir, 0.0 ) * matrix ).xyz );
387: }
388: vec3 projectOnPlane(in vec3 point, in vec3 pointOnPlane, in vec3 planeNormal ) {
389: float distance = dot( planeNormal, point - pointOnPlane );
390: return - distance * planeNormal + point;
391: }
392: float sideOfPlane( in vec3 point, in vec3 pointOnPlane, in vec3 planeNormal ) {
393: return sign( dot( point - pointOnPlane, planeNormal ) );
394: }
395: vec3 linePlaneIntersect( in vec3 pointOnLine, in vec3 lineDirection, in vec3 pointOnPlane, in vec3 planeNormal ) {
396: return lineDirection * ( dot( planeNormal, pointOnPlane - pointOnLine ) / dot( planeNormal, lineDirection ) ) + pointOnLine;
397: }
398: mat3 transposeMat3( const in mat3 m ) {
399: mat3 tmp;
400: tmp[ 0 ] = vec3( m[ 0 ].x, m[ 1 ].x, m[ 2 ].x );
401: tmp[ 1 ] = vec3( m[ 0 ].y, m[ 1 ].y, m[ 2 ].y );
402: tmp[ 2 ] = vec3( m[ 0 ].z, m[ 1 ].z, m[ 2 ].z );
403: return tmp;
404: }
405: float linearToRelativeLuminance( const in vec3 color ) {
406: vec3 weights = vec3( 0.2126, 0.7152, 0.0722 );
407: return dot( weights, color.rgb );
408: }
409: bool isPerspectiveMatrix( mat4 m ) {
410: return m[ 2 ][ 3 ] == - 1.0;
411: }
412: vec2 equirectUv( in vec3 dir ) {
413: float u = atan( dir.z, dir.x ) * RECIPROCAL_PI2 + 0.5;
414: float v = asin( clamp( dir.y, - 1.0, 1.0 ) ) * RECIPROCAL_PI + 0.5;
415: return vec2( u, v );
416: }
417: vec3 packNormalToRGB( const in vec3 normal ) {
418: return normalize( normal ) * 0.5 + 0.5;
419: }
420: vec3 unpackRGBToNormal( const in vec3 rgb ) {
421: return 2.0 * rgb.xyz - 1.0;
422: }
423: const float PackUpscale = 256. / 255.;const float UnpackDownscale = 255. / 256.;
424: const vec3 PackFactors = vec3( 256. * 256. * 256., 256. * 256., 256. );
425: const vec4 UnpackFactors = UnpackDownscale / vec4( PackFactors, 1. );
426: const float ShiftRight8 = 1. / 256.;
427: vec4 packDepthToRGBA( const in float v ) {
428: vec4 r = vec4( fract( v * PackFactors ), v );
429: r.yzw -= r.xyz * ShiftRight8; return r * PackUpscale;
430: }
431: float unpackRGBAToDepth( const in vec4 v ) {
432: return dot( v, UnpackFactors );
433: }
434: vec4 pack2HalfToRGBA( vec2 v ) {
435: vec4 r = vec4( v.x, fract( v.x * 255.0 ), v.y, fract( v.y * 255.0 ));
436: return vec4( r.x - r.y / 255.0, r.y, r.z - r.w / 255.0, r.w);
437: }
438: vec2 unpackRGBATo2Half( vec4 v ) {
439: return vec2( v.x + ( v.y / 255.0 ), v.z + ( v.w / 255.0 ) );
440: }
441: float viewZToOrthographicDepth( const in float viewZ, const in float near, const in float far ) {
442: return ( viewZ + near ) / ( near - far );
443: }
444: float orthographicDepthToViewZ( const in float linearClipZ, const in float near, const in float far ) {
445: return linearClipZ * ( near - far ) - near;
446: }
447: float viewZToPerspectiveDepth( const in float viewZ, const in float near, const in float far ) {
448: return (( near + viewZ ) * far ) / (( far - near ) * viewZ );
449: }
450: float perspectiveDepthToViewZ( const in float invClipZ, const in float near, const in float far ) {
451: return ( near * far ) / ( ( far - near ) * invClipZ - far );
452: }
453: #ifdef DITHERING
454: vec3 dithering( vec3 color ) {
455: float grid_position = rand( gl_FragCoord.xy );
456: vec3 dither_shift_RGB = vec3( 0.25 / 255.0, -0.25 / 255.0, 0.25 / 255.0 );
457: dither_shift_RGB = mix( 2.0 * dither_shift_RGB, -2.0 * dither_shift_RGB, grid_position );
458: return color + dither_shift_RGB;
459: }
460: #endif
461: #if defined( USE_COLOR_ALPHA )
462: varying vec4 vColor;
463: #elif defined( USE_COLOR )
464: varying vec3 vColor;
465: #endif
466: #if ( defined( USE_UV ) && ! defined( UVS_VERTEX_ONLY ) )
467: varying vec2 vUv;
468: #endif
469: #if defined( USE_LIGHTMAP ) || defined( USE_AOMAP )
470: varying vec2 vUv2;
471: #endif
472: #ifdef USE_MAP
473: uniform sampler2D map;
474: #endif
475: #ifdef USE_ALPHAMAP
476: uniform sampler2D alphaMap;
477: #endif
478: #ifdef USE_AOMAP
479: uniform sampler2D aoMap;
480: uniform float aoMapIntensity;
481: #endif
482: #ifdef USE_LIGHTMAP
483: uniform sampler2D lightMap;
484: uniform float lightMapIntensity;
485: #endif
486: #ifdef USE_EMISSIVEMAP
487: uniform sampler2D emissiveMap;
488: #endif
489: #ifdef USE_TRANSMISSIONMAP
490: uniform sampler2D transmissionMap;
491: #endif
492: vec2 integrateSpecularBRDF( const in float dotNV, const in float roughness ) {
493: const vec4 c0 = vec4( - 1, - 0.0275, - 0.572, 0.022 );
494: const vec4 c1 = vec4( 1, 0.0425, 1.04, - 0.04 );
495: vec4 r = roughness * c0 + c1;
496: float a004 = min( r.x * r.x, exp2( - 9.28 * dotNV ) ) * r.x + r.y;
497: return vec2( -1.04, 1.04 ) * a004 + r.zw;
498: }
499: float punctualLightIntensityToIrradianceFactor( const in float lightDistance, const in float cutoffDistance, const in float decayExponent ) {
500: #if defined ( PHYSICALLY_CORRECT_LIGHTS )
501: float distanceFalloff = 1.0 / max( pow( lightDistance, decayExponent ), 0.01 );
502: if( cutoffDistance > 0.0 ) {
503: distanceFalloff *= pow2( saturate( 1.0 - pow4( lightDistance / cutoffDistance ) ) );
504: }
505: return distanceFalloff;
506: #else
507: if( cutoffDistance > 0.0 && decayExponent > 0.0 ) {
508: return pow( saturate( -lightDistance / cutoffDistance + 1.0 ), decayExponent );
509: }
510: return 1.0;
511: #endif
512: }
513: vec3 BRDF_Diffuse_Lambert( const in vec3 diffuseColor ) {
514: return RECIPROCAL_PI * diffuseColor;
515: }
516: vec3 F_Schlick( const in vec3 specularColor, const in float dotLH ) {
517: float fresnel = exp2( ( -5.55473 * dotLH - 6.98316 ) * dotLH );
518: return ( 1.0 - specularColor ) * fresnel + specularColor;
519: }
520: vec3 F_Schlick_RoughnessDependent( const in vec3 F0, const in float dotNV, const in float roughness ) {
521: float fresnel = exp2( ( -5.55473 * dotNV - 6.98316 ) * dotNV );
522: vec3 Fr = max( vec3( 1.0 - roughness ), F0 ) - F0;
523: return Fr * fresnel + F0;
524: }
525: float G_GGX_Smith( const in float alpha, const in float dotNL, const in float dotNV ) {
526: float a2 = pow2( alpha );
527: float gl = dotNL + sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNL ) );
528: float gv = dotNV + sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNV ) );
529: return 1.0 / ( gl * gv );
530: }
531: float G_GGX_SmithCorrelated( const in float alpha, const in float dotNL, const in float dotNV ) {
532: float a2 = pow2( alpha );
533: float gv = dotNL * sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNV ) );
534: float gl = dotNV * sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNL ) );
535: return 0.5 / max( gv + gl, EPSILON );
536: }
537: float D_GGX( const in float alpha, const in float dotNH ) {
538: float a2 = pow2( alpha );
539: float denom = pow2( dotNH ) * ( a2 - 1.0 ) + 1.0;
540: return RECIPROCAL_PI * a2 / pow2( denom );
541: }
542: vec3 BRDF_Specular_GGX( const in IncidentLight incidentLight, const in vec3 viewDir, const in vec3 normal, const in vec3 specularColor, const in float roughness ) {
543: float alpha = pow2( roughness );
544: vec3 halfDir = normalize( incidentLight.direction + viewDir );
545: float dotNL = saturate( dot( normal, incidentLight.direction ) );
546: float dotNV = saturate( dot( normal, viewDir ) );
547: float dotNH = saturate( dot( normal, halfDir ) );
548: float dotLH = saturate( dot( incidentLight.direction, halfDir ) );
549: vec3 F = F_Schlick( specularColor, dotLH );
550: float G = G_GGX_SmithCorrelated( alpha, dotNL, dotNV );
551: float D = D_GGX( alpha, dotNH );
552: return F * ( G * D );
553: }
554: vec2 LTC_Uv( const in vec3 N, const in vec3 V, const in float roughness ) {
555: const float LUT_SIZE = 64.0;
556: const float LUT_SCALE = ( LUT_SIZE - 1.0 ) / LUT_SIZE;
557: const float LUT_BIAS = 0.5 / LUT_SIZE;
558: float dotNV = saturate( dot( N, V ) );
559: vec2 uv = vec2( roughness, sqrt( 1.0 - dotNV ) );
560: uv = uv * LUT_SCALE + LUT_BIAS;
561: return uv;
562: }
563: float LTC_ClippedSphereFormFactor( const in vec3 f ) {
564: float l = length( f );
565: return max( ( l * l + f.z ) / ( l + 1.0 ), 0.0 );
566: }
567: vec3 LTC_EdgeVectorFormFactor( const in vec3 v1, const in vec3 v2 ) {
568: float x = dot( v1, v2 );
569: float y = abs( x );
570: float a = 0.8543985 + ( 0.4965155 + 0.0145206 * y ) * y;
571: float b = 3.4175940 + ( 4.1616724 + y ) * y;
572: float v = a / b;
573: float theta_sintheta = ( x > 0.0 ) ? v : 0.5 * inversesqrt( max( 1.0 - x * x, 1e-7 ) ) - v;
574: return cross( v1, v2 ) * theta_sintheta;
575: }
576: vec3 LTC_Evaluate( const in vec3 N, const in vec3 V, const in vec3 P, const in mat3 mInv, const in vec3 rectCoords[ 4 ] ) {
577: vec3 v1 = rectCoords[ 1 ] - rectCoords[ 0 ];
578: vec3 v2 = rectCoords[ 3 ] - rectCoords[ 0 ];
579: vec3 lightNormal = cross( v1, v2 );
580: if( dot( lightNormal, P - rectCoords[ 0 ] ) < 0.0 ) return vec3( 0.0 );
581: vec3 T1, T2;
582: T1 = normalize( V - N * dot( V, N ) );
583: T2 = - cross( N, T1 );
584: mat3 mat = mInv * transposeMat3( mat3( T1, T2, N ) );
585: vec3 coords[ 4 ];
586: coords[ 0 ] = mat * ( rectCoords[ 0 ] - P );
587: coords[ 1 ] = mat * ( rectCoords[ 1 ] - P );
588: coords[ 2 ] = mat * ( rectCoords[ 2 ] - P );
589: coords[ 3 ] = mat * ( rectCoords[ 3 ] - P );
590: coords[ 0 ] = normalize( coords[ 0 ] );
591: coords[ 1 ] = normalize( coords[ 1 ] );
592: coords[ 2 ] = normalize( coords[ 2 ] );
593: coords[ 3 ] = normalize( coords[ 3 ] );
594: vec3 vectorFormFactor = vec3( 0.0 );
595: vectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 0 ], coords[ 1 ] );
596: vectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 1 ], coords[ 2 ] );
597: vectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 2 ], coords[ 3 ] );
598: vectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 3 ], coords[ 0 ] );
599: float result = LTC_ClippedSphereFormFactor( vectorFormFactor );
600: return vec3( result );
601: }
602: vec3 BRDF_Specular_GGX_Environment( const in vec3 viewDir, const in vec3 normal, const in vec3 specularColor, const in float roughness ) {
603: float dotNV = saturate( dot( normal, viewDir ) );
604: vec2 brdf = integrateSpecularBRDF( dotNV, roughness );
605: return specularColor * brdf.x + brdf.y;
606: }
607: void BRDF_Specular_Multiscattering_Environment( const in GeometricContext geometry, const in vec3 specularColor, const in float roughness, inout vec3 singleScatter, inout vec3 multiScatter ) {
608: float dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );
609: vec3 F = F_Schlick_RoughnessDependent( specularColor, dotNV, roughness );
610: vec2 brdf = integrateSpecularBRDF( dotNV, roughness );
611: vec3 FssEss = F * brdf.x + brdf.y;
612: float Ess = brdf.x + brdf.y;
613: float Ems = 1.0 - Ess;
614: vec3 Favg = specularColor + ( 1.0 - specularColor ) * 0.047619; vec3 Fms = FssEss * Favg / ( 1.0 - Ems * Favg );
615: singleScatter += FssEss;
616: multiScatter += Fms * Ems;
617: }
618: float G_BlinnPhong_Implicit( ) {
619: return 0.25;
620: }
621: float D_BlinnPhong( const in float shininess, const in float dotNH ) {
622: return RECIPROCAL_PI * ( shininess * 0.5 + 1.0 ) * pow( dotNH, shininess );
623: }
624: vec3 BRDF_Specular_BlinnPhong( const in IncidentLight incidentLight, const in GeometricContext geometry, const in vec3 specularColor, const in float shininess ) {
625: vec3 halfDir = normalize( incidentLight.direction + geometry.viewDir );
626: float dotNH = saturate( dot( geometry.normal, halfDir ) );
627: float dotLH = saturate( dot( incidentLight.direction, halfDir ) );
628: vec3 F = F_Schlick( specularColor, dotLH );
629: float G = G_BlinnPhong_Implicit( );
630: float D = D_BlinnPhong( shininess, dotNH );
631: return F * ( G * D );
632: }
633: float GGXRoughnessToBlinnExponent( const in float ggxRoughness ) {
634: return ( 2.0 / pow2( ggxRoughness + 0.0001 ) - 2.0 );
635: }
636: float BlinnExponentToGGXRoughness( const in float blinnExponent ) {
637: return sqrt( 2.0 / ( blinnExponent + 2.0 ) );
638: }
639: #if defined( USE_SHEEN )
640: float D_Charlie(float roughness, float NoH) {
641: float invAlpha = 1.0 / roughness;
642: float cos2h = NoH * NoH;
643: float sin2h = max(1.0 - cos2h, 0.0078125); return (2.0 + invAlpha) * pow(sin2h, invAlpha * 0.5) / (2.0 * PI);
644: }
645: float V_Neubelt(float NoV, float NoL) {
646: return saturate(1.0 / (4.0 * (NoL + NoV - NoL * NoV)));
647: }
648: vec3 BRDF_Specular_Sheen( const in float roughness, const in vec3 L, const in GeometricContext geometry, vec3 specularColor ) {
649: vec3 N = geometry.normal;
650: vec3 V = geometry.viewDir;
651: vec3 H = normalize( V + L );
652: float dotNH = saturate( dot( N, H ) );
653: return specularColor * D_Charlie( roughness, dotNH ) * V_Neubelt( dot(N, V), dot(N, L) );
654: }
655: #endif
656: #ifdef ENVMAP_TYPE_CUBE_UV
657: #define cubeUV_maxMipLevel 8.0
658: #define cubeUV_minMipLevel 4.0
659: #define cubeUV_maxTileSize 256.0
660: #define cubeUV_minTileSize 16.0
661: float getFace( vec3 direction ) {
662: vec3 absDirection = abs( direction );
663: float face = - 1.0;
664: if ( absDirection.x > absDirection.z ) {
665: if ( absDirection.x > absDirection.y )
666: face = direction.x > 0.0 ? 0.0 : 3.0;
667: else
668: face = direction.y > 0.0 ? 1.0 : 4.0;
669: } else {
670: if ( absDirection.z > absDirection.y )
671: face = direction.z > 0.0 ? 2.0 : 5.0;
672: else
673: face = direction.y > 0.0 ? 1.0 : 4.0;
674: }
675: return face;
676: }
677: vec2 getUV( vec3 direction, float face ) {
678: vec2 uv;
679: if ( face == 0.0 ) {
680: uv = vec2( direction.z, direction.y ) / abs( direction.x );
681: } else if ( face == 1.0 ) {
682: uv = vec2( - direction.x, - direction.z ) / abs( direction.y );
683: } else if ( face == 2.0 ) {
684: uv = vec2( - direction.x, direction.y ) / abs( direction.z );
685: } else if ( face == 3.0 ) {
686: uv = vec2( - direction.z, direction.y ) / abs( direction.x );
687: } else if ( face == 4.0 ) {
688: uv = vec2( - direction.x, direction.z ) / abs( direction.y );
689: } else {
690: uv = vec2( direction.x, direction.y ) / abs( direction.z );
691: }
692: return 0.5 * ( uv + 1.0 );
693: }
694: vec3 bilinearCubeUV( sampler2D envMap, vec3 direction, float mipInt ) {
695: float face = getFace( direction );
696: float filterInt = max( cubeUV_minMipLevel - mipInt, 0.0 );
697: mipInt = max( mipInt, cubeUV_minMipLevel );
698: float faceSize = exp2( mipInt );
699: float texelSize = 1.0 / ( 3.0 * cubeUV_maxTileSize );
700: vec2 uv = getUV( direction, face ) * ( faceSize - 1.0 );
701: vec2 f = fract( uv );
702: uv += 0.5 - f;
703: if ( face > 2.0 ) {
704: uv.y += faceSize;
705: face -= 3.0;
706: }
707: uv.x += face * faceSize;
708: if ( mipInt < cubeUV_maxMipLevel ) {
709: uv.y += 2.0 * cubeUV_maxTileSize;
710: }
711: uv.y += filterInt * 2.0 * cubeUV_minTileSize;
712: uv.x += 3.0 * max( 0.0, cubeUV_maxTileSize - 2.0 * faceSize );
713: uv *= texelSize;
714: vec3 tl = envMapTexelToLinear( texture2D( envMap, uv ) ).rgb;
715: uv.x += texelSize;
716: vec3 tr = envMapTexelToLinear( texture2D( envMap, uv ) ).rgb;
717: uv.y += texelSize;
718: vec3 br = envMapTexelToLinear( texture2D( envMap, uv ) ).rgb;
719: uv.x -= texelSize;
720: vec3 bl = envMapTexelToLinear( texture2D( envMap, uv ) ).rgb;
721: vec3 tm = mix( tl, tr, f.x );
722: vec3 bm = mix( bl, br, f.x );
723: return mix( tm, bm, f.y );
724: }
725: #define r0 1.0
726: #define v0 0.339
727: #define m0 - 2.0
728: #define r1 0.8
729: #define v1 0.276
730: #define m1 - 1.0
731: #define r4 0.4
732: #define v4 0.046
733: #define m4 2.0
734: #define r5 0.305
735: #define v5 0.016
736: #define m5 3.0
737: #define r6 0.21
738: #define v6 0.0038
739: #define m6 4.0
740: float roughnessToMip( float roughness ) {
741: float mip = 0.0;
742: if ( roughness >= r1 ) {
743: mip = ( r0 - roughness ) * ( m1 - m0 ) / ( r0 - r1 ) + m0;
744: } else if ( roughness >= r4 ) {
745: mip = ( r1 - roughness ) * ( m4 - m1 ) / ( r1 - r4 ) + m1;
746: } else if ( roughness >= r5 ) {
747: mip = ( r4 - roughness ) * ( m5 - m4 ) / ( r4 - r5 ) + m4;
748: } else if ( roughness >= r6 ) {
749: mip = ( r5 - roughness ) * ( m6 - m5 ) / ( r5 - r6 ) + m5;
750: } else {
751: mip = - 2.0 * log2( 1.16 * roughness ); }
752: return mip;
753: }
754: vec4 textureCubeUV( sampler2D envMap, vec3 sampleDir, float roughness ) {
755: float mip = clamp( roughnessToMip( roughness ), m0, cubeUV_maxMipLevel );
756: float mipF = fract( mip );
757: float mipInt = floor( mip );
758: vec3 color0 = bilinearCubeUV( envMap, sampleDir, mipInt );
759: if ( mipF == 0.0 ) {
760: return vec4( color0, 1.0 );
761: } else {
762: vec3 color1 = bilinearCubeUV( envMap, sampleDir, mipInt + 1.0 );
763: return vec4( mix( color0, color1, mipF ), 1.0 );
764: }
765: }
766: #endif
767: #ifdef USE_ENVMAP
768: uniform float envMapIntensity;
769: uniform float flipEnvMap;
770: uniform int maxMipLevel;
771: #ifdef ENVMAP_TYPE_CUBE
772: uniform samplerCube envMap;
773: #else
774: uniform sampler2D envMap;
775: #endif
776:
777: #endif
778: #if defined( USE_ENVMAP )
779: #ifdef ENVMAP_MODE_REFRACTION
780: uniform float refractionRatio;
781: #endif
782: vec3 getLightProbeIndirectIrradiance( const in GeometricContext geometry, const in int maxMIPLevel ) {
783: vec3 worldNormal = inverseTransformDirection( geometry.normal, viewMatrix );
784: #ifdef ENVMAP_TYPE_CUBE
785: vec3 queryVec = vec3( flipEnvMap * worldNormal.x, worldNormal.yz );
786: #ifdef TEXTURE_LOD_EXT
787: vec4 envMapColor = textureCubeLodEXT( envMap, queryVec, float( maxMIPLevel ) );
788: #else
789: vec4 envMapColor = textureCube( envMap, queryVec, float( maxMIPLevel ) );
790: #endif
791: envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
792: #elif defined( ENVMAP_TYPE_CUBE_UV )
793: vec4 envMapColor = textureCubeUV( envMap, worldNormal, 1.0 );
794: #else
795: vec4 envMapColor = vec4( 0.0 );
796: #endif
797: return PI * envMapColor.rgb * envMapIntensity;
798: }
799: float getSpecularMIPLevel( const in float roughness, const in int maxMIPLevel ) {
800: float maxMIPLevelScalar = float( maxMIPLevel );
801: float sigma = PI * roughness * roughness / ( 1.0 + roughness );
802: float desiredMIPLevel = maxMIPLevelScalar + log2( sigma );
803: return clamp( desiredMIPLevel, 0.0, maxMIPLevelScalar );
804: }
805: vec3 getLightProbeIndirectRadiance( const in vec3 viewDir, const in vec3 normal, const in float roughness, const in int maxMIPLevel ) {
806: #ifdef ENVMAP_MODE_REFLECTION
807: vec3 reflectVec = reflect( -viewDir, normal );
808: reflectVec = normalize( mix( reflectVec, normal, roughness * roughness) );
809: #else
810: vec3 reflectVec = refract( -viewDir, normal, refractionRatio );
811: #endif
812: reflectVec = inverseTransformDirection( reflectVec, viewMatrix );
813: float specularMIPLevel = getSpecularMIPLevel( roughness, maxMIPLevel );
814: #ifdef ENVMAP_TYPE_CUBE
815: vec3 queryReflectVec = vec3( flipEnvMap * reflectVec.x, reflectVec.yz );
816: #ifdef TEXTURE_LOD_EXT
817: vec4 envMapColor = textureCubeLodEXT( envMap, queryReflectVec, specularMIPLevel );
818: #else
819: vec4 envMapColor = textureCube( envMap, queryReflectVec, specularMIPLevel );
820: #endif
821: envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
822: #elif defined( ENVMAP_TYPE_CUBE_UV )
823: vec4 envMapColor = textureCubeUV( envMap, reflectVec, roughness );
824: #endif
825: return envMapColor.rgb * envMapIntensity;
826: }
827: #endif
828: #ifdef USE_FOG
829: uniform vec3 fogColor;
830: varying float fogDepth;
831: #ifdef FOG_EXP2
832: uniform float fogDensity;
833: #else
834: uniform float fogNear;
835: uniform float fogFar;
836: #endif
837: #endif
838: #ifdef USE_FOG
839: varying vec3 vWorldPosition;
840:
841: float fogHeight = 450.0;
842: #endif
843:
844: uniform bool receiveShadow;
845: uniform vec3 ambientLightColor;
846: uniform vec3 lightProbe[ 9 ];
847: vec3 shGetIrradianceAt( in vec3 normal, in vec3 shCoefficients[ 9 ] ) {
848: float x = normal.x, y = normal.y, z = normal.z;
849: vec3 result = shCoefficients[ 0 ] * 0.886227;
850: result += shCoefficients[ 1 ] * 2.0 * 0.511664 * y;
851: result += shCoefficients[ 2 ] * 2.0 * 0.511664 * z;
852: result += shCoefficients[ 3 ] * 2.0 * 0.511664 * x;
853: result += shCoefficients[ 4 ] * 2.0 * 0.429043 * x * y;
854: result += shCoefficients[ 5 ] * 2.0 * 0.429043 * y * z;
855: result += shCoefficients[ 6 ] * ( 0.743125 * z * z - 0.247708 );
856: result += shCoefficients[ 7 ] * 2.0 * 0.429043 * x * z;
857: result += shCoefficients[ 8 ] * 0.429043 * ( x * x - y * y );
858: return result;
859: }
860: vec3 getLightProbeIrradiance( const in vec3 lightProbe[ 9 ], const in GeometricContext geometry ) {
861: vec3 worldNormal = inverseTransformDirection( geometry.normal, viewMatrix );
862: vec3 irradiance = shGetIrradianceAt( worldNormal, lightProbe );
863: return irradiance;
864: }
865: vec3 getAmbientLightIrradiance( const in vec3 ambientLightColor ) {
866: vec3 irradiance = ambientLightColor;
867: #ifndef PHYSICALLY_CORRECT_LIGHTS
868: irradiance *= PI;
869: #endif
870: return irradiance;
871: }
872: #if 1 > 0
873: struct DirectionalLight {
874: vec3 direction;
875: vec3 color;
876: };
877: uniform DirectionalLight directionalLights[ 1 ];
878: void getDirectionalDirectLightIrradiance( const in DirectionalLight directionalLight, const in GeometricContext geometry, out IncidentLight directLight ) {
879: directLight.color = directionalLight.color;
880: directLight.direction = directionalLight.direction;
881: directLight.visible = true;
882: }
883: #endif
884: #if 18 > 0
885: struct PointLight {
886: vec3 position;
887: vec3 color;
888: float distance;
889: float decay;
890: };
891: uniform PointLight pointLights[ 18 ];
892: void getPointDirectLightIrradiance( const in PointLight pointLight, const in GeometricContext geometry, out IncidentLight directLight ) {
893: vec3 lVector = pointLight.position - geometry.position;
894: directLight.direction = normalize( lVector );
895: float lightDistance = length( lVector );
896: directLight.color = pointLight.color;
897: directLight.color *= punctualLightIntensityToIrradianceFactor( lightDistance, pointLight.distance, pointLight.decay );
898: directLight.visible = ( directLight.color != vec3( 0.0 ) );
899: }
900: #endif
901: #if 6 > 0
902: struct SpotLight {
903: vec3 position;
904: vec3 direction;
905: vec3 color;
906: float distance;
907: float decay;
908: float coneCos;
909: float penumbraCos;
910: };
911: uniform SpotLight spotLights[ 6 ];
912: void getSpotDirectLightIrradiance( const in SpotLight spotLight, const in GeometricContext geometry, out IncidentLight directLight ) {
913: vec3 lVector = spotLight.position - geometry.position;
914: directLight.direction = normalize( lVector );
915: float lightDistance = length( lVector );
916: float angleCos = dot( directLight.direction, spotLight.direction );
917: if ( angleCos > spotLight.coneCos ) {
918: float spotEffect = smoothstep( spotLight.coneCos, spotLight.penumbraCos, angleCos );
919: directLight.color = spotLight.color;
920: directLight.color *= spotEffect * punctualLightIntensityToIrradianceFactor( lightDistance, spotLight.distance, spotLight.decay );
921: directLight.visible = true;
922: } else {
923: directLight.color = vec3( 0.0 );
924: directLight.visible = false;
925: }
926: }
927: #endif
928: #if 0 > 0
929: struct RectAreaLight {
930: vec3 color;
931: vec3 position;
932: vec3 halfWidth;
933: vec3 halfHeight;
934: };
935: uniform sampler2D ltc_1; uniform sampler2D ltc_2;
936: uniform RectAreaLight rectAreaLights[ 0 ];
937: #endif
938: #if 0 > 0
939: struct HemisphereLight {
940: vec3 direction;
941: vec3 skyColor;
942: vec3 groundColor;
943: };
944: uniform HemisphereLight hemisphereLights[ 0 ];
945: vec3 getHemisphereLightIrradiance( const in HemisphereLight hemiLight, const in GeometricContext geometry ) {
946: float dotNL = dot( geometry.normal, hemiLight.direction );
947: float hemiDiffuseWeight = 0.5 * dotNL + 0.5;
948: vec3 irradiance = mix( hemiLight.groundColor, hemiLight.skyColor, hemiDiffuseWeight );
949: #ifndef PHYSICALLY_CORRECT_LIGHTS
950: irradiance *= PI;
951: #endif
952: return irradiance;
953: }
954: #endif
955: struct PhysicalMaterial {
956: vec3 diffuseColor;
957: float specularRoughness;
958: vec3 specularColor;
959: #ifdef CLEARCOAT
960: float clearcoat;
961: float clearcoatRoughness;
962: #endif
963: #ifdef USE_SHEEN
964: vec3 sheenColor;
965: #endif
966: };
967: #define MAXIMUM_SPECULAR_COEFFICIENT 0.16
968: #define DEFAULT_SPECULAR_COEFFICIENT 0.04
969: float clearcoatDHRApprox( const in float roughness, const in float dotNL ) {
970: return DEFAULT_SPECULAR_COEFFICIENT + ( 1.0 - DEFAULT_SPECULAR_COEFFICIENT ) * ( pow( 1.0 - dotNL, 5.0 ) * pow( 1.0 - roughness, 2.0 ) );
971: }
972: #if 0 > 0
973: void RE_Direct_RectArea_Physical( const in RectAreaLight rectAreaLight, const in GeometricContext geometry, const in PhysicalMaterial material, inout ReflectedLight reflectedLight ) {
974: vec3 normal = geometry.normal;
975: vec3 viewDir = geometry.viewDir;
976: vec3 position = geometry.position;
977: vec3 lightPos = rectAreaLight.position;
978: vec3 halfWidth = rectAreaLight.halfWidth;
979: vec3 halfHeight = rectAreaLight.halfHeight;
980: vec3 lightColor = rectAreaLight.color;
981: float roughness = material.specularRoughness;
982: vec3 rectCoords[ 4 ];
983: rectCoords[ 0 ] = lightPos + halfWidth - halfHeight; rectCoords[ 1 ] = lightPos - halfWidth - halfHeight;
984: rectCoords[ 2 ] = lightPos - halfWidth + halfHeight;
985: rectCoords[ 3 ] = lightPos + halfWidth + halfHeight;
986: vec2 uv = LTC_Uv( normal, viewDir, roughness );
987: vec4 t1 = texture2D( ltc_1, uv );
988: vec4 t2 = texture2D( ltc_2, uv );
989: mat3 mInv = mat3(
990: vec3( t1.x, 0, t1.y ),
991: vec3( 0, 1, 0 ),
992: vec3( t1.z, 0, t1.w )
993: );
994: vec3 fresnel = ( material.specularColor * t2.x + ( vec3( 1.0 ) - material.specularColor ) * t2.y );
995: reflectedLight.directSpecular += lightColor * fresnel * LTC_Evaluate( normal, viewDir, position, mInv, rectCoords );
996: reflectedLight.directDiffuse += lightColor * material.diffuseColor * LTC_Evaluate( normal, viewDir, position, mat3( 1.0 ), rectCoords );
997: }
998: #endif
999: void RE_Direct_Physical( const in IncidentLight directLight, const in GeometricContext geometry, const in PhysicalMaterial material, inout ReflectedLight reflectedLight ) {
1000: float dotNL = saturate( dot( geometry.normal, directLight.direction ) );
1001: vec3 irradiance = dotNL * directLight.color;
1002: #ifndef PHYSICALLY_CORRECT_LIGHTS
1003: irradiance *= PI;
1004: #endif
1005: #ifdef CLEARCOAT
1006: float ccDotNL = saturate( dot( geometry.clearcoatNormal, directLight.direction ) );
1007: vec3 ccIrradiance = ccDotNL * directLight.color;
1008: #ifndef PHYSICALLY_CORRECT_LIGHTS
1009: ccIrradiance *= PI;
1010: #endif
1011: float clearcoatDHR = material.clearcoat * clearcoatDHRApprox( material.clearcoatRoughness, ccDotNL );
1012: reflectedLight.directSpecular += ccIrradiance * material.clearcoat * BRDF_Specular_GGX( directLight, geometry.viewDir, geometry.clearcoatNormal, vec3( DEFAULT_SPECULAR_COEFFICIENT ), material.clearcoatRoughness );
1013: #else
1014: float clearcoatDHR = 0.0;
1015: #endif
1016: #ifdef USE_SHEEN
1017: reflectedLight.directSpecular += ( 1.0 - clearcoatDHR ) * irradiance * BRDF_Specular_Sheen(
1018: material.specularRoughness,
1019: directLight.direction,
1020: geometry,
1021: material.sheenColor
1022: );
1023: #else
1024: reflectedLight.directSpecular += ( 1.0 - clearcoatDHR ) * irradiance * BRDF_Specular_GGX( directLight, geometry.viewDir, geometry.normal, material.specularColor, material.specularRoughness);
1025: #endif
1026: reflectedLight.directDiffuse += ( 1.0 - clearcoatDHR ) * irradiance * BRDF_Diffuse_Lambert( material.diffuseColor );
1027: }
1028: void RE_IndirectDiffuse_Physical( const in vec3 irradiance, const in GeometricContext geometry, const in PhysicalMaterial material, inout ReflectedLight reflectedLight ) {
1029: reflectedLight.indirectDiffuse += irradiance * BRDF_Diffuse_Lambert( material.diffuseColor );
1030: }
1031: void RE_IndirectSpecular_Physical( const in vec3 radiance, const in vec3 irradiance, const in vec3 clearcoatRadiance, const in GeometricContext geometry, const in PhysicalMaterial material, inout ReflectedLight reflectedLight) {
1032: #ifdef CLEARCOAT
1033: float ccDotNV = saturate( dot( geometry.clearcoatNormal, geometry.viewDir ) );
1034: reflectedLight.indirectSpecular += clearcoatRadiance * material.clearcoat * BRDF_Specular_GGX_Environment( geometry.viewDir, geometry.clearcoatNormal, vec3( DEFAULT_SPECULAR_COEFFICIENT ), material.clearcoatRoughness );
1035: float ccDotNL = ccDotNV;
1036: float clearcoatDHR = material.clearcoat * clearcoatDHRApprox( material.clearcoatRoughness, ccDotNL );
1037: #else
1038: float clearcoatDHR = 0.0;
1039: #endif
1040: float clearcoatInv = 1.0 - clearcoatDHR;
1041: vec3 singleScattering = vec3( 0.0 );
1042: vec3 multiScattering = vec3( 0.0 );
1043: vec3 cosineWeightedIrradiance = irradiance * RECIPROCAL_PI;
1044: BRDF_Specular_Multiscattering_Environment( geometry, material.specularColor, material.specularRoughness, singleScattering, multiScattering );
1045: vec3 diffuse = material.diffuseColor * ( 1.0 - ( singleScattering + multiScattering ) );
1046: reflectedLight.indirectSpecular += clearcoatInv * radiance * singleScattering;
1047: reflectedLight.indirectSpecular += multiScattering * cosineWeightedIrradiance;
1048: reflectedLight.indirectDiffuse += diffuse * cosineWeightedIrradiance;
1049: }
1050: #define RE_Direct RE_Direct_Physical
1051: #define RE_Direct_RectArea RE_Direct_RectArea_Physical
1052: #define RE_IndirectDiffuse RE_IndirectDiffuse_Physical
1053: #define RE_IndirectSpecular RE_IndirectSpecular_Physical
1054: float computeSpecularOcclusion( const in float dotNV, const in float ambientOcclusion, const in float roughness ) {
1055: return saturate( pow( dotNV + ambientOcclusion, exp2( - 16.0 * roughness - 1.0 ) ) - 1.0 + ambientOcclusion );
1056: }
1057: #ifdef USE_SHADOWMAP
1058: #if 1 > 0
1059: uniform sampler2D directionalShadowMap[ 1 ];
1060: varying vec4 vDirectionalShadowCoord[ 1 ];
1061: struct DirectionalLightShadow {
1062: float shadowBias;
1063: float shadowNormalBias;
1064: float shadowRadius;
1065: vec2 shadowMapSize;
1066: };
1067: uniform DirectionalLightShadow directionalLightShadows[ 1 ];
1068: #endif
1069: #if 6 > 0
1070: uniform sampler2D spotShadowMap[ 6 ];
1071: varying vec4 vSpotShadowCoord[ 6 ];
1072: struct SpotLightShadow {
1073: float shadowBias;
1074: float shadowNormalBias;
1075: float shadowRadius;
1076: vec2 shadowMapSize;
1077: };
1078: uniform SpotLightShadow spotLightShadows[ 6 ];
1079: #endif
1080: #if 0 > 0
1081: uniform sampler2D pointShadowMap[ 0 ];
1082: varying vec4 vPointShadowCoord[ 0 ];
1083: struct PointLightShadow {
1084: float shadowBias;
1085: float shadowNormalBias;
1086: float shadowRadius;
1087: vec2 shadowMapSize;
1088: float shadowCameraNear;
1089: float shadowCameraFar;
1090: };
1091: uniform PointLightShadow pointLightShadows[ 0 ];
1092: #endif
1093: float texture2DCompare( sampler2D depths, vec2 uv, float compare ) {
1094: return step( compare, unpackRGBAToDepth( texture2D( depths, uv ) ) );
1095: }
1096: vec2 texture2DDistribution( sampler2D shadow, vec2 uv ) {
1097: return unpackRGBATo2Half( texture2D( shadow, uv ) );
1098: }
1099: float VSMShadow (sampler2D shadow, vec2 uv, float compare ){
1100: float occlusion = 1.0;
1101: vec2 distribution = texture2DDistribution( shadow, uv );
1102: float hard_shadow = step( compare , distribution.x );
1103: if (hard_shadow != 1.0 ) {
1104: float distance = compare - distribution.x ;
1105: float variance = max( 0.00000, distribution.y * distribution.y );
1106: float softness_probability = variance / (variance + distance * distance ); softness_probability = clamp( ( softness_probability - 0.3 ) / ( 0.95 - 0.3 ), 0.0, 1.0 ); occlusion = clamp( max( hard_shadow, softness_probability ), 0.0, 1.0 );
1107: }
1108: return occlusion;
1109: }
1110: float getShadow( sampler2D shadowMap, vec2 shadowMapSize, float shadowBias, float shadowRadius, vec4 shadowCoord ) {
1111: float shadow = 1.0;
1112: shadowCoord.xyz /= shadowCoord.w;
1113: shadowCoord.z += shadowBias;
1114: bvec4 inFrustumVec = bvec4 ( shadowCoord.x >= 0.0, shadowCoord.x <= 1.0, shadowCoord.y >= 0.0, shadowCoord.y <= 1.0 );
1115: bool inFrustum = all( inFrustumVec );
1116: bvec2 frustumTestVec = bvec2( inFrustum, shadowCoord.z <= 1.0 );
1117: bool frustumTest = all( frustumTestVec );
1118: if ( frustumTest ) {
1119: #if defined( SHADOWMAP_TYPE_PCF )
1120: vec2 texelSize = vec2( 1.0 ) / shadowMapSize;
1121: float dx0 = - texelSize.x * shadowRadius;
1122: float dy0 = - texelSize.y * shadowRadius;
1123: float dx1 = + texelSize.x * shadowRadius;
1124: float dy1 = + texelSize.y * shadowRadius;
1125: float dx2 = dx0 / 2.0;
1126: float dy2 = dy0 / 2.0;
1127: float dx3 = dx1 / 2.0;
1128: float dy3 = dy1 / 2.0;
1129: shadow = (
1130: texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx0, dy0 ), shadowCoord.z ) +
1131: texture2DCompare( shadowMap, shadowCoord.xy + vec2( 0.0, dy0 ), shadowCoord.z ) +
1132: texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx1, dy0 ), shadowCoord.z ) +
1133: texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx2, dy2 ), shadowCoord.z ) +
1134: texture2DCompare( shadowMap, shadowCoord.xy + vec2( 0.0, dy2 ), shadowCoord.z ) +
1135: texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx3, dy2 ), shadowCoord.z ) +
1136: texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx0, 0.0 ), shadowCoord.z ) +
1137: texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx2, 0.0 ), shadowCoord.z ) +
1138: texture2DCompare( shadowMap, shadowCoord.xy, shadowCoord.z ) +
1139: texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx3, 0.0 ), shadowCoord.z ) +
1140: texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx1, 0.0 ), shadowCoord.z ) +
1141: texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx2, dy3 ), shadowCoord.z ) +
1142: texture2DCompare( shadowMap, shadowCoord.xy + vec2( 0.0, dy3 ), shadowCoord.z ) +
1143: texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx3, dy3 ), shadowCoord.z ) +
1144: texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx0, dy1 ), shadowCoord.z ) +
1145: texture2DCompare( shadowMap, shadowCoord.xy + vec2( 0.0, dy1 ), shadowCoord.z ) +
1146: texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx1, dy1 ), shadowCoord.z )
1147: ) * ( 1.0 / 17.0 );
1148: #elif defined( SHADOWMAP_TYPE_PCF_SOFT )
1149: vec2 texelSize = vec2( 1.0 ) / shadowMapSize;
1150: float dx = texelSize.x;
1151: float dy = texelSize.y;
1152: vec2 uv = shadowCoord.xy;
1153: vec2 f = fract( uv * shadowMapSize + 0.5 );
1154: uv -= f * texelSize;
1155: shadow = (
1156: texture2DCompare( shadowMap, uv, shadowCoord.z ) +
1157: texture2DCompare( shadowMap, uv + vec2( dx, 0.0 ), shadowCoord.z ) +
1158: texture2DCompare( shadowMap, uv + vec2( 0.0, dy ), shadowCoord.z ) +
1159: texture2DCompare( shadowMap, uv + texelSize, shadowCoord.z ) +
1160: mix( texture2DCompare( shadowMap, uv + vec2( -dx, 0.0 ), shadowCoord.z ),
1161: texture2DCompare( shadowMap, uv + vec2( 2.0 * dx, 0.0 ), shadowCoord.z ),
1162: f.x ) +
1163: mix( texture2DCompare( shadowMap, uv + vec2( -dx, dy ), shadowCoord.z ),
1164: texture2DCompare( shadowMap, uv + vec2( 2.0 * dx, dy ), shadowCoord.z ),
1165: f.x ) +
1166: mix( texture2DCompare( shadowMap, uv + vec2( 0.0, -dy ), shadowCoord.z ),
1167: texture2DCompare( shadowMap, uv + vec2( 0.0, 2.0 * dy ), shadowCoord.z ),
1168: f.y ) +
1169: mix( texture2DCompare( shadowMap, uv + vec2( dx, -dy ), shadowCoord.z ),
1170: texture2DCompare( shadowMap, uv + vec2( dx, 2.0 * dy ), shadowCoord.z ),
1171: f.y ) +
1172: mix( mix( texture2DCompare( shadowMap, uv + vec2( -dx, -dy ), shadowCoord.z ),
1173: texture2DCompare( shadowMap, uv + vec2( 2.0 * dx, -dy ), shadowCoord.z ),
1174: f.x ),
1175: mix( texture2DCompare( shadowMap, uv + vec2( -dx, 2.0 * dy ), shadowCoord.z ),
1176: texture2DCompare( shadowMap, uv + vec2( 2.0 * dx, 2.0 * dy ), shadowCoord.z ),
1177: f.x ),
1178: f.y )
1179: ) * ( 1.0 / 9.0 );
1180: #elif defined( SHADOWMAP_TYPE_VSM )
1181: shadow = VSMShadow( shadowMap, shadowCoord.xy, shadowCoord.z );
1182: #else
1183: shadow = texture2DCompare( shadowMap, shadowCoord.xy, shadowCoord.z );
1184: #endif
1185: }
1186: return shadow;
1187: }
1188: vec2 cubeToUV( vec3 v, float texelSizeY ) {
1189: vec3 absV = abs( v );
1190: float scaleToCube = 1.0 / max( absV.x, max( absV.y, absV.z ) );
1191: absV *= scaleToCube;
1192: v *= scaleToCube * ( 1.0 - 2.0 * texelSizeY );
1193: vec2 planar = v.xy;
1194: float almostATexel = 1.5 * texelSizeY;
1195: float almostOne = 1.0 - almostATexel;
1196: if ( absV.z >= almostOne ) {
1197: if ( v.z > 0.0 )
1198: planar.x = 4.0 - v.x;
1199: } else if ( absV.x >= almostOne ) {
1200: float signX = sign( v.x );
1201: planar.x = v.z * signX + 2.0 * signX;
1202: } else if ( absV.y >= almostOne ) {
1203: float signY = sign( v.y );
1204: planar.x = v.x + 2.0 * signY + 2.0;
1205: planar.y = v.z * signY - 2.0;
1206: }
1207: return vec2( 0.125, 0.25 ) * planar + vec2( 0.375, 0.75 );
1208: }
1209: float getPointShadow( sampler2D shadowMap, vec2 shadowMapSize, float shadowBias, float shadowRadius, vec4 shadowCoord, float shadowCameraNear, float shadowCameraFar ) {
1210: vec2 texelSize = vec2( 1.0 ) / ( shadowMapSize * vec2( 4.0, 2.0 ) );
1211: vec3 lightToPosition = shadowCoord.xyz;
1212: float dp = ( length( lightToPosition ) - shadowCameraNear ) / ( shadowCameraFar - shadowCameraNear ); dp += shadowBias;
1213: vec3 bd3D = normalize( lightToPosition );
1214: #if defined( SHADOWMAP_TYPE_PCF ) || defined( SHADOWMAP_TYPE_PCF_SOFT ) || defined( SHADOWMAP_TYPE_VSM )
1215: vec2 offset = vec2( - 1, 1 ) * shadowRadius * texelSize.y;
1216: return (
1217: texture2DCompare( shadowMap, cubeToUV( bd3D + offset.xyy, texelSize.y ), dp ) +
1218: texture2DCompare( shadowMap, cubeToUV( bd3D + offset.yyy, texelSize.y ), dp ) +
1219: texture2DCompare( shadowMap, cubeToUV( bd3D + offset.xyx, texelSize.y ), dp ) +
1220: texture2DCompare( shadowMap, cubeToUV( bd3D + offset.yyx, texelSize.y ), dp ) +
1221: texture2DCompare( shadowMap, cubeToUV( bd3D, texelSize.y ), dp ) +
1222: texture2DCompare( shadowMap, cubeToUV( bd3D + offset.xxy, texelSize.y ), dp ) +
1223: texture2DCompare( shadowMap, cubeToUV( bd3D + offset.yxy, texelSize.y ), dp ) +
1224: texture2DCompare( shadowMap, cubeToUV( bd3D + offset.xxx, texelSize.y ), dp ) +
1225: texture2DCompare( shadowMap, cubeToUV( bd3D + offset.yxx, texelSize.y ), dp )
1226: ) * ( 1.0 / 9.0 );
1227: #else
1228: return texture2DCompare( shadowMap, cubeToUV( bd3D, texelSize.y ), dp );
1229: #endif
1230: }
1231: #endif
1232: #ifdef USE_BUMPMAP
1233: uniform sampler2D bumpMap;
1234: uniform float bumpScale;
1235: vec2 dHdxy_fwd() {
1236: vec2 dSTdx = dFdx( vUv );
1237: vec2 dSTdy = dFdy( vUv );
1238: float Hll = bumpScale * texture2D( bumpMap, vUv ).x;
1239: float dBx = bumpScale * texture2D( bumpMap, vUv + dSTdx ).x - Hll;
1240: float dBy = bumpScale * texture2D( bumpMap, vUv + dSTdy ).x - Hll;
1241: return vec2( dBx, dBy );
1242: }
1243: vec3 perturbNormalArb( vec3 surf_pos, vec3 surf_norm, vec2 dHdxy, float faceDirection ) {
1244: vec3 vSigmaX = vec3( dFdx( surf_pos.x ), dFdx( surf_pos.y ), dFdx( surf_pos.z ) );
1245: vec3 vSigmaY = vec3( dFdy( surf_pos.x ), dFdy( surf_pos.y ), dFdy( surf_pos.z ) );
1246: vec3 vN = surf_norm;
1247: vec3 R1 = cross( vSigmaY, vN );
1248: vec3 R2 = cross( vN, vSigmaX );
1249: float fDet = dot( vSigmaX, R1 ) * faceDirection;
1250: vec3 vGrad = sign( fDet ) * ( dHdxy.x * R1 + dHdxy.y * R2 );
1251: return normalize( abs( fDet ) * surf_norm - vGrad );
1252: }
1253: #endif
1254: #ifdef USE_NORMALMAP
1255: uniform sampler2D normalMap;
1256: uniform vec2 normalScale;
1257: #endif
1258: #ifdef OBJECTSPACE_NORMALMAP
1259: uniform mat3 normalMatrix;
1260: #endif
1261: #if ! defined ( USE_TANGENT ) && ( defined ( TANGENTSPACE_NORMALMAP ) || defined ( USE_CLEARCOAT_NORMALMAP ) )
1262: vec3 perturbNormal2Arb( vec3 eye_pos, vec3 surf_norm, vec3 mapN, float faceDirection ) {
1263: vec3 q0 = vec3( dFdx( eye_pos.x ), dFdx( eye_pos.y ), dFdx( eye_pos.z ) );
1264: vec3 q1 = vec3( dFdy( eye_pos.x ), dFdy( eye_pos.y ), dFdy( eye_pos.z ) );
1265: vec2 st0 = dFdx( vUv.st );
1266: vec2 st1 = dFdy( vUv.st );
1267: vec3 N = surf_norm;
1268: vec3 q1perp = cross( q1, N );
1269: vec3 q0perp = cross( N, q0 );
1270: vec3 T = q1perp * st0.x + q0perp * st1.x;
1271: vec3 B = q1perp * st0.y + q0perp * st1.y;
1272: float det = max( dot( T, T ), dot( B, B ) );
1273: float scale = ( det == 0.0 ) ? 0.0 : faceDirection * inversesqrt( det );
1274: return normalize( T * ( mapN.x * scale ) + B * ( mapN.y * scale ) + N * mapN.z );
1275: }
1276: #endif
1277: #ifdef USE_CLEARCOATMAP
1278: uniform sampler2D clearcoatMap;
1279: #endif
1280: #ifdef USE_CLEARCOAT_ROUGHNESSMAP
1281: uniform sampler2D clearcoatRoughnessMap;
1282: #endif
1283: #ifdef USE_CLEARCOAT_NORMALMAP
1284: uniform sampler2D clearcoatNormalMap;
1285: uniform vec2 clearcoatNormalScale;
1286: #endif
1287: #ifdef USE_ROUGHNESSMAP
1288: uniform sampler2D roughnessMap;
1289: #endif
1290: #ifdef USE_METALNESSMAP
1291: uniform sampler2D metalnessMap;
1292: #endif
1293: #if defined( USE_LOGDEPTHBUF ) && defined( USE_LOGDEPTHBUF_EXT )
1294: uniform float logDepthBufFC;
1295: varying float vFragDepth;
1296: varying float vIsPerspective;
1297: #endif
1298: #if 0 > 0
1299: varying vec3 vClipPosition;
1300: uniform vec4 clippingPlanes[ 0 ];
1301: #endif
1302: void main() {
1303: #if 0 > 0
1304: vec4 plane;
1305:
1306: #if 0 < 0
1307: bool clipped = true;
1308:
1309: if ( clipped ) discard;
1310: #endif
1311: #endif
1312: vec4 diffuseColor = vec4( diffuse, opacity );
1313: ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );
1314: vec3 totalEmissiveRadiance = emissive;
1315: #ifdef TRANSMISSION
1316: float totalTransmission = transmission;
1317: #endif
1318: #if defined( USE_LOGDEPTHBUF ) && defined( USE_LOGDEPTHBUF_EXT )
1319: gl_FragDepthEXT = vIsPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;
1320: #endif
1321: #ifdef USE_MAP
1322: vec4 texelColor = texture2D( map, vUv );
1323: texelColor = mapTexelToLinear( texelColor );
1324: diffuseColor *= texelColor;
1325: #endif
1326: #if defined( USE_COLOR_ALPHA )
1327: diffuseColor *= vColor;
1328: #elif defined( USE_COLOR )
1329: diffuseColor.rgb *= vColor;
1330: #endif
1331: #ifdef USE_ALPHAMAP
1332: diffuseColor.a *= texture2D( alphaMap, vUv ).g;
1333: #endif
1334: #ifdef ALPHATEST
1335: if ( diffuseColor.a < ALPHATEST ) discard;
1336: #endif
1337: float roughnessFactor = roughness;
1338: #ifdef USE_ROUGHNESSMAP
1339: vec4 texelRoughness = texture2D( roughnessMap, vUv );
1340: roughnessFactor *= texelRoughness.g;
1341: #endif
1342: float metalnessFactor = metalness;
1343: #ifdef USE_METALNESSMAP
1344: vec4 texelMetalness = texture2D( metalnessMap, vUv );
1345: metalnessFactor *= texelMetalness.b;
1346: #endif
1347: float faceDirection = gl_FrontFacing ? 1.0 : - 1.0;
1348: #ifdef FLAT_SHADED
1349: vec3 fdx = vec3( dFdx( vViewPosition.x ), dFdx( vViewPosition.y ), dFdx( vViewPosition.z ) );
1350: vec3 fdy = vec3( dFdy( vViewPosition.x ), dFdy( vViewPosition.y ), dFdy( vViewPosition.z ) );
1351: vec3 normal = normalize( cross( fdx, fdy ) );
1352: #else
1353: vec3 normal = normalize( vNormal );
1354: #ifdef DOUBLE_SIDED
1355: normal = normal * faceDirection;
1356: #endif
1357: #ifdef USE_TANGENT
1358: vec3 tangent = normalize( vTangent );
1359: vec3 bitangent = normalize( vBitangent );
1360: #ifdef DOUBLE_SIDED
1361: tangent = tangent * faceDirection;
1362: bitangent = bitangent * faceDirection;
1363: #endif
1364: #if defined( TANGENTSPACE_NORMALMAP ) || defined( USE_CLEARCOAT_NORMALMAP )
1365: mat3 vTBN = mat3( tangent, bitangent, normal );
1366: #endif
1367: #endif
1368: #endif
1369: vec3 geometryNormal = normal;
1370: #ifdef OBJECTSPACE_NORMALMAP
1371: normal = texture2D( normalMap, vUv ).xyz * 2.0 - 1.0;
1372: #ifdef FLIP_SIDED
1373: normal = - normal;
1374: #endif
1375: #ifdef DOUBLE_SIDED
1376: normal = normal * faceDirection;
1377: #endif
1378: normal = normalize( normalMatrix * normal );
1379: #elif defined( TANGENTSPACE_NORMALMAP )
1380: vec3 mapN = texture2D( normalMap, vUv ).xyz * 2.0 - 1.0;
1381: mapN.xy *= normalScale;
1382: #ifdef USE_TANGENT
1383: normal = normalize( vTBN * mapN );
1384: #else
1385: normal = perturbNormal2Arb( -vViewPosition, normal, mapN, faceDirection );
1386: #endif
1387: #elif defined( USE_BUMPMAP )
1388: normal = perturbNormalArb( -vViewPosition, normal, dHdxy_fwd(), faceDirection );
1389: #endif
1390: #ifdef CLEARCOAT
1391: vec3 clearcoatNormal = geometryNormal;
1392: #endif
1393: #ifdef USE_CLEARCOAT_NORMALMAP
1394: vec3 clearcoatMapN = texture2D( clearcoatNormalMap, vUv ).xyz * 2.0 - 1.0;
1395: clearcoatMapN.xy *= clearcoatNormalScale;
1396: #ifdef USE_TANGENT
1397: clearcoatNormal = normalize( vTBN * clearcoatMapN );
1398: #else
1399: clearcoatNormal = perturbNormal2Arb( - vViewPosition, clearcoatNormal, clearcoatMapN, faceDirection );
1400: #endif
1401: #endif
1402: #ifdef USE_EMISSIVEMAP
1403: vec4 emissiveColor = texture2D( emissiveMap, vUv );
1404: emissiveColor.rgb = emissiveMapTexelToLinear( emissiveColor ).rgb;
1405: totalEmissiveRadiance *= emissiveColor.rgb;
1406: #endif
1407: #ifdef USE_TRANSMISSIONMAP
1408: totalTransmission *= texture2D( transmissionMap, vUv ).r;
1409: #endif
1410:
1411:
1412: {
1413: vec3 worldPosition = vCoords;
1414:
1415: float weightIndices[4] = float[4](vWeights1.x, vWeights1.y, vWeights1.z, vWeights1.w);
1416: float weightValues[4] = float[4](vWeights2.x, vWeights2.y, vWeights2.z, vWeights2.w);
1417:
1418: // TRIPLANAR SPLATTING w/ NORMALS & UVS
1419: vec3 worldSpaceNormal = normalize(vNormal);
1420: vec4 diffuseSamples[4];
1421: // vec4 normalSamples[4];
1422:
1423: for (int i = 0; i < 4; ++i) {
1424: vec4 d = vec4(0.0);
1425: // vec4 n = vec4(0.0);
1426: if (weightValues[i] > 0.0) {
1427: d = _Triplanar_UV(
1428: worldPosition, worldSpaceNormal, weightIndices[i], TRIPLANAR_diffuseMap);
1429: // n = _TriplanarN_UV(
1430: // worldPosition, worldSpaceNormal, weightIndices[i], TRIPLANAR_normalMap);
1431:
1432: d.w *= weightValues[i];
1433: // n.w = d.w;
1434: }
1435:
1436: diffuseSamples[i] = d;
1437: // normalSamples[i] = n;
1438: }
1439:
1440: vec4 diffuseBlended = _TerrainBlend_4(diffuseSamples);
1441: // vec4 normalBlended = _TerrainBlend_4(normalSamples);
1442: diffuseColor = sRGBToLinear(diffuseBlended);
1443: // normal = normalBlended.xyz;
1444: }
1445:
1446: PhysicalMaterial material;
1447: material.diffuseColor = diffuseColor.rgb * ( 1.0 - metalnessFactor );
1448: vec3 dxy = max( abs( dFdx( geometryNormal ) ), abs( dFdy( geometryNormal ) ) );
1449: float geometryRoughness = max( max( dxy.x, dxy.y ), dxy.z );
1450: material.specularRoughness = max( roughnessFactor, 0.0525 );material.specularRoughness += geometryRoughness;
1451: material.specularRoughness = min( material.specularRoughness, 1.0 );
1452: #ifdef REFLECTIVITY
1453: material.specularColor = mix( vec3( MAXIMUM_SPECULAR_COEFFICIENT * pow2( reflectivity ) ), diffuseColor.rgb, metalnessFactor );
1454: #else
1455: material.specularColor = mix( vec3( DEFAULT_SPECULAR_COEFFICIENT ), diffuseColor.rgb, metalnessFactor );
1456: #endif
1457: #ifdef CLEARCOAT
1458: material.clearcoat = clearcoat;
1459: material.clearcoatRoughness = clearcoatRoughness;
1460: #ifdef USE_CLEARCOATMAP
1461: material.clearcoat *= texture2D( clearcoatMap, vUv ).x;
1462: #endif
1463: #ifdef USE_CLEARCOAT_ROUGHNESSMAP
1464: material.clearcoatRoughness *= texture2D( clearcoatRoughnessMap, vUv ).y;
1465: #endif
1466: material.clearcoat = saturate( material.clearcoat ); material.clearcoatRoughness = max( material.clearcoatRoughness, 0.0525 );
1467: material.clearcoatRoughness += geometryRoughness;
1468: material.clearcoatRoughness = min( material.clearcoatRoughness, 1.0 );
1469: #endif
1470: #ifdef USE_SHEEN
1471: material.sheenColor = sheen;
1472: #endif
1473:
1474: GeometricContext geometry;
1475: geometry.position = - vViewPosition;
1476: geometry.normal = normal;
1477: geometry.viewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( vViewPosition );
1478: #ifdef CLEARCOAT
1479: geometry.clearcoatNormal = clearcoatNormal;
1480: #endif
1481: IncidentLight directLight;
1482: #if ( 18 > 0 ) && defined( RE_Direct )
1483: PointLight pointLight;
1484: #if defined( USE_SHADOWMAP ) && 0 > 0
1485: PointLightShadow pointLightShadow;
1486: #endif
1487:
1488: pointLight = pointLights[ 0 ];
1489: getPointDirectLightIrradiance( pointLight, geometry, directLight );
1490: #if defined( USE_SHADOWMAP ) && ( 0 < 0 )
1491: pointLightShadow = pointLightShadows[ 0 ];
1492: directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getPointShadow( pointShadowMap[ 0 ], pointLightShadow.shadowMapSize, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ 0 ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;
1493: #endif
1494: RE_Direct( directLight, geometry, material, reflectedLight );
1495:
1496: pointLight = pointLights[ 1 ];
1497: getPointDirectLightIrradiance( pointLight, geometry, directLight );
1498: #if defined( USE_SHADOWMAP ) && ( 1 < 0 )
1499: pointLightShadow = pointLightShadows[ 1 ];
1500: directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getPointShadow( pointShadowMap[ 1 ], pointLightShadow.shadowMapSize, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ 1 ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;
1501: #endif
1502: RE_Direct( directLight, geometry, material, reflectedLight );
1503:
1504: pointLight = pointLights[ 2 ];
1505: getPointDirectLightIrradiance( pointLight, geometry, directLight );
1506: #if defined( USE_SHADOWMAP ) && ( 2 < 0 )
1507: pointLightShadow = pointLightShadows[ 2 ];
1508: directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getPointShadow( pointShadowMap[ 2 ], pointLightShadow.shadowMapSize, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ 2 ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;
1509: #endif
1510: RE_Direct( directLight, geometry, material, reflectedLight );
1511:
1512: pointLight = pointLights[ 3 ];
1513: getPointDirectLightIrradiance( pointLight, geometry, directLight );
1514: #if defined( USE_SHADOWMAP ) && ( 3 < 0 )
1515: pointLightShadow = pointLightShadows[ 3 ];
1516: directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getPointShadow( pointShadowMap[ 3 ], pointLightShadow.shadowMapSize, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ 3 ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;
1517: #endif
1518: RE_Direct( directLight, geometry, material, reflectedLight );
1519:
1520: pointLight = pointLights[ 4 ];
1521: getPointDirectLightIrradiance( pointLight, geometry, directLight );
1522: #if defined( USE_SHADOWMAP ) && ( 4 < 0 )
1523: pointLightShadow = pointLightShadows[ 4 ];
1524: directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getPointShadow( pointShadowMap[ 4 ], pointLightShadow.shadowMapSize, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ 4 ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;
1525: #endif
1526: RE_Direct( directLight, geometry, material, reflectedLight );
1527:
1528: pointLight = pointLights[ 5 ];
1529: getPointDirectLightIrradiance( pointLight, geometry, directLight );
1530: #if defined( USE_SHADOWMAP ) && ( 5 < 0 )
1531: pointLightShadow = pointLightShadows[ 5 ];
1532: directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getPointShadow( pointShadowMap[ 5 ], pointLightShadow.shadowMapSize, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ 5 ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;
1533: #endif
1534: RE_Direct( directLight, geometry, material, reflectedLight );
1535:
1536: pointLight = pointLights[ 6 ];
1537: getPointDirectLightIrradiance( pointLight, geometry, directLight );
1538: #if defined( USE_SHADOWMAP ) && ( 6 < 0 )
1539: pointLightShadow = pointLightShadows[ 6 ];
1540: directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getPointShadow( pointShadowMap[ 6 ], pointLightShadow.shadowMapSize, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ 6 ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;
1541: #endif
1542: RE_Direct( directLight, geometry, material, reflectedLight );
1543:
1544: pointLight = pointLights[ 7 ];
1545: getPointDirectLightIrradiance( pointLight, geometry, directLight );
1546: #if defined( USE_SHADOWMAP ) && ( 7 < 0 )
1547: pointLightShadow = pointLightShadows[ 7 ];
1548: directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getPointShadow( pointShadowMap[ 7 ], pointLightShadow.shadowMapSize, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ 7 ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;
1549: #endif
1550: RE_Direct( directLight, geometry, material, reflectedLight );
1551:
1552: pointLight = pointLights[ 8 ];
1553: getPointDirectLightIrradiance( pointLight, geometry, directLight );
1554: #if defined( USE_SHADOWMAP ) && ( 8 < 0 )
1555: pointLightShadow = pointLightShadows[ 8 ];
1556: directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getPointShadow( pointShadowMap[ 8 ], pointLightShadow.shadowMapSize, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ 8 ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;
1557: #endif
1558: RE_Direct( directLight, geometry, material, reflectedLight );
1559:
1560: pointLight = pointLights[ 9 ];
1561: getPointDirectLightIrradiance( pointLight, geometry, directLight );
1562: #if defined( USE_SHADOWMAP ) && ( 9 < 0 )
1563: pointLightShadow = pointLightShadows[ 9 ];
1564: directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getPointShadow( pointShadowMap[ 9 ], pointLightShadow.shadowMapSize, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ 9 ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;
1565: #endif
1566: RE_Direct( directLight, geometry, material, reflectedLight );
1567:
1568: pointLight = pointLights[ 10 ];
1569: getPointDirectLightIrradiance( pointLight, geometry, directLight );
1570: #if defined( USE_SHADOWMAP ) && ( 10 < 0 )
1571: pointLightShadow = pointLightShadows[ 10 ];
1572: directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getPointShadow( pointShadowMap[ 10 ], pointLightShadow.shadowMapSize, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ 10 ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;
1573: #endif
1574: RE_Direct( directLight, geometry, material, reflectedLight );
1575:
1576: pointLight = pointLights[ 11 ];
1577: getPointDirectLightIrradiance( pointLight, geometry, directLight );
1578: #if defined( USE_SHADOWMAP ) && ( 11 < 0 )
1579: pointLightShadow = pointLightShadows[ 11 ];
1580: directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getPointShadow( pointShadowMap[ 11 ], pointLightShadow.shadowMapSize, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ 11 ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;
1581: #endif
1582: RE_Direct( directLight, geometry, material, reflectedLight );
1583:
1584: pointLight = pointLights[ 12 ];
1585: getPointDirectLightIrradiance( pointLight, geometry, directLight );
1586: #if defined( USE_SHADOWMAP ) && ( 12 < 0 )
1587: pointLightShadow = pointLightShadows[ 12 ];
1588: directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getPointShadow( pointShadowMap[ 12 ], pointLightShadow.shadowMapSize, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ 12 ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;
1589: #endif
1590: RE_Direct( directLight, geometry, material, reflectedLight );
1591:
1592: pointLight = pointLights[ 13 ];
1593: getPointDirectLightIrradiance( pointLight, geometry, directLight );
1594: #if defined( USE_SHADOWMAP ) && ( 13 < 0 )
1595: pointLightShadow = pointLightShadows[ 13 ];
1596: directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getPointShadow( pointShadowMap[ 13 ], pointLightShadow.shadowMapSize, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ 13 ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;
1597: #endif
1598: RE_Direct( directLight, geometry, material, reflectedLight );
1599:
1600: pointLight = pointLights[ 14 ];
1601: getPointDirectLightIrradiance( pointLight, geometry, directLight );
1602: #if defined( USE_SHADOWMAP ) && ( 14 < 0 )
1603: pointLightShadow = pointLightShadows[ 14 ];
1604: directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getPointShadow( pointShadowMap[ 14 ], pointLightShadow.shadowMapSize, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ 14 ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;
1605: #endif
1606: RE_Direct( directLight, geometry, material, reflectedLight );
1607:
1608: pointLight = pointLights[ 15 ];
1609: getPointDirectLightIrradiance( pointLight, geometry, directLight );
1610: #if defined( USE_SHADOWMAP ) && ( 15 < 0 )
1611: pointLightShadow = pointLightShadows[ 15 ];
1612: directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getPointShadow( pointShadowMap[ 15 ], pointLightShadow.shadowMapSize, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ 15 ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;
1613: #endif
1614: RE_Direct( directLight, geometry, material, reflectedLight );
1615:
1616: pointLight = pointLights[ 16 ];
1617: getPointDirectLightIrradiance( pointLight, geometry, directLight );
1618: #if defined( USE_SHADOWMAP ) && ( 16 < 0 )
1619: pointLightShadow = pointLightShadows[ 16 ];
1620: directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getPointShadow( pointShadowMap[ 16 ], pointLightShadow.shadowMapSize, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ 16 ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;
1621: #endif
1622: RE_Direct( directLight, geometry, material, reflectedLight );
1623:
1624: pointLight = pointLights[ 17 ];
1625: getPointDirectLightIrradiance( pointLight, geometry, directLight );
1626: #if defined( USE_SHADOWMAP ) && ( 17 < 0 )
1627: pointLightShadow = pointLightShadows[ 17 ];
1628: directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getPointShadow( pointShadowMap[ 17 ], pointLightShadow.shadowMapSize, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ 17 ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;
1629: #endif
1630: RE_Direct( directLight, geometry, material, reflectedLight );
1631:
1632: #endif
1633: #if ( 6 > 0 ) && defined( RE_Direct )
1634: SpotLight spotLight;
1635: #if defined( USE_SHADOWMAP ) && 6 > 0
1636: SpotLightShadow spotLightShadow;
1637: #endif
1638:
1639: spotLight = spotLights[ 0 ];
1640: getSpotDirectLightIrradiance( spotLight, geometry, directLight );
1641: #if defined( USE_SHADOWMAP ) && ( 0 < 6 )
1642: spotLightShadow = spotLightShadows[ 0 ];
1643: directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( spotShadowMap[ 0 ], spotLightShadow.shadowMapSize, spotLightShadow.shadowBias, spotLightShadow.shadowRadius, vSpotShadowCoord[ 0 ] ) : 1.0;
1644: #endif
1645: RE_Direct( directLight, geometry, material, reflectedLight );
1646:
1647: spotLight = spotLights[ 1 ];
1648: getSpotDirectLightIrradiance( spotLight, geometry, directLight );
1649: #if defined( USE_SHADOWMAP ) && ( 1 < 6 )
1650: spotLightShadow = spotLightShadows[ 1 ];
1651: directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( spotShadowMap[ 1 ], spotLightShadow.shadowMapSize, spotLightShadow.shadowBias, spotLightShadow.shadowRadius, vSpotShadowCoord[ 1 ] ) : 1.0;
1652: #endif
1653: RE_Direct( directLight, geometry, material, reflectedLight );
1654:
1655: spotLight = spotLights[ 2 ];
1656: getSpotDirectLightIrradiance( spotLight, geometry, directLight );
1657: #if defined( USE_SHADOWMAP ) && ( 2 < 6 )
1658: spotLightShadow = spotLightShadows[ 2 ];
1659: directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( spotShadowMap[ 2 ], spotLightShadow.shadowMapSize, spotLightShadow.shadowBias, spotLightShadow.shadowRadius, vSpotShadowCoord[ 2 ] ) : 1.0;
1660: #endif
1661: RE_Direct( directLight, geometry, material, reflectedLight );
1662:
1663: spotLight = spotLights[ 3 ];
1664: getSpotDirectLightIrradiance( spotLight, geometry, directLight );
1665: #if defined( USE_SHADOWMAP ) && ( 3 < 6 )
1666: spotLightShadow = spotLightShadows[ 3 ];
1667: directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( spotShadowMap[ 3 ], spotLightShadow.shadowMapSize, spotLightShadow.shadowBias, spotLightShadow.shadowRadius, vSpotShadowCoord[ 3 ] ) : 1.0;
1668: #endif
1669: RE_Direct( directLight, geometry, material, reflectedLight );
1670:
1671: spotLight = spotLights[ 4 ];
1672: getSpotDirectLightIrradiance( spotLight, geometry, directLight );
1673: #if defined( USE_SHADOWMAP ) && ( 4 < 6 )
1674: spotLightShadow = spotLightShadows[ 4 ];
1675: directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( spotShadowMap[ 4 ], spotLightShadow.shadowMapSize, spotLightShadow.shadowBias, spotLightShadow.shadowRadius, vSpotShadowCoord[ 4 ] ) : 1.0;
1676: #endif
1677: RE_Direct( directLight, geometry, material, reflectedLight );
1678:
1679: spotLight = spotLights[ 5 ];
1680: getSpotDirectLightIrradiance( spotLight, geometry, directLight );
1681: #if defined( USE_SHADOWMAP ) && ( 5 < 6 )
1682: spotLightShadow = spotLightShadows[ 5 ];
1683: directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( spotShadowMap[ 5 ], spotLightShadow.shadowMapSize, spotLightShadow.shadowBias, spotLightShadow.shadowRadius, vSpotShadowCoord[ 5 ] ) : 1.0;
1684: #endif
1685: RE_Direct( directLight, geometry, material, reflectedLight );
1686:
1687: #endif
1688: #if ( 1 > 0 ) && defined( RE_Direct )
1689: DirectionalLight directionalLight;
1690: #if defined( USE_SHADOWMAP ) && 1 > 0
1691: DirectionalLightShadow directionalLightShadow;
1692: #endif
1693:
1694: directionalLight = directionalLights[ 0 ];
1695: getDirectionalDirectLightIrradiance( directionalLight, geometry, directLight );
1696: #if defined( USE_SHADOWMAP ) && ( 0 < 1 )
1697: directionalLightShadow = directionalLightShadows[ 0 ];
1698: directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( directionalShadowMap[ 0 ], directionalLightShadow.shadowMapSize, directionalLightShadow.shadowBias, directionalLightShadow.shadowRadius, vDirectionalShadowCoord[ 0 ] ) : 1.0;
1699: #endif
1700: RE_Direct( directLight, geometry, material, reflectedLight );
1701:
1702: #endif
1703: #if ( 0 > 0 ) && defined( RE_Direct_RectArea )
1704: RectAreaLight rectAreaLight;
1705:
1706: #endif
1707: #if defined( RE_IndirectDiffuse )
1708: vec3 iblIrradiance = vec3( 0.0 );
1709: vec3 irradiance = getAmbientLightIrradiance( ambientLightColor );
1710: irradiance += getLightProbeIrradiance( lightProbe, geometry );
1711: #if ( 0 > 0 )
1712:
1713: #endif
1714: #endif
1715: #if defined( RE_IndirectSpecular )
1716: vec3 radiance = vec3( 0.0 );
1717: vec3 clearcoatRadiance = vec3( 0.0 );
1718: #endif
1719: #if defined( RE_IndirectDiffuse )
1720: #ifdef USE_LIGHTMAP
1721: vec4 lightMapTexel= texture2D( lightMap, vUv2 );
1722: vec3 lightMapIrradiance = lightMapTexelToLinear( lightMapTexel ).rgb * lightMapIntensity;
1723: #ifndef PHYSICALLY_CORRECT_LIGHTS
1724: lightMapIrradiance *= PI;
1725: #endif
1726: irradiance += lightMapIrradiance;
1727: #endif
1728: #if defined( USE_ENVMAP ) && defined( STANDARD ) && defined( ENVMAP_TYPE_CUBE_UV )
1729: iblIrradiance += getLightProbeIndirectIrradiance( geometry, maxMipLevel );
1730: #endif
1731: #endif
1732: #if defined( USE_ENVMAP ) && defined( RE_IndirectSpecular )
1733: radiance += getLightProbeIndirectRadiance( geometry.viewDir, geometry.normal, material.specularRoughness, maxMipLevel );
1734: #ifdef CLEARCOAT
1735: clearcoatRadiance += getLightProbeIndirectRadiance( geometry.viewDir, geometry.clearcoatNormal, material.clearcoatRoughness, maxMipLevel );
1736: #endif
1737: #endif
1738: #if defined( RE_IndirectDiffuse )
1739: RE_IndirectDiffuse( irradiance, geometry, material, reflectedLight );
1740: #endif
1741: #if defined( RE_IndirectSpecular )
1742: RE_IndirectSpecular( radiance, iblIrradiance, clearcoatRadiance, geometry, material, reflectedLight );
1743: #endif
1744: #ifdef USE_AOMAP
1745: float ambientOcclusion = ( texture2D( aoMap, vUv2 ).r - 1.0 ) * aoMapIntensity + 1.0;
1746: reflectedLight.indirectDiffuse *= ambientOcclusion;
1747: #if defined( USE_ENVMAP ) && defined( STANDARD )
1748: float dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );
1749: reflectedLight.indirectSpecular *= computeSpecularOcclusion( dotNV, ambientOcclusion, material.specularRoughness );
1750: #endif
1751: #endif
1752: vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveRadiance;
1753: #ifdef TRANSMISSION
1754: diffuseColor.a *= mix( saturate( 1. - totalTransmission + linearToRelativeLuminance( reflectedLight.directSpecular + reflectedLight.indirectSpecular ) ), 1.0, metalness );
1755: #endif
1756: gl_FragColor = vec4( outgoingLight, diffuseColor.a );
1757: #if defined( TONE_MAPPING )
1758: gl_FragColor.rgb = toneMapping( gl_FragColor.rgb );
1759: #endif
1760: gl_FragColor = linearToOutputTexel( gl_FragColor );
1761: #ifdef USE_FOG
1762: #ifdef FOG_EXP2
1763: float fogFactor = 1.0 - exp( - fogDensity * fogDensity * fogDepth * fogDepth );
1764: #else
1765: float fogFactor = smoothstep( fogNear, fogFar, fogDepth );
1766: #endif
1767:
1768: float fogFactor2 = smoothstep( fogHeight, 0.0, vWorldPosition.y );
1769: float fogFactor3 = smoothstep( fogHeight, 0.0, cameraPosition.y );
1770:
1771: fogFactor = fogFactor * max(fogFactor2, fogFactor3);
1772:
1773: gl_FragColor.rgb = mix( gl_FragColor.rgb, fogColor, fogFactor );
1774:
1775: #endif
1776: #ifdef PREMULTIPLIED_ALPHA
1777: gl_FragColor.rgb *= gl_FragColor.a;
1778: #endif
1779: #ifdef DITHERING
1780: gl_FragColor.rgb = dithering( gl_FragColor.rgb );
1781: #endif
1782: }

the error is with this shader not the one i thought it was: (ill make a new thread as its a different issue)