cubemap.chunk 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // for specular-gloss workflow which support by legacy compatible lighting
  2. float RoughnessToPerceptualRoughness(float roughness)
  3. {
  4. return sqrt(roughness);
  5. }
  6. // for legacy only
  7. #pragma extension([GL_OES_standard_derivatives, __VERSION__ < 110])
  8. vec3 EnvReflectionWithMipFiltering(vec3 R, float roughness, float mipCount, float denoiseIntensity) {
  9. #if CC_USE_IBL
  10. // simulate GGX convolution
  11. #if !CC_SURFACES_USE_LEGACY_COMPATIBLE_LIGHTING && !CC_IBL_CONVOLUTED
  12. roughness = RoughnessToPerceptualRoughness(roughness);
  13. #endif
  14. //#todo: add GL400 calcmip
  15. float mip = roughness * (mipCount - 1.0);
  16. float delta = (dot(dFdx(R), dFdy(R))) * 1000.0;
  17. float mipBias = mix(0.0, 5.0, clamp(delta, 0.0, 1.0));
  18. vec3 rotationDir = RotationVecFromAxisY(R.xyz, cc_surfaceTransform.z, cc_surfaceTransform.w);
  19. vec4 biased = fragTextureLod(cc_environment, rotationDir, mip + mipBias);
  20. vec4 filtered = texture(cc_environment, rotationDir);
  21. #if CC_USE_IBL == IBL_RGBE
  22. biased.rgb = unpackRGBE(biased);
  23. filtered.rgb = unpackRGBE(filtered);
  24. #else
  25. biased.rgb = SRGBToLinear(biased.rgb);
  26. filtered.rgb = SRGBToLinear(filtered.rgb);
  27. #endif
  28. return mix(biased.rgb, filtered.rgb, denoiseIntensity);
  29. #else
  30. return vec3(0.0, 0.0, 0.0);
  31. #endif
  32. }
  33. // for skybox IBL
  34. vec3 EnvReflection(samplerCube tex, vec3 R, float roughness, float mipCount) {
  35. // simulate GGX convolution
  36. #if !CC_SURFACES_USE_LEGACY_COMPATIBLE_LIGHTING && !CC_IBL_CONVOLUTED
  37. roughness = RoughnessToPerceptualRoughness(roughness);
  38. #endif
  39. vec3 rotationDir = RotationVecFromAxisY(R.xyz, cc_surfaceTransform.z, cc_surfaceTransform.w);
  40. vec4 envmap = fragTextureLod(tex, rotationDir, roughness * (mipCount - 1.0));
  41. #if CC_USE_IBL == IBL_RGBE
  42. return unpackRGBE(envmap);
  43. #else
  44. return SRGBToLinear(envmap.rgb);
  45. #endif
  46. }
  47. // for reflection probe
  48. vec3 EnvReflectionOfReflectionProbe(samplerCube tex, vec3 R, float roughness, float mipCount, bool isRGBE) {
  49. // simulate GGX convolution
  50. #if !CC_SURFACES_USE_LEGACY_COMPATIBLE_LIGHTING && !CC_IBL_CONVOLUTED
  51. roughness = RoughnessToPerceptualRoughness(roughness);
  52. #endif
  53. vec4 envmap = fragTextureLod(tex, R, roughness * (mipCount - 1.0));
  54. return isRGBE ? unpackRGBE(envmap) : SRGBToLinear(envmap.rgb);
  55. }