先上代码
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "Unlit/NewUnlitShader"
{
Properties
{
_MainTexOne ("TEXONE", 2D) = "white" {}//正面贴图
_MainTexTwo ("TEXTWO", 2D) = "white" {}//反面贴图
}
SubShader
{
Tags { "RenderType"="Opaque" }//渲染队列
LOD 100
Pass//pass用来渲染前面,裁剪掉背面
{
Tags{"LightMode" = "ForwardBase"}
Cull Back//裁剪背面
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTexOne;
float4 _MainTexOne_ST;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTexOne);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTexOne, i.uv);
return col;
}
ENDCG
}
Pass//pass用来渲染背面,裁剪前面
{
Tags{"LightMode" = "ForwardBase"}
Cull Front//裁剪前面
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTexTwo;
float4 _MainTexTwo_ST;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTexTwo);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTexTwo, i.uv);
return col;
}
ENDCG
}
}
Unity3d制作双面材质:
Shader中Cull指令的使用:
通过Cull指令来控制需要剔除那个面的渲染图元。Cull指令的语法如下:
Cull Back | Front | Off
设置为Back,背对摄像机的渲染图源就不会被渲染,设置为Front,朝向摄像机的渲染图元就不会被渲染。
设置为Off就会关闭剔除功能,即所有图元都会被渲染。
步骤:
1.在项目面板中创建plane作为演示。
2.导入两张图片作为贴图演示。
3.在项目面板中创建DoubleSurface shader
4.打开创建的shader进行编辑:
Shader "Custom/DoubleSurface" {
Properties{
_Color("Main Color", Color) = (1,1,1,1)//Tint Color
_MainTex("Base (RGB)", 2D) = "white" {} //背面纹理
_MainTex_2("Base (RGB)", 2D) = "white" {} //正面纹理
}
SubShader{
Tags{ "RenderType" = "Opaque" } //设置渲染类型 Opaque不透明
LOD 100
Pass{
Cull Front //关闭正面渲染
Lighting Off
SetTexture[_MainTex]{ combine texture }
SetTexture[_MainTex]
{
ConstantColor[_Color]
Combine Previous * Constant
}
}
Pass
{
Cull Back //关闭背面渲染
Lighting Off
SetTexture[_MainTex_2]{ combine texture }
SetTexture[_MainTex_2]
{
ConstantColor[_Color]
Combine Previous * Constant
}
}
}
}
参考:
《Shader入门精要》,冯乐乐
如果是透明通道的图片可以替换类型
Tags{"Queue" = "Transparent" "RenderType" = "Transparent" "IgnoreProjector" = "True"}
Blend SrcAlpha OneMinusSrcAlpha