Yeah I tried different variations of <, <=, but I just get slightly different variations of the patchwork effect seen above.
I might try unifying the depth calculations in the command buffer shader and the surface shader. When rendering the depth to a texture I just calculate clip pos in the vertex shader and then return i.vertex.z in the frag shader which is rendered to the depth buffer. But when doing the calculation in the surface shader I have to use IN.clipPos.z / IN.screenPos.w for some reason.
Depth pass shader:
Shader "Unlit/OceanDepthPass"
{
SubShader
{
Tags
{
"RenderType" = "Opaque"
"Queue" = "Geometry"
}
Pass
{
ZWrite On
ZTest LEqual
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "WaterWaveUtils.cginc"
struct appdata
{
float4 vertex : POSITION;
float4 normal : NORMAL;
};
struct v2f
{
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
AddWaterWaves(v.vertex, v.normal.xyz);
o.vertex = UnityObjectToClipPos(v.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
return i.vertex.z;
}
ENDCG
}
}
}
Surface shader:
Shader "Custom/Ocean"
{
Properties
{
_WaterColour ("Water Colour", Color) = (0, 0, 0, 0)
_WaterFogColourLow ("Water Fog Colour Low", Color) = (0, 0, 0, 0)
_WaterFogColourHigh ("Water Fog Colour High", Color) = (0, 0, 0, 0)
_WaterMaxHeight ("Water High Colour End", float) = 10
_WaterFogDensity ("Water Fog Density", Range(0, 2)) = 0.1
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
_AtmosphereColour ("Atmosphere Colour", Color) = (0, 0, 0, 0)
}
SubShader
{
Tags { "RenderType" = "Transparent" "Queue" = "Transparent"}
Cull OFF
GrabPass { "_WaterBackground" }
CGPROGRAM
#include "WaterShaderUtils.cginc"
#include "CustomFog.cginc"
#include "WaterWaveUtils.cginc"
#pragma surface surf Standard alpha vertex:vert finalcolor:ResetAlpha
#pragma target 5.0
struct Input
{
float2 uv_MainTex;
float4 screenPos;
float3 worldPos;
float4 clipPos;
};
float4 _WaterColour;
half _Glossiness;
half _Metallic;
fixed4 _Color;
float4 _AtmosphereColour;
float _FullAtmosphereDepth;
sampler2D _OceanDepthPrePass;
void vert (inout appdata_full v, out Input i)
{
AddWaterWaves(v.vertex, v.normal);
UNITY_INITIALIZE_OUTPUT(Input, i);
i.screenPos = ComputeGrabScreenPos(UnityObjectToClipPos(v.vertex));
i.clipPos = UnityObjectToClipPos(v.vertex);
}
void surf (Input IN, inout SurfaceOutputStandard o)
{
float2 screenUV = IN.screenPos.xy / IN.screenPos.w;
if ((IN.clipPos.z / IN.screenPos.w) <= (SAMPLE_DEPTH_TEXTURE(_OceanDepthPrePass, screenUV) - 0.00001))
{
discard;
}
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Albedo = _WaterColour.rgb;
o.Alpha = _WaterColour.a;
float3 emissionCol = ColorBelowWater(IN.screenPos, IN.worldPos) * (1 - _WaterColour.a);
o.Emission = emissionCol;
}
void ResetAlpha (Input IN, SurfaceOutputStandard o, inout fixed4 colour)
{
float distance = length(_WorldSpaceCameraPos - IN.worldPos);
float depth = distance / _ProjectionParams.z;
colour.rgb = lerp(colour.rgb, _AtmosphereColour, saturate(depth / _FullAtmosphereDepth));
float3 fogColour = LerpToFog(colour.rgb, distance);
colour.rgb = fogColour;
colour.a = 1;
}
ENDCG
}
}