envmap_physical_pars_fragment.glsl.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. export default /* glsl */`
  2. #if defined( USE_ENVMAP )
  3. #ifdef ENVMAP_MODE_REFRACTION
  4. uniform float refractionRatio;
  5. #endif
  6. vec3 getLightProbeIndirectIrradiance( /*const in SpecularLightProbe specularLightProbe,*/ const in GeometricContext geometry, const in int maxMIPLevel ) {
  7. vec3 worldNormal = inverseTransformDirection( geometry.normal, viewMatrix );
  8. #ifdef ENVMAP_TYPE_CUBE
  9. vec3 queryVec = vec3( flipEnvMap * worldNormal.x, worldNormal.yz );
  10. // TODO: replace with properly filtered cubemaps and access the irradiance LOD level, be it the last LOD level
  11. // of a specular cubemap, or just the default level of a specially created irradiance cubemap.
  12. #ifdef TEXTURE_LOD_EXT
  13. vec4 envMapColor = textureCubeLodEXT( envMap, queryVec, float( maxMIPLevel ) );
  14. #else
  15. // force the bias high to get the last LOD level as it is the most blurred.
  16. vec4 envMapColor = textureCube( envMap, queryVec, float( maxMIPLevel ) );
  17. #endif
  18. envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
  19. #elif defined( ENVMAP_TYPE_CUBE_UV )
  20. vec4 envMapColor = textureCubeUV( envMap, worldNormal, 1.0 );
  21. #else
  22. vec4 envMapColor = vec4( 0.0 );
  23. #endif
  24. return PI * envMapColor.rgb * envMapIntensity;
  25. }
  26. // Trowbridge-Reitz distribution to Mip level, following the logic of http://casual-effects.blogspot.ca/2011/08/plausible-environment-lighting-in-two.html
  27. float getSpecularMIPLevel( const in float roughness, const in int maxMIPLevel ) {
  28. float maxMIPLevelScalar = float( maxMIPLevel );
  29. float sigma = PI * roughness * roughness / ( 1.0 + roughness );
  30. float desiredMIPLevel = maxMIPLevelScalar + log2( sigma );
  31. // clamp to allowable LOD ranges.
  32. return clamp( desiredMIPLevel, 0.0, maxMIPLevelScalar );
  33. }
  34. vec3 getLightProbeIndirectRadiance( /*const in SpecularLightProbe specularLightProbe,*/ const in vec3 viewDir, const in vec3 normal, const in float roughness, const in int maxMIPLevel ) {
  35. #ifdef ENVMAP_MODE_REFLECTION
  36. vec3 reflectVec = reflect( -viewDir, normal );
  37. // Mixing the reflection with the normal is more accurate and keeps rough objects from gathering light from behind their tangent plane.
  38. reflectVec = normalize( mix( reflectVec, normal, roughness * roughness) );
  39. #else
  40. vec3 reflectVec = refract( -viewDir, normal, refractionRatio );
  41. #endif
  42. reflectVec = inverseTransformDirection( reflectVec, viewMatrix );
  43. float specularMIPLevel = getSpecularMIPLevel( roughness, maxMIPLevel );
  44. #ifdef ENVMAP_TYPE_CUBE
  45. vec3 queryReflectVec = vec3( flipEnvMap * reflectVec.x, reflectVec.yz );
  46. #ifdef TEXTURE_LOD_EXT
  47. vec4 envMapColor = textureCubeLodEXT( envMap, queryReflectVec, specularMIPLevel );
  48. #else
  49. vec4 envMapColor = textureCube( envMap, queryReflectVec, specularMIPLevel );
  50. #endif
  51. envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
  52. #elif defined( ENVMAP_TYPE_CUBE_UV )
  53. vec4 envMapColor = textureCubeUV( envMap, reflectVec, roughness );
  54. #elif defined( ENVMAP_TYPE_EQUIREC )
  55. vec2 sampleUV = equirectUv( reflectVec );
  56. #ifdef TEXTURE_LOD_EXT
  57. vec4 envMapColor = texture2DLodEXT( envMap, sampleUV, specularMIPLevel );
  58. #else
  59. vec4 envMapColor = texture2D( envMap, sampleUV, specularMIPLevel );
  60. #endif
  61. envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
  62. #endif
  63. return envMapColor.rgb * envMapIntensity;
  64. }
  65. #endif
  66. `;