Hi,
I've made a presentation for a client in Aframe. All is working well except on iOS devices iPhone 5S, 6, iPad mini 1 and the iPad Air 1. iOs is at the latest version (10.3.2).
The presentation is 86MB in total and contains a GLTF 1 model, and about 15 sky spheres. The offending device(s) will reload the presentation at random points in time as well as at random events.
While debugging iOS Safari the following appeared on the dev console on my Macbook when they where connected:
===========================================================
[Warning] Invalid timestamps detected. Time step between successive gyroscope sensor samples is very small or not monotonic (aframe-master.min.js, line 134)
[Debug] GLTFLoader: 3616.442ms (aframe-master.min.js, line 120)
[Warning] THREE.WebGLShader: gl.getShaderInfoLog() (3) (aframe-master.min.js, line 101)
"fragment"
"WARNING: 0:187: Overflow in implicit constant conversion, minimum range for lowp float is (-2,2)
"
"1: #extension GL_OES_standard_derivatives : enable
2: #extension GL_EXT_shader_texture_lod : enable
3: precision highp float;
4: precision highp int;
5: #define SHADER_NAME MeshStandardMaterial
6: #define STANDARD
7: #define GAMMA_FACTOR 2
8: #define USE_ENVMAP
9: #define ENVMAP_TYPE_SPHERE
10: #define ENVMAP_MODE_REFLECTION
11: #define ENVMAP_BLENDING_MULTIPLY
12: #define USE_NORMALMAP
13: #define NUM_CLIPPING_PLANES 0
14: #define UNION_CLIPPING_PLANES 0
15: #define TEXTURE_LOD_EXT
16: uniform mat4 viewMatrix;
17: uniform vec3 cameraPosition;
18: #define TONE_MAPPING
19: #define saturate(a) clamp( a, 0.0, 1.0 )
20: uniform float toneMappingExposure;
21: uniform float toneMappingWhitePoint;
22: vec3 LinearToneMapping( vec3 color ) {
23: return toneMappingExposure * color;
24: }
25: vec3 ReinhardToneMapping( vec3 color ) {
26: color = toneMappingExposure;
27: return saturate( color / ( vec3( 1.0 ) + color ) );
28: }
29: #define Uncharted2Helper( x ) max( ( ( x * ( 0.15 * x + 0.10 * 0.50 ) + 0.20 * 0.02 ) / ( x * ( 0.15 * x + 0.50 ) + 0.20 * 0.30 ) ) - 0.02 / 0.30, vec3( 0.0 ) )
30: vec3 Uncharted2ToneMapping( vec3 color ) {
31: color = toneMappingExposure;
32: return saturate( Uncharted2Helper( color ) / Uncharted2Helper( vec3( toneMappingWhitePoint ) ) );
33: }
34: vec3 OptimizedCineonToneMapping( vec3 color ) {
35: color = toneMappingExposure;
36: color = max( vec3( 0.0 ), color - 0.004 );
37: return pow( ( color * ( 6.2 * color + 0.5 ) ) / ( color * ( 6.2 * color + 1.7 ) + 0.06 ), vec3( 2.2 ) );
38: }
39:
40: vec3 toneMapping( vec3 color ) { return LinearToneMapping( color ); }
41:
42: vec4 LinearToLinear( in vec4 value ) {
43: return value;
44: }
45: vec4 GammaToLinear( in vec4 value, in float gammaFactor ) {
46: return vec4( pow( value.xyz, vec3( gammaFactor ) ), value.w );
47: }
48: vec4 LinearToGamma( in vec4 value, in float gammaFactor ) {
49: return vec4( pow( value.xyz, vec3( 1.0 / gammaFactor ) ), value.w );
50: }
51: vec4 sRGBToLinear( in vec4 value ) {
52: 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.w );
53: }
54: vec4 LinearTosRGB( in vec4 value ) {
55: 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.w );
56: }
57: vec4 RGBEToLinear( in vec4 value ) {
58: return vec4( value.rgb * exp2( value.a * 255.0 - 128.0 ), 1.0 );
59: }
60: vec4 LinearToRGBE( in vec4 value ) {
61: float maxComponent = max( max( value.r, value.g ), value.b );
62: float fExp = clamp( ceil( log2( maxComponent ) ), -128.0, 127.0 );
63: return vec4( value.rgb / exp2( fExp ), ( fExp + 128.0 ) / 255.0 );
64: }
65: vec4 RGBMToLinear( in vec4 value, in float maxRange ) {
66: return vec4( value.xyz * value.w * maxRange, 1.0 );
67: }
68: vec4 LinearToRGBM( in vec4 value, in float maxRange ) {
69: float maxRGB = max( value.x, max( value.g, value.b ) );
70: float M = clamp( maxRGB / maxRange, 0.0, 1.0 );
71: M = ceil( M * 255.0 ) / 255.0;
72: return vec4( value.rgb / ( M * maxRange ), M );
73: }
74: vec4 RGBDToLinear( in vec4 value, in float maxRange ) {
75: return vec4( value.rgb * ( ( maxRange / 255.0 ) / value.a ), 1.0 );
76: }
77: vec4 LinearToRGBD( in vec4 value, in float maxRange ) {
78: float maxRGB = max( value.x, max( value.g, value.b ) );
79: float D = max( maxRange / maxRGB, 1.0 );
80: D = min( floor( D ) / 255.0, 1.0 );
81: return vec4( value.rgb * ( D * ( 255.0 / maxRange ) ), D );
82: }
83: const mat3 cLogLuvM = mat3( 0.2209, 0.3390, 0.4184, 0.1138, 0.6780, 0.7319, 0.0102, 0.1130, 0.2969 );
84: vec4 LinearToLogLuv( in vec4 value ) {
85: vec3 Xp_Y_XYZp = value.rgb * cLogLuvM;
86: Xp_Y_XYZp = max(Xp_Y_XYZp, vec3(1e-6, 1e-6, 1e-6));
87: vec4 vResult;
88: vResult.xy = Xp_Y_XYZp.xy / Xp_Y_XYZp.z;
89: float Le = 2.0 * log2(Xp_Y_XYZp.y) + 127.0;
90: vResult.w = fract(Le);
91: vResult.z = (Le - (floor(vResult.w255.0))/255.0)/255.0;
92: return vResult;
93: }
94: const mat3 cLogLuvInverseM = mat3( 6.0014, -2.7008, -1.7996, -1.3320, 3.1029, -5.7721, 0.3008, -1.0882, 5.6268 );
95: vec4 LogLuvToLinear( in vec4 value ) {
96: float Le = value.z * 255.0 + value.w;
97: vec3 Xp_Y_XYZp;
98: Xp_Y_XYZp.y = exp2((Le - 127.0) / 2.0);
99: Xp_Y_XYZp.z = Xp_Y_XYZp.y / value.y;
100: Xp_Y_XYZp.x = value.x * Xp_Y_XYZp.z;
101: vec3 vRGB = Xp_Y_XYZp.rgb * cLogLuvInverseM;
102: return vec4( max(vRGB, 0.0), 1.0 );
103: }
104:
105: vec4 mapTexelToLinear( vec4 value ) { return LinearToLinear( value ); }
106: vec4 envMapTexelToLinear( vec4 value ) { return LinearToLinear( value ); }
107: vec4 emissiveMapTexelToLinear( vec4 value ) { return LinearToLinear( value ); }
108: vec4 linearToOutputTexel( vec4 value ) { return LinearToLinear( value ); }
109:
110: #define PHYSICAL
111: uniform vec3 diffuse;
112: uniform vec3 emissive;
113: uniform float roughness;
114: uniform float metalness;
115: uniform float opacity;
116: #ifndef STANDARD
117: uniform float clearCoat;
118: uniform float clearCoatRoughness;
119: #endif
120: varying vec3 vViewPosition;
121: #ifndef FLAT_SHADED
122: varying vec3 vNormal;
123: #endif
124: #define PI 3.14159265359
125: #define PI2 6.28318530718
126: #define PI_HALF 1.5707963267949
127: #define RECIPROCAL_PI 0.31830988618
128: #define RECIPROCAL_PI2 0.15915494
129: #define LOG2 1.442695
130: #define EPSILON 1e-6
131: #define saturate(a) clamp( a, 0.0, 1.0 )
132: #define whiteCompliment(a) ( 1.0 - saturate( a ) )
133: float pow2( const in float x ) { return xx; }
134: float pow3( const in float x ) { return xxx; }
135: float pow4( const in float x ) { float x2 = xx; return x2x2; }
136: float average( const in vec3 color ) { return dot( color, vec3( 0.3333 ) ); }
137: highp float rand( const in vec2 uv ) {
138: const highp float a = 12.9898, b = 78.233, c = 43758.5453;
139: highp float dt = dot( uv.xy, vec2( a,b ) ), sn = mod( dt, PI );
140: return fract(sin(sn) * c);
141: }
142: struct IncidentLight {
143: vec3 color;
144: vec3 direction;
145: bool visible;
146: };
147: struct ReflectedLight {
148: vec3 directDiffuse;
149: vec3 directSpecular;
150: vec3 indirectDiffuse;
151: vec3 indirectSpecular;
152: };
153: struct GeometricContext {
154: vec3 position;
155: vec3 normal;
156: vec3 viewDir;
157: };
158: vec3 transformDirection( in vec3 dir, in mat4 matrix ) {
159: return normalize( ( matrix * vec4( dir, 0.0 ) ).xyz );
160: }
161: vec3 inverseTransformDirection( in vec3 dir, in mat4 matrix ) {
162: return normalize( ( vec4( dir, 0.0 ) * matrix ).xyz );
163: }
164: vec3 projectOnPlane(in vec3 point, in vec3 pointOnPlane, in vec3 planeNormal ) {
165: float distance = dot( planeNormal, point - pointOnPlane );
166: return - distance * planeNormal + point;
167: }
168: float sideOfPlane( in vec3 point, in vec3 pointOnPlane, in vec3 planeNormal ) {
169: return sign( dot( point - pointOnPlane, planeNormal ) );
170: }
171: vec3 linePlaneIntersect( in vec3 pointOnLine, in vec3 lineDirection, in vec3 pointOnPlane, in vec3 planeNormal ) {
172: return lineDirection * ( dot( planeNormal, pointOnPlane - pointOnLine ) / dot( planeNormal, lineDirection ) ) + pointOnLine;
173: }
174: mat3 transpose( const in mat3 v ) {
175: mat3 tmp;
176: tmp[0] = vec3(v[0].x, v[1].x, v[2].x);
177: tmp[1] = vec3(v[0].y, v[1].y, v[2].y);
178: tmp[2] = vec3(v[0].z, v[1].z, v[2].z);
179: return tmp;
180: }
181:
182: vec3 packNormalToRGB( const in vec3 normal ) {
183: return normalize( normal ) * 0.5 + 0.5;
184: }
185: vec3 unpackRGBToNormal( const in vec3 rgb ) {
186: return 1.0 - 2.0 * rgb.xyz;
187: }
188: const float PackUpscale = 256. / 255.;const float UnpackDownscale = 255. / 256.;
189: const vec3 PackFactors = vec3( 256. * 256. * 256., 256. * 256., 256. );
190: const vec4 UnpackFactors = UnpackDownscale / vec4( PackFactors, 1. );
191: const float ShiftRight8 = 1. / 256.;
192: vec4 packDepthToRGBA( const in float v ) {
193: vec4 r = vec4( fract( v * PackFactors ), v );
194: r.yzw -= r.xyz * ShiftRight8; return r * PackUpscale;
195: }
196: float unpackRGBAToDepth( const in vec4 v ) {
197: return dot( v, UnpackFactors );
198: }
199: float viewZToOrthographicDepth( const in float viewZ, const in float near, const in float far ) {
200: return ( viewZ + near ) / ( near - far );
201: }
202: float orthographicDepthToViewZ( const in float linearClipZ, const in float near, const in float far ) {
203: return linearClipZ * ( near - far ) - near;
204: }
205: float viewZToPerspectiveDepth( const in float viewZ, const in float near, const in float far ) {
206: return (( near + viewZ ) * far ) / (( far - near ) * viewZ );
207: }
208: float perspectiveDepthToViewZ( const in float invClipZ, const in float near, const in float far ) {
209: return ( near * far ) / ( ( far - near ) * invClipZ - far );
210: }
211:
212: #ifdef USE_COLOR
213: varying vec3 vColor;
214: #endif
215:
216: #if defined( USE_MAP ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( USE_SPECULARMAP ) || defined( USE_ALPHAMAP ) || defined( USE_EMISSIVEMAP ) || defined( USE_ROUGHNESSMAP ) || defined( USE_METALNESSMAP )
217: varying vec2 vUv;
218: #endif
219: #if defined( USE_LIGHTMAP ) || defined( USE_AOMAP )
220: varying vec2 vUv2;
221: #endif
222: #ifdef USE_MAP
223: uniform sampler2D map;
224: #endif
225:
226: #ifdef USE_ALPHAMAP
227: uniform sampler2D alphaMap;
228: #endif
229:
230: #ifdef USE_AOMAP
231: uniform sampler2D aoMap;
232: uniform float aoMapIntensity;
233: #endif
234: #ifdef USE_LIGHTMAP
235: uniform sampler2D lightMap;
236: uniform float lightMapIntensity;
237: #endif
238: #ifdef USE_EMISSIVEMAP
239: uniform sampler2D emissiveMap;
240: #endif
241:
242: #if defined( USE_ENVMAP ) || defined( PHYSICAL )
243: uniform float reflectivity;
244: uniform float envMapIntensity;
245: #endif
246: #ifdef USE_ENVMAP
247: #if ! defined( PHYSICAL ) && ( defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( PHONG ) )
248: varying vec3 vWorldPosition;
249: #endif
250: #ifdef ENVMAP_TYPE_CUBE
251: uniform samplerCube envMap;
252: #else
253: uniform sampler2D envMap;
254: #endif
255: uniform float flipEnvMap;
256: #if defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( PHONG ) || defined( PHYSICAL )
257: uniform float refractionRatio;
258: #else
259: varying vec3 vReflect;
260: #endif
261: #endif
262:
263: #ifdef USE_FOG
264: uniform vec3 fogColor;
265: varying float fogDepth;
266: #ifdef FOG_EXP2
267: uniform float fogDensity;
268: #else
269: uniform float fogNear;
270: uniform float fogFar;
271: #endif
272: #endif
273:
274: float punctualLightIntensityToIrradianceFactor( const in float lightDistance, const in float cutoffDistance, const in float decayExponent ) {
275: if( decayExponent > 0.0 ) {
276: #if defined ( PHYSICALLY_CORRECT_LIGHTS )
277: float distanceFalloff = 1.0 / max( pow( lightDistance, decayExponent ), 0.01 );
278: float maxDistanceCutoffFactor = pow2( saturate( 1.0 - pow4( lightDistance / cutoffDistance ) ) );
279: return distanceFalloff * maxDistanceCutoffFactor;
280: #else
281: return pow( saturate( -lightDistance / cutoffDistance + 1.0 ), decayExponent );
282: #endif
283: }
284: return 1.0;
285: }
286: vec3 BRDF_Diffuse_Lambert( const in vec3 diffuseColor ) {
287: return RECIPROCAL_PI * diffuseColor;
288: }
289: vec3 F_Schlick( const in vec3 specularColor, const in float dotLH ) {
290: float fresnel = exp2( ( -5.55473 * dotLH - 6.98316 ) * dotLH );
291: return ( 1.0 - specularColor ) * fresnel + specularColor;
292: }
293: float G_GGX_Smith( const in float alpha, const in float dotNL, const in float dotNV ) {
294: float a2 = pow2( alpha );
295: float gl = dotNL + sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNL ) );
296: float gv = dotNV + sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNV ) );
297: return 1.0 / ( gl * gv );
298: }
299: float G_GGX_SmithCorrelated( const in float alpha, const in float dotNL, const in float dotNV ) {
300: float a2 = pow2( alpha );
301: float gv = dotNL * sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNV ) );
302: float gl = dotNV * sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNL ) );
303: return 0.5 / max( gv + gl, EPSILON );
304: }
305: float D_GGX( const in float alpha, const in float dotNH ) {
306: float a2 = pow2( alpha );
307: float denom = pow2( dotNH ) * ( a2 - 1.0 ) + 1.0;
308: return RECIPROCAL_PI * a2 / pow2( denom );
309: }
310: vec3 BRDF_Specular_GGX( const in IncidentLight incidentLight, const in GeometricContext geometry, const in vec3 specularColor, const in float roughness ) {
311: float alpha = pow2( roughness );
312: vec3 halfDir = normalize( incidentLight.direction + geometry.viewDir );
313: float dotNL = saturate( dot( geometry.normal, incidentLight.direction ) );
314: float dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );
315: float dotNH = saturate( dot( geometry.normal, halfDir ) );
316: float dotLH = saturate( dot( incidentLight.direction, halfDir ) );
317: vec3 F = F_Schlick( specularColor, dotLH );
318: float G = G_GGX_SmithCorrelated( alpha, dotNL, dotNV );
319: float D = D_GGX( alpha, dotNH );
320: return F * ( G * D );
321: }
322: vec2 ltcTextureCoords( const in GeometricContext geometry, const in float roughness ) {
323: const float LUT_SIZE = 64.0;
324: const float LUT_SCALE = (LUT_SIZE - 1.0)/LUT_SIZE;
325: const float LUT_BIAS = 0.5/LUT_SIZE;
326: vec3 N = geometry.normal;
327: vec3 V = geometry.viewDir;
328: vec3 P = geometry.position;
329: float theta = acos( dot( N, V ) );
330: vec2 uv = vec2(
331: sqrt( saturate( roughness ) ),
332: saturate( theta / ( 0.5 * PI ) ) );
333: uv = uv * LUT_SCALE + LUT_BIAS;
334: return uv;
335: }
336: void clipQuadToHorizon( inout vec3 L[5], out int n ) {
337: int config = 0;
338: if ( L[0].z > 0.0 ) config += 1;
339: if ( L[1].z > 0.0 ) config += 2;
340: if ( L[2].z > 0.0 ) config += 4;
341: if ( L[3].z > 0.0 ) config += 8;
342: n = 0;
343: if ( config == 0 ) {
344: } else if ( config == 1 ) {
345: n = 3;
346: L[1] = -L[1].z * L[0] + L[0].z * L[1];
347: L[2] = -L[3].z * L[0] + L[0].z * L[3];
348: } else if ( config == 2 ) {
349: n = 3;
350: L[0] = -L[0].z * L[1] + L[1].z * L[0];
351: L[2] = -L[2].z * L[1] + L[1].z * L[2];
352: } else if ( config == 3 ) {
353: n = 4;
354: L[2] = -L[2].z * L[1] + L[1].z * L[2];
355: L[3] = -L[3].z * L[0] + L[0].z * L[3];
356: } else if ( config == 4 ) {
357: n = 3;
358: L[0] = -L[3].z * L[2] + L[2].z * L[3];
359: L[1] = -L[1].z * L[2] + L[2].z * L[1];
360: } else if ( config == 5 ) {
361: n = 0;
362: } else if ( config == 6 ) {
363: n = 4;
364: L[0] = -L[0].z * L[1] + L[1].z * L[0];
365: L[3] = -L[3].z * L[2] + L[2].z * L[3];
366: } else if ( config == 7 ) {
367: n = 5;
368: L[4] = -L[3].z * L[0] + L[0].z * L[3];
369: L[3] = -L[3].z * L[2] + L[2].z * L[3];
370: } else if ( config == 8 ) {
371: n = 3;
372: L[0] = -L[0].z * L[3] + L[3].z * L[0];
373: L[1] = -L[2].z * L[3] + L[3].z * L[2];
374: L[2] = L[3];
375: } else if ( config == 9 ) {
376: n = 4;
377: L[1] = -L[1].z * L[0] + L[0].z * L[1];
378: L[2] = -L[2].z * L[3] + L[3].z * L[2];
379: } else if ( config == 10 ) {
380: n = 0;
381: } else if ( config == 11 ) {
382: n = 5;
383: L[4] = L[3];
384: L[3] = -L[2].z * L[3] + L[3].z * L[2];
385: L[2] = -L[2].z * L[1] + L[1].z * L[2];
386: } else if ( config == 12 ) {
387: n = 4;
388: L[1] = -L[1].z * L[2] + L[2].z * L[1];
389: L[0] = -L[0].z * L[3] + L[3].z * L[0];
390: } else if ( config == 13 ) {
391: n = 5;
392: L[4] = L[3];
393: L[3] = L[2];
394: L[2] = -L[1].z * L[2] + L[2].z * L[1];
395: L[1] = -L[1].z * L[0] + L[0].z * L[1];
396: } else if ( config == 14 ) {
397: n = 5;
398: L[4] = -L[0].z * L[3] + L[3].z * L[0];
399: L[0] = -L[0].z * L[1] + L[1].z * L[0];
400: } else if ( config == 15 ) {
401: n = 4;
402: }
403: if ( n == 3 )
404: L[3] = L[0];
405: if ( n == 4 )
406: L[4] = L[0];
407: }
408: float integrateLtcBrdfOverRectEdge( vec3 v1, vec3 v2 ) {
409: float cosTheta = dot( v1, v2 );
410: float theta = acos( cosTheta );
411: float res = cross( v1, v2 ).z * ( ( theta > 0.001 ) ? theta / sin( theta ) : 1.0 );
412: return res;
413: }
414: void initRectPoints( const in vec3 pos, const in vec3 halfWidth, const in vec3 halfHeight, out vec3 rectPoints[4] ) {
415: rectPoints[0] = pos - halfWidth - halfHeight;
416: rectPoints[1] = pos + halfWidth - halfHeight;
417: rectPoints[2] = pos + halfWidth + halfHeight;
418: rectPoints[3] = pos - halfWidth + halfHeight;
419: }
420: vec3 integrateLtcBrdfOverRect( const in GeometricContext geometry, const in mat3 brdfMat, const in vec3 rectPoints[4] ) {
421: vec3 N = geometry.normal;
422: vec3 V = geometry.viewDir;
423: vec3 P = geometry.position;
424: vec3 T1, T2;
425: T1 = normalize(V - N * dot( V, N ));
426: T2 = - cross( N, T1 );
427: mat3 brdfWrtSurface = brdfMat * transpose( mat3( T1, T2, N ) );
428: vec3 clippedRect[5];
429: clippedRect[0] = brdfWrtSurface * ( rectPoints[0] - P );
430: clippedRect[1] = brdfWrtSurface * ( rectPoints[1] - P );
431: clippedRect[2] = brdfWrtSurface * ( rectPoints[2] - P );
432: clippedRect[3] = brdfWrtSurface * ( rectPoints[3] - P );
433: int n;
434: clipQuadToHorizon(clippedRect, n);
435: if ( n == 0 )
436: return vec3( 0, 0, 0 );
437: clippedRect[0] = normalize( clippedRect[0] );
438: clippedRect[1] = normalize( clippedRect[1] );
439: clippedRect[2] = normalize( clippedRect[2] );
440: clippedRect[3] = normalize( clippedRect[3] );
441: clippedRect[4] = normalize( clippedRect[4] );
442: float sum = 0.0;
443: sum += integrateLtcBrdfOverRectEdge( clippedRect[0], clippedRect[1] );
444: sum += integrateLtcBrdfOverRectEdge( clippedRect[1], clippedRect[2] );
445: sum += integrateLtcBrdfOverRectEdge( clippedRect[2], clippedRect[3] );
446: if (n >= 4)
447: sum += integrateLtcBrdfOverRectEdge( clippedRect[3], clippedRect[4] );
448: if (n == 5)
449: sum += integrateLtcBrdfOverRectEdge( clippedRect[4], clippedRect[0] );
450: sum = max( 0.0, sum );
451: vec3 Lo_i = vec3( sum, sum, sum );
452: return Lo_i;
453: }
454: vec3 Rect_Area_Light_Specular_Reflectance(
455: const in GeometricContext geometry,
456: const in vec3 lightPos, const in vec3 lightHalfWidth, const in vec3 lightHalfHeight,
457: const in float roughness,
458: const in sampler2D ltcMat, const in sampler2D ltcMag ) {
459: vec3 rectPoints[4];
460: initRectPoints( lightPos, lightHalfWidth, lightHalfHeight, rectPoints );
461: vec2 uv = ltcTextureCoords( geometry, roughness );
462: vec4 brdfLtcApproxParams, t;
463: brdfLtcApproxParams = texture2D( ltcMat, uv );
464: t = texture2D( ltcMat, uv );
465: float brdfLtcScalar = texture2D( ltcMag, uv ).a;
466: mat3 brdfLtcApproxMat = mat3(
467: vec3( 1, 0, t.y ),
468: vec3( 0, t.z, 0 ),
469: vec3( t.w, 0, t.x )
470: );
471: vec3 specularReflectance = integrateLtcBrdfOverRect( geometry, brdfLtcApproxMat, rectPoints );
472: specularReflectance = brdfLtcScalar;
473: return specularReflectance;
474: }
475: vec3 Rect_Area_Light_Diffuse_Reflectance(
476: const in GeometricContext geometry,
477: const in vec3 lightPos, const in vec3 lightHalfWidth, const in vec3 lightHalfHeight ) {
478: vec3 rectPoints[4];
479: initRectPoints( lightPos, lightHalfWidth, lightHalfHeight, rectPoints );
480: mat3 diffuseBrdfMat = mat3(1);
481: vec3 diffuseReflectance = integrateLtcBrdfOverRect( geometry, diffuseBrdfMat, rectPoints );
482: return diffuseReflectance;
483: }
484: vec3 BRDF_Specular_GGX_Environment( const in GeometricContext geometry, const in vec3 specularColor, const in float roughness ) {
485: float dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );
486: const vec4 c0 = vec4( - 1, - 0.0275, - 0.572, 0.022 );
487: const vec4 c1 = vec4( 1, 0.0425, 1.04, - 0.04 );
488: vec4 r = roughness * c0 + c1;
489: float a004 = min( r.x * r.x, exp2( - 9.28 * dotNV ) ) * r.x + r.y;
490: vec2 AB = vec2( -1.04, 1.04 ) * a004 + r.zw;
491: return specularColor * AB.x + AB.y;
492: }
493: float G_BlinnPhong_Implicit( ) {
494: return 0.25;
495: }
496: float D_BlinnPhong( const in float shininess, const in float dotNH ) {
497: return RECIPROCAL_PI * ( shininess * 0.5 + 1.0 ) * pow( dotNH, shininess );
498: }
499: vec3 BRDF_Specular_BlinnPhong( const in IncidentLight incidentLight, const in GeometricContext geometry, const in vec3 specularColor, const in float shininess ) {
500: vec3 halfDir = normalize( incidentLight.direction + geometry.viewDir );
501: float dotNH = saturate( dot( geometry.normal, halfDir ) );
502: float dotLH = saturate( dot( incidentLight.direction, halfDir ) );
503: vec3 F = F_Schlick( specularColor, dotLH );
504: float G = G_BlinnPhong_Implicit( );
505: float D = D_BlinnPhong( shininess, dotNH );
506: return F * ( G * D );
507: }
508: float GGXRoughnessToBlinnExponent( const in float ggxRoughness ) {
509: return ( 2.0 / pow2( ggxRoughness + 0.0001 ) - 2.0 );
510: }
511: float BlinnExponentToGGXRoughness( const in float blinnExponent ) {
512: return sqrt( 2.0 / ( blinnExponent + 2.0 ) );
513: }
514:
515: #ifdef ENVMAP_TYPE_CUBE_UV
516: #define cubeUV_textureSize (1024.0)
517: int getFaceFromDirection(vec3 direction) {
518: vec3 absDirection = abs(direction);
519: int face = -1;
520: if( absDirection.x > absDirection.z ) {
521: if(absDirection.x > absDirection.y )
522: face = direction.x > 0.0 ? 0 : 3;
523: else
524: face = direction.y > 0.0 ? 1 : 4;
525: }
526: else {
527: if(absDirection.z > absDirection.y )
528: face = direction.z > 0.0 ? 2 : 5;
529: else
530: face = direction.y > 0.0 ? 1 : 4;
531: }
532: return face;
533: }
534: #define cubeUV_maxLods1 (log2(cubeUV_textureSize0.25) - 1.0)
535: #define cubeUV_rangeClamp (exp2((6.0 - 1.0) * 2.0))
536: vec2 MipLevelInfo( vec3 vec, float roughnessLevel, float roughness ) {
537: float scale = exp2(cubeUV_maxLods1 - roughnessLevel);
538: float dxRoughness = dFdx(roughness);
539: float dyRoughness = dFdy(roughness);
540: vec3 dx = dFdx( vec * scale * dxRoughness );
541: vec3 dy = dFdy( vec * scale * dyRoughness );
542: float d = max( dot( dx, dx ), dot( dy, dy ) );
543: d = clamp(d, 1.0, cubeUV_rangeClamp);
544: float mipLevel = 0.5 * log2(d);
545: return vec2(floor(mipLevel), fract(mipLevel));
546: }
547: #define cubeUV_maxLods2 (log2(cubeUV_textureSize0.25) - 2.0)
548: #define cubeUV_rcpTextureSize (1.0 / cubeUV_textureSize)
549: vec2 getCubeUV(vec3 direction, float roughnessLevel, float mipLevel) {
550: mipLevel = roughnessLevel > cubeUV_maxLods2 - 3.0 ? 0.0 : mipLevel;
551: float a = 16.0 * cubeUV_rcpTextureSize;
552: vec2 exp2_packed = exp2( vec2( roughnessLevel, mipLevel ) );
553: vec2 rcp_exp2_packed = vec2( 1.0 ) / exp2_packed;
554: float powScale = exp2_packed.x * exp2_packed.y;
555: float scale = rcp_exp2_packed.x * rcp_exp2_packed.y * 0.25;
556: float mipOffset = 0.75*(1.0 - rcp_exp2_packed.y) * rcp_exp2_packed.x;
557: bool bRes = mipLevel == 0.0;
558: scale = bRes && (scale < a) ? a : scale;
559: vec3 r;
560: vec2 offset;
561: int face = getFaceFromDirection(direction);
562: float rcpPowScale = 1.0 / powScale;
563: if( face == 0) {
564: r = vec3(direction.x, -direction.z, direction.y);
565: offset = vec2(0.0+mipOffset,0.75 * rcpPowScale);
566: offset.y = bRes && (offset.y < 2.0a) ? a : offset.y;
567: }
568: else if( face == 1) {
569: r = vec3(direction.y, direction.x, direction.z);
570: offset = vec2(scale+mipOffset, 0.75 * rcpPowScale);
571: offset.y = bRes && (offset.y < 2.0a) ? a : offset.y;
572: }
573: else if( face == 2) {
574: r = vec3(direction.z, direction.x, direction.y);
575: offset = vec2(2.0scale+mipOffset, 0.75 * rcpPowScale);
576: offset.y = bRes && (offset.y < 2.0a) ? a : offset.y;
577: }
578: else if( face == 3) {
579: r = vec3(direction.x, direction.z, direction.y);
580: offset = vec2(0.0+mipOffset,0.5 * rcpPowScale);
581: offset.y = bRes && (offset.y < 2.0a) ? 0.0 : offset.y;
582: }
583: else if( face == 4) {
584: r = vec3(direction.y, direction.x, -direction.z);
585: offset = vec2(scale+mipOffset, 0.5 * rcpPowScale);
586: offset.y = bRes && (offset.y < 2.0a) ? 0.0 : offset.y;
587: }
588: else {
589: r = vec3(direction.z, -direction.x, direction.y);
590: offset = vec2(2.0scale+mipOffset, 0.5 * rcpPowScale);
591: offset.y = bRes && (offset.y < 2.0a) ? 0.0 : offset.y;
592: }
593: r = normalize(r);
594: float texelOffset = 0.5 * cubeUV_rcpTextureSize;
595: vec2 s = ( r.yz / abs( r.x ) + vec2( 1.0 ) ) * 0.5;
596: vec2 base = offset + vec2( texelOffset );
597: return base + s * ( scale - 2.0 * texelOffset );
598: }
599: #define cubeUV_maxLods3 (log2(cubeUV_textureSize0.25) - 3.0)
600: vec4 textureCubeUV(vec3 reflectedDirection, float roughness ) {
601: float roughnessVal = roughness cubeUV_maxLods3;
602: float r1 = floor(roughnessVal);
603: float r2 = r1 + 1.0;
604: float t = fract(roughnessVal);
605: vec2 mipInfo = MipLevelInfo(reflectedDirection, r1, roughness);
606: float s = mipInfo.y;
607: float level0 = mipInfo.x;
608: float level1 = level0 + 1.0;
609: level1 = level1 > 5.0 ? 5.0 : level1;
610: level0 += min( floor( s + 0.5 ), 5.0 );
611: vec2 uv_10 = getCubeUV(reflectedDirection, r1, level0);
612: vec4 color10 = envMapTexelToLinear(texture2D(envMap, uv_10));
613: vec2 uv_20 = getCubeUV(reflectedDirection, r2, level0);
614: vec4 color20 = envMapTexelToLinear(texture2D(envMap, uv_20));
615: vec4 result = mix(color10, color20, t);
616: return vec4(result.rgb, 1.0);
617: }
618: #endif
619:
620: uniform vec3 ambientLightColor;
621: vec3 getAmbientLightIrradiance( const in vec3 ambientLightColor ) {
622: vec3 irradiance = ambientLightColor;
623: #ifndef PHYSICALLY_CORRECT_LIGHTS
624: irradiance *= PI;
625: #endif
626: return irradiance;
627: }
628: #if 1 > 0
629: struct DirectionalLight {
630: vec3 direction;
631: vec3 color;
632: int shadow;
633: float shadowBias;
634: float shadowRadius;
635: vec2 shadowMapSize;
636: };
637: uniform DirectionalLight directionalLights[ 1 ];
638: void getDirectionalDirectLightIrradiance( const in DirectionalLight directionalLight, const in GeometricContext geometry, out IncidentLight directLight ) {
639: directLight.color = directionalLight.color;
640: directLight.direction = directionalLight.direction;
641: directLight.visible = true;
642: }
643: #endif
644: #if 2 > 0
645: struct PointLight {
646: vec3 position;
647: vec3 color;
648: float distance;
649: float decay;
650: int shadow;
651: float shadowBias;
652: float shadowRadius;
653: vec2 shadowMapSize;
654: };
655: uniform PointLight pointLights[ 2 ];
656: void getPointDirectLightIrradiance( const in PointLight pointLight, const in GeometricContext geometry, out IncidentLight directLight ) {
657: vec3 lVector = pointLight.position - geometry.position;
658: directLight.direction = normalize( lVector );
659: float lightDistance = length( lVector );
660: directLight.color = pointLight.color;
661: directLight.color *= punctualLightIntensityToIrradianceFactor( lightDistance, pointLight.distance, pointLight.decay );
662: directLight.visible = ( directLight.color != vec3( 0.0 ) );
663: }
664: #endif
665: #if 0 > 0
666: struct SpotLight {
667: vec3 position;
668: vec3 direction;
669: vec3 color;
670: float distance;
671: float decay;
672: float coneCos;
673: float penumbraCos;
674: int shadow;
675: float shadowBias;
676: float shadowRadius;
677: vec2 shadowMapSize;
678: };
679: uniform SpotLight spotLights[ 0 ];
680: void getSpotDirectLightIrradiance( const in SpotLight spotLight, const in GeometricContext geometry, out IncidentLight directLight ) {
681: vec3 lVector = spotLight.position - geometry.position;
682: directLight.direction = normalize( lVector );
683: float lightDistance = length( lVector );
684: float angleCos = dot( directLight.direction, spotLight.direction );
685: if ( angleCos > spotLight.coneCos ) {
686: float spotEffect = smoothstep( spotLight.coneCos, spotLight.penumbraCos, angleCos );
687: directLight.color = spotLight.color;
688: directLight.color *= spotEffect * punctualLightIntensityToIrradianceFactor( lightDistance, spotLight.distance, spotLight.decay );
689: directLight.visible = true;
690: } else {
691: directLight.color = vec3( 0.0 );
692: directLight.visible = false;
693: }
694: }
695: #endif
696: #if 0 > 0
697: struct RectAreaLight {
698: vec3 color;
699: vec3 position;
700: vec3 halfWidth;
701: vec3 halfHeight;
702: };
703: uniform sampler2D ltcMat; uniform sampler2D ltcMag;
704: uniform RectAreaLight rectAreaLights[ 0 ];
705: #endif
706: #if 0 > 0
707: struct HemisphereLight {
708: vec3 direction;
709: vec3 skyColor;
710: vec3 groundColor;
711: };
712: uniform HemisphereLight hemisphereLights[ 0 ];
713: vec3 getHemisphereLightIrradiance( const in HemisphereLight hemiLight, const in GeometricContext geometry ) {
714: float dotNL = dot( geometry.normal, hemiLight.direction );
715: float hemiDiffuseWeight = 0.5 * dotNL + 0.5;
716: vec3 irradiance = mix( hemiLight.groundColor, hemiLight.skyColor, hemiDiffuseWeight );
717: #ifndef PHYSICALLY_CORRECT_LIGHTS
718: irradiance *= PI;
719: #endif
720: return irradiance;
721: }
722: #endif
723: #if defined( USE_ENVMAP ) && defined( PHYSICAL )
724: vec3 getLightProbeIndirectIrradiance( const in GeometricContext geometry, const in int maxMIPLevel ) {
725: vec3 worldNormal = inverseTransformDirection( geometry.normal, viewMatrix );
726: #ifdef ENVMAP_TYPE_CUBE
727: vec3 queryVec = vec3( flipEnvMap * worldNormal.x, worldNormal.yz );
728: #ifdef TEXTURE_LOD_EXT
729: vec4 envMapColor = textureCubeLodEXT( envMap, queryVec, float( maxMIPLevel ) );
730: #else
731: vec4 envMapColor = textureCube( envMap, queryVec, float( maxMIPLevel ) );
732: #endif
733: envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
734: #elif defined( ENVMAP_TYPE_CUBE_UV )
735: vec3 queryVec = vec3( flipEnvMap * worldNormal.x, worldNormal.yz );
736: vec4 envMapColor = textureCubeUV( queryVec, 1.0 );
737: #else
738: vec4 envMapColor = vec4( 0.0 );
739: #endif
740: return PI * envMapColor.rgb * envMapIntensity;
741: }
742: float getSpecularMIPLevel( const in float blinnShininessExponent, const in int maxMIPLevel ) {
743: float maxMIPLevelScalar = float( maxMIPLevel );
744: float desiredMIPLevel = maxMIPLevelScalar - 0.79248 - 0.5 * log2( pow2( blinnShininessExponent ) + 1.0 );
745: return clamp( desiredMIPLevel, 0.0, maxMIPLevelScalar );
746: }
747: vec3 getLightProbeIndirectRadiance( const in GeometricContext geometry, const in float blinnShininessExponent, const in int maxMIPLevel ) {
748: #ifdef ENVMAP_MODE_REFLECTION
749: vec3 reflectVec = reflect( -geometry.viewDir, geometry.normal );
750: #else
751: vec3 reflectVec = refract( -geometry.viewDir, geometry.normal, refractionRatio );
752: #endif
753: reflectVec = inverseTransformDirection( reflectVec, viewMatrix );
754: float specularMIPLevel = getSpecularMIPLevel( blinnShininessExponent, maxMIPLevel );
755: #ifdef ENVMAP_TYPE_CUBE
756: vec3 queryReflectVec = vec3( flipEnvMap * reflectVec.x, reflectVec.yz );
757: #ifdef TEXTURE_LOD_EXT
758: vec4 envMapColor = textureCubeLodEXT( envMap, queryReflectVec, specularMIPLevel );
759: #else
760: vec4 envMapColor = textureCube( envMap, queryReflectVec, specularMIPLevel );
761: #endif
762: envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
763: #elif defined( ENVMAP_TYPE_CUBE_UV )
764: vec3 queryReflectVec = vec3( flipEnvMap * reflectVec.x, reflectVec.yz );
765: vec4 envMapColor = textureCubeUV(queryReflectVec, BlinnExponentToGGXRoughness(blinnShininessExponent));
766: #elif defined( ENVMAP_TYPE_EQUIREC )
767: vec2 sampleUV;
768: sampleUV.y = saturate( reflectVec.y * 0.5 + 0.5 );
769: sampleUV.x = atan( reflectVec.z, reflectVec.x ) * RECIPROCAL_PI2 + 0.5;
770: #ifdef TEXTURE_LOD_EXT
771: vec4 envMapColor = texture2DLodEXT( envMap, sampleUV, specularMIPLevel );
772: #else
773: vec4 envMapColor = texture2D( envMap, sampleUV, specularMIPLevel );
774: #endif
775: envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
776: #elif defined( ENVMAP_TYPE_SPHERE )
777: vec3 reflectView = normalize( ( viewMatrix * vec4( reflectVec, 0.0 ) ).xyz + vec3( 0.0,0.0,1.0 ) );
778: #ifdef TEXTURE_LOD_EXT
779: vec4 envMapColor = texture2DLodEXT( envMap, reflectView.xy * 0.5 + 0.5, specularMIPLevel );
780: #else
781: vec4 envMapColor = texture2D( envMap, reflectView.xy * 0.5 + 0.5, specularMIPLevel );
782: #endif
783: envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
784: #endif
785: return envMapColor.rgb * envMapIntensity;
786: }
787: #endif
788:
789: struct PhysicalMaterial {
790: vec3 diffuseColor;
791: float specularRoughness;
792: vec3 specularColor;
793: #ifndef STANDARD
794: float clearCoat;
795: float clearCoatRoughness;
796: #endif
797: };
798: #define MAXIMUM_SPECULAR_COEFFICIENT 0.16
799: #define DEFAULT_SPECULAR_COEFFICIENT 0.04
800: float clearCoatDHRApprox( const in float roughness, const in float dotNL ) {
801: return DEFAULT_SPECULAR_COEFFICIENT + ( 1.0 - DEFAULT_SPECULAR_COEFFICIENT ) * ( pow( 1.0 - dotNL, 5.0 ) * pow( 1.0 - roughness, 2.0 ) );
802: }
803: #if 0 > 0
804: void RE_Direct_RectArea_Physical( const in RectAreaLight rectAreaLight, const in GeometricContext geometry, const in PhysicalMaterial material, inout ReflectedLight reflectedLight ) {
805: vec3 matDiffColor = material.diffuseColor;
806: vec3 matSpecColor = material.specularColor;
807: vec3 lightColor = rectAreaLight.color;
808: float roughness = material.specularRoughness;
809: vec3 spec = Rect_Area_Light_Specular_Reflectance(
810: geometry,
811: rectAreaLight.position, rectAreaLight.halfWidth, rectAreaLight.halfHeight,
812: roughness,
813: ltcMat, ltcMag );
814: vec3 diff = Rect_Area_Light_Diffuse_Reflectance(
815: geometry,
816: rectAreaLight.position, rectAreaLight.halfWidth, rectAreaLight.halfHeight );
817: reflectedLight.directSpecular += lightColor * matSpecColor * spec;
818: reflectedLight.directDiffuse += lightColor * matDiffColor * diff;
819: }
820: #endif
821: void RE_Direct_Physical( const in IncidentLight directLight, const in GeometricContext geometry, const in PhysicalMaterial material, inout ReflectedLight reflectedLight ) {
822: float dotNL = saturate( dot( geometry.normal, directLight.direction ) );
823: vec3 irradiance = dotNL * directLight.color;
824: #ifndef PHYSICALLY_CORRECT_LIGHTS
825: irradiance *= PI;
826: #endif
827: #ifndef STANDARD
828: float clearCoatDHR = material.clearCoat * clearCoatDHRApprox( material.clearCoatRoughness, dotNL );
829: #else
830: float clearCoatDHR = 0.0;
831: #endif
832: reflectedLight.directSpecular += ( 1.0 - clearCoatDHR ) * irradiance * BRDF_Specular_GGX( directLight, geometry, material.specularColor, material.specularRoughness );
833: reflectedLight.directDiffuse += ( 1.0 - clearCoatDHR ) * irradiance * BRDF_Diffuse_Lambert( material.diffuseColor );
834: #ifndef STANDARD
835: reflectedLight.directSpecular += irradiance * material.clearCoat * BRDF_Specular_GGX( directLight, geometry, vec3( DEFAULT_SPECULAR_COEFFICIENT ), material.clearCoatRoughness );
836: #endif
837: }
838: void RE_IndirectDiffuse_Physical( const in vec3 irradiance, const in GeometricContext geometry, const in PhysicalMaterial material, inout ReflectedLight reflectedLight ) {
839: reflectedLight.indirectDiffuse += irradiance * BRDF_Diffuse_Lambert( material.diffuseColor );
840: }
841: void RE_IndirectSpecular_Physical( const in vec3 radiance, const in vec3 clearCoatRadiance, const in GeometricContext geometry, const in PhysicalMaterial material, inout ReflectedLight reflectedLight ) {
842: #ifndef STANDARD
843: float dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );
844: float dotNL = dotNV;
845: float clearCoatDHR = material.clearCoat * clearCoatDHRApprox( material.clearCoatRoughness, dotNL );
846: #else
847: float clearCoatDHR = 0.0;
848: #endif
849: reflectedLight.indirectSpecular += ( 1.0 - clearCoatDHR ) * radiance * BRDF_Specular_GGX_Environment( geometry, material.specularColor, material.specularRoughness );
850: #ifndef STANDARD
851: reflectedLight.indirectSpecular += clearCoatRadiance * material.clearCoat * BRDF_Specular_GGX_Environment( geometry, vec3( DEFAULT_SPECULAR_COEFFICIENT ), material.clearCoatRoughness );
852: #endif
853: }
854: #define RE_Direct RE_Direct_Physical
855: #define RE_Direct_RectArea RE_Direct_RectArea_Physical
856: #define RE_IndirectDiffuse RE_IndirectDiffuse_Physical
857: #define RE_IndirectSpecular RE_IndirectSpecular_Physical
858: #define Material_BlinnShininessExponent( material ) GGXRoughnessToBlinnExponent( material.specularRoughness )
859: #define Material_ClearCoat_BlinnShininessExponent( material ) GGXRoughnessToBlinnExponent( material.clearCoatRoughness )
860: float computeSpecularOcclusion( const in float dotNV, const in float ambientOcclusion, const in float roughness ) {
861: return saturate( pow( dotNV + ambientOcclusion, exp2( - 16.0 * roughness - 1.0 ) ) - 1.0 + ambientOcclusion );
862: }
863:
864: #ifdef USE_SHADOWMAP
865: #if 1 > 0
866: uniform sampler2D directionalShadowMap[ 1 ];
867: varying vec4 vDirectionalShadowCoord[ 1 ];
868: #endif
869: #if 0 > 0
870: uniform sampler2D spotShadowMap[ 0 ];
871: varying vec4 vSpotShadowCoord[ 0 ];
872: #endif
873: #if 2 > 0
874: uniform sampler2D pointShadowMap[ 2 ];
875: varying vec4 vPointShadowCoord[ 2 ];
876: #endif
877: float texture2DCompare( sampler2D depths, vec2 uv, float compare ) {
878: return step( compare, unpackRGBAToDepth( texture2D( depths, uv ) ) );
879: }
880: float texture2DShadowLerp( sampler2D depths, vec2 size, vec2 uv, float compare ) {
881: const vec2 offset = vec2( 0.0, 1.0 );
882: vec2 texelSize = vec2( 1.0 ) / size;
883: vec2 centroidUV = floor( uv * size + 0.5 ) / size;
884: float lb = texture2DCompare( depths, centroidUV + texelSize * offset.xx, compare );
885: float lt = texture2DCompare( depths, centroidUV + texelSize * offset.xy, compare );
886: float rb = texture2DCompare( depths, centroidUV + texelSize * offset.yx, compare );
887: float rt = texture2DCompare( depths, centroidUV + texelSize * offset.yy, compare );
888: vec2 f = fract( uv * size + 0.5 );
889: float a = mix( lb, lt, f.y );
890: float b = mix( rb, rt, f.y );
891: float c = mix( a, b, f.x );
892: return c;
893: }
894: float getShadow( sampler2D shadowMap, vec2 shadowMapSize, float shadowBias, float shadowRadius, vec4 shadowCoord ) {
895: shadowCoord.xyz /= shadowCoord.w;
896: shadowCoord.z += shadowBias;
897: bvec4 inFrustumVec = bvec4 ( shadowCoord.x >= 0.0, shadowCoord.x <= 1.0, shadowCoord.y >= 0.0, shadowCoord.y <= 1.0 );
898: bool inFrustum = all( inFrustumVec );
899: bvec2 frustumTestVec = bvec2( inFrustum, shadowCoord.z <= 1.0 );
900: bool frustumTest = all( frustumTestVec );
901: if ( frustumTest ) {
902: #if defined( SHADOWMAP_TYPE_PCF )
903: vec2 texelSize = vec2( 1.0 ) / shadowMapSize;
904: float dx0 = - texelSize.x * shadowRadius;
905: float dy0 = - texelSize.y * shadowRadius;
906: float dx1 = + texelSize.x * shadowRadius;
907: float dy1 = + texelSize.y * shadowRadius;
908: return (
909: texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx0, dy0 ), shadowCoord.z ) +
910: texture2DCompare( shadowMap, shadowCoord.xy + vec2( 0.0, dy0 ), shadowCoord.z ) +
911: texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx1, dy0 ), shadowCoord.z ) +
912: texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx0, 0.0 ), shadowCoord.z ) +
913: texture2DCompare( shadowMap, shadowCoord.xy, shadowCoord.z ) +
914: texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx1, 0.0 ), shadowCoord.z ) +
915: texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx0, dy1 ), shadowCoord.z ) +
916: texture2DCompare( shadowMap, shadowCoord.xy + vec2( 0.0, dy1 ), shadowCoord.z ) +
917: texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx1, dy1 ), shadowCoord.z )
918: ) * ( 1.0 / 9.0 );
919: #elif defined( SHADOWMAP_TYPE_PCF_SOFT )
920: vec2 texelSize = vec2( 1.0 ) / shadowMapSize;
921: float dx0 = - texelSize.x * shadowRadius;
922: float dy0 = - texelSize.y * shadowRadius;
923: float dx1 = + texelSize.x * shadowRadius;
924: float dy1 = + texelSize.y * shadowRadius;
925: return (
926: texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy + vec2( dx0, dy0 ), shadowCoord.z ) +
927: texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy + vec2( 0.0, dy0 ), shadowCoord.z ) +
928: texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy + vec2( dx1, dy0 ), shadowCoord.z ) +
929: texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy + vec2( dx0, 0.0 ), shadowCoord.z ) +
930: texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy, shadowCoord.z ) +
931: texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy + vec2( dx1, 0.0 ), shadowCoord.z ) +
932: texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy + vec2( dx0, dy1 ), shadowCoord.z ) +
933: texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy + vec2( 0.0, dy1 ), shadowCoord.z ) +
934: texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy + vec2( dx1, dy1 ), shadowCoord.z )
935: ) * ( 1.0 / 9.0 );
936: #else
937: return texture2DCompare( shadowMap, shadowCoord.xy, shadowCoord.z );
938: #endif
939: }
940: return 1.0;
941: }
942: vec2 cubeToUV( vec3 v, float texelSizeY ) {
943: vec3 absV = abs( v );
944: float scaleToCube = 1.0 / max( absV.x, max( absV.y, absV.z ) );
945: absV *= scaleToCube;
946: v *= scaleToCube * ( 1.0 - 2.0 * texelSizeY );
947: vec2 planar = v.xy;
948: float almostATexel = 1.5 * texelSizeY;
949: float almostOne = 1.0 - almostATexel;
950: if ( absV.z >= almostOne ) {
951: if ( v.z > 0.0 )
952: planar.x = 4.0 - v.x;
953: } else if ( absV.x >= almostOne ) {
954: float signX = sign( v.x );
955: planar.x = v.z * signX + 2.0 * signX;
956: } else if ( absV.y >= almostOne ) {
957: float signY = sign( v.y );
958: planar.x = v.x + 2.0 * signY + 2.0;
959: planar.y = v.z * signY - 2.0;
960: }
961: return vec2( 0.125, 0.25 ) * planar + vec2( 0.375, 0.75 );
962: }
963: float getPointShadow( sampler2D shadowMap, vec2 shadowMapSize, float shadowBias, float shadowRadius, vec4 shadowCoord ) {
964: vec2 texelSize = vec2( 1.0 ) / ( shadowMapSize * vec2( 4.0, 2.0 ) );
965: vec3 lightToPosition = shadowCoord.xyz;
966: vec3 bd3D = normalize( lightToPosition );
967: float dp = ( length( lightToPosition ) - shadowBias ) / 1000.0;
968: #if defined( SHADOWMAP_TYPE_PCF ) || defined( SHADOWMAP_TYPE_PCF_SOFT )
969: vec2 offset = vec2( - 1, 1 ) * shadowRadius * texelSize.y;
970: return (
971: texture2DCompare( shadowMap, cubeToUV( bd3D + offset.xyy, texelSize.y ), dp ) +
972: texture2DCompare( shadowMap, cubeToUV( bd3D + offset.yyy, texelSize.y ), dp ) +
973: texture2DCompare( shadowMap, cubeToUV( bd3D + offset.xyx, texelSize.y ), dp ) +
974: texture2DCompare( shadowMap, cubeToUV( bd3D + offset.yyx, texelSize.y ), dp ) +
975: texture2DCompare( shadowMap, cubeToUV( bd3D, texelSize.y ), dp ) +
976: texture2DCompare( shadowMap, cubeToUV( bd3D + offset.xxy, texelSize.y ), dp ) +
977: texture2DCompare( shadowMap, cubeToUV( bd3D + offset.yxy, texelSize.y ), dp ) +
978: texture2DCompare( shadowMap, cubeToUV( bd3D + offset.xxx, texelSize.y ), dp ) +
979: texture2DCompare( shadowMap, cubeToUV( bd3D + offset.yxx, texelSize.y ), dp )
980: ) * ( 1.0 / 9.0 );
981: #else
982: return texture2DCompare( shadowMap, cubeToUV( bd3D, texelSize.y ), dp );
983: #endif
984: }
985: #endif
986:
987: #ifdef USE_BUMPMAP
988: uniform sampler2D bumpMap;
989: uniform float bumpScale;
990: vec2 dHdxy_fwd() {
991: vec2 dSTdx = dFdx( vUv );
992: vec2 dSTdy = dFdy( vUv );
993: float Hll = bumpScale * texture2D( bumpMap, vUv ).x;
994: float dBx = bumpScale * texture2D( bumpMap, vUv + dSTdx ).x - Hll;
995: float dBy = bumpScale * texture2D( bumpMap, vUv + dSTdy ).x - Hll;
996: return vec2( dBx, dBy );
997: }
998: vec3 perturbNormalArb( vec3 surf_pos, vec3 surf_norm, vec2 dHdxy ) {
999: vec3 vSigmaX = dFdx( surf_pos );
1000: vec3 vSigmaY = dFdy( surf_pos );
1001: vec3 vN = surf_norm;
1002: vec3 R1 = cross( vSigmaY, vN );
1003: vec3 R2 = cross( vN, vSigmaX );
1004: float fDet = dot( vSigmaX, R1 );
1005: vec3 vGrad = sign( fDet ) * ( dHdxy.x * R1 + dHdxy.y * R2 );
1006: return normalize( abs( fDet ) * surf_norm - vGrad );
1007: }
1008: #endif
1009:
1010: #ifdef USE_NORMALMAP
1011: uniform sampler2D normalMap;
1012: uniform vec2 normalScale;
1013: vec3 perturbNormal2Arb( vec3 eye_pos, vec3 surf_norm ) {
1014: vec3 q0 = dFdx( eye_pos.xyz );
1015: vec3 q1 = dFdy( eye_pos.xyz );
1016: vec2 st0 = dFdx( vUv.st );
1017: vec2 st1 = dFdy( vUv.st );
1018: vec3 S = normalize( q0 * st1.t - q1 * st0.t );
1019: vec3 T = normalize( -q0 * st1.s + q1 * st0.s );
1020: vec3 N = normalize( surf_norm );
1021: vec3 mapN = texture2D( normalMap, vUv ).xyz * 2.0 - 1.0;
1022: mapN.xy = normalScale * mapN.xy;
1023: mat3 tsn = mat3( S, T, N );
1024: return normalize( tsn * mapN );
1025: }
1026: #endif
1027:
1028: #ifdef USE_ROUGHNESSMAP
1029: uniform sampler2D roughnessMap;
1030: #endif
1031: #ifdef USE_METALNESSMAP
1032: uniform sampler2D metalnessMap;
1033: #endif
1034: #ifdef USE_LOGDEPTHBUF
1035: uniform float logDepthBufFC;
1036: #ifdef USE_LOGDEPTHBUF_EXT
1037: varying float vFragDepth;
1038: #endif
1039: #endif
1040:
1041: #if NUM_CLIPPING_PLANES > 0
1042: #if ! defined( PHYSICAL ) && ! defined( PHONG )
1043: varying vec3 vViewPosition;
1044: #endif
1045: uniform vec4 clippingPlanes[ NUM_CLIPPING_PLANES ];
1046: #endif
1047:
1048: void main() {
1049: #if NUM_CLIPPING_PLANES > 0
1050: for ( int i = 0; i < UNION_CLIPPING_PLANES; ++ i ) {
1051: vec4 plane = clippingPlanes[ i ];
1052: if ( dot( vViewPosition, plane.xyz ) > plane.w ) discard;
1053: }
1054:
1055: #if UNION_CLIPPING_PLANES < NUM_CLIPPING_PLANES
1056: bool clipped = true;
1057: for ( int i = UNION_CLIPPING_PLANES; i < NUM_CLIPPING_PLANES; ++ i ) {
1058: vec4 plane = clippingPlanes[ i ];
1059: clipped = ( dot( vViewPosition, plane.xyz ) > plane.w ) && clipped;
1060: }
1061: if ( clipped ) discard;
1062:
1063: #endif
1064: #endif
1065:
1066: vec4 diffuseColor = vec4( diffuse, opacity );
1067: ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );
1068: vec3 totalEmissiveRadiance = emissive;
1069: #if defined(USE_LOGDEPTHBUF) && defined(USE_LOGDEPTHBUF_EXT)
1070: gl_FragDepthEXT = log2(vFragDepth) * logDepthBufFC * 0.5;
1071: #endif
1072: #ifdef USE_MAP
1073: vec4 texelColor = texture2D( map, vUv );
1074: texelColor = mapTexelToLinear( texelColor );
1075: diffuseColor *= texelColor;
1076: #endif
1077:
1078: #ifdef USE_COLOR
1079: diffuseColor.rgb *= vColor;
1080: #endif
1081: #ifdef USE_ALPHAMAP
1082: diffuseColor.a *= texture2D( alphaMap, vUv ).g;
1083: #endif
1084:
1085: #ifdef ALPHATEST
1086: if ( diffuseColor.a < ALPHATEST ) discard;
1087: #endif
1088:
1089: float specularStrength;
1090: #ifdef USE_SPECULARMAP
1091: vec4 texelSpecular = texture2D( specularMap, vUv );
1092: specularStrength = texelSpecular.r;
1093: #else
1094: specularStrength = 1.0;
1095: #endif
1096: float roughnessFactor = roughness;
1097: #ifdef USE_ROUGHNESSMAP
1098: vec4 texelRoughness = texture2D( roughnessMap, vUv );
1099: roughnessFactor *= texelRoughness.r;
1100: #endif
1101:
1102: float metalnessFactor = metalness;
1103: #ifdef USE_METALNESSMAP
1104: vec4 texelMetalness = texture2D( metalnessMap, vUv );
1105: metalnessFactor *= texelMetalness.r;
1106: #endif
1107:
1108: #ifdef DOUBLE_SIDED
1109: float flipNormal = ( float( gl_FrontFacing ) * 2.0 - 1.0 );
1110: #else
1111: float flipNormal = 1.0;
1112: #endif
1113:
1114: #ifdef FLAT_SHADED
1115: vec3 fdx = vec3( dFdx( vViewPosition.x ), dFdx( vViewPosition.y ), dFdx( vViewPosition.z ) );
1116: vec3 fdy = vec3( dFdy( vViewPosition.x ), dFdy( vViewPosition.y ), dFdy( vViewPosition.z ) );
1117: vec3 normal = normalize( cross( fdx, fdy ) );
1118: #else
1119: vec3 normal = normalize( vNormal ) * flipNormal;
1120: #endif
1121: #ifdef USE_NORMALMAP
1122: normal = perturbNormal2Arb( -vViewPosition, normal );
1123: #elif defined( USE_BUMPMAP )
1124: normal = perturbNormalArb( -vViewPosition, normal, dHdxy_fwd() );
1125: #endif
1126:
1127: #ifdef USE_EMISSIVEMAP
1128: vec4 emissiveColor = texture2D( emissiveMap, vUv );
1129: emissiveColor.rgb = emissiveMapTexelToLinear( emissiveColor ).rgb;
1130: totalEmissiveRadiance *= emissiveColor.rgb;
1131: #endif
1132:
1133: PhysicalMaterial material;
1134: material.diffuseColor = diffuseColor.rgb * ( 1.0 - metalnessFactor );
1135: material.specularRoughness = clamp( roughnessFactor, 0.04, 1.0 );
1136: #ifdef STANDARD
1137: material.specularColor = mix( vec3( DEFAULT_SPECULAR_COEFFICIENT ), diffuseColor.rgb, metalnessFactor );
1138: #else
1139: material.specularColor = mix( vec3( MAXIMUM_SPECULAR_COEFFICIENT * pow2( reflectivity ) ), diffuseColor.rgb, metalnessFactor );
1140: material.clearCoat = saturate( clearCoat ); material.clearCoatRoughness = clamp( clearCoatRoughness, 0.04, 1.0 );
1141: #endif
1142:
1143:
1144: GeometricContext geometry;
1145: geometry.position = - vViewPosition;
1146: geometry.normal = normal;
1147: geometry.viewDir = normalize( vViewPosition );
1148: IncidentLight directLight;
1149: #if ( 2 > 0 ) && defined( RE_Direct )
1150: PointLight pointLight;
1151:
1152: pointLight = pointLights[ 0 ];
1153: getPointDirectLightIrradiance( pointLight, geometry, directLight );
1154: #ifdef USE_SHADOWMAP
1155: directLight.color *= all( bvec2( pointLight.shadow, directLight.visible ) ) ? getPointShadow( pointShadowMap[ 0 ], pointLight.shadowMapSize, pointLight.shadowBias, pointLight.shadowRadius, vPointShadowCoord[ 0 ] ) : 1.0;
1156: #endif
1157: RE_Direct( directLight, geometry, material, reflectedLight );
1158:
1159: pointLight = pointLights[ 1 ];
1160: getPointDirectLightIrradiance( pointLight, geometry, directLight );
1161: #ifdef USE_SHADOWMAP
1162: directLight.color *= all( bvec2( pointLight.shadow, directLight.visible ) ) ? getPointShadow( pointShadowMap[ 1 ], pointLight.shadowMapSize, pointLight.shadowBias, pointLight.shadowRadius, vPointShadowCoord[ 1 ] ) : 1.0;
1163: #endif
1164: RE_Direct( directLight, geometry, material, reflectedLight );
1165:
1166: #endif
1167: #if ( 0 > 0 ) && defined( RE_Direct )
1168: SpotLight spotLight;
1169:
1170: #endif
1171: #if ( 1 > 0 ) && defined( RE_Direct )
1172: DirectionalLight directionalLight;
1173:
1174: directionalLight = directionalLights[ 0 ];
1175: getDirectionalDirectLightIrradiance( directionalLight, geometry, directLight );
1176: #ifdef USE_SHADOWMAP
1177: directLight.color *= all( bvec2( directionalLight.shadow, directLight.visible ) ) ? getShadow( directionalShadowMap[ 0 ], directionalLight.shadowMapSize, directionalLight.shadowBias, directionalLight.shadowRadius, vDirectionalShadowCoord[ 0 ] ) : 1.0;
1178: #endif
1179: RE_Direct( directLight, geometry, material, reflectedLight );
1180:
1181: #endif
1182: #if ( 0 > 0 ) && defined( RE_Direct_RectArea )
1183: RectAreaLight rectAreaLight;
1184:
1185: #endif
1186: #if defined( RE_IndirectDiffuse )
1187: vec3 irradiance = getAmbientLightIrradiance( ambientLightColor );
1188: #ifdef USE_LIGHTMAP
1189: vec3 lightMapIrradiance = texture2D( lightMap, vUv2 ).xyz * lightMapIntensity;
1190: #ifndef PHYSICALLY_CORRECT_LIGHTS
1191: lightMapIrradiance *= PI;
1192: #endif
1193: irradiance += lightMapIrradiance;
1194: #endif
1195: #if ( 0 > 0 )
1196:
1197: #endif
1198: #if defined( USE_ENVMAP ) && defined( PHYSICAL ) && defined( ENVMAP_TYPE_CUBE_UV )
1199: irradiance += getLightProbeIndirectIrradiance( geometry, 8 );
1200: #endif
1201: RE_IndirectDiffuse( irradiance, geometry, material, reflectedLight );
1202: #endif
1203: #if defined( USE_ENVMAP ) && defined( RE_IndirectSpecular )
1204: vec3 radiance = getLightProbeIndirectRadiance( geometry, Material_BlinnShininessExponent( material ), 8 );
1205: #ifndef STANDARD
1206: vec3 clearCoatRadiance = getLightProbeIndirectRadiance( geometry, Material_ClearCoat_BlinnShininessExponent( material ), 8 );
1207: #else
1208: vec3 clearCoatRadiance = vec3( 0.0 );
1209: #endif
1210: RE_IndirectSpecular( radiance, clearCoatRadiance, geometry, material, reflectedLight );
1211: #endif
1212:
1213: #ifdef USE_AOMAP
1214: float ambientOcclusion = ( texture2D( aoMap, vUv2 ).r - 1.0 ) * aoMapIntensity + 1.0;
1215: reflectedLight.indirectDiffuse *= ambientOcclusion;
1216: #if defined( USE_ENVMAP ) && defined( PHYSICAL )
1217: float dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );
1218: reflectedLight.indirectSpecular *= computeSpecularOcclusion( dotNV, ambientOcclusion, material.specularRoughness );
1219: #endif
1220: #endif
1221:
1222: vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveRadiance;
1223: gl_FragColor = vec4( outgoingLight, diffuseColor.a );
1224: #ifdef PREMULTIPLIED_ALPHA
1225: gl_FragColor.rgb *= gl_FragColor.a;
1226: #endif
1227:
1228: #if defined( TONE_MAPPING )
1229: gl_FragColor.rgb = toneMapping( gl_FragColor.rgb );
1230: #endif
1231:
1232: gl_FragColor = linearToOutputTexel( gl_FragColor );
1233:
1234: #ifdef USE_FOG
1235: #ifdef FOG_EXP2
1236: float fogFactor = whiteCompliment( exp2( - fogDensity * fogDensity * fogDepth * fogDepth * LOG2 ) );
1237: #else
1238: float fogFactor = smoothstep( fogNear, fogFar, fogDepth );
1239: #endif
1240: gl_FragColor.rgb = mix( gl_FragColor.rgb, fogColor, fogFactor );
1241: #endif
1242:
1243: }
1244: "
[Warning] Invalid timestamps detected. Time step between successive gyroscope sensor samples is very small or not monotonic (aframe-master.min.js, line 134, x4)
=====================================================
I don't know if it is related or not.
- A-Frame Version: master
- Platform / Device: iPhone / iPad (except iPhone 7)
- Reproducible Code Snippet or URL:
Hi,
I've made a presentation for a client in Aframe. All is working well except on iOS devices iPhone 5S, 6, iPad mini 1 and the iPad Air 1. iOs is at the latest version (10.3.2).
The presentation is 86MB in total and contains a GLTF 1 model, and about 15 sky spheres. The offending device(s) will reload the presentation at random points in time as well as at random events.
While debugging iOS Safari the following appeared on the dev console on my Macbook when they where connected:
===========================================================
[Warning] Invalid timestamps detected. Time step between successive gyroscope sensor samples is very small or not monotonic (aframe-master.min.js, line 134)
[Debug] GLTFLoader: 3616.442ms (aframe-master.min.js, line 120)
[Warning] THREE.WebGLShader: gl.getShaderInfoLog() (3) (aframe-master.min.js, line 101)
"fragment"
"WARNING: 0:187: Overflow in implicit constant conversion, minimum range for lowp float is (-2,2)
"
"1: #extension GL_OES_standard_derivatives : enable
2: #extension GL_EXT_shader_texture_lod : enable
3: precision highp float;
4: precision highp int;
5: #define SHADER_NAME MeshStandardMaterial
6: #define STANDARD
7: #define GAMMA_FACTOR 2
8: #define USE_ENVMAP
9: #define ENVMAP_TYPE_SPHERE
10: #define ENVMAP_MODE_REFLECTION
11: #define ENVMAP_BLENDING_MULTIPLY
12: #define USE_NORMALMAP
13: #define NUM_CLIPPING_PLANES 0
14: #define UNION_CLIPPING_PLANES 0
15: #define TEXTURE_LOD_EXT
16: uniform mat4 viewMatrix;
17: uniform vec3 cameraPosition;
18: #define TONE_MAPPING
19: #define saturate(a) clamp( a, 0.0, 1.0 )
20: uniform float toneMappingExposure;
21: uniform float toneMappingWhitePoint;
22: vec3 LinearToneMapping( vec3 color ) {
23: return toneMappingExposure * color;
24: }
25: vec3 ReinhardToneMapping( vec3 color ) {
26: color = toneMappingExposure;
27: return saturate( color / ( vec3( 1.0 ) + color ) );
28: }
29: #define Uncharted2Helper( x ) max( ( ( x * ( 0.15 * x + 0.10 * 0.50 ) + 0.20 * 0.02 ) / ( x * ( 0.15 * x + 0.50 ) + 0.20 * 0.30 ) ) - 0.02 / 0.30, vec3( 0.0 ) )
30: vec3 Uncharted2ToneMapping( vec3 color ) {
31: color = toneMappingExposure;
32: return saturate( Uncharted2Helper( color ) / Uncharted2Helper( vec3( toneMappingWhitePoint ) ) );
33: }
34: vec3 OptimizedCineonToneMapping( vec3 color ) {
35: color = toneMappingExposure;
36: color = max( vec3( 0.0 ), color - 0.004 );
37: return pow( ( color * ( 6.2 * color + 0.5 ) ) / ( color * ( 6.2 * color + 1.7 ) + 0.06 ), vec3( 2.2 ) );
38: }
39:
40: vec3 toneMapping( vec3 color ) { return LinearToneMapping( color ); }
41:
42: vec4 LinearToLinear( in vec4 value ) {
43: return value;
44: }
45: vec4 GammaToLinear( in vec4 value, in float gammaFactor ) {
46: return vec4( pow( value.xyz, vec3( gammaFactor ) ), value.w );
47: }
48: vec4 LinearToGamma( in vec4 value, in float gammaFactor ) {
49: return vec4( pow( value.xyz, vec3( 1.0 / gammaFactor ) ), value.w );
50: }
51: vec4 sRGBToLinear( in vec4 value ) {
52: 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.w );
53: }
54: vec4 LinearTosRGB( in vec4 value ) {
55: 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.w );
56: }
57: vec4 RGBEToLinear( in vec4 value ) {
58: return vec4( value.rgb * exp2( value.a * 255.0 - 128.0 ), 1.0 );
59: }
60: vec4 LinearToRGBE( in vec4 value ) {
61: float maxComponent = max( max( value.r, value.g ), value.b );
62: float fExp = clamp( ceil( log2( maxComponent ) ), -128.0, 127.0 );
63: return vec4( value.rgb / exp2( fExp ), ( fExp + 128.0 ) / 255.0 );
64: }
65: vec4 RGBMToLinear( in vec4 value, in float maxRange ) {
66: return vec4( value.xyz * value.w * maxRange, 1.0 );
67: }
68: vec4 LinearToRGBM( in vec4 value, in float maxRange ) {
69: float maxRGB = max( value.x, max( value.g, value.b ) );
70: float M = clamp( maxRGB / maxRange, 0.0, 1.0 );
71: M = ceil( M * 255.0 ) / 255.0;
72: return vec4( value.rgb / ( M * maxRange ), M );
73: }
74: vec4 RGBDToLinear( in vec4 value, in float maxRange ) {
75: return vec4( value.rgb * ( ( maxRange / 255.0 ) / value.a ), 1.0 );
76: }
77: vec4 LinearToRGBD( in vec4 value, in float maxRange ) {
78: float maxRGB = max( value.x, max( value.g, value.b ) );
79: float D = max( maxRange / maxRGB, 1.0 );
80: D = min( floor( D ) / 255.0, 1.0 );
81: return vec4( value.rgb * ( D * ( 255.0 / maxRange ) ), D );
82: }
83: const mat3 cLogLuvM = mat3( 0.2209, 0.3390, 0.4184, 0.1138, 0.6780, 0.7319, 0.0102, 0.1130, 0.2969 );
84: vec4 LinearToLogLuv( in vec4 value ) {
85: vec3 Xp_Y_XYZp = value.rgb * cLogLuvM;
86: Xp_Y_XYZp = max(Xp_Y_XYZp, vec3(1e-6, 1e-6, 1e-6));
87: vec4 vResult;
88: vResult.xy = Xp_Y_XYZp.xy / Xp_Y_XYZp.z;
89: float Le = 2.0 * log2(Xp_Y_XYZp.y) + 127.0;
90: vResult.w = fract(Le);
91: vResult.z = (Le - (floor(vResult.w255.0))/255.0)/255.0;
92: return vResult;
93: }
94: const mat3 cLogLuvInverseM = mat3( 6.0014, -2.7008, -1.7996, -1.3320, 3.1029, -5.7721, 0.3008, -1.0882, 5.6268 );
95: vec4 LogLuvToLinear( in vec4 value ) {
96: float Le = value.z * 255.0 + value.w;
97: vec3 Xp_Y_XYZp;
98: Xp_Y_XYZp.y = exp2((Le - 127.0) / 2.0);
99: Xp_Y_XYZp.z = Xp_Y_XYZp.y / value.y;
100: Xp_Y_XYZp.x = value.x * Xp_Y_XYZp.z;
101: vec3 vRGB = Xp_Y_XYZp.rgb * cLogLuvInverseM;
102: return vec4( max(vRGB, 0.0), 1.0 );
103: }
104:
105: vec4 mapTexelToLinear( vec4 value ) { return LinearToLinear( value ); }
106: vec4 envMapTexelToLinear( vec4 value ) { return LinearToLinear( value ); }
107: vec4 emissiveMapTexelToLinear( vec4 value ) { return LinearToLinear( value ); }
108: vec4 linearToOutputTexel( vec4 value ) { return LinearToLinear( value ); }
109:
110: #define PHYSICAL
111: uniform vec3 diffuse;
112: uniform vec3 emissive;
113: uniform float roughness;
114: uniform float metalness;
115: uniform float opacity;
116: #ifndef STANDARD
117: uniform float clearCoat;
118: uniform float clearCoatRoughness;
119: #endif
120: varying vec3 vViewPosition;
121: #ifndef FLAT_SHADED
122: varying vec3 vNormal;
123: #endif
124: #define PI 3.14159265359
125: #define PI2 6.28318530718
126: #define PI_HALF 1.5707963267949
127: #define RECIPROCAL_PI 0.31830988618
128: #define RECIPROCAL_PI2 0.15915494
129: #define LOG2 1.442695
130: #define EPSILON 1e-6
131: #define saturate(a) clamp( a, 0.0, 1.0 )
132: #define whiteCompliment(a) ( 1.0 - saturate( a ) )
133: float pow2( const in float x ) { return xx; }
134: float pow3( const in float x ) { return xxx; }
135: float pow4( const in float x ) { float x2 = xx; return x2x2; }
136: float average( const in vec3 color ) { return dot( color, vec3( 0.3333 ) ); }
137: highp float rand( const in vec2 uv ) {
138: const highp float a = 12.9898, b = 78.233, c = 43758.5453;
139: highp float dt = dot( uv.xy, vec2( a,b ) ), sn = mod( dt, PI );
140: return fract(sin(sn) * c);
141: }
142: struct IncidentLight {
143: vec3 color;
144: vec3 direction;
145: bool visible;
146: };
147: struct ReflectedLight {
148: vec3 directDiffuse;
149: vec3 directSpecular;
150: vec3 indirectDiffuse;
151: vec3 indirectSpecular;
152: };
153: struct GeometricContext {
154: vec3 position;
155: vec3 normal;
156: vec3 viewDir;
157: };
158: vec3 transformDirection( in vec3 dir, in mat4 matrix ) {
159: return normalize( ( matrix * vec4( dir, 0.0 ) ).xyz );
160: }
161: vec3 inverseTransformDirection( in vec3 dir, in mat4 matrix ) {
162: return normalize( ( vec4( dir, 0.0 ) * matrix ).xyz );
163: }
164: vec3 projectOnPlane(in vec3 point, in vec3 pointOnPlane, in vec3 planeNormal ) {
165: float distance = dot( planeNormal, point - pointOnPlane );
166: return - distance * planeNormal + point;
167: }
168: float sideOfPlane( in vec3 point, in vec3 pointOnPlane, in vec3 planeNormal ) {
169: return sign( dot( point - pointOnPlane, planeNormal ) );
170: }
171: vec3 linePlaneIntersect( in vec3 pointOnLine, in vec3 lineDirection, in vec3 pointOnPlane, in vec3 planeNormal ) {
172: return lineDirection * ( dot( planeNormal, pointOnPlane - pointOnLine ) / dot( planeNormal, lineDirection ) ) + pointOnLine;
173: }
174: mat3 transpose( const in mat3 v ) {
175: mat3 tmp;
176: tmp[0] = vec3(v[0].x, v[1].x, v[2].x);
177: tmp[1] = vec3(v[0].y, v[1].y, v[2].y);
178: tmp[2] = vec3(v[0].z, v[1].z, v[2].z);
179: return tmp;
180: }
181:
182: vec3 packNormalToRGB( const in vec3 normal ) {
183: return normalize( normal ) * 0.5 + 0.5;
184: }
185: vec3 unpackRGBToNormal( const in vec3 rgb ) {
186: return 1.0 - 2.0 * rgb.xyz;
187: }
188: const float PackUpscale = 256. / 255.;const float UnpackDownscale = 255. / 256.;
189: const vec3 PackFactors = vec3( 256. * 256. * 256., 256. * 256., 256. );
190: const vec4 UnpackFactors = UnpackDownscale / vec4( PackFactors, 1. );
191: const float ShiftRight8 = 1. / 256.;
192: vec4 packDepthToRGBA( const in float v ) {
193: vec4 r = vec4( fract( v * PackFactors ), v );
194: r.yzw -= r.xyz * ShiftRight8; return r * PackUpscale;
195: }
196: float unpackRGBAToDepth( const in vec4 v ) {
197: return dot( v, UnpackFactors );
198: }
199: float viewZToOrthographicDepth( const in float viewZ, const in float near, const in float far ) {
200: return ( viewZ + near ) / ( near - far );
201: }
202: float orthographicDepthToViewZ( const in float linearClipZ, const in float near, const in float far ) {
203: return linearClipZ * ( near - far ) - near;
204: }
205: float viewZToPerspectiveDepth( const in float viewZ, const in float near, const in float far ) {
206: return (( near + viewZ ) * far ) / (( far - near ) * viewZ );
207: }
208: float perspectiveDepthToViewZ( const in float invClipZ, const in float near, const in float far ) {
209: return ( near * far ) / ( ( far - near ) * invClipZ - far );
210: }
211:
212: #ifdef USE_COLOR
213: varying vec3 vColor;
214: #endif
215:
216: #if defined( USE_MAP ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( USE_SPECULARMAP ) || defined( USE_ALPHAMAP ) || defined( USE_EMISSIVEMAP ) || defined( USE_ROUGHNESSMAP ) || defined( USE_METALNESSMAP )
217: varying vec2 vUv;
218: #endif
219: #if defined( USE_LIGHTMAP ) || defined( USE_AOMAP )
220: varying vec2 vUv2;
221: #endif
222: #ifdef USE_MAP
223: uniform sampler2D map;
224: #endif
225:
226: #ifdef USE_ALPHAMAP
227: uniform sampler2D alphaMap;
228: #endif
229:
230: #ifdef USE_AOMAP
231: uniform sampler2D aoMap;
232: uniform float aoMapIntensity;
233: #endif
234: #ifdef USE_LIGHTMAP
235: uniform sampler2D lightMap;
236: uniform float lightMapIntensity;
237: #endif
238: #ifdef USE_EMISSIVEMAP
239: uniform sampler2D emissiveMap;
240: #endif
241:
242: #if defined( USE_ENVMAP ) || defined( PHYSICAL )
243: uniform float reflectivity;
244: uniform float envMapIntensity;
245: #endif
246: #ifdef USE_ENVMAP
247: #if ! defined( PHYSICAL ) && ( defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( PHONG ) )
248: varying vec3 vWorldPosition;
249: #endif
250: #ifdef ENVMAP_TYPE_CUBE
251: uniform samplerCube envMap;
252: #else
253: uniform sampler2D envMap;
254: #endif
255: uniform float flipEnvMap;
256: #if defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( PHONG ) || defined( PHYSICAL )
257: uniform float refractionRatio;
258: #else
259: varying vec3 vReflect;
260: #endif
261: #endif
262:
263: #ifdef USE_FOG
264: uniform vec3 fogColor;
265: varying float fogDepth;
266: #ifdef FOG_EXP2
267: uniform float fogDensity;
268: #else
269: uniform float fogNear;
270: uniform float fogFar;
271: #endif
272: #endif
273:
274: float punctualLightIntensityToIrradianceFactor( const in float lightDistance, const in float cutoffDistance, const in float decayExponent ) {
275: if( decayExponent > 0.0 ) {
276: #if defined ( PHYSICALLY_CORRECT_LIGHTS )
277: float distanceFalloff = 1.0 / max( pow( lightDistance, decayExponent ), 0.01 );
278: float maxDistanceCutoffFactor = pow2( saturate( 1.0 - pow4( lightDistance / cutoffDistance ) ) );
279: return distanceFalloff * maxDistanceCutoffFactor;
280: #else
281: return pow( saturate( -lightDistance / cutoffDistance + 1.0 ), decayExponent );
282: #endif
283: }
284: return 1.0;
285: }
286: vec3 BRDF_Diffuse_Lambert( const in vec3 diffuseColor ) {
287: return RECIPROCAL_PI * diffuseColor;
288: }
289: vec3 F_Schlick( const in vec3 specularColor, const in float dotLH ) {
290: float fresnel = exp2( ( -5.55473 * dotLH - 6.98316 ) * dotLH );
291: return ( 1.0 - specularColor ) * fresnel + specularColor;
292: }
293: float G_GGX_Smith( const in float alpha, const in float dotNL, const in float dotNV ) {
294: float a2 = pow2( alpha );
295: float gl = dotNL + sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNL ) );
296: float gv = dotNV + sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNV ) );
297: return 1.0 / ( gl * gv );
298: }
299: float G_GGX_SmithCorrelated( const in float alpha, const in float dotNL, const in float dotNV ) {
300: float a2 = pow2( alpha );
301: float gv = dotNL * sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNV ) );
302: float gl = dotNV * sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNL ) );
303: return 0.5 / max( gv + gl, EPSILON );
304: }
305: float D_GGX( const in float alpha, const in float dotNH ) {
306: float a2 = pow2( alpha );
307: float denom = pow2( dotNH ) * ( a2 - 1.0 ) + 1.0;
308: return RECIPROCAL_PI * a2 / pow2( denom );
309: }
310: vec3 BRDF_Specular_GGX( const in IncidentLight incidentLight, const in GeometricContext geometry, const in vec3 specularColor, const in float roughness ) {
311: float alpha = pow2( roughness );
312: vec3 halfDir = normalize( incidentLight.direction + geometry.viewDir );
313: float dotNL = saturate( dot( geometry.normal, incidentLight.direction ) );
314: float dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );
315: float dotNH = saturate( dot( geometry.normal, halfDir ) );
316: float dotLH = saturate( dot( incidentLight.direction, halfDir ) );
317: vec3 F = F_Schlick( specularColor, dotLH );
318: float G = G_GGX_SmithCorrelated( alpha, dotNL, dotNV );
319: float D = D_GGX( alpha, dotNH );
320: return F * ( G * D );
321: }
322: vec2 ltcTextureCoords( const in GeometricContext geometry, const in float roughness ) {
323: const float LUT_SIZE = 64.0;
324: const float LUT_SCALE = (LUT_SIZE - 1.0)/LUT_SIZE;
325: const float LUT_BIAS = 0.5/LUT_SIZE;
326: vec3 N = geometry.normal;
327: vec3 V = geometry.viewDir;
328: vec3 P = geometry.position;
329: float theta = acos( dot( N, V ) );
330: vec2 uv = vec2(
331: sqrt( saturate( roughness ) ),
332: saturate( theta / ( 0.5 * PI ) ) );
333: uv = uv * LUT_SCALE + LUT_BIAS;
334: return uv;
335: }
336: void clipQuadToHorizon( inout vec3 L[5], out int n ) {
337: int config = 0;
338: if ( L[0].z > 0.0 ) config += 1;
339: if ( L[1].z > 0.0 ) config += 2;
340: if ( L[2].z > 0.0 ) config += 4;
341: if ( L[3].z > 0.0 ) config += 8;
342: n = 0;
343: if ( config == 0 ) {
344: } else if ( config == 1 ) {
345: n = 3;
346: L[1] = -L[1].z * L[0] + L[0].z * L[1];
347: L[2] = -L[3].z * L[0] + L[0].z * L[3];
348: } else if ( config == 2 ) {
349: n = 3;
350: L[0] = -L[0].z * L[1] + L[1].z * L[0];
351: L[2] = -L[2].z * L[1] + L[1].z * L[2];
352: } else if ( config == 3 ) {
353: n = 4;
354: L[2] = -L[2].z * L[1] + L[1].z * L[2];
355: L[3] = -L[3].z * L[0] + L[0].z * L[3];
356: } else if ( config == 4 ) {
357: n = 3;
358: L[0] = -L[3].z * L[2] + L[2].z * L[3];
359: L[1] = -L[1].z * L[2] + L[2].z * L[1];
360: } else if ( config == 5 ) {
361: n = 0;
362: } else if ( config == 6 ) {
363: n = 4;
364: L[0] = -L[0].z * L[1] + L[1].z * L[0];
365: L[3] = -L[3].z * L[2] + L[2].z * L[3];
366: } else if ( config == 7 ) {
367: n = 5;
368: L[4] = -L[3].z * L[0] + L[0].z * L[3];
369: L[3] = -L[3].z * L[2] + L[2].z * L[3];
370: } else if ( config == 8 ) {
371: n = 3;
372: L[0] = -L[0].z * L[3] + L[3].z * L[0];
373: L[1] = -L[2].z * L[3] + L[3].z * L[2];
374: L[2] = L[3];
375: } else if ( config == 9 ) {
376: n = 4;
377: L[1] = -L[1].z * L[0] + L[0].z * L[1];
378: L[2] = -L[2].z * L[3] + L[3].z * L[2];
379: } else if ( config == 10 ) {
380: n = 0;
381: } else if ( config == 11 ) {
382: n = 5;
383: L[4] = L[3];
384: L[3] = -L[2].z * L[3] + L[3].z * L[2];
385: L[2] = -L[2].z * L[1] + L[1].z * L[2];
386: } else if ( config == 12 ) {
387: n = 4;
388: L[1] = -L[1].z * L[2] + L[2].z * L[1];
389: L[0] = -L[0].z * L[3] + L[3].z * L[0];
390: } else if ( config == 13 ) {
391: n = 5;
392: L[4] = L[3];
393: L[3] = L[2];
394: L[2] = -L[1].z * L[2] + L[2].z * L[1];
395: L[1] = -L[1].z * L[0] + L[0].z * L[1];
396: } else if ( config == 14 ) {
397: n = 5;
398: L[4] = -L[0].z * L[3] + L[3].z * L[0];
399: L[0] = -L[0].z * L[1] + L[1].z * L[0];
400: } else if ( config == 15 ) {
401: n = 4;
402: }
403: if ( n == 3 )
404: L[3] = L[0];
405: if ( n == 4 )
406: L[4] = L[0];
407: }
408: float integrateLtcBrdfOverRectEdge( vec3 v1, vec3 v2 ) {
409: float cosTheta = dot( v1, v2 );
410: float theta = acos( cosTheta );
411: float res = cross( v1, v2 ).z * ( ( theta > 0.001 ) ? theta / sin( theta ) : 1.0 );
412: return res;
413: }
414: void initRectPoints( const in vec3 pos, const in vec3 halfWidth, const in vec3 halfHeight, out vec3 rectPoints[4] ) {
415: rectPoints[0] = pos - halfWidth - halfHeight;
416: rectPoints[1] = pos + halfWidth - halfHeight;
417: rectPoints[2] = pos + halfWidth + halfHeight;
418: rectPoints[3] = pos - halfWidth + halfHeight;
419: }
420: vec3 integrateLtcBrdfOverRect( const in GeometricContext geometry, const in mat3 brdfMat, const in vec3 rectPoints[4] ) {
421: vec3 N = geometry.normal;
422: vec3 V = geometry.viewDir;
423: vec3 P = geometry.position;
424: vec3 T1, T2;
425: T1 = normalize(V - N * dot( V, N ));
426: T2 = - cross( N, T1 );
427: mat3 brdfWrtSurface = brdfMat * transpose( mat3( T1, T2, N ) );
428: vec3 clippedRect[5];
429: clippedRect[0] = brdfWrtSurface * ( rectPoints[0] - P );
430: clippedRect[1] = brdfWrtSurface * ( rectPoints[1] - P );
431: clippedRect[2] = brdfWrtSurface * ( rectPoints[2] - P );
432: clippedRect[3] = brdfWrtSurface * ( rectPoints[3] - P );
433: int n;
434: clipQuadToHorizon(clippedRect, n);
435: if ( n == 0 )
436: return vec3( 0, 0, 0 );
437: clippedRect[0] = normalize( clippedRect[0] );
438: clippedRect[1] = normalize( clippedRect[1] );
439: clippedRect[2] = normalize( clippedRect[2] );
440: clippedRect[3] = normalize( clippedRect[3] );
441: clippedRect[4] = normalize( clippedRect[4] );
442: float sum = 0.0;
443: sum += integrateLtcBrdfOverRectEdge( clippedRect[0], clippedRect[1] );
444: sum += integrateLtcBrdfOverRectEdge( clippedRect[1], clippedRect[2] );
445: sum += integrateLtcBrdfOverRectEdge( clippedRect[2], clippedRect[3] );
446: if (n >= 4)
447: sum += integrateLtcBrdfOverRectEdge( clippedRect[3], clippedRect[4] );
448: if (n == 5)
449: sum += integrateLtcBrdfOverRectEdge( clippedRect[4], clippedRect[0] );
450: sum = max( 0.0, sum );
451: vec3 Lo_i = vec3( sum, sum, sum );
452: return Lo_i;
453: }
454: vec3 Rect_Area_Light_Specular_Reflectance(
455: const in GeometricContext geometry,
456: const in vec3 lightPos, const in vec3 lightHalfWidth, const in vec3 lightHalfHeight,
457: const in float roughness,
458: const in sampler2D ltcMat, const in sampler2D ltcMag ) {
459: vec3 rectPoints[4];
460: initRectPoints( lightPos, lightHalfWidth, lightHalfHeight, rectPoints );
461: vec2 uv = ltcTextureCoords( geometry, roughness );
462: vec4 brdfLtcApproxParams, t;
463: brdfLtcApproxParams = texture2D( ltcMat, uv );
464: t = texture2D( ltcMat, uv );
465: float brdfLtcScalar = texture2D( ltcMag, uv ).a;
466: mat3 brdfLtcApproxMat = mat3(
467: vec3( 1, 0, t.y ),
468: vec3( 0, t.z, 0 ),
469: vec3( t.w, 0, t.x )
470: );
471: vec3 specularReflectance = integrateLtcBrdfOverRect( geometry, brdfLtcApproxMat, rectPoints );
472: specularReflectance = brdfLtcScalar;
473: return specularReflectance;
474: }
475: vec3 Rect_Area_Light_Diffuse_Reflectance(
476: const in GeometricContext geometry,
477: const in vec3 lightPos, const in vec3 lightHalfWidth, const in vec3 lightHalfHeight ) {
478: vec3 rectPoints[4];
479: initRectPoints( lightPos, lightHalfWidth, lightHalfHeight, rectPoints );
480: mat3 diffuseBrdfMat = mat3(1);
481: vec3 diffuseReflectance = integrateLtcBrdfOverRect( geometry, diffuseBrdfMat, rectPoints );
482: return diffuseReflectance;
483: }
484: vec3 BRDF_Specular_GGX_Environment( const in GeometricContext geometry, const in vec3 specularColor, const in float roughness ) {
485: float dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );
486: const vec4 c0 = vec4( - 1, - 0.0275, - 0.572, 0.022 );
487: const vec4 c1 = vec4( 1, 0.0425, 1.04, - 0.04 );
488: vec4 r = roughness * c0 + c1;
489: float a004 = min( r.x * r.x, exp2( - 9.28 * dotNV ) ) * r.x + r.y;
490: vec2 AB = vec2( -1.04, 1.04 ) * a004 + r.zw;
491: return specularColor * AB.x + AB.y;
492: }
493: float G_BlinnPhong_Implicit( ) {
494: return 0.25;
495: }
496: float D_BlinnPhong( const in float shininess, const in float dotNH ) {
497: return RECIPROCAL_PI * ( shininess * 0.5 + 1.0 ) * pow( dotNH, shininess );
498: }
499: vec3 BRDF_Specular_BlinnPhong( const in IncidentLight incidentLight, const in GeometricContext geometry, const in vec3 specularColor, const in float shininess ) {
500: vec3 halfDir = normalize( incidentLight.direction + geometry.viewDir );
501: float dotNH = saturate( dot( geometry.normal, halfDir ) );
502: float dotLH = saturate( dot( incidentLight.direction, halfDir ) );
503: vec3 F = F_Schlick( specularColor, dotLH );
504: float G = G_BlinnPhong_Implicit( );
505: float D = D_BlinnPhong( shininess, dotNH );
506: return F * ( G * D );
507: }
508: float GGXRoughnessToBlinnExponent( const in float ggxRoughness ) {
509: return ( 2.0 / pow2( ggxRoughness + 0.0001 ) - 2.0 );
510: }
511: float BlinnExponentToGGXRoughness( const in float blinnExponent ) {
512: return sqrt( 2.0 / ( blinnExponent + 2.0 ) );
513: }
514:
515: #ifdef ENVMAP_TYPE_CUBE_UV
516: #define cubeUV_textureSize (1024.0)
517: int getFaceFromDirection(vec3 direction) {
518: vec3 absDirection = abs(direction);
519: int face = -1;
520: if( absDirection.x > absDirection.z ) {
521: if(absDirection.x > absDirection.y )
522: face = direction.x > 0.0 ? 0 : 3;
523: else
524: face = direction.y > 0.0 ? 1 : 4;
525: }
526: else {
527: if(absDirection.z > absDirection.y )
528: face = direction.z > 0.0 ? 2 : 5;
529: else
530: face = direction.y > 0.0 ? 1 : 4;
531: }
532: return face;
533: }
534: #define cubeUV_maxLods1 (log2(cubeUV_textureSize0.25) - 1.0)
535: #define cubeUV_rangeClamp (exp2((6.0 - 1.0) * 2.0))
536: vec2 MipLevelInfo( vec3 vec, float roughnessLevel, float roughness ) {
537: float scale = exp2(cubeUV_maxLods1 - roughnessLevel);
538: float dxRoughness = dFdx(roughness);
539: float dyRoughness = dFdy(roughness);
540: vec3 dx = dFdx( vec * scale * dxRoughness );
541: vec3 dy = dFdy( vec * scale * dyRoughness );
542: float d = max( dot( dx, dx ), dot( dy, dy ) );
543: d = clamp(d, 1.0, cubeUV_rangeClamp);
544: float mipLevel = 0.5 * log2(d);
545: return vec2(floor(mipLevel), fract(mipLevel));
546: }
547: #define cubeUV_maxLods2 (log2(cubeUV_textureSize0.25) - 2.0)
548: #define cubeUV_rcpTextureSize (1.0 / cubeUV_textureSize)
549: vec2 getCubeUV(vec3 direction, float roughnessLevel, float mipLevel) {
550: mipLevel = roughnessLevel > cubeUV_maxLods2 - 3.0 ? 0.0 : mipLevel;
551: float a = 16.0 * cubeUV_rcpTextureSize;
552: vec2 exp2_packed = exp2( vec2( roughnessLevel, mipLevel ) );
553: vec2 rcp_exp2_packed = vec2( 1.0 ) / exp2_packed;
554: float powScale = exp2_packed.x * exp2_packed.y;
555: float scale = rcp_exp2_packed.x * rcp_exp2_packed.y * 0.25;
556: float mipOffset = 0.75*(1.0 - rcp_exp2_packed.y) * rcp_exp2_packed.x;
557: bool bRes = mipLevel == 0.0;
558: scale = bRes && (scale < a) ? a : scale;
559: vec3 r;
560: vec2 offset;
561: int face = getFaceFromDirection(direction);
562: float rcpPowScale = 1.0 / powScale;
563: if( face == 0) {
564: r = vec3(direction.x, -direction.z, direction.y);
565: offset = vec2(0.0+mipOffset,0.75 * rcpPowScale);
566: offset.y = bRes && (offset.y < 2.0a) ? a : offset.y;
567: }
568: else if( face == 1) {
569: r = vec3(direction.y, direction.x, direction.z);
570: offset = vec2(scale+mipOffset, 0.75 * rcpPowScale);
571: offset.y = bRes && (offset.y < 2.0a) ? a : offset.y;
572: }
573: else if( face == 2) {
574: r = vec3(direction.z, direction.x, direction.y);
575: offset = vec2(2.0scale+mipOffset, 0.75 * rcpPowScale);
576: offset.y = bRes && (offset.y < 2.0a) ? a : offset.y;
577: }
578: else if( face == 3) {
579: r = vec3(direction.x, direction.z, direction.y);
580: offset = vec2(0.0+mipOffset,0.5 * rcpPowScale);
581: offset.y = bRes && (offset.y < 2.0a) ? 0.0 : offset.y;
582: }
583: else if( face == 4) {
584: r = vec3(direction.y, direction.x, -direction.z);
585: offset = vec2(scale+mipOffset, 0.5 * rcpPowScale);
586: offset.y = bRes && (offset.y < 2.0a) ? 0.0 : offset.y;
587: }
588: else {
589: r = vec3(direction.z, -direction.x, direction.y);
590: offset = vec2(2.0scale+mipOffset, 0.5 * rcpPowScale);
591: offset.y = bRes && (offset.y < 2.0a) ? 0.0 : offset.y;
592: }
593: r = normalize(r);
594: float texelOffset = 0.5 * cubeUV_rcpTextureSize;
595: vec2 s = ( r.yz / abs( r.x ) + vec2( 1.0 ) ) * 0.5;
596: vec2 base = offset + vec2( texelOffset );
597: return base + s * ( scale - 2.0 * texelOffset );
598: }
599: #define cubeUV_maxLods3 (log2(cubeUV_textureSize0.25) - 3.0)
600: vec4 textureCubeUV(vec3 reflectedDirection, float roughness ) {
601: float roughnessVal = roughness cubeUV_maxLods3;
602: float r1 = floor(roughnessVal);
603: float r2 = r1 + 1.0;
604: float t = fract(roughnessVal);
605: vec2 mipInfo = MipLevelInfo(reflectedDirection, r1, roughness);
606: float s = mipInfo.y;
607: float level0 = mipInfo.x;
608: float level1 = level0 + 1.0;
609: level1 = level1 > 5.0 ? 5.0 : level1;
610: level0 += min( floor( s + 0.5 ), 5.0 );
611: vec2 uv_10 = getCubeUV(reflectedDirection, r1, level0);
612: vec4 color10 = envMapTexelToLinear(texture2D(envMap, uv_10));
613: vec2 uv_20 = getCubeUV(reflectedDirection, r2, level0);
614: vec4 color20 = envMapTexelToLinear(texture2D(envMap, uv_20));
615: vec4 result = mix(color10, color20, t);
616: return vec4(result.rgb, 1.0);
617: }
618: #endif
619:
620: uniform vec3 ambientLightColor;
621: vec3 getAmbientLightIrradiance( const in vec3 ambientLightColor ) {
622: vec3 irradiance = ambientLightColor;
623: #ifndef PHYSICALLY_CORRECT_LIGHTS
624: irradiance *= PI;
625: #endif
626: return irradiance;
627: }
628: #if 1 > 0
629: struct DirectionalLight {
630: vec3 direction;
631: vec3 color;
632: int shadow;
633: float shadowBias;
634: float shadowRadius;
635: vec2 shadowMapSize;
636: };
637: uniform DirectionalLight directionalLights[ 1 ];
638: void getDirectionalDirectLightIrradiance( const in DirectionalLight directionalLight, const in GeometricContext geometry, out IncidentLight directLight ) {
639: directLight.color = directionalLight.color;
640: directLight.direction = directionalLight.direction;
641: directLight.visible = true;
642: }
643: #endif
644: #if 2 > 0
645: struct PointLight {
646: vec3 position;
647: vec3 color;
648: float distance;
649: float decay;
650: int shadow;
651: float shadowBias;
652: float shadowRadius;
653: vec2 shadowMapSize;
654: };
655: uniform PointLight pointLights[ 2 ];
656: void getPointDirectLightIrradiance( const in PointLight pointLight, const in GeometricContext geometry, out IncidentLight directLight ) {
657: vec3 lVector = pointLight.position - geometry.position;
658: directLight.direction = normalize( lVector );
659: float lightDistance = length( lVector );
660: directLight.color = pointLight.color;
661: directLight.color *= punctualLightIntensityToIrradianceFactor( lightDistance, pointLight.distance, pointLight.decay );
662: directLight.visible = ( directLight.color != vec3( 0.0 ) );
663: }
664: #endif
665: #if 0 > 0
666: struct SpotLight {
667: vec3 position;
668: vec3 direction;
669: vec3 color;
670: float distance;
671: float decay;
672: float coneCos;
673: float penumbraCos;
674: int shadow;
675: float shadowBias;
676: float shadowRadius;
677: vec2 shadowMapSize;
678: };
679: uniform SpotLight spotLights[ 0 ];
680: void getSpotDirectLightIrradiance( const in SpotLight spotLight, const in GeometricContext geometry, out IncidentLight directLight ) {
681: vec3 lVector = spotLight.position - geometry.position;
682: directLight.direction = normalize( lVector );
683: float lightDistance = length( lVector );
684: float angleCos = dot( directLight.direction, spotLight.direction );
685: if ( angleCos > spotLight.coneCos ) {
686: float spotEffect = smoothstep( spotLight.coneCos, spotLight.penumbraCos, angleCos );
687: directLight.color = spotLight.color;
688: directLight.color *= spotEffect * punctualLightIntensityToIrradianceFactor( lightDistance, spotLight.distance, spotLight.decay );
689: directLight.visible = true;
690: } else {
691: directLight.color = vec3( 0.0 );
692: directLight.visible = false;
693: }
694: }
695: #endif
696: #if 0 > 0
697: struct RectAreaLight {
698: vec3 color;
699: vec3 position;
700: vec3 halfWidth;
701: vec3 halfHeight;
702: };
703: uniform sampler2D ltcMat; uniform sampler2D ltcMag;
704: uniform RectAreaLight rectAreaLights[ 0 ];
705: #endif
706: #if 0 > 0
707: struct HemisphereLight {
708: vec3 direction;
709: vec3 skyColor;
710: vec3 groundColor;
711: };
712: uniform HemisphereLight hemisphereLights[ 0 ];
713: vec3 getHemisphereLightIrradiance( const in HemisphereLight hemiLight, const in GeometricContext geometry ) {
714: float dotNL = dot( geometry.normal, hemiLight.direction );
715: float hemiDiffuseWeight = 0.5 * dotNL + 0.5;
716: vec3 irradiance = mix( hemiLight.groundColor, hemiLight.skyColor, hemiDiffuseWeight );
717: #ifndef PHYSICALLY_CORRECT_LIGHTS
718: irradiance *= PI;
719: #endif
720: return irradiance;
721: }
722: #endif
723: #if defined( USE_ENVMAP ) && defined( PHYSICAL )
724: vec3 getLightProbeIndirectIrradiance( const in GeometricContext geometry, const in int maxMIPLevel ) {
725: vec3 worldNormal = inverseTransformDirection( geometry.normal, viewMatrix );
726: #ifdef ENVMAP_TYPE_CUBE
727: vec3 queryVec = vec3( flipEnvMap * worldNormal.x, worldNormal.yz );
728: #ifdef TEXTURE_LOD_EXT
729: vec4 envMapColor = textureCubeLodEXT( envMap, queryVec, float( maxMIPLevel ) );
730: #else
731: vec4 envMapColor = textureCube( envMap, queryVec, float( maxMIPLevel ) );
732: #endif
733: envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
734: #elif defined( ENVMAP_TYPE_CUBE_UV )
735: vec3 queryVec = vec3( flipEnvMap * worldNormal.x, worldNormal.yz );
736: vec4 envMapColor = textureCubeUV( queryVec, 1.0 );
737: #else
738: vec4 envMapColor = vec4( 0.0 );
739: #endif
740: return PI * envMapColor.rgb * envMapIntensity;
741: }
742: float getSpecularMIPLevel( const in float blinnShininessExponent, const in int maxMIPLevel ) {
743: float maxMIPLevelScalar = float( maxMIPLevel );
744: float desiredMIPLevel = maxMIPLevelScalar - 0.79248 - 0.5 * log2( pow2( blinnShininessExponent ) + 1.0 );
745: return clamp( desiredMIPLevel, 0.0, maxMIPLevelScalar );
746: }
747: vec3 getLightProbeIndirectRadiance( const in GeometricContext geometry, const in float blinnShininessExponent, const in int maxMIPLevel ) {
748: #ifdef ENVMAP_MODE_REFLECTION
749: vec3 reflectVec = reflect( -geometry.viewDir, geometry.normal );
750: #else
751: vec3 reflectVec = refract( -geometry.viewDir, geometry.normal, refractionRatio );
752: #endif
753: reflectVec = inverseTransformDirection( reflectVec, viewMatrix );
754: float specularMIPLevel = getSpecularMIPLevel( blinnShininessExponent, maxMIPLevel );
755: #ifdef ENVMAP_TYPE_CUBE
756: vec3 queryReflectVec = vec3( flipEnvMap * reflectVec.x, reflectVec.yz );
757: #ifdef TEXTURE_LOD_EXT
758: vec4 envMapColor = textureCubeLodEXT( envMap, queryReflectVec, specularMIPLevel );
759: #else
760: vec4 envMapColor = textureCube( envMap, queryReflectVec, specularMIPLevel );
761: #endif
762: envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
763: #elif defined( ENVMAP_TYPE_CUBE_UV )
764: vec3 queryReflectVec = vec3( flipEnvMap * reflectVec.x, reflectVec.yz );
765: vec4 envMapColor = textureCubeUV(queryReflectVec, BlinnExponentToGGXRoughness(blinnShininessExponent));
766: #elif defined( ENVMAP_TYPE_EQUIREC )
767: vec2 sampleUV;
768: sampleUV.y = saturate( reflectVec.y * 0.5 + 0.5 );
769: sampleUV.x = atan( reflectVec.z, reflectVec.x ) * RECIPROCAL_PI2 + 0.5;
770: #ifdef TEXTURE_LOD_EXT
771: vec4 envMapColor = texture2DLodEXT( envMap, sampleUV, specularMIPLevel );
772: #else
773: vec4 envMapColor = texture2D( envMap, sampleUV, specularMIPLevel );
774: #endif
775: envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
776: #elif defined( ENVMAP_TYPE_SPHERE )
777: vec3 reflectView = normalize( ( viewMatrix * vec4( reflectVec, 0.0 ) ).xyz + vec3( 0.0,0.0,1.0 ) );
778: #ifdef TEXTURE_LOD_EXT
779: vec4 envMapColor = texture2DLodEXT( envMap, reflectView.xy * 0.5 + 0.5, specularMIPLevel );
780: #else
781: vec4 envMapColor = texture2D( envMap, reflectView.xy * 0.5 + 0.5, specularMIPLevel );
782: #endif
783: envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
784: #endif
785: return envMapColor.rgb * envMapIntensity;
786: }
787: #endif
788:
789: struct PhysicalMaterial {
790: vec3 diffuseColor;
791: float specularRoughness;
792: vec3 specularColor;
793: #ifndef STANDARD
794: float clearCoat;
795: float clearCoatRoughness;
796: #endif
797: };
798: #define MAXIMUM_SPECULAR_COEFFICIENT 0.16
799: #define DEFAULT_SPECULAR_COEFFICIENT 0.04
800: float clearCoatDHRApprox( const in float roughness, const in float dotNL ) {
801: return DEFAULT_SPECULAR_COEFFICIENT + ( 1.0 - DEFAULT_SPECULAR_COEFFICIENT ) * ( pow( 1.0 - dotNL, 5.0 ) * pow( 1.0 - roughness, 2.0 ) );
802: }
803: #if 0 > 0
804: void RE_Direct_RectArea_Physical( const in RectAreaLight rectAreaLight, const in GeometricContext geometry, const in PhysicalMaterial material, inout ReflectedLight reflectedLight ) {
805: vec3 matDiffColor = material.diffuseColor;
806: vec3 matSpecColor = material.specularColor;
807: vec3 lightColor = rectAreaLight.color;
808: float roughness = material.specularRoughness;
809: vec3 spec = Rect_Area_Light_Specular_Reflectance(
810: geometry,
811: rectAreaLight.position, rectAreaLight.halfWidth, rectAreaLight.halfHeight,
812: roughness,
813: ltcMat, ltcMag );
814: vec3 diff = Rect_Area_Light_Diffuse_Reflectance(
815: geometry,
816: rectAreaLight.position, rectAreaLight.halfWidth, rectAreaLight.halfHeight );
817: reflectedLight.directSpecular += lightColor * matSpecColor * spec;
818: reflectedLight.directDiffuse += lightColor * matDiffColor * diff;
819: }
820: #endif
821: void RE_Direct_Physical( const in IncidentLight directLight, const in GeometricContext geometry, const in PhysicalMaterial material, inout ReflectedLight reflectedLight ) {
822: float dotNL = saturate( dot( geometry.normal, directLight.direction ) );
823: vec3 irradiance = dotNL * directLight.color;
824: #ifndef PHYSICALLY_CORRECT_LIGHTS
825: irradiance *= PI;
826: #endif
827: #ifndef STANDARD
828: float clearCoatDHR = material.clearCoat * clearCoatDHRApprox( material.clearCoatRoughness, dotNL );
829: #else
830: float clearCoatDHR = 0.0;
831: #endif
832: reflectedLight.directSpecular += ( 1.0 - clearCoatDHR ) * irradiance * BRDF_Specular_GGX( directLight, geometry, material.specularColor, material.specularRoughness );
833: reflectedLight.directDiffuse += ( 1.0 - clearCoatDHR ) * irradiance * BRDF_Diffuse_Lambert( material.diffuseColor );
834: #ifndef STANDARD
835: reflectedLight.directSpecular += irradiance * material.clearCoat * BRDF_Specular_GGX( directLight, geometry, vec3( DEFAULT_SPECULAR_COEFFICIENT ), material.clearCoatRoughness );
836: #endif
837: }
838: void RE_IndirectDiffuse_Physical( const in vec3 irradiance, const in GeometricContext geometry, const in PhysicalMaterial material, inout ReflectedLight reflectedLight ) {
839: reflectedLight.indirectDiffuse += irradiance * BRDF_Diffuse_Lambert( material.diffuseColor );
840: }
841: void RE_IndirectSpecular_Physical( const in vec3 radiance, const in vec3 clearCoatRadiance, const in GeometricContext geometry, const in PhysicalMaterial material, inout ReflectedLight reflectedLight ) {
842: #ifndef STANDARD
843: float dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );
844: float dotNL = dotNV;
845: float clearCoatDHR = material.clearCoat * clearCoatDHRApprox( material.clearCoatRoughness, dotNL );
846: #else
847: float clearCoatDHR = 0.0;
848: #endif
849: reflectedLight.indirectSpecular += ( 1.0 - clearCoatDHR ) * radiance * BRDF_Specular_GGX_Environment( geometry, material.specularColor, material.specularRoughness );
850: #ifndef STANDARD
851: reflectedLight.indirectSpecular += clearCoatRadiance * material.clearCoat * BRDF_Specular_GGX_Environment( geometry, vec3( DEFAULT_SPECULAR_COEFFICIENT ), material.clearCoatRoughness );
852: #endif
853: }
854: #define RE_Direct RE_Direct_Physical
855: #define RE_Direct_RectArea RE_Direct_RectArea_Physical
856: #define RE_IndirectDiffuse RE_IndirectDiffuse_Physical
857: #define RE_IndirectSpecular RE_IndirectSpecular_Physical
858: #define Material_BlinnShininessExponent( material ) GGXRoughnessToBlinnExponent( material.specularRoughness )
859: #define Material_ClearCoat_BlinnShininessExponent( material ) GGXRoughnessToBlinnExponent( material.clearCoatRoughness )
860: float computeSpecularOcclusion( const in float dotNV, const in float ambientOcclusion, const in float roughness ) {
861: return saturate( pow( dotNV + ambientOcclusion, exp2( - 16.0 * roughness - 1.0 ) ) - 1.0 + ambientOcclusion );
862: }
863:
864: #ifdef USE_SHADOWMAP
865: #if 1 > 0
866: uniform sampler2D directionalShadowMap[ 1 ];
867: varying vec4 vDirectionalShadowCoord[ 1 ];
868: #endif
869: #if 0 > 0
870: uniform sampler2D spotShadowMap[ 0 ];
871: varying vec4 vSpotShadowCoord[ 0 ];
872: #endif
873: #if 2 > 0
874: uniform sampler2D pointShadowMap[ 2 ];
875: varying vec4 vPointShadowCoord[ 2 ];
876: #endif
877: float texture2DCompare( sampler2D depths, vec2 uv, float compare ) {
878: return step( compare, unpackRGBAToDepth( texture2D( depths, uv ) ) );
879: }
880: float texture2DShadowLerp( sampler2D depths, vec2 size, vec2 uv, float compare ) {
881: const vec2 offset = vec2( 0.0, 1.0 );
882: vec2 texelSize = vec2( 1.0 ) / size;
883: vec2 centroidUV = floor( uv * size + 0.5 ) / size;
884: float lb = texture2DCompare( depths, centroidUV + texelSize * offset.xx, compare );
885: float lt = texture2DCompare( depths, centroidUV + texelSize * offset.xy, compare );
886: float rb = texture2DCompare( depths, centroidUV + texelSize * offset.yx, compare );
887: float rt = texture2DCompare( depths, centroidUV + texelSize * offset.yy, compare );
888: vec2 f = fract( uv * size + 0.5 );
889: float a = mix( lb, lt, f.y );
890: float b = mix( rb, rt, f.y );
891: float c = mix( a, b, f.x );
892: return c;
893: }
894: float getShadow( sampler2D shadowMap, vec2 shadowMapSize, float shadowBias, float shadowRadius, vec4 shadowCoord ) {
895: shadowCoord.xyz /= shadowCoord.w;
896: shadowCoord.z += shadowBias;
897: bvec4 inFrustumVec = bvec4 ( shadowCoord.x >= 0.0, shadowCoord.x <= 1.0, shadowCoord.y >= 0.0, shadowCoord.y <= 1.0 );
898: bool inFrustum = all( inFrustumVec );
899: bvec2 frustumTestVec = bvec2( inFrustum, shadowCoord.z <= 1.0 );
900: bool frustumTest = all( frustumTestVec );
901: if ( frustumTest ) {
902: #if defined( SHADOWMAP_TYPE_PCF )
903: vec2 texelSize = vec2( 1.0 ) / shadowMapSize;
904: float dx0 = - texelSize.x * shadowRadius;
905: float dy0 = - texelSize.y * shadowRadius;
906: float dx1 = + texelSize.x * shadowRadius;
907: float dy1 = + texelSize.y * shadowRadius;
908: return (
909: texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx0, dy0 ), shadowCoord.z ) +
910: texture2DCompare( shadowMap, shadowCoord.xy + vec2( 0.0, dy0 ), shadowCoord.z ) +
911: texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx1, dy0 ), shadowCoord.z ) +
912: texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx0, 0.0 ), shadowCoord.z ) +
913: texture2DCompare( shadowMap, shadowCoord.xy, shadowCoord.z ) +
914: texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx1, 0.0 ), shadowCoord.z ) +
915: texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx0, dy1 ), shadowCoord.z ) +
916: texture2DCompare( shadowMap, shadowCoord.xy + vec2( 0.0, dy1 ), shadowCoord.z ) +
917: texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx1, dy1 ), shadowCoord.z )
918: ) * ( 1.0 / 9.0 );
919: #elif defined( SHADOWMAP_TYPE_PCF_SOFT )
920: vec2 texelSize = vec2( 1.0 ) / shadowMapSize;
921: float dx0 = - texelSize.x * shadowRadius;
922: float dy0 = - texelSize.y * shadowRadius;
923: float dx1 = + texelSize.x * shadowRadius;
924: float dy1 = + texelSize.y * shadowRadius;
925: return (
926: texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy + vec2( dx0, dy0 ), shadowCoord.z ) +
927: texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy + vec2( 0.0, dy0 ), shadowCoord.z ) +
928: texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy + vec2( dx1, dy0 ), shadowCoord.z ) +
929: texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy + vec2( dx0, 0.0 ), shadowCoord.z ) +
930: texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy, shadowCoord.z ) +
931: texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy + vec2( dx1, 0.0 ), shadowCoord.z ) +
932: texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy + vec2( dx0, dy1 ), shadowCoord.z ) +
933: texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy + vec2( 0.0, dy1 ), shadowCoord.z ) +
934: texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy + vec2( dx1, dy1 ), shadowCoord.z )
935: ) * ( 1.0 / 9.0 );
936: #else
937: return texture2DCompare( shadowMap, shadowCoord.xy, shadowCoord.z );
938: #endif
939: }
940: return 1.0;
941: }
942: vec2 cubeToUV( vec3 v, float texelSizeY ) {
943: vec3 absV = abs( v );
944: float scaleToCube = 1.0 / max( absV.x, max( absV.y, absV.z ) );
945: absV *= scaleToCube;
946: v *= scaleToCube * ( 1.0 - 2.0 * texelSizeY );
947: vec2 planar = v.xy;
948: float almostATexel = 1.5 * texelSizeY;
949: float almostOne = 1.0 - almostATexel;
950: if ( absV.z >= almostOne ) {
951: if ( v.z > 0.0 )
952: planar.x = 4.0 - v.x;
953: } else if ( absV.x >= almostOne ) {
954: float signX = sign( v.x );
955: planar.x = v.z * signX + 2.0 * signX;
956: } else if ( absV.y >= almostOne ) {
957: float signY = sign( v.y );
958: planar.x = v.x + 2.0 * signY + 2.0;
959: planar.y = v.z * signY - 2.0;
960: }
961: return vec2( 0.125, 0.25 ) * planar + vec2( 0.375, 0.75 );
962: }
963: float getPointShadow( sampler2D shadowMap, vec2 shadowMapSize, float shadowBias, float shadowRadius, vec4 shadowCoord ) {
964: vec2 texelSize = vec2( 1.0 ) / ( shadowMapSize * vec2( 4.0, 2.0 ) );
965: vec3 lightToPosition = shadowCoord.xyz;
966: vec3 bd3D = normalize( lightToPosition );
967: float dp = ( length( lightToPosition ) - shadowBias ) / 1000.0;
968: #if defined( SHADOWMAP_TYPE_PCF ) || defined( SHADOWMAP_TYPE_PCF_SOFT )
969: vec2 offset = vec2( - 1, 1 ) * shadowRadius * texelSize.y;
970: return (
971: texture2DCompare( shadowMap, cubeToUV( bd3D + offset.xyy, texelSize.y ), dp ) +
972: texture2DCompare( shadowMap, cubeToUV( bd3D + offset.yyy, texelSize.y ), dp ) +
973: texture2DCompare( shadowMap, cubeToUV( bd3D + offset.xyx, texelSize.y ), dp ) +
974: texture2DCompare( shadowMap, cubeToUV( bd3D + offset.yyx, texelSize.y ), dp ) +
975: texture2DCompare( shadowMap, cubeToUV( bd3D, texelSize.y ), dp ) +
976: texture2DCompare( shadowMap, cubeToUV( bd3D + offset.xxy, texelSize.y ), dp ) +
977: texture2DCompare( shadowMap, cubeToUV( bd3D + offset.yxy, texelSize.y ), dp ) +
978: texture2DCompare( shadowMap, cubeToUV( bd3D + offset.xxx, texelSize.y ), dp ) +
979: texture2DCompare( shadowMap, cubeToUV( bd3D + offset.yxx, texelSize.y ), dp )
980: ) * ( 1.0 / 9.0 );
981: #else
982: return texture2DCompare( shadowMap, cubeToUV( bd3D, texelSize.y ), dp );
983: #endif
984: }
985: #endif
986:
987: #ifdef USE_BUMPMAP
988: uniform sampler2D bumpMap;
989: uniform float bumpScale;
990: vec2 dHdxy_fwd() {
991: vec2 dSTdx = dFdx( vUv );
992: vec2 dSTdy = dFdy( vUv );
993: float Hll = bumpScale * texture2D( bumpMap, vUv ).x;
994: float dBx = bumpScale * texture2D( bumpMap, vUv + dSTdx ).x - Hll;
995: float dBy = bumpScale * texture2D( bumpMap, vUv + dSTdy ).x - Hll;
996: return vec2( dBx, dBy );
997: }
998: vec3 perturbNormalArb( vec3 surf_pos, vec3 surf_norm, vec2 dHdxy ) {
999: vec3 vSigmaX = dFdx( surf_pos );
1000: vec3 vSigmaY = dFdy( surf_pos );
1001: vec3 vN = surf_norm;
1002: vec3 R1 = cross( vSigmaY, vN );
1003: vec3 R2 = cross( vN, vSigmaX );
1004: float fDet = dot( vSigmaX, R1 );
1005: vec3 vGrad = sign( fDet ) * ( dHdxy.x * R1 + dHdxy.y * R2 );
1006: return normalize( abs( fDet ) * surf_norm - vGrad );
1007: }
1008: #endif
1009:
1010: #ifdef USE_NORMALMAP
1011: uniform sampler2D normalMap;
1012: uniform vec2 normalScale;
1013: vec3 perturbNormal2Arb( vec3 eye_pos, vec3 surf_norm ) {
1014: vec3 q0 = dFdx( eye_pos.xyz );
1015: vec3 q1 = dFdy( eye_pos.xyz );
1016: vec2 st0 = dFdx( vUv.st );
1017: vec2 st1 = dFdy( vUv.st );
1018: vec3 S = normalize( q0 * st1.t - q1 * st0.t );
1019: vec3 T = normalize( -q0 * st1.s + q1 * st0.s );
1020: vec3 N = normalize( surf_norm );
1021: vec3 mapN = texture2D( normalMap, vUv ).xyz * 2.0 - 1.0;
1022: mapN.xy = normalScale * mapN.xy;
1023: mat3 tsn = mat3( S, T, N );
1024: return normalize( tsn * mapN );
1025: }
1026: #endif
1027:
1028: #ifdef USE_ROUGHNESSMAP
1029: uniform sampler2D roughnessMap;
1030: #endif
1031: #ifdef USE_METALNESSMAP
1032: uniform sampler2D metalnessMap;
1033: #endif
1034: #ifdef USE_LOGDEPTHBUF
1035: uniform float logDepthBufFC;
1036: #ifdef USE_LOGDEPTHBUF_EXT
1037: varying float vFragDepth;
1038: #endif
1039: #endif
1040:
1041: #if NUM_CLIPPING_PLANES > 0
1042: #if ! defined( PHYSICAL ) && ! defined( PHONG )
1043: varying vec3 vViewPosition;
1044: #endif
1045: uniform vec4 clippingPlanes[ NUM_CLIPPING_PLANES ];
1046: #endif
1047:
1048: void main() {
1049: #if NUM_CLIPPING_PLANES > 0
1050: for ( int i = 0; i < UNION_CLIPPING_PLANES; ++ i ) {
1051: vec4 plane = clippingPlanes[ i ];
1052: if ( dot( vViewPosition, plane.xyz ) > plane.w ) discard;
1053: }
1054:
1055: #if UNION_CLIPPING_PLANES < NUM_CLIPPING_PLANES
1056: bool clipped = true;
1057: for ( int i = UNION_CLIPPING_PLANES; i < NUM_CLIPPING_PLANES; ++ i ) {
1058: vec4 plane = clippingPlanes[ i ];
1059: clipped = ( dot( vViewPosition, plane.xyz ) > plane.w ) && clipped;
1060: }
1061: if ( clipped ) discard;
1062:
1063: #endif
1064: #endif
1065:
1066: vec4 diffuseColor = vec4( diffuse, opacity );
1067: ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );
1068: vec3 totalEmissiveRadiance = emissive;
1069: #if defined(USE_LOGDEPTHBUF) && defined(USE_LOGDEPTHBUF_EXT)
1070: gl_FragDepthEXT = log2(vFragDepth) * logDepthBufFC * 0.5;
1071: #endif
1072: #ifdef USE_MAP
1073: vec4 texelColor = texture2D( map, vUv );
1074: texelColor = mapTexelToLinear( texelColor );
1075: diffuseColor *= texelColor;
1076: #endif
1077:
1078: #ifdef USE_COLOR
1079: diffuseColor.rgb *= vColor;
1080: #endif
1081: #ifdef USE_ALPHAMAP
1082: diffuseColor.a *= texture2D( alphaMap, vUv ).g;
1083: #endif
1084:
1085: #ifdef ALPHATEST
1086: if ( diffuseColor.a < ALPHATEST ) discard;
1087: #endif
1088:
1089: float specularStrength;
1090: #ifdef USE_SPECULARMAP
1091: vec4 texelSpecular = texture2D( specularMap, vUv );
1092: specularStrength = texelSpecular.r;
1093: #else
1094: specularStrength = 1.0;
1095: #endif
1096: float roughnessFactor = roughness;
1097: #ifdef USE_ROUGHNESSMAP
1098: vec4 texelRoughness = texture2D( roughnessMap, vUv );
1099: roughnessFactor *= texelRoughness.r;
1100: #endif
1101:
1102: float metalnessFactor = metalness;
1103: #ifdef USE_METALNESSMAP
1104: vec4 texelMetalness = texture2D( metalnessMap, vUv );
1105: metalnessFactor *= texelMetalness.r;
1106: #endif
1107:
1108: #ifdef DOUBLE_SIDED
1109: float flipNormal = ( float( gl_FrontFacing ) * 2.0 - 1.0 );
1110: #else
1111: float flipNormal = 1.0;
1112: #endif
1113:
1114: #ifdef FLAT_SHADED
1115: vec3 fdx = vec3( dFdx( vViewPosition.x ), dFdx( vViewPosition.y ), dFdx( vViewPosition.z ) );
1116: vec3 fdy = vec3( dFdy( vViewPosition.x ), dFdy( vViewPosition.y ), dFdy( vViewPosition.z ) );
1117: vec3 normal = normalize( cross( fdx, fdy ) );
1118: #else
1119: vec3 normal = normalize( vNormal ) * flipNormal;
1120: #endif
1121: #ifdef USE_NORMALMAP
1122: normal = perturbNormal2Arb( -vViewPosition, normal );
1123: #elif defined( USE_BUMPMAP )
1124: normal = perturbNormalArb( -vViewPosition, normal, dHdxy_fwd() );
1125: #endif
1126:
1127: #ifdef USE_EMISSIVEMAP
1128: vec4 emissiveColor = texture2D( emissiveMap, vUv );
1129: emissiveColor.rgb = emissiveMapTexelToLinear( emissiveColor ).rgb;
1130: totalEmissiveRadiance *= emissiveColor.rgb;
1131: #endif
1132:
1133: PhysicalMaterial material;
1134: material.diffuseColor = diffuseColor.rgb * ( 1.0 - metalnessFactor );
1135: material.specularRoughness = clamp( roughnessFactor, 0.04, 1.0 );
1136: #ifdef STANDARD
1137: material.specularColor = mix( vec3( DEFAULT_SPECULAR_COEFFICIENT ), diffuseColor.rgb, metalnessFactor );
1138: #else
1139: material.specularColor = mix( vec3( MAXIMUM_SPECULAR_COEFFICIENT * pow2( reflectivity ) ), diffuseColor.rgb, metalnessFactor );
1140: material.clearCoat = saturate( clearCoat ); material.clearCoatRoughness = clamp( clearCoatRoughness, 0.04, 1.0 );
1141: #endif
1142:
1143:
1144: GeometricContext geometry;
1145: geometry.position = - vViewPosition;
1146: geometry.normal = normal;
1147: geometry.viewDir = normalize( vViewPosition );
1148: IncidentLight directLight;
1149: #if ( 2 > 0 ) && defined( RE_Direct )
1150: PointLight pointLight;
1151:
1152: pointLight = pointLights[ 0 ];
1153: getPointDirectLightIrradiance( pointLight, geometry, directLight );
1154: #ifdef USE_SHADOWMAP
1155: directLight.color *= all( bvec2( pointLight.shadow, directLight.visible ) ) ? getPointShadow( pointShadowMap[ 0 ], pointLight.shadowMapSize, pointLight.shadowBias, pointLight.shadowRadius, vPointShadowCoord[ 0 ] ) : 1.0;
1156: #endif
1157: RE_Direct( directLight, geometry, material, reflectedLight );
1158:
1159: pointLight = pointLights[ 1 ];
1160: getPointDirectLightIrradiance( pointLight, geometry, directLight );
1161: #ifdef USE_SHADOWMAP
1162: directLight.color *= all( bvec2( pointLight.shadow, directLight.visible ) ) ? getPointShadow( pointShadowMap[ 1 ], pointLight.shadowMapSize, pointLight.shadowBias, pointLight.shadowRadius, vPointShadowCoord[ 1 ] ) : 1.0;
1163: #endif
1164: RE_Direct( directLight, geometry, material, reflectedLight );
1165:
1166: #endif
1167: #if ( 0 > 0 ) && defined( RE_Direct )
1168: SpotLight spotLight;
1169:
1170: #endif
1171: #if ( 1 > 0 ) && defined( RE_Direct )
1172: DirectionalLight directionalLight;
1173:
1174: directionalLight = directionalLights[ 0 ];
1175: getDirectionalDirectLightIrradiance( directionalLight, geometry, directLight );
1176: #ifdef USE_SHADOWMAP
1177: directLight.color *= all( bvec2( directionalLight.shadow, directLight.visible ) ) ? getShadow( directionalShadowMap[ 0 ], directionalLight.shadowMapSize, directionalLight.shadowBias, directionalLight.shadowRadius, vDirectionalShadowCoord[ 0 ] ) : 1.0;
1178: #endif
1179: RE_Direct( directLight, geometry, material, reflectedLight );
1180:
1181: #endif
1182: #if ( 0 > 0 ) && defined( RE_Direct_RectArea )
1183: RectAreaLight rectAreaLight;
1184:
1185: #endif
1186: #if defined( RE_IndirectDiffuse )
1187: vec3 irradiance = getAmbientLightIrradiance( ambientLightColor );
1188: #ifdef USE_LIGHTMAP
1189: vec3 lightMapIrradiance = texture2D( lightMap, vUv2 ).xyz * lightMapIntensity;
1190: #ifndef PHYSICALLY_CORRECT_LIGHTS
1191: lightMapIrradiance *= PI;
1192: #endif
1193: irradiance += lightMapIrradiance;
1194: #endif
1195: #if ( 0 > 0 )
1196:
1197: #endif
1198: #if defined( USE_ENVMAP ) && defined( PHYSICAL ) && defined( ENVMAP_TYPE_CUBE_UV )
1199: irradiance += getLightProbeIndirectIrradiance( geometry, 8 );
1200: #endif
1201: RE_IndirectDiffuse( irradiance, geometry, material, reflectedLight );
1202: #endif
1203: #if defined( USE_ENVMAP ) && defined( RE_IndirectSpecular )
1204: vec3 radiance = getLightProbeIndirectRadiance( geometry, Material_BlinnShininessExponent( material ), 8 );
1205: #ifndef STANDARD
1206: vec3 clearCoatRadiance = getLightProbeIndirectRadiance( geometry, Material_ClearCoat_BlinnShininessExponent( material ), 8 );
1207: #else
1208: vec3 clearCoatRadiance = vec3( 0.0 );
1209: #endif
1210: RE_IndirectSpecular( radiance, clearCoatRadiance, geometry, material, reflectedLight );
1211: #endif
1212:
1213: #ifdef USE_AOMAP
1214: float ambientOcclusion = ( texture2D( aoMap, vUv2 ).r - 1.0 ) * aoMapIntensity + 1.0;
1215: reflectedLight.indirectDiffuse *= ambientOcclusion;
1216: #if defined( USE_ENVMAP ) && defined( PHYSICAL )
1217: float dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );
1218: reflectedLight.indirectSpecular *= computeSpecularOcclusion( dotNV, ambientOcclusion, material.specularRoughness );
1219: #endif
1220: #endif
1221:
1222: vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveRadiance;
1223: gl_FragColor = vec4( outgoingLight, diffuseColor.a );
1224: #ifdef PREMULTIPLIED_ALPHA
1225: gl_FragColor.rgb *= gl_FragColor.a;
1226: #endif
1227:
1228: #if defined( TONE_MAPPING )
1229: gl_FragColor.rgb = toneMapping( gl_FragColor.rgb );
1230: #endif
1231:
1232: gl_FragColor = linearToOutputTexel( gl_FragColor );
1233:
1234: #ifdef USE_FOG
1235: #ifdef FOG_EXP2
1236: float fogFactor = whiteCompliment( exp2( - fogDensity * fogDensity * fogDepth * fogDepth * LOG2 ) );
1237: #else
1238: float fogFactor = smoothstep( fogNear, fogFar, fogDepth );
1239: #endif
1240: gl_FragColor.rgb = mix( gl_FragColor.rgb, fogColor, fogFactor );
1241: #endif
1242:
1243: }
1244: "
[Warning] Invalid timestamps detected. Time step between successive gyroscope sensor samples is very small or not monotonic (aframe-master.min.js, line 134, x4)
=====================================================
I don't know if it is related or not.