How can I port Bgolus’ PostDepthToWorldPos logic to a surface shader? As a fragment shader the result outputs correctly, but when rewritten as a surface shader it simply flickers in response to the position/orientation of the camera. For reference, after stripping unrelated functionality, my surface shader port looks more or less like this:
Shader "Custom/Water"
{
Properties
{
}
SubShader
{
Tags { "RenderType"="Transparent" }
LOD 200
CGPROGRAM
#pragma surface surf Standard alpha addshadow fullforwardshadows vertex:vert
#pragma target 3.0
struct Input
{
float4 color : COLOR;
float4 pos : SV_POSITION;
};
UNITY_INSTANCING_BUFFER_START(Props)
UNITY_INSTANCING_BUFFER_END(Props)
void vert(inout appdata_full v)
{
}
UNITY_DECLARE_DEPTH_TEXTURE(_CameraDepthTexture);
float4 _CameraDepthTexture_TexelSize;
// Reconstructs view position from just the pixel position and the camera depth texture
// source: https://gist.github.com/bgolus/1933a1b0b4e1c422021a7baa2e6b745d
float3 viewSpacePosAtPixelPosition(float2 vpos)
{
float2 uv = vpos * _CameraDepthTexture_TexelSize.xy;
#if UNITY_UV_STARTS_AT_TOP
uv.y = (1.0 - uv.y);
uv.y = (uv.y - 0.5) * _ProjectionParams.x + 0.5;
#endif
float3 viewSpaceRay = mul(unity_CameraInvProjection, float4(uv * 2.0 - 1.0, 1.0, 1.0) * _ProjectionParams.z);
float rawDepth = SAMPLE_DEPTH_TEXTURE_LOD(_CameraDepthTexture, float4(uv, 0.0, 0.0));
return viewSpaceRay * Linear01Depth(rawDepth);
}
void surf (Input IN, inout SurfaceOutputStandard o)
{
float3 viewPos = viewSpacePosAtPixelPosition(IN.pos.xy);
// Camera space and view space are flipped on the Z axis
float3 worldPos = mul(unity_CameraToWorld, float4(viewPos * float3(1.0, 1.0,-1.0), 1.0));
o.Albedo = (1 - saturate((3 + worldPos.y) * 1.5));
}
ENDCG
}
}