lights_pars.glsl 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 getDirectionalDirectLight( 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 getPointDirectLight( 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 *= calcLightAttenuation( 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 angleCos;
  55. float penumbra;
  56. int shadow;
  57. float shadowBias;
  58. float shadowRadius;
  59. vec2 shadowMapSize;
  60. };
  61. uniform SpotLight spotLights[ NUM_SPOT_LIGHTS ];
  62. IncidentLight getSpotDirectLight( 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 spotEffect = dot( directLight.direction, spotLight.direction );
  68. if ( all( bvec2( spotEffect > spotLight.angleCos, testLightInRange( lightDistance, spotLight.distance ) ) ) ) {
  69. float spotEffect = dot( spotLight.direction, directLight.direction );
  70. spotEffect *= clamp( ( spotEffect - spotLight.angleCos ) / spotLight.penumbra, 0.0, 1.0 );
  71. directLight.color = spotLight.color;
  72. directLight.color *= ( spotEffect * calcLightAttenuation( lightDistance, spotLight.distance, spotLight.decay ) );
  73. directLight.visible = true;
  74. } else {
  75. directLight.color = vec3( 0.0 );
  76. directLight.visible = false;
  77. }
  78. return directLight;
  79. }
  80. #endif
  81. #if NUM_HEMI_LIGHTS > 0
  82. struct HemisphereLight {
  83. vec3 direction;
  84. vec3 skyColor;
  85. vec3 groundColor;
  86. };
  87. uniform HemisphereLight hemisphereLights[ NUM_HEMI_LIGHTS ];
  88. vec3 getHemisphereLightIrradiance( const in HemisphereLight hemiLight, const in GeometricContext geometry ) {
  89. float dotNL = dot( geometry.normal, hemiLight.direction );
  90. float hemiDiffuseWeight = 0.5 * dotNL + 0.5;
  91. return PI * mix( hemiLight.groundColor, hemiLight.skyColor, hemiDiffuseWeight );
  92. }
  93. #endif
  94. #if defined( USE_ENVMAP ) && defined( STANDARD )
  95. vec3 getLightProbeIndirectIrradiance( /*const in SpecularLightProbe specularLightProbe,*/ const in GeometricContext geometry, const in int maxMIPLevel ) {
  96. #ifdef DOUBLE_SIDED
  97. float flipNormal = ( float( gl_FrontFacing ) * 2.0 - 1.0 );
  98. #else
  99. float flipNormal = 1.0;
  100. #endif
  101. vec3 worldNormal = inverseTransformDirection( geometry.normal, viewMatrix );
  102. #ifdef ENVMAP_TYPE_CUBE
  103. vec3 queryVec = flipNormal * vec3( flipEnvMap * worldNormal.x, worldNormal.yz );
  104. // TODO: replace with properly filtered cubemaps and access the irradiance LOD level, be it the last LOD level
  105. // of a specular cubemap, or just the default level of a specially created irradiance cubemap.
  106. #ifdef TEXTURE_LOD_EXT
  107. vec4 envMapColor = textureCubeLodEXT( envMap, queryVec, float( maxMIPLevel ) );
  108. #else
  109. // force the bias high to get the last LOD level as it is the most blurred.
  110. vec4 envMapColor = textureCube( envMap, queryVec, float( maxMIPLevel ) );
  111. #endif
  112. #elif defined( ENVMAP_TYPE_CUBE_UV )
  113. vec3 queryVec = flipNormal * vec3( flipEnvMap * worldNormal.x, worldNormal.yz );
  114. vec4 envMapColor = textureCubeUV(queryVec, 1.0, 1024.0);
  115. #else
  116. vec3 envMapColor = vec3( 0.0 );
  117. #endif
  118. envMapColor.rgb = inputToLinear( envMapColor.rgb );
  119. return PI * envMapColor.rgb * envMapIntensity;
  120. }
  121. // taken from here: http://casual-effects.blogspot.ca/2011/08/plausible-environment-lighting-in-two.html
  122. float getSpecularMIPLevel( const in float blinnShininessExponent, const in int maxMIPLevel ) {
  123. //float envMapWidth = pow( 2.0, maxMIPLevelScalar );
  124. //float desiredMIPLevel = log2( envMapWidth * sqrt( 3.0 ) ) - 0.5 * log2( square( blinnShininessExponent ) + 1.0 );
  125. float maxMIPLevelScalar = float( maxMIPLevel );
  126. float desiredMIPLevel = maxMIPLevelScalar - 0.79248 - 0.5 * log2( square( blinnShininessExponent ) + 1.0 );
  127. // clamp to allowable LOD ranges.
  128. return clamp( desiredMIPLevel, 0.0, maxMIPLevelScalar );
  129. }
  130. vec3 getLightProbeIndirectRadiance( /*const in SpecularLightProbe specularLightProbe,*/ const in GeometricContext geometry, const in float blinnShininessExponent, const in int maxMIPLevel ) {
  131. #ifdef ENVMAP_MODE_REFLECTION
  132. vec3 reflectVec = reflect( -geometry.viewDir, geometry.normal );
  133. #else
  134. vec3 reflectVec = refract( -geometry.viewDir, geometry.normal, refractionRatio );
  135. #endif
  136. #ifdef DOUBLE_SIDED
  137. float flipNormal = ( float( gl_FrontFacing ) * 2.0 - 1.0 );
  138. #else
  139. float flipNormal = 1.0;
  140. #endif
  141. reflectVec = inverseTransformDirection( reflectVec, viewMatrix );
  142. float specularMIPLevel = getSpecularMIPLevel( blinnShininessExponent, maxMIPLevel );
  143. #ifdef ENVMAP_TYPE_CUBE
  144. vec3 queryReflectVec = flipNormal * vec3( flipEnvMap * reflectVec.x, reflectVec.yz );
  145. #ifdef TEXTURE_LOD_EXT
  146. vec4 envMapColor = textureCubeLodEXT( envMap, queryReflectVec, specularMIPLevel );
  147. #else
  148. vec4 envMapColor = textureCube( envMap, queryReflectVec, specularMIPLevel );
  149. #endif
  150. #elif defined( ENVMAP_TYPE_CUBE_UV )
  151. vec3 queryReflectVec = flipNormal * vec3( flipEnvMap * reflectVec.x, reflectVec.yz );
  152. vec4 envMapColor = textureCubeUV(queryReflectVec, BlinnExponentToGGXRoughness(blinnShininessExponent), 1024.0);
  153. #elif defined( ENVMAP_TYPE_EQUIREC )
  154. vec2 sampleUV;
  155. sampleUV.y = saturate( flipNormal * reflectVec.y * 0.5 + 0.5 );
  156. sampleUV.x = atan( flipNormal * reflectVec.z, flipNormal * reflectVec.x ) * RECIPROCAL_PI2 + 0.5;
  157. #ifdef TEXTURE_LOD_EXT
  158. vec4 envMapColor = texture2DLodEXT( envMap, sampleUV, specularMIPLevel );
  159. #else
  160. vec4 envMapColor = texture2D( envMap, sampleUV, specularMIPLevel );
  161. #endif
  162. #elif defined( ENVMAP_TYPE_SPHERE )
  163. vec3 reflectView = flipNormal * normalize((viewMatrix * vec4( reflectVec, 0.0 )).xyz + vec3(0.0,0.0,1.0));
  164. #ifdef TEXTURE_LOD_EXT
  165. vec4 envMapColor = texture2DLodEXT( envMap, reflectView.xy * 0.5 + 0.5, specularMIPLevel );
  166. #else
  167. vec4 envMapColor = texture2D( envMap, reflectView.xy * 0.5 + 0.5, specularMIPLevel );
  168. #endif
  169. #endif
  170. envMapColor.rgb = inputToLinear( envMapColor.rgb );
  171. return envMapColor.rgb * envMapIntensity;
  172. }
  173. #endif