Advertisement
Guest User

Untitled

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