Hi there,
I am trying to use Unity for a research application and am having a bit of difficulty sorting things out. (not very well versed with Unity in general)
Background:
I have a scene with occluding objects (for testing, a few capsules and spheres) which is viewed by the main camera (orthographic, far-near: [2,1], sitting at (-30,0,-1) looking along z+ axis).
There is another camera (same config, sitting at (0,0,-1)) pointed at a group of objects without occlusion and which renders to a RTT (set to color format Depth) .
What I need to do is to only draw content from the first group of objects which are at the same depth as the objects in the second group.
At the moment, I have a post-processing script attached to the main camera (built using the tutorial https://www.ronja-tutorials.com/post/017-postprocessing-depth/ ). The shader compares the depths from the RTT passed to the shader and the depth texture (_CameraDepthTexture) and pulls color from the _MainTexture if the depths match.
Obviously, this doesn’t work for occluded objects as the _CameraDepthTexture has no information about occulded objects and the _MainTexture has no information about the color either. But overall, this configuration works for non-occluded objects in the main camera view.
Attempt 2, the idea is to somehow get the vertex distance from the camera in the main scene and compare it with the RTT depth. If the depth matches, then “somehow” get the color also.
The first stumbling block is to get distance of the vertex from camera into the fragment shader. I found this Unity - Manual: Cameras and depth textures (which doesn’t work out of the box itself).
Attempt 3-ish, for object color, to use a surface shader ( https://www.ronja-tutorials.com/post/021-plane-clipping/ ) by replacing the plane configuration with RTT.
Can anyone point me to an alternative method or something that I am getting wrong?
Thanks.
—The shader code—
Shader "Custom/Backup-RTTMainCam"{
//show values to edit in inspector
Properties{
[HideInInspector]_MainTex ("Texture", 2D) = "white" {}
_RTTDepth ("Texture", 2D) = "white" {}
}
SubShader{
// markers that specify that we don't need culling
// or reading/writing to the depth buffer
Cull Off
ZWrite Off
ZTest Always
Pass{
CGPROGRAM
//include useful shader functions
#include "UnityCG.cginc"
//define vertex and fragment shader
#pragma vertex vert
#pragma fragment frag
//texture and transforms of the texture
sampler2D _MainTex;
//the depth texture
sampler2D _CameraDepthTexture;
//source depth texture
sampler2D _RTTDepth;
//the object data that's put into the vertex shader
struct appdata{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
//the data that's used to generate fragments and can be read by the fragment shader
struct v2f{
float4 position : SV_POSITION;
float2 uv : TEXCOORD0;
};
//the vertex shader
v2f vert(appdata v){
v2f o;
//convert the vertex positions from object space to clip space so they can be rendered
o.position = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
//the fragment shader
fixed4 frag(v2f i) : SV_TARGET{
//get source color from texture
fixed4 col = tex2D(_MainTex, i.uv);
float2 dPos = float2(i.uv.x, 0.5);
float depth = tex2D(_CameraDepthTexture, i.uv).r;
float dSlice = tex2D(_RTTDepth, dPos).r;
float2 dist = float2(LinearEyeDepth(depth)-1,LinearEyeDepth(dSlice)-1);
if(abs(dist.x-dist.y)>0.001) col=0;
if(dist.x==1 ||dist.y==1) col=0;
//return depth;
return col;
}
ENDCG
}
}
}