AmbientOcclusion.hlsl 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #ifndef AMBIENT_OCCLUSION_INCLUDED
  2. #define AMBIENT_OCCLUSION_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  4. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/SurfaceData.hlsl"
  5. // Ambient occlusion
  6. TEXTURE2D_X(_ScreenSpaceOcclusionTexture);
  7. SAMPLER(sampler_ScreenSpaceOcclusionTexture);
  8. struct AmbientOcclusionFactor
  9. {
  10. half indirectAmbientOcclusion;
  11. half directAmbientOcclusion;
  12. };
  13. half SampleAmbientOcclusion(float2 normalizedScreenSpaceUV)
  14. {
  15. float2 uv = UnityStereoTransformScreenSpaceTex(normalizedScreenSpaceUV);
  16. return half(SAMPLE_TEXTURE2D_X(_ScreenSpaceOcclusionTexture, sampler_ScreenSpaceOcclusionTexture, uv).x);
  17. }
  18. AmbientOcclusionFactor GetScreenSpaceAmbientOcclusion(float2 normalizedScreenSpaceUV)
  19. {
  20. AmbientOcclusionFactor aoFactor;
  21. #if defined(_SCREEN_SPACE_OCCLUSION) && !defined(_SURFACE_TYPE_TRANSPARENT)
  22. float ssao = SampleAmbientOcclusion(normalizedScreenSpaceUV);
  23. aoFactor.indirectAmbientOcclusion = ssao;
  24. aoFactor.directAmbientOcclusion = lerp(half(1.0), ssao, _AmbientOcclusionParam.w);
  25. #else
  26. aoFactor.directAmbientOcclusion = 1;
  27. aoFactor.indirectAmbientOcclusion = 1;
  28. #endif
  29. #if defined(DEBUG_DISPLAY)
  30. switch(_DebugLightingMode)
  31. {
  32. case DEBUGLIGHTINGMODE_LIGHTING_WITHOUT_NORMAL_MAPS:
  33. aoFactor.directAmbientOcclusion = 0.5;
  34. aoFactor.indirectAmbientOcclusion = 0.5;
  35. break;
  36. case DEBUGLIGHTINGMODE_LIGHTING_WITH_NORMAL_MAPS:
  37. aoFactor.directAmbientOcclusion *= 0.5;
  38. aoFactor.indirectAmbientOcclusion *= 0.5;
  39. break;
  40. }
  41. #endif
  42. return aoFactor;
  43. }
  44. AmbientOcclusionFactor CreateAmbientOcclusionFactor(float2 normalizedScreenSpaceUV, half occlusion)
  45. {
  46. AmbientOcclusionFactor aoFactor = GetScreenSpaceAmbientOcclusion(normalizedScreenSpaceUV);
  47. aoFactor.indirectAmbientOcclusion = min(aoFactor.indirectAmbientOcclusion, occlusion);
  48. return aoFactor;
  49. }
  50. AmbientOcclusionFactor CreateAmbientOcclusionFactor(InputData inputData, SurfaceData surfaceData)
  51. {
  52. return CreateAmbientOcclusionFactor(inputData.normalizedScreenSpaceUV, surfaceData.occlusion);
  53. }
  54. #endif