in the article http://blog.csdn.net/wodownload2/article/details/79449760
we give an example of legacy deferred shader. we recall the steps we take to bring legacy deferred into effect.
1, we first give the light mode to implement the deferred lighting mode.
2, we set the camera’s render path to legacy deferred.
then we will set the object will be rendered.
we also know that if we set the camera’s rendering path to forward or others mode. the object will not be seen.
in this article, we will learn how to write a compatible shader that the object will be seen in both forward mode and legacy deferred mode.
yep, we give the shader like this:
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "Course/ForwardAndDeferred"
{
SubShader
{
pass
{
Tags{ "LightMode"="ForwardBase"}
Blend One Zero
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
struct vertOut{
float4 pos:SV_POSITION;
float4 color:COLOR;
};
vertOut vert(appdata_base v)
{
vertOut o;
o.pos=UnityObjectToClipPos(v.vertex);
o.color=_LightColor0.rgba;
return o;
}
float4 frag(vertOut i):COLOR
{
return i.color;
}
ENDCG
}
Blend One Zero
CGPROGRAM
#pragma surface surf MyDeferred
half4 LightingMyDeferred_PrePass (SurfaceOutput s, half4 light)
{
half4 c;
c.rgb = s.Albedo;
c.a = s.Alpha;
return c;
}
struct Input {
float2 uv_MainTex;
};
sampler2D _MainTex;
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo=float3(1,0,0);
}
ENDCG
}
}
when we set the camera’s rendering path to forward or deferred, the object will be rendered by pass. that means when unity fails from the legacy deferred mode. the unity will use forward pass to render object. now we set the camera’s rendering mode to forward or deferred, the following shader code will be invalid.
Blend One Zero
CGPROGRAM
#pragma surface surf MyDeferred
half4 LightingMyDeferred_PrePass (SurfaceOutput s, half4 light)
{
half4 c;
c.rgb = s.Albedo;
c.a = s.Alpha;
return c;
}
struct Input {
float2 uv_MainTex;
};
sampler2D _MainTex;
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo=float3(0,1,0); //green
}
ENDCG
the above shader will only be executed when camera’s rendering mode be set to legacy deferred.
ok, when we set the camera’s rendering mode to legacy deferred, the object will we rendered to the color green.
we should pay attention to that we make alpha blend to be equal to one zero, so itis clear to see the effect by changing the render color value in shader.
the color of object is the color of the light
if the rendering path of camera is set to legacy deferred, the effect is like:
ok, now we have given the compatible shader.