Hi everyone,
I am quite new at writing Camera effects and I am facing some issues. Here is my code.
Camera script
// A part of the camera script
void Start() {
Camera.main.depthTextureMode = DepthTextureMode.Depth;
}
void OnRenderImage(RenderTexture sourceTexture, RenderTexture destTexture) {
Graphics.Blit(sourceTexture, destTexture, material);
}
}
Shader
Shader "Debug/Test" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader {
Pass {
ZTest Always Cull Off ZWrite Off
Fog { Mode off }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
uniform sampler2D _MainTex;
uniform float4 _MainTex_TexelSize;
uniform sampler2D _CameraDepthTexture;
struct appdata // or appdata_img
{
float4 vertex : POSITION;
half2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 pos : SV_POSITION;
half2 uv[2] : TEXCOORD0;
};
v2f vert(appdata v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
half2 uv = MultiplyUV(UNITY_MATRIX_TEXTURE0, v.texcoord);
o.uv[0] = uv;
#if UNITY_UV_STARTS_AT_TOP
if (_MainTex_TexelSize.y < 0)
{ uv.y = 1 - uv.y; }
#endif
o.uv[1] = uv;
return o;
}
fixed4 frag(v2f i) : COLOR
{
fixed4 color = fixed4(1, 0, 0, 1);
// Depth
float depth = UNITY_SAMPLE_DEPTH( tex2D(_CameraDepthTexture, i.uv[1].xy));
depth = Linear01Depth(depth);
if(depth > 0.99999)
{ color = half4(1, 1, 1, 1); }
else
{ color = EncodeFloatRGBA(depth); }
half3 screen = tex2D(_MainTex, i.uv[0]).rgb;
return color;
}
ENDCG
}
}
FallBack off
}
By using this shader, I am having a strange output, I am not getting the classic black and white depth output. What’s the point of using EncodeFloatRGBA ? If I don’t use this function, I am only getting some silhouettes of my objects and not a nice black and white gradient.
How can I output the classic depth texture, the aim is also to use the output of this shader in an other one using a new Graphics.Blit but this is not a problem.
I also would like to know if it’s possible to render only backfaces of the meshes using a Camera effect ? How ?
Thank you.