- Home /
Multipass Surface Shader with _CameraDepthNormalsTexture
Hello,
I'm trying to write a shader that does some depth testing in the first pass, outputs a calculated value into the color and then does a grab pass to fetch it. In a third pass a second surface shader should render a plane with the values from the grabbed Texture.
Somehow the second surface shader only outputs the color red, disregarding whatever I calculated in the first pass.
However if I use
Pass { SetTexture [_GrabTexture] }
instead of the second surface shader it outputs the correct color. In essence I'm trying to get both versions of the shader to do the same thing for now. I'm not sure what I did wrong with my third pass, but maybe one of you knows. Searching this site for answers didn't bring up anything that solved this issue. Any help is much appreciated.
EDIT: After some more debugging I found out that I can receive the right texture in the second surface shader pass if I set the render Queue to Transparent. However, this prevents me from receiving shadows and is therefore not a good solution. I also don't quite understand how the Queue setting affects this since the shader doesn't use transparency at all.
Two pictures that show the different outcomes (first picture is the desired outcome):
Here the complete shader:
Shader "Custom/WaterShader"
{
Properties
{
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
_InvFade ("Fade Factor", Range(0,10.0)) = 1.0
_Threshold ("_Threshold", Range(0,10.0)) = 1.0
}
SubShader
{
Tags { "RenderType"="Opaque" "Queue"="Geometry+20"}
ZWrite Off
Cull Off
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard vertex:surfVert
struct Input
{
float4 vertex;
float4 projPos;
};
void surfVert (inout appdata_full v, out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input,o);
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.projPos = ComputeScreenPos (o.vertex);
COMPUTE_EYEDEPTH(o.projPos.z);
}
sampler2D _CameraDepthTexture;
float _InvFade;
void surf (Input IN, inout SurfaceOutputStandard o)
{
float sceneZ = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(IN.projPos)));
float partZ = IN.projPos.z;
float fade = 0;
fade = saturate (_InvFade * (sceneZ-partZ));
o.Albedo = float3(fade,fade,fade);
o.Alpha = 1;
}
ENDCG
GrabPass
{
"_GrabTexture"
Tags { "LightMode" = "Always" } // no lighting
}
Pass
{
SetTexture [_GrabTexture]
}
ZWrite On
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows vertex:surfVert
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
sampler2D _GrabTexture;
half _Glossiness;
half _Metallic;
fixed4 _Color;
struct Input {
float4 grabUV;
};
void surfVert (inout appdata_full v, out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input,o);
float4 hpos = mul (UNITY_MATRIX_MVP, v.vertex);
o.grabUV = ComputeGrabScreenPos(hpos);
}
void surf (Input IN, inout SurfaceOutputStandard o)
{
float4 grabVector = float4(tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(IN.grabUV)));
o.Albedo = grabVector.rgb;
o.Alpha = 1;
}
ENDCG
}
Fallback "VertexLit"
}