当前位置:网站首页>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 .
边栏推荐
- 3. 拳王阿里
- Unity 一个好用的UI灰度Shader
- 什么是数字经济,它是如何改变商业模式的?
- x 的平方根
- 堆排序相关知识总结
- 玩客云搭配zerotier保姆级教学,保证学废
- In depth learning report (1)
- 2. 错误的里程表
- Are you ready for the Internet of things to revolutionize manufacturing?
- Compile Darknet under vscode2015 to generate darknet Ext error msb3721: XXX has exited with a return code of 1.
猜你喜欢

Esp8266 --- JSON data exchange format

Doris or starrocks JMeter pressure measurement

Keil开发环境的搭建送安装包

创建MDK工程

链表常规OJ

数据库期中(一)

IDEA导入外部项目时pom文件的依赖无效问题解决

Compile Darknet under vscode2015 to generate darknet Ext error msb3721: XXX has exited with a return code of 1.

Jenkins -- Basic -- 5.2 -- system configuration -- system configuration

Unity 截屏小工具
随机推荐
1. 众数
Small programs related to a large number of digital collections off the shelves of wechat: is NFT products the future or a trap?
4. European Champions League
数学建模简介-从现实对象到数学建模[2]
When a transaction encounters a distributed lock
Spark ---- shuffle and partition of RDD
Longest common substring
Unity常用的一些简易扩展方法
集中式版本控制工具代码合并问题
6. The world cup is coming
李宏毅机器学习(2017版)_P13:深度学习
SQL学习(3)——表的复杂查询与函数操作
玩客云刷机 5.9
Come and help you understand the Internet of things in three minutes
matlab基本介绍[1]
What is kubernetes?
Promoting practice with competition - Reflection on the 303th weekly match
PlantCV中文文档
Li Hongyi machine learning (2017 Edition)_ P6-8: gradient descent
数据库期中(二)