pbrBlockReflectivity.fx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. struct reflectivityOutParams
  2. {
  3. float microSurface;
  4. float roughness;
  5. vec3 surfaceReflectivityColor;
  6. #ifdef METALLICWORKFLOW
  7. vec3 surfaceAlbedo;
  8. #endif
  9. #if defined(METALLICWORKFLOW) && defined(REFLECTIVITY) && defined(AOSTOREINMETALMAPRED)
  10. vec3 ambientOcclusionColor;
  11. #endif
  12. #if DEBUGMODE > 0
  13. vec4 surfaceMetallicColorMap;
  14. vec4 surfaceReflectivityColorMap;
  15. vec2 metallicRoughness;
  16. vec3 metallicF0;
  17. #endif
  18. };
  19. void reflectivityBlock(
  20. const in vec4 vReflectivityColor,
  21. #ifdef METALLICWORKFLOW
  22. const in vec3 surfaceAlbedo,
  23. #endif
  24. #ifdef REFLECTIVITY
  25. const in vec3 vReflectivityInfos,
  26. const in vec4 surfaceMetallicOrReflectivityColorMap,
  27. #endif
  28. #if defined(METALLICWORKFLOW) && defined(REFLECTIVITY) && defined(AOSTOREINMETALMAPRED)
  29. const in vec3 ambientOcclusionColor,
  30. #endif
  31. #ifdef MICROSURFACEMAP
  32. const in vec4 microSurfaceTexel,
  33. #endif
  34. out reflectivityOutParams outParams
  35. )
  36. {
  37. float microSurface = vReflectivityColor.a;
  38. vec3 surfaceReflectivityColor = vReflectivityColor.rgb;
  39. #ifdef METALLICWORKFLOW
  40. vec2 metallicRoughness = surfaceReflectivityColor.rg;
  41. #ifdef REFLECTIVITY
  42. #if DEBUGMODE > 0
  43. outParams.surfaceMetallicColorMap = surfaceMetallicOrReflectivityColorMap;
  44. #endif
  45. #ifdef AOSTOREINMETALMAPRED
  46. vec3 aoStoreInMetalMap = vec3(surfaceMetallicOrReflectivityColorMap.r, surfaceMetallicOrReflectivityColorMap.r, surfaceMetallicOrReflectivityColorMap.r);
  47. outParams.ambientOcclusionColor = mix(ambientOcclusionColor, aoStoreInMetalMap, vReflectivityInfos.z);
  48. #endif
  49. #ifdef METALLNESSSTOREINMETALMAPBLUE
  50. metallicRoughness.r *= surfaceMetallicOrReflectivityColorMap.b;
  51. #else
  52. metallicRoughness.r *= surfaceMetallicOrReflectivityColorMap.r;
  53. #endif
  54. #ifdef ROUGHNESSSTOREINMETALMAPALPHA
  55. metallicRoughness.g *= surfaceMetallicOrReflectivityColorMap.a;
  56. #else
  57. #ifdef ROUGHNESSSTOREINMETALMAPGREEN
  58. metallicRoughness.g *= surfaceMetallicOrReflectivityColorMap.g;
  59. #endif
  60. #endif
  61. #endif
  62. #ifdef MICROSURFACEMAP
  63. metallicRoughness.g *= microSurfaceTexel.r;
  64. #endif
  65. #if DEBUGMODE > 0
  66. outParams.metallicRoughness = metallicRoughness;
  67. #endif
  68. #define CUSTOM_FRAGMENT_UPDATE_METALLICROUGHNESS
  69. // Compute microsurface from roughness.
  70. microSurface = 1.0 - metallicRoughness.g;
  71. // Diffuse is used as the base of the reflectivity.
  72. vec3 baseColor = surfaceAlbedo;
  73. #ifdef REFLECTANCE
  74. // *** NOT USED ANYMORE ***
  75. // Following Frostbite Remapping,
  76. // https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf page 115
  77. // vec3 f0 = 0.16 * reflectance * reflectance * (1.0 - metallic) + baseColor * metallic;
  78. // where 0.16 * reflectance * reflectance remaps the reflectance to allow storage in 8 bit texture
  79. // Compute the converted diffuse.
  80. outParams.surfaceAlbedo = baseColor.rgb * (1.0 - metallicRoughness.r);
  81. // Compute the converted reflectivity.
  82. surfaceReflectivityColor = mix(0.16 * reflectance * reflectance, baseColor, metallicRoughness.r);
  83. #else
  84. vec3 metallicF0 = vec3(vReflectivityColor.a, vReflectivityColor.a, vReflectivityColor.a);
  85. #ifdef METALLICF0FACTORFROMMETALLICMAP
  86. #ifdef REFLECTIVITY
  87. metallicF0 *= surfaceMetallicOrReflectivityColorMap.a;
  88. #endif
  89. #endif
  90. #if DEBUGMODE > 0
  91. outParams.metallicF0 = metallicF0;
  92. #endif
  93. // Compute the converted diffuse.
  94. outParams.surfaceAlbedo = mix(baseColor.rgb * (1.0 - metallicF0.r), vec3(0., 0., 0.), metallicRoughness.r);
  95. // Compute the converted reflectivity.
  96. surfaceReflectivityColor = mix(metallicF0, baseColor, metallicRoughness.r);
  97. #endif
  98. #else
  99. #ifdef REFLECTIVITY
  100. surfaceReflectivityColor *= surfaceMetallicOrReflectivityColorMap.rgb;
  101. #if DEBUGMODE > 0
  102. outParams.surfaceReflectivityColorMap = surfaceMetallicOrReflectivityColorMap;
  103. #endif
  104. #ifdef MICROSURFACEFROMREFLECTIVITYMAP
  105. microSurface *= surfaceMetallicOrReflectivityColorMap.a;
  106. microSurface *= vReflectivityInfos.z;
  107. #else
  108. #ifdef MICROSURFACEAUTOMATIC
  109. microSurface *= computeDefaultMicroSurface(microSurface, surfaceReflectivityColor);
  110. #endif
  111. #ifdef MICROSURFACEMAP
  112. microSurface *= microSurfaceTexel.r;
  113. #endif
  114. #define CUSTOM_FRAGMENT_UPDATE_MICROSURFACE
  115. #endif
  116. #endif
  117. #endif
  118. // Adapt microSurface.
  119. microSurface = saturate(microSurface);
  120. // Compute roughness.
  121. float roughness = 1. - microSurface;
  122. outParams.microSurface = microSurface;
  123. outParams.roughness = roughness;
  124. outParams.surfaceReflectivityColor = surfaceReflectivityColor;
  125. }