Surface.cginc 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #ifndef VOLUMETRIC_FOG_2_SURFACE
  2. #define VOLUMETRIC_FOG_2_SURFACE
  3. #if VF2_SURFACE
  4. float4x4 _SurfaceCaptureMatrix;
  5. TEXTURE2D_FLOAT(_SurfaceDepthTexture);
  6. SAMPLER(sampler_SurfaceDepthTexture);
  7. float4 _SurfaceData;
  8. #define SURFACE_CAM_ALTITUDE _SurfaceData.x
  9. #define TERRAIN_FOG_HEIGHT _SurfaceData.y
  10. #define TERRAIN_FOG_MIN_ALTITUDE _SurfaceData.z
  11. #define TERRAIN_FOG_MAX_ALTITUDE _SurfaceData.w
  12. float4 surfaceTexCoordsStart, surfaceTexCoordsEnd;
  13. void SurfaceComputeEndPoints(float3 wposStart, float3 wposEnd) {
  14. #if defined(FOG_ROTATION)
  15. wposStart = Rotate(wposStart);
  16. wposEnd = Rotate(wposEnd);
  17. #endif
  18. surfaceTexCoordsStart = mul(_SurfaceCaptureMatrix, float4(wposStart, 1.0));
  19. surfaceTexCoordsStart.xy /= surfaceTexCoordsStart.w;
  20. surfaceTexCoordsEnd = mul(_SurfaceCaptureMatrix, float4(wposEnd, 1.0));
  21. surfaceTexCoordsEnd.xy /= surfaceTexCoordsEnd.w;
  22. }
  23. void SurfaceApply(inout float3 boundsCenter, inout float3 boundsExtents) {
  24. float2 surfaceTexCoords = lerp(surfaceTexCoordsStart.xy, surfaceTexCoordsEnd.xy, loop_t);
  25. float surfaceDepth = SAMPLE_TEXTURE2D_LOD(_SurfaceDepthTexture, sampler_SurfaceDepthTexture, surfaceTexCoords, 0).r;
  26. #if UNITY_REVERSED_Z
  27. surfaceDepth = 1.0 - surfaceDepth;
  28. #endif
  29. boundsExtents.y = TERRAIN_FOG_HEIGHT;
  30. boundsCenter.y = clamp(SURFACE_CAM_ALTITUDE - surfaceDepth * 10000, TERRAIN_FOG_MIN_ALTITUDE, TERRAIN_FOG_MAX_ALTITUDE);
  31. }
  32. #else
  33. #define SurfaceComputeEndPoints(wposStart, wposEnd)
  34. #define SurfaceApply(boundsCenter, boundsExtents)
  35. #endif // VF2_SURFACE
  36. #endif // VOLUMETRIC_FOG_2_SURFACE