Unity shader實現(xiàn)遮罩效果
本文實例為大家分享了Unity shader實現(xiàn)遮罩效果的具體代碼,供大家參考,具體內(nèi)容如下
效果:

shader代碼:
Shader "Custom/Mask" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}//目標(biāo)圖片,即需要被遮罩的圖片
_MaskLayer("Culling Mask",2D) = "white"{}//混合的圖片,設(shè)置為白色的圖片,任何顏色與白色混合,其顏色不變
_Cutoff("Alpha cutoff",Range(0,1)) = 0
}
SubShader {
Tags {
"Queue"="Transparent"
}//渲染隊列設(shè)置為 以從后往前的順序渲染透明物體
Lighting off //關(guān)閉光照
ZWrite off //關(guān)閉深度緩存
Blend off //關(guān)閉混合
AlphaTest GEqual[_Cutoff] //啟用alpha測試
Pass{
SetTexture[_MaskLayer]{combine texture}//混合貼圖
//混合貼圖,previous為放置在前一序列這樣在進行AlphaTest的時候會以這個圖片為主來進行混合
SetTexture[_MainTex]{combine texture,previous}
}
}
}
新建一個材質(zhì)球,然后將目標(biāo)圖片和遮擋圖片賦予一下,即可看到效果

小編再為大家分享另一段代碼:Unity shader無鋸齒遮罩效果,忘記作者名字了,感謝這位朋友的分享。
這個Shader可以用于UGUI制作頭像框遮罩,沒有鋸齒,非常nice
Shader "Custom/CircleMask" {
Properties {
_MainTex ("MainTex", 2D) = "white" {}
_MaskTex ("MaskTex", 2D) = "white" {}
[HideInInspector]_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
//MASK SUPPORT ADD
_StencilComp ("Stencil Comparison", Float) = 8
_Stencil ("Stencil ID", Float) = 0
_StencilOp ("Stencil Operation", Float) = 0
_StencilWriteMask ("Stencil Write Mask", Float) = 255
_StencilReadMask ("Stencil Read Mask", Float) = 255
_ColorMask ("Color Mask", Float) = 15
//MASK SUPPORT END
}
SubShader {
Tags {
"IgnoreProjector"="True"
"Queue"="Transparent"
"RenderType"="Transparent"
}
//MASK SUPPORT ADD
Stencil
{
Ref [_Stencil]
Comp [_StencilComp]
Pass [_StencilOp]
ReadMask [_StencilReadMask]
WriteMask [_StencilWriteMask]
}
ColorMask [_ColorMask]
//MASK SUPPORT END
Pass {
Name "FORWARD"
Tags {
"LightMode"="ForwardBase"
}
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#define UNITY_PASS_FORWARDBASE
#include "UnityCG.cginc"
#pragma multi_compile_fwdbase
#pragma only_renderers d3d9 d3d11 glcore gles
#pragma target 3.0
uniform sampler2D _MainTex; uniform float4 _MainTex_ST;
uniform sampler2D _MaskTex; uniform float4 _MaskTex_ST;
struct VertexInput {
float4 vertex : POSITION;
float2 texcoord0 : TEXCOORD0;
};
struct VertexOutput {
float4 pos : SV_POSITION;
float2 uv0 : TEXCOORD0;
};
VertexOutput vert (VertexInput v) {
VertexOutput o = (VertexOutput)0;
o.uv0 = v.texcoord0;
o.pos = UnityObjectToClipPos( v.vertex );
return o;
}
float4 frag(VertexOutput i) : COLOR {
////// Lighting:
float4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(i.uv0, _MainTex));
float3 finalColor = _MainTex_var.rgb;
float4 _MaskTex_var = tex2D(_MaskTex,TRANSFORM_TEX(i.uv0, _MaskTex));
return fixed4(finalColor,_MaskTex_var.a);
}
ENDCG
}
}
FallBack "Diffuse"
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#開發(fā)Winform實現(xiàn)窗體間相互傳值
這篇文章介紹了C#開發(fā)Winform實現(xiàn)窗體間相互傳值的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03
Win10 系統(tǒng)下VisualStudio2019 配置點云庫 PCL1.11.0的圖文教程
這篇文章主要介紹了Win10 系統(tǒng)下VisualStudio2019 配置點云庫 PCL1.11.0的圖文教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
C#使用Consul集群進行服務(wù)注冊與發(fā)現(xiàn)
這篇文章主要介紹了C#使用Consul集群進行服務(wù)注冊與發(fā)現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Winform實現(xiàn)將網(wǎng)頁生成圖片的方法
這篇文章主要介紹了Winform實現(xiàn)將網(wǎng)頁生成圖片的方法,類似于一般瀏覽器自帶的網(wǎng)頁生成圖片的功能,需要的朋友可以參考下2014-09-09

