lights_pars.glsl 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. #if NUM_DIR_LIGHTS > 0
  2. struct DirectionalLight {
  3. vec3 direction;
  4. vec3 color;
  5. int shadow;
  6. float shadowBias;
  7. float shadowRadius;
  8. vec2 shadowMapSize;
  9. };
  10. uniform DirectionalLight directionalLights[ NUM_DIR_LIGHTS ];
  11. IncidentLight getDirectionalDirectLightIrradiance( const in DirectionalLight directionalLight, const in GeometricContext geometry ) {
  12. IncidentLight directLight;
  13. directLight.color = directionalLight.color;
  14. directLight.direction = directionalLight.direction;
  15. directLight.visible = true;
  16. return directLight;
  17. }
  18. #endif
  19. #if NUM_POINT_LIGHTS > 0
  20. struct PointLight {
  21. vec3 position;
  22. vec3 color;
  23. float distance;
  24. float decay;
  25. int shadow;
  26. float shadowBias;
  27. float shadowRadius;
  28. vec2 shadowMapSize;
  29. };
  30. uniform PointLight pointLights[ NUM_POINT_LIGHTS ];
  31. IncidentLight getPointDirectLightIrradiance( const in PointLight pointLight, const in GeometricContext geometry ) {
  32. IncidentLight directLight;
  33. vec3 lVector = pointLight.position - geometry.position;
  34. directLight.direction = normalize( lVector );
  35. float lightDistance = length( lVector );
  36. if ( testLightInRange( lightDistance, pointLight.distance ) ) {
  37. directLight.color = pointLight.color;
  38. directLight.color *= punctualLightIntensityToIrradianceFactor( lightDistance, pointLight.distance, pointLight.decay );
  39. directLight.visible = true;
  40. } else {
  41. directLight.color = vec3( 0.0 );
  42. directLight.visible = false;
  43. }
  44. return directLight;
  45. }
  46. #endif
  47. #if NUM_SPOT_LIGHTS > 0
  48. struct SpotLight {
  49. vec3 position;
  50. vec3 direction;
  51. vec3 color;
  52. float distance;
  53. float decay;
  54. float coneCos;
  55. float penumbraCos;
  56. int shadow;
  57. float shadowBias;
  58. float shadowRadius;
  59. vec2 shadowMapSize;
  60. };
  61. uniform SpotLight spotLights[ NUM_SPOT_LIGHTS ];
  62. IncidentLight getSpotDirectLightIrradiance( const in SpotLight spotLight, const in GeometricContext geometry ) {
  63. IncidentLight directLight;
  64. vec3 lVector = spotLight.position - geometry.position;
  65. directLight.direction = normalize( lVector );
  66. float lightDistance = length( lVector );
  67. float angleCos = dot( directLight.direction, spotLight.direction );
  68. if ( all( bvec2( angleCos > spotLight.coneCos, testLightInRange( lightDistance, spotLight.distance ) ) ) ) {
  69. float spotEffect = smoothstep( spotLight.coneCos, spotLight.penumbraCos, angleCos );
  70. directLight.color = spotLight.color;
  71. directLight.color *= spotEffect * punctualLightIntensityToIrradianceFactor( lightDistance, spotLight.distance, spotLight.decay );
  72. directLight.visible = true;
  73. } else {
  74. directLight.color = vec3( 0.0 );
  75. directLight.visible = false;
  76. }
  77. return directLight;
  78. }
  79. #endif
  80. #if NUM_HEMI_LIGHTS > 0
  81. struct HemisphereLight {
  82. vec3 direction;
  83. vec3 skyColor;
  84. vec3 groundColor;
  85. };
  86. uniform HemisphereLight hemisphereLights[ NUM_HEMI_LIGHTS ];
  87. vec3 getHemisphereLightIrradiance( const in HemisphereLight hemiLight, const in GeometricContext geometry ) {
  88. float dotNL = dot( geometry.normal, hemiLight.direction );
  89. float hemiDiffuseWeight = 0.5 * dotNL + 0.5;
  90. vec3 irradiance = mix( hemiLight.groundColor, hemiLight.skyColor, hemiDiffuseWeight );
  91. #ifndef PHYSICALLY_CORRECT_LIGHTS
  92. irradiance *= PI;
  93. #endif
  94. return irradiance;
  95. }
  96. #endif
  97. #if defined( USE_ENVMAP ) && defined( STANDARD )
  98. vec3 getLightProbeIndirectIrradiance( /*const in SpecularLightProbe specularLightProbe,*/ const in GeometricContext geometry, const in int maxMIPLevel ) {
  99. #ifdef DOUBLE_SIDED
  100. float flipNormal = ( float( gl_FrontFacing ) * 2.0 - 1.0 );
  101. #else
  102. float flipNormal = 1.0;
  103. #endif
  104. vec3 worldNormal = inverseTransformDirection( geometry.normal, viewMatrix );
  105. #ifdef ENVMAP_TYPE_CUBE
  106. vec3 queryVec = flipNormal * vec3( flipEnvMap * worldNormal.x, worldNormal.yz );
  107. // TODO: replace with properly filtered cubemaps and access the irradiance LOD level, be it the last LOD level
  108. // of a specular cubemap, or just the default level of a specially created irradiance cubemap.
  109. #ifdef TEXTURE_LOD_EXT
  110. vec4 envMapColor = textureCubeLodEXT( envMap, queryVec, float( maxMIPLevel ) );
  111. #else
  112. // force the bias high to get the last LOD level as it is the most blurred.
  113. vec4 envMapColor = textureCube( envMap, queryVec, float( maxMIPLevel ) );
  114. #endif
  115. #elif defined( ENVMAP_TYPE_CUBE_UV )
  116. vec3 queryVec = flipNormal * vec3( flipEnvMap * worldNormal.x, worldNormal.yz );
  117. vec4 envMapColor = textureCubeUV(queryVec, 1.0, 1024.0);
  118. #else
  119. vec3 envMapColor = vec3( 0.0 );
  120. #endif
  121. envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
  122. return PI * envMapColor.rgb * envMapIntensity;
  123. }
  124. // taken from here: http://casual-effects.blogspot.ca/2011/08/plausible-environment-lighting-in-two.html
  125. float getSpecularMIPLevel( const in float blinnShininessExponent, const in int maxMIPLevel ) {
  126. //float envMapWidth = pow( 2.0, maxMIPLevelScalar );
  127. //float desiredMIPLevel = log2( envMapWidth * sqrt( 3.0 ) ) - 0.5 * log2( pow2( blinnShininessExponent ) + 1.0 );
  128. float maxMIPLevelScalar = float( maxMIPLevel );
  129. float desiredMIPLevel = maxMIPLevelScalar - 0.79248 - 0.5 * log2( pow2( blinnShininessExponent ) + 1.0 );
  130. // clamp to allowable LOD ranges.
  131. return clamp( desiredMIPLevel, 0.0, maxMIPLevelScalar );
  132. }
  133. vec3 getLightProbeIndirectRadiance( /*const in SpecularLightProbe specularLightProbe,*/ const in GeometricContext geometry, const in float blinnShininessExponent, const in int maxMIPLevel ) {
  134. #ifdef ENVMAP_MODE_REFLECTION
  135. vec3 reflectVec = reflect( -geometry.viewDir, geometry.normal );
  136. #else
  137. vec3 reflectVec = refract( -geometry.viewDir, geometry.normal, refractionRatio );
  138. #endif
  139. #ifdef DOUBLE_SIDED
  140. float flipNormal = ( float( gl_FrontFacing ) * 2.0 - 1.0 );
  141. #else
  142. float flipNormal = 1.0;
  143. #endif
  144. reflectVec = inverseTransformDirection( reflectVec, viewMatrix );
  145. float specularMIPLevel = getSpecularMIPLevel( blinnShininessExponent, maxMIPLevel );
  146. #ifdef ENVMAP_TYPE_CUBE
  147. vec3 queryReflectVec = flipNormal * vec3( flipEnvMap * reflectVec.x, reflectVec.yz );
  148. #ifdef TEXTURE_LOD_EXT
  149. vec4 envMapColor = textureCubeLodEXT( envMap, queryReflectVec, specularMIPLevel );
  150. #else
  151. vec4 envMapColor = textureCube( envMap, queryReflectVec, specularMIPLevel );
  152. #endif
  153. #elif defined( ENVMAP_TYPE_CUBE_UV )
  154. vec3 queryReflectVec = flipNormal * vec3( flipEnvMap * reflectVec.x, reflectVec.yz );
  155. vec4 envMapColor = textureCubeUV(queryReflectVec, BlinnExponentToGGXRoughness(blinnShininessExponent), 1024.0);
  156. #elif defined( ENVMAP_TYPE_EQUIREC )
  157. vec2 sampleUV;
  158. sampleUV.y = saturate( flipNormal * reflectVec.y * 0.5 + 0.5 );
  159. sampleUV.x = atan( flipNormal * reflectVec.z, flipNormal * reflectVec.x ) * RECIPROCAL_PI2 + 0.5;
  160. #ifdef TEXTURE_LOD_EXT
  161. vec4 envMapColor = texture2DLodEXT( envMap, sampleUV, specularMIPLevel );
  162. #else
  163. vec4 envMapColor = texture2D( envMap, sampleUV, specularMIPLevel );
  164. #endif
  165. #elif defined( ENVMAP_TYPE_SPHERE )
  166. vec3 reflectView = flipNormal * normalize((viewMatrix * vec4( reflectVec, 0.0 )).xyz + vec3(0.0,0.0,1.0));
  167. #ifdef TEXTURE_LOD_EXT
  168. vec4 envMapColor = texture2DLodEXT( envMap, reflectView.xy * 0.5 + 0.5, specularMIPLevel );
  169. #else
  170. vec4 envMapColor = texture2D( envMap, reflectView.xy * 0.5 + 0.5, specularMIPLevel );
  171. #endif
  172. #endif
  173. envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
  174. return envMapColor.rgb * envMapIntensity;
  175. }
  176. #endif