envmap_physical_pars_fragment.glsl.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. vec3 queryVec = vec3( flipEnvMap * worldNormal.x, worldNormal.yz );
  21. vec4 envMapColor = textureCubeUV( envMap, queryVec, 1.0 );
  22. #else
  23. vec4 envMapColor = vec4( 0.0 );
  24. #endif
  25. return PI * envMapColor.rgb * envMapIntensity;
  26. }
  27. // taken from here: http://casual-effects.blogspot.ca/2011/08/plausible-environment-lighting-in-two.html
  28. float getSpecularMIPLevel( const in float blinnShininessExponent, const in int maxMIPLevel ) {
  29. //float envMapWidth = pow( 2.0, maxMIPLevelScalar );
  30. //float desiredMIPLevel = log2( envMapWidth * sqrt( 3.0 ) ) - 0.5 * log2( pow2( blinnShininessExponent ) + 1.0 );
  31. float maxMIPLevelScalar = float( maxMIPLevel );
  32. float desiredMIPLevel = maxMIPLevelScalar + 0.79248 - 0.5 * log2( pow2( blinnShininessExponent ) + 1.0 );
  33. // clamp to allowable LOD ranges.
  34. return clamp( desiredMIPLevel, 0.0, maxMIPLevelScalar );
  35. }
  36. vec3 getLightProbeIndirectRadiance( /*const in SpecularLightProbe specularLightProbe,*/ const in vec3 viewDir, const in vec3 normal, const in float blinnShininessExponent, const in int maxMIPLevel ) {
  37. #ifdef ENVMAP_MODE_REFLECTION
  38. vec3 reflectVec = reflect( -viewDir, normal );
  39. #else
  40. vec3 reflectVec = refract( -viewDir, normal, refractionRatio );
  41. #endif
  42. reflectVec = inverseTransformDirection( reflectVec, viewMatrix );
  43. float specularMIPLevel = getSpecularMIPLevel( blinnShininessExponent, 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. vec3 queryReflectVec = vec3( flipEnvMap * reflectVec.x, reflectVec.yz );
  54. vec4 envMapColor = textureCubeUV( envMap, queryReflectVec, BlinnExponentToGGXRoughness(blinnShininessExponent ));
  55. #elif defined( ENVMAP_TYPE_EQUIREC )
  56. vec2 sampleUV;
  57. sampleUV.y = asin( clamp( reflectVec.y, - 1.0, 1.0 ) ) * RECIPROCAL_PI + 0.5;
  58. sampleUV.x = atan( reflectVec.z, reflectVec.x ) * RECIPROCAL_PI2 + 0.5;
  59. #ifdef TEXTURE_LOD_EXT
  60. vec4 envMapColor = texture2DLodEXT( envMap, sampleUV, specularMIPLevel );
  61. #else
  62. vec4 envMapColor = texture2D( envMap, sampleUV, specularMIPLevel );
  63. #endif
  64. envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
  65. #elif defined( ENVMAP_TYPE_SPHERE )
  66. vec3 reflectView = normalize( ( viewMatrix * vec4( reflectVec, 0.0 ) ).xyz + vec3( 0.0,0.0,1.0 ) );
  67. #ifdef TEXTURE_LOD_EXT
  68. vec4 envMapColor = texture2DLodEXT( envMap, reflectView.xy * 0.5 + 0.5, specularMIPLevel );
  69. #else
  70. vec4 envMapColor = texture2D( envMap, reflectView.xy * 0.5 + 0.5, specularMIPLevel );
  71. #endif
  72. envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
  73. #endif
  74. return envMapColor.rgb * envMapIntensity;
  75. }
  76. #endif
  77. `;