HBAO_Composite.cginc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #ifndef HBAO_COMPOSITE_INCLUDED
  2. #define HBAO_COMPOSITE_INCLUDED
  3. #include "UnityCG.cginc"
  4. #include "HBAO_Common.cginc"
  5. inline half4 FetchOcclusion(float2 uv) {
  6. return UNITY_SAMPLE_SCREENSPACE_TEXTURE(_HBAOTex, uv * _TargetScale.zw);
  7. }
  8. inline half4 FetchSceneColor(float2 uv) {
  9. return UNITY_SAMPLE_SCREENSPACE_TEXTURE(_MainTex, uv);
  10. }
  11. inline half4 FetchGBuffer0(float2 uv) {
  12. return UNITY_SAMPLE_SCREENSPACE_TEXTURE(_TempTex, uv);
  13. }
  14. inline half4 FetchGBuffer3(float2 uv) {
  15. return UNITY_SAMPLE_SCREENSPACE_TEXTURE(_MainTex, uv);
  16. }
  17. inline half3 MultiBounceAO(float visibility, half3 albedo) {
  18. half3 a = 2.0404 * albedo - 0.3324;
  19. half3 b = -4.7951 * albedo + 0.6417;
  20. half3 c = 2.7552 * albedo + 0.6903;
  21. float x = visibility;
  22. return max(x, ((x * a + b) * x + c) * x);
  23. }
  24. half4 Composite_Frag(Varyings input) : SV_Target
  25. {
  26. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  27. //uint2 positionSS = input.uv * _ScreenSize.xy;
  28. half4 ao = FetchOcclusion(input.uv);
  29. ao.a = saturate(pow(abs(ao.a), _Intensity));
  30. half3 aoColor = lerp(_BaseColor.rgb, half3(1.0, 1.0, 1.0), ao.a);
  31. half4 col = FetchSceneColor(input.uv);
  32. #if LIGHTING_LOG_ENCODED
  33. col.rgb = -log2(col.rgb);
  34. #endif
  35. #if MULTIBOUNCE
  36. aoColor = lerp(aoColor, MultiBounceAO(ao.a, lerp(col.rgb, _BaseColor.rgb, _BaseColor.rgb)), _MultiBounceInfluence);
  37. #endif
  38. col.rgb *= aoColor;
  39. #if COLOR_BLEEDING
  40. //col.rgb += 1 - ao.rgb;
  41. col.rgb += ao.rgb;
  42. #endif
  43. #if LIGHTING_LOG_ENCODED
  44. col.rgb = exp2(-col.rgb);
  45. #endif
  46. #if DEBUG_AO
  47. col.rgb = aoColor;
  48. #elif DEBUG_COLORBLEEDING && COLOR_BLEEDING
  49. //col.rgb = 1 - ao.rgb;
  50. col.rgb = ao.rgb;
  51. #elif DEBUG_NOAO_AO || DEBUG_AO_AOONLY || DEBUG_NOAO_AOONLY
  52. if (input.uv.x <= 0.4985) {
  53. #if DEBUG_NOAO_AO || DEBUG_NOAO_AOONLY
  54. col = FetchSceneColor(input.uv);
  55. #endif // DEBUG_NOAO_AO || DEBUG_NOAO_AOONLY
  56. return col;
  57. }
  58. if (input.uv.x > 0.4985 && input.uv.x < 0.5015) {
  59. return half4(0, 0, 0, 1);
  60. }
  61. #if DEBUG_AO_AOONLY || DEBUG_NOAO_AOONLY
  62. col.rgb = aoColor;
  63. #endif // DEBUG_AO_AOONLY) || DEBUG_NOAO_AOONLY
  64. #endif // DEBUG_AO
  65. return col;
  66. }
  67. struct CombinedOutput {
  68. half4 gbuffer0 : SV_Target0; // albedo (RGB), occlusion (A)
  69. half4 gbuffer3 : SV_Target1; // emission (RGB), unused(A)
  70. };
  71. CombinedOutput Composite_Lit_Frag(Varyings input)
  72. {
  73. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  74. half4 ao = FetchOcclusion(input.uv);
  75. ao.a = saturate(pow(abs(ao.a), _Intensity));
  76. half3 aoColor = lerp(_BaseColor.rgb, half3(1.0, 1.0, 1.0), ao.a);
  77. half4 albedoOcc = FetchGBuffer0(input.uv);
  78. half4 emission = FetchGBuffer3(input.uv);
  79. #if LIGHTING_LOG_ENCODED
  80. emission.rgb = -log2(emission.rgb);
  81. #endif
  82. CombinedOutput o;
  83. o.gbuffer0 = half4(albedoOcc.rgb, albedoOcc.a * ao.a);
  84. o.gbuffer3 = half4(emission.rgb * lerp(aoColor, half3(1.0, 1.0, 1.0), saturate((emission.r + emission.g + emission.b) / 3)), emission.a);
  85. #if COLOR_BLEEDING
  86. //o.gbuffer3.rgb += 1 - ao.rgb;
  87. o.gbuffer3.rgb += ao.rgb;
  88. #endif
  89. #if LIGHTING_LOG_ENCODED
  90. o.gbuffer3.rgb = exp2(-o.gbuffer3.rgb);
  91. #endif
  92. return o;
  93. }
  94. #endif // HBAO_COMPOSITE_INCLUDED