lights_pars.glsl 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. uniform vec3 ambientLightColor;
  2. vec3 getAmbientLightIrradiance( const in vec3 ambientLightColor ) {
  3. vec3 irradiance = ambientLightColor;
  4. #ifndef PHYSICALLY_CORRECT_LIGHTS
  5. irradiance *= PI;
  6. #endif
  7. return irradiance;
  8. }
  9. #if NUM_DIR_LIGHTS > 0
  10. struct DirectionalLight {
  11. vec3 direction;
  12. vec3 color;
  13. int shadow;
  14. float shadowBias;
  15. float shadowRadius;
  16. vec2 shadowMapSize;
  17. };
  18. uniform DirectionalLight directionalLights[ NUM_DIR_LIGHTS ];
  19. void getDirectionalDirectLightIrradiance( const in DirectionalLight directionalLight, const in GeometricContext geometry, out IncidentLight directLight ) {
  20. directLight.color = directionalLight.color;
  21. directLight.direction = directionalLight.direction;
  22. directLight.visible = true;
  23. }
  24. #endif
  25. #if NUM_POINT_LIGHTS > 0
  26. struct PointLight {
  27. vec3 position;
  28. vec3 color;
  29. float distance;
  30. float decay;
  31. int shadow;
  32. float shadowBias;
  33. float shadowRadius;
  34. vec2 shadowMapSize;
  35. };
  36. uniform PointLight pointLights[ NUM_POINT_LIGHTS ];
  37. // directLight is an out parameter as having it as a return value caused compiler errors on some devices
  38. void getPointDirectLightIrradiance( const in PointLight pointLight, const in GeometricContext geometry, out IncidentLight directLight ) {
  39. vec3 lVector = pointLight.position - geometry.position;
  40. directLight.direction = normalize( lVector );
  41. float lightDistance = length( lVector );
  42. directLight.color = pointLight.color;
  43. directLight.color *= punctualLightIntensityToIrradianceFactor( lightDistance, pointLight.distance, pointLight.decay );
  44. directLight.visible = ( directLight.color != vec3( 0.0 ) );
  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. // directLight is an out parameter as having it as a return value caused compiler errors on some devices
  63. void getSpotDirectLightIrradiance( const in SpotLight spotLight, const in GeometricContext geometry, out 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 ( angleCos > spotLight.coneCos ) {
  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. }
  78. #endif
  79. #if NUM_RECT_AREA_LIGHTS > 0
  80. struct RectAreaLight {
  81. vec3 color;
  82. vec3 position;
  83. vec3 halfWidth;
  84. vec3 halfHeight;
  85. };
  86. // Pre-computed values of LinearTransformedCosine approximation of BRDF
  87. // BRDF approximation Texture is 64x64
  88. uniform sampler2D ltcMat; // RGBA Float
  89. uniform sampler2D ltcMag; // Alpha Float (only has w component)
  90. uniform RectAreaLight rectAreaLights[ NUM_RECT_AREA_LIGHTS ];
  91. #endif
  92. #if NUM_HEMI_LIGHTS > 0
  93. struct HemisphereLight {
  94. vec3 direction;
  95. vec3 skyColor;
  96. vec3 groundColor;
  97. };
  98. uniform HemisphereLight hemisphereLights[ NUM_HEMI_LIGHTS ];
  99. vec3 getHemisphereLightIrradiance( const in HemisphereLight hemiLight, const in GeometricContext geometry ) {
  100. float dotNL = dot( geometry.normal, hemiLight.direction );
  101. float hemiDiffuseWeight = 0.5 * dotNL + 0.5;
  102. vec3 irradiance = mix( hemiLight.groundColor, hemiLight.skyColor, hemiDiffuseWeight );
  103. #ifndef PHYSICALLY_CORRECT_LIGHTS
  104. irradiance *= PI;
  105. #endif
  106. return irradiance;
  107. }
  108. #endif
  109. #if defined( USE_ENVMAP ) && defined( PHYSICAL )
  110. vec3 getLightProbeIndirectIrradiance( /*const in SpecularLightProbe specularLightProbe,*/ const in GeometricContext geometry, const in int maxMIPLevel ) {
  111. #include <normal_flip>
  112. vec3 worldNormal = inverseTransformDirection( geometry.normal, viewMatrix );
  113. #ifdef ENVMAP_TYPE_CUBE
  114. vec3 queryVec = flipNormal * vec3( flipEnvMap * worldNormal.x, worldNormal.yz );
  115. // TODO: replace with properly filtered cubemaps and access the irradiance LOD level, be it the last LOD level
  116. // of a specular cubemap, or just the default level of a specially created irradiance cubemap.
  117. #ifdef TEXTURE_LOD_EXT
  118. vec4 envMapColor = textureCubeLodEXT( envMap, queryVec, float( maxMIPLevel ) );
  119. #else
  120. // force the bias high to get the last LOD level as it is the most blurred.
  121. vec4 envMapColor = textureCube( envMap, queryVec, float( maxMIPLevel ) );
  122. #endif
  123. envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
  124. #elif defined( ENVMAP_TYPE_CUBE_UV )
  125. vec3 queryVec = flipNormal * vec3( flipEnvMap * worldNormal.x, worldNormal.yz );
  126. vec4 envMapColor = textureCubeUV( queryVec, 1.0 );
  127. #else
  128. vec4 envMapColor = vec4( 0.0 );
  129. #endif
  130. return PI * envMapColor.rgb * envMapIntensity;
  131. }
  132. // taken from here: http://casual-effects.blogspot.ca/2011/08/plausible-environment-lighting-in-two.html
  133. float getSpecularMIPLevel( const in float blinnShininessExponent, const in int maxMIPLevel ) {
  134. //float envMapWidth = pow( 2.0, maxMIPLevelScalar );
  135. //float desiredMIPLevel = log2( envMapWidth * sqrt( 3.0 ) ) - 0.5 * log2( pow2( blinnShininessExponent ) + 1.0 );
  136. float maxMIPLevelScalar = float( maxMIPLevel );
  137. float desiredMIPLevel = maxMIPLevelScalar - 0.79248 - 0.5 * log2( pow2( blinnShininessExponent ) + 1.0 );
  138. // clamp to allowable LOD ranges.
  139. return clamp( desiredMIPLevel, 0.0, maxMIPLevelScalar );
  140. }
  141. vec3 getLightProbeIndirectRadiance( /*const in SpecularLightProbe specularLightProbe,*/ const in GeometricContext geometry, const in float blinnShininessExponent, const in int maxMIPLevel ) {
  142. #ifdef ENVMAP_MODE_REFLECTION
  143. vec3 reflectVec = reflect( -geometry.viewDir, geometry.normal );
  144. #else
  145. vec3 reflectVec = refract( -geometry.viewDir, geometry.normal, refractionRatio );
  146. #endif
  147. #include <normal_flip>
  148. reflectVec = inverseTransformDirection( reflectVec, viewMatrix );
  149. float specularMIPLevel = getSpecularMIPLevel( blinnShininessExponent, maxMIPLevel );
  150. #ifdef ENVMAP_TYPE_CUBE
  151. vec3 queryReflectVec = flipNormal * vec3( flipEnvMap * reflectVec.x, reflectVec.yz );
  152. #ifdef TEXTURE_LOD_EXT
  153. vec4 envMapColor = textureCubeLodEXT( envMap, queryReflectVec, specularMIPLevel );
  154. #else
  155. vec4 envMapColor = textureCube( envMap, queryReflectVec, specularMIPLevel );
  156. #endif
  157. envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
  158. #elif defined( ENVMAP_TYPE_CUBE_UV )
  159. vec3 queryReflectVec = flipNormal * vec3( flipEnvMap * reflectVec.x, reflectVec.yz );
  160. vec4 envMapColor = textureCubeUV(queryReflectVec, BlinnExponentToGGXRoughness(blinnShininessExponent));
  161. #elif defined( ENVMAP_TYPE_EQUIREC )
  162. vec2 sampleUV;
  163. sampleUV.y = saturate( flipNormal * reflectVec.y * 0.5 + 0.5 );
  164. sampleUV.x = atan( flipNormal * reflectVec.z, flipNormal * reflectVec.x ) * RECIPROCAL_PI2 + 0.5;
  165. #ifdef TEXTURE_LOD_EXT
  166. vec4 envMapColor = texture2DLodEXT( envMap, sampleUV, specularMIPLevel );
  167. #else
  168. vec4 envMapColor = texture2D( envMap, sampleUV, specularMIPLevel );
  169. #endif
  170. envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
  171. #elif defined( ENVMAP_TYPE_SPHERE )
  172. vec3 reflectView = flipNormal * normalize( ( viewMatrix * vec4( reflectVec, 0.0 ) ).xyz + vec3( 0.0,0.0,1.0 ) );
  173. #ifdef TEXTURE_LOD_EXT
  174. vec4 envMapColor = texture2DLodEXT( envMap, reflectView.xy * 0.5 + 0.5, specularMIPLevel );
  175. #else
  176. vec4 envMapColor = texture2D( envMap, reflectView.xy * 0.5 + 0.5, specularMIPLevel );
  177. #endif
  178. envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
  179. #endif
  180. return envMapColor.rgb * envMapIntensity;
  181. }
  182. #endif