CalmWater_Refraction.cginc 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #ifndef CALMWATER_REFRACTION_INCLUDED
  2. #define CALMWATER_REFRACTION_INCLUDED
  3. void ComputeRefraction(inout GlobalData data, v2f IN)
  4. {
  5. float2 offset = data.worldNormal.xz * _GrabTexture_TexelSize.xy * _Distortion;
  6. // Depth Distortion ===================================================
  7. data.DepthUV = OffsetDepth(IN.DepthUV, offset);
  8. // GrabPass Distortion ================================================
  9. data.GrabUV = OffsetUV(IN.GrabUV, offset);
  10. // Refraction ============================================================
  11. // RGB = Color
  12. // A = Depth
  13. // =======================================================================
  14. float4 refraction = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_GrabTexture, data.GrabUV.xy / data.GrabUV.w);
  15. float4 cleanRefraction = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_GrabTexture, IN.GrabUV.xy / IN.GrabUV.w);
  16. //Depth Texture Clean
  17. data.cleanDepth = texDepth(IN.DepthUV);
  18. //Depth Texture Distorted
  19. data.depth = texDepth(data.DepthUV);
  20. //Depth
  21. refraction.a = DistanceFade(data.depth, data.DepthUV.z, _DepthStart, _DepthEnd);
  22. //Clean Depth
  23. cleanRefraction.a = DistanceFade(data.cleanDepth, IN.DepthUV.z, _DepthStart, _DepthEnd);
  24. //TODO: Remove keyword in cull front pass and remove check CULL_FRONT here
  25. #ifndef CULL_FRONT
  26. #if _DISTORTIONQUALITY_HIGH
  27. //Hide refraction from objects over the surface
  28. refraction = data.DepthUV.z > data.depth ? cleanRefraction : refraction;
  29. #endif
  30. #endif
  31. //Final color with depth and refraction
  32. #ifndef CULL_FRONT
  33. #if _DEPTHFOG_ON
  34. float3 finalColor = lerp(_Color.rgb * refraction.rgb, _DepthColor.rgb * _LightColor0.rgb, 1.0 - refraction.a);
  35. #else
  36. float3 finalColor = lerp(_Color.rgb, _DepthColor.rgb, 1.0 - refraction.a) * refraction.rgb;
  37. #endif
  38. #else
  39. float3 finalColor = lerp(_Color.rgb, _DepthColor.rgb, 0.5) * refraction.rgb;
  40. #endif
  41. data.refractedBuffers = refraction;
  42. data.cleanBuffers = cleanRefraction;
  43. data.finalColor = finalColor;
  44. }
  45. #endif