Unity支持3种RenderingPath(绘制路径),分别是VertexLit,ForWard和DeferredLighting。
path1
这是一个被设计只为在Camera的RenderingPath为Deferred(延迟)模式下输出红色的shader。
Deferred.shader
Shader "Custom/deferred"{
Properties{
_MainTex("Base(RGB)",2D) = "white"{}
}
Subshader{
//Opaque(不透明): 用于大多数着色器(法线着色器、自发光着色器、反射着色器以及地形的着色器)。
Tags{"RenderType" = "Opaque"}//通过使用tags来告诉渲染引擎在什么时候该如何渲染他们所期望的效果。
//贴图和背景叠加,无Alpha透明通道处理。仅仅是颜色rgb数值的叠加更趋近于白色即(1,1,1)
Blend One One
CGPROGRAM
#pragma surface surf MyDeferred //声明surface shader的函数
//
//half:中精度浮点值。通常是16位,范围是-60000至+60000,它适合存储UV坐标,颜色值
//自定义光照类型:half4 LightingName_PrePass (SurfaceOutput s, half4 light); 如果需要光照延时(deferred)不写则全部采用快速光照(forward)。
half4 LightingMyDeferred_PrrePass (SurfaceOutput s,half4 light){
/*
struct SurfaceOutput {
half3 Albedo;
half3 Normal;
half3 Emission;
half Specular;
half Gloss;
half Alpha;
};
*/
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){//第一个参数,纯输入上述结构体参数,inout,意思是为输入参数也可输出参数
o.Albedo = float3(1,0,0);
}
ENDCG
}
}
Deferred.shader
Shader "Custom/deferred"{
Properties{
_MainTex("Base(RGB)",2D) = "white"{}
}
Subshader{
//Opaque(不透明): 用于大多数着色器(法线着色器、自发光着色器、反射着色器以及地形的着色器)。
Tags{"RenderType" = "Opaque"}//通过使用tags来告诉渲染引擎在什么时候该如何渲染他们所期望的效果。
//贴图和背景叠加,无Alpha透明通道处理。仅仅是颜色rgb数值的叠加更趋近于白色即(1,1,1)
Blend One One
CGPROGRAM
#pragma surface surf MyDeferred //声明surface shader的函数
//
//half:中精度浮点值。通常是16位,范围是-60000至+60000,它适合存储UV坐标,颜色值
//自定义光照类型:half4 LightingName_PrePass (SurfaceOutput s, half4 light); 如果需要光照延时(deferred)不写则全部采用快速光照(forward)。
half4 LightingMyDeferred_PrrePass (SurfaceOutput s,half4 light){
/*
struct SurfaceOutput {
half3 Albedo;
half3 Normal;
half3 Emission;
half Specular;
half Gloss;
half Alpha;
};
*/
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){//第一个参数,纯输入上述结构体参数,inout,意思是为输入参数也可输出参数
o.Albedo = float3(1,0,0);
}
ENDCG
}
}
path2.
这是一个包换可以适应Deferred以及Forward渲染路径的材质,其中适应Deferred的部分输出(1,0,0,1)的红色,而适应Forward的部分只会输出(0,1,0,1)的绿色。两个部分都是用了Blend One One混合模式,也就是说如果两部部分都 执行,应该会输出(1,1,0,1)的黄色,反之只会输出被执行的哪一个输出颜色,也就是红色或者绿色。
shader "Deferred_Forward"
SubShader{
Blend One One
Pass{
Tags{"LightMode" = "ForwardBase"}//用于正向渲染,环境主要方向灯和定点光/SH 等的应用 ,第一个像素光所在的反射通道标记为Forward
//贴图和背景叠加,无Alpha透明通道处理。仅仅是颜色rgb数值的叠加更趋近于白色即(1,1,1)
Blend One One
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 = mul(UNITY_MATRIX_MVP,v.vertex);//model“View”Projection
o.color = float4(0,1,0,1);
return o;
}
float4 frag(verOut i):COLOR
{
return i.color;
}
ENDCG
}
CGPROGRAM
#pragma surface surf MyDeferred
half4 LightingMyDeferred_PrePass(SurfaceOutput s , half4 light){
half c;
c.rgb = s.Albedo;
c.a = s.Alpha;
return c;
}
struct Input{
float2 uv_MainTex;
};
sample2D _MainTex;
void surf (Input IN,inout SurfaceOutput o){
o.Albedo = float3(1,0,0);
}
ENDCG
}
}
shader "Deferred_Forward"
SubShader{
Blend One One
Pass{
Tags{"LightMode" = "ForwardBase"}//用于正向渲染,环境主要方向灯和定点光/SH 等的应用 ,第一个像素光所在的反射通道标记为Forward
//贴图和背景叠加,无Alpha透明通道处理。仅仅是颜色rgb数值的叠加更趋近于白色即(1,1,1)
Blend One One
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 = mul(UNITY_MATRIX_MVP,v.vertex);//model“View”Projection
o.color = float4(0,1,0,1);
return o;
}
float4 frag(verOut i):COLOR
{
return i.color;
}
ENDCG
}
CGPROGRAM
#pragma surface surf MyDeferred
half4 LightingMyDeferred_PrePass(SurfaceOutput s , half4 light){
half c;
c.rgb = s.Albedo;
c.a = s.Alpha;
return c;
}
struct Input{
float2 uv_MainTex;
};
sample2D _MainTex;
void surf (Input IN,inout SurfaceOutput o){
o.Albedo = float3(1,0,0);
}
ENDCG
}
}
path3.
这个shader添加了可以适应VertexLit渲染路径的Pass,会输出蓝色。也就是说,如果三个都执行,则会输出(1,1,1,1),否则会输出红蓝绿3个颜色的一个或者两个的组合。
shader "Deferred_Forward_Vertex"{
SubShader{
Blend One One
//.1
Pass{
Tag{"LightMode" = "Vertex"}//用于顶点光照渲染,当物体没有光照映射时,所有顶点光照被应用
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 = mul(UNITY_MATRIX_MVP,v.vertex);
o.color = float4(0,0,1,1);
return o;
}
float4 frag(vertOut i):COLOR
{
return i.color;
}
ENDCG
}
//.2
Pass{
Tags{"LightMode" = "ForwardBase"}
Blend One One
CGPROGRAM
#pragram vertex vert
#pragram 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 = mul(UNITY_MATRIX_MVP,v.vertex);
o.color = float4(0,1,0,1);
return o;
}
float4 frag(vertOut i):COLOR
{
return i.color;
}
ENDCG
}
//.3
CGPROGRAM
#pragram 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
}
}
shader "Deferred_Forward_Vertex"{
SubShader{
Blend One One
//.1
Pass{
Tag{"LightMode" = "Vertex"}//用于顶点光照渲染,当物体没有光照映射时,所有顶点光照被应用
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 = mul(UNITY_MATRIX_MVP,v.vertex);
o.color = float4(0,0,1,1);
return o;
}
float4 frag(vertOut i):COLOR
{
return i.color;
}
ENDCG
}
//.2
Pass{
Tags{"LightMode" = "ForwardBase"}
Blend One One
CGPROGRAM
#pragram vertex vert
#pragram 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 = mul(UNITY_MATRIX_MVP,v.vertex);
o.color = float4(0,1,0,1);
return o;
}
float4 frag(vertOut i):COLOR
{
return i.color;
}
ENDCG
}
//.3
CGPROGRAM
#pragram 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
}
}