Hi in this project you can see how to downscale depth (im not the author). In that project the shader that does the depth downscaling is this
Shader "Custom/DownscaleDepth" {
CGINCLUDE
#include "UnityCG.cginc"
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
};
sampler2D _CameraDepthTexture;
float4 _CameraDepthTexture_TexelSize; // (1.0/width, 1.0/height, width, height)
v2f vert(appdata_img v)
{
v2f o = (v2f)0;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = v.texcoord;
return o;
}
float frag(v2f input) : SV_Target
{
float2 texelSize = 0.5 * _CameraDepthTexture_TexelSize.xy;
float2 taps[4] = { float2(input.uv + float2(-1,-1)*texelSize),
float2(input.uv + float2(-1,1)*texelSize),
float2(input.uv + float2(1,-1)*texelSize),
float2(input.uv + float2(1,1)*texelSize) };
float depth1 = tex2D(_CameraDepthTexture, taps[0]);
float depth2 = tex2D(_CameraDepthTexture, taps[1]);
float depth3 = tex2D(_CameraDepthTexture, taps[2]);
float depth4 = tex2D(_CameraDepthTexture, taps[3]);
float result = min(depth1, min(depth2, min(depth3, depth4)));
return result;
}
ENDCG
SubShader
{
Pass
{
ZTest Always Cull Off ZWrite Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
ENDCG
}
}
Fallback off
}
You just blit a downsampled rendertexture to the downscale shader’s material
//----DEPTH
RenderTextureFormat formatRF32 = RenderTextureFormat.RFloat;
int lowresDepthWidth = source.width / 4;
int lowresDepthHeight = source.height / 4;
RenderTexture lowresDepthRT = RenderTexture.GetTemporary(lowresDepthWidth, lowresDepthHeight, 0, formatRF32);
//downscale depth buffer to quarter resolution
Graphics.Blit(source, lowresDepthRT, DownscaleDepthMaterial);
Edit: Then you pass the downsampled depth to the raymarching shader
e.g.
FogMarchedMaterial.SetTexture("LowResDepth", lowresDepthRT);
and in the raymarching shader:
// read low res depth and reconstruct world position
float depth = SAMPLE_DEPTH_TEXTURE(LowResDepth, i.uv);
//linearise depth
float lindepth = Linear01Depth(depth);
//get view and then world positions
float4 viewPos = float4(i.cameraRay.xyz * lindepth,1);
float3 worldPos = mul(InverseViewMatrix, viewPos).xyz;
//get the ray direction in world space, raymarching is towards the camera
float3 rayDir = normalize(_WorldSpaceCameraPos.xyz - worldPos);
float rayDistance = length(_WorldSpaceCameraPos.xyz - worldPos);
.......
Just copied most relevant stuff in case you were bored to check the whole project