Advertisement
Guest User

Untitled

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