Unity shader实现遮罩效果
作者:贪玩的孩纸时代 时间:2023-06-28 10:20:09
本文实例为大家分享了Unity shader实现遮罩效果的具体代码,供大家参考,具体内容如下
效果:
shader代码:
Shader "Custom/Mask" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}//目标图片,即需要被遮罩的图片
_MaskLayer("Culling Mask",2D) = "white"{}//混合的图片,设置为白色的图片,任何颜色与白色混合,其颜色不变
_Cutoff("Alpha cutoff",Range(0,1)) = 0
}
SubShader {
Tags {
"Queue"="Transparent"
}//渲染队列设置为 以从后往前的顺序渲染透明物体
Lighting off //关闭光照
ZWrite off //关闭深度缓存
Blend off //关闭混合
AlphaTest GEqual[_Cutoff] //启用alpha测试
Pass{
SetTexture[_MaskLayer]{combine texture}//混合贴图
//混合贴图,previous为放置在前一序列这样在进行AlphaTest的时候会以这个图片为主来进行混合
SetTexture[_MainTex]{combine texture,previous}
}
}
}
新建一个材质球,然后将目标图片和遮挡图片赋予一下,即可看到效果
小编再为大家分享另一段代码: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"
}
来源:https://blog.csdn.net/yiwei151/article/details/78687163
标签:unity,shader,遮罩
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringAOP如何获取方法参数上的注解
2021-09-19 23:22:18
![](https://img.aspxhome.com/file/2023/3/73123_0s.png)
Android布局之帧布局FrameLayout详解
2023-08-07 04:45:29
![](https://img.aspxhome.com/file/2023/5/85985_0s.png)
解决java读取EXCEL数据变成科学计数法的问题
2023-06-14 02:17:56
![](https://img.aspxhome.com/file/2023/5/83675_0s.png)
Struts2实现对action请求对象的拦截操作方法
2023-06-08 01:54:13
![](https://img.aspxhome.com/file/2023/2/70342_0s.jpg)
Java深入探究关键字abstract的使用
2023-08-03 07:48:20
Java集合定义与用法实例总结【Set、List与Map】
2023-11-21 08:26:40
![](https://img.aspxhome.com/file/2023/8/59588_0s.png)
Android编程之短信窃听器实现方法
2023-11-01 00:48:22
解决Android 沉浸式状态栏和华为虚拟按键冲突问题
2023-10-15 14:08:14
![](https://img.aspxhome.com/file/2023/4/125404_0s.png)
Android ActionBarActivity设置全屏无标题实现方法总结
2023-09-09 10:44:57
kotlin之协程的理解与使用详解
2023-10-21 15:55:57
![](https://img.aspxhome.com/file/2023/0/64420_0s.png)
C#读取二进制文件方法分析
2022-04-23 11:17:43
Android编程中的消息机制实例详解
2022-11-14 06:50:05
![](https://img.aspxhome.com/file/2023/0/85970_0s.png)
C#实现XML文件读取
2023-03-06 13:38:44
Android列表选择框Spinner使用方法详解
2023-10-04 13:18:02
![](https://img.aspxhome.com/file/2023/6/139126_0s.png)
Android自定义View实现带4圆角或者2圆角的效果
2023-04-08 04:32:10
![](https://img.aspxhome.com/file/2023/9/101869_0s.png)
java实现将数字转换成人民币大写
2023-08-11 05:07:29
Spring的事件监听机制示例详解
2023-01-25 18:40:42
![](https://img.aspxhome.com/file/2023/7/129977_0s.jpg)
Android硬件解码组件MediaCodec使用教程
2023-03-14 01:35:36
Spring Cloud 的 Hystrix.功能及实践详解
2023-11-19 06:40:46
![](https://img.aspxhome.com/file/2023/9/59249_0s.jpg)
c#生成随机数示例分享
2023-10-04 06:54:50