当前位置:网站首页>Unity a user-friendly UI grayscale shader
Unity a user-friendly UI grayscale shader
2022-07-27 01:24:00 【Calm nine】
Unity A good one UI Grayscale Shader
introduction
stay unity During the development of daily projects , This is often the case , The display button cannot be clicked , The easiest way , Direct will Button Of Interactable Change it to false, however UI The performance is not very good . We can consider remaking a grayscale UI, If there are many demands , You need to make a lot of pictures , At this time, you can consider using gray shader To replace , If you pursue UI Picture quality , It's better to make a new picture .
Shader
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "Common/UI/UIGray"
{
Properties
{
[PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
_Color("Tint", Color) = (1,1,1,1)
_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
[Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip("Use Alpha Clip", Float) = 0
//-------------------add----------------------
[Toggle(UNITY_UI_GRAYSCALE)] _UseUIGrayScale("Use Gray Scale", Float) = 1
//-------------------add----------------------
}
SubShader
{
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
"PreviewType" = "Plane"
"CanUseSpriteAtlas" = "True"
}
Stencil
{
Ref[_Stencil]
Comp[_StencilComp]
Pass[_StencilOp]
ReadMask[_StencilReadMask]
WriteMask[_StencilWriteMask]
}
Cull Off
Lighting Off
ZWrite Off
ZTest[unity_GUIZTestMode]
Blend SrcAlpha OneMinusSrcAlpha
ColorMask[_ColorMask]
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "UnityUI.cginc"
#pragma multi_compile __ UNITY_UI_ALPHACLIP
//-------------------add----------------------
#pragma multi_compile __ UNITY_UI_GRAYSCALE
//-------------------add----------------------
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
half2 texcoord : TEXCOORD0;
float4 worldPosition : TEXCOORD1;
};
fixed4 _Color;
fixed4 _TextureSampleAdd;
float4 _ClipRect;
v2f vert(appdata_t IN)
{
v2f OUT;
OUT.worldPosition = IN.vertex;
OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);
OUT.texcoord = IN.texcoord;
#ifdef UNITY_HALF_TEXEL_OFFSET
OUT.vertex.xy += (_ScreenParams.zw - 1.0)*float2(-1,1);
#endif
OUT.color = IN.color * _Color;
return OUT;
}
sampler2D _MainTex;
fixed4 frag(v2f IN) : SV_Target
{
half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;
color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
#ifdef UNITY_UI_ALPHACLIP
clip(color.a - 0.001);
#endif
//-------------------add----------------------
#ifdef UNITY_UI_GRAYSCALE
color.rgb = Luminance(color.rgb);
#endif
//-------------------add----------------------
return color;
}
ENDCG
}
}
}
Usage method
1、 Create a Material, Use this shader
2、 take Material Assign to corresponding Image Of Material That's all right. .
If there is a better way , Welcome to discuss .
边栏推荐
- 顺序表之OJ题
- 05 - attack and defense of phishing websites
- Verilog procedure assignment statement
- 快来帮你三分钟了解物联网
- What is kubernetes?
- 深度学习笔记
- SQL关系代数——除法
- How can Tencent cloud live plug-in mlvb make use of these advantages to become the divine assistant of anchor Live Streaming?
- 6. 世界杯来了
- Li Hongyi machine learning (2017 Edition)_ P21: convolutional neural network CNN
猜你喜欢

SQL learning (3) -- complex query and function operation of tables

c语言实现扫雷游戏:

力扣刷题量300记录帖

MTCNN

Six ways for the Internet of things to improve our lives

What is kubernetes?

FaceNet

Li Kou brushes 300 record posts

快来:鼓励高校毕业生返乡创业就业,助力乡村振兴

The MySQL character set is set to UTF-8, but the console still has the problem of Chinese garbled code
随机推荐
Jenkins--基础--5.2--系统配置--系统配置
5. Xshell connection server denied access, password error
报错信息 WARNING: IPv4 forwarding is disabled. Networking will not work.
快来帮您一分钟了解移动互联网
2. 错误的里程表
Iptables detailed explanation and practical cases
Li Kou brushes 300 record posts
玩客云刷机 5.9
快来:鼓励高校毕业生返乡创业就业,助力乡村振兴
SQL学习(3)——表的复杂查询与函数操作
Uni app applet app's advertising realization path: banner information flow advertising
Li Hongyi machine learning (2017 Edition)_ P13: deep learning
7. F1方程式冠军
记一次RawImage渲染出来的场景错乱
Play guest cloud with zerotier nanny level teaching to ensure learning waste
初中高三部曲音视频下载Pronunciation Pairs+Ship or Sheep+Tree or Three
Unity常用的一些简易扩展方法
数学建模简介-从现实对象到数学建模[2]
Unity Twitter登录接入
5. Legal bracket string