Hi, i am quite new in programming shaders so please bear with me.
I have a prerendered 360 fractal color + zbuffer texture.
My goal is to clip objects based on the fractal zbuffer.
It kinda works, but when I rotate the camera the position of the sphere shifts.
I created the camera shader below to occlude the objects.
Could somebody please tell me what I am missing?
Many thanks!
Shader "Custom/CameraClipEffect"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_ZBufferTex("Texture", 2D) = "white" {}
_ColorTex("Texture", 2D) = "white" {}
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
sampler2D _CameraDepthTexture;
sampler2D _ZBufferTex;
sampler2D _ColorTex;
fixed _DepthPower;
fixed4 frag (v2f i) : SV_Target
{
fixed4 output;
fixed4 colorCam = tex2D(_MainTex, i.uv);
fixed4 zbufferTex = tex2D(_ZBufferTex, i.uv);
zbufferTex = 1 - zbufferTex;
fixed4 colorTex = tex2D(_ColorTex, i.uv);
// get lineardepth value
float d = UNITY_SAMPLE_DEPTH(tex2D(_CameraDepthTexture, i.uv));
d = Linear01Depth(d);
// compare depth value with zbuffer r channel
if (d - zbufferTex.g < 0) {
output = colorCam;
}
else
{
output = colorTex;
}
return output;
}
ENDCG
}
}
}