NGUI配合shader,把精灵图片置灰
发布日期:2021-06-30 19:40:29
浏览次数:2
分类:技术文章
本文共 8688 字,大约阅读时间需要 28 分钟。
方法一
图集的材质球的shader使用下面的shader脚本
Shader "Unlit/Transparent Colored (AlphaClip_r) "{ Properties { _MainTex ("Base (RGB)", 2D) = "white" {} _AlphaTex ("Trans (A)", 2D) = "white" {} } SubShader { LOD 100 Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" } Pass { Cull Off Lighting Off ZWrite Off Offset -1, -1 Fog { Mode Off } ColorMask RGB Blend SrcAlpha OneMinusSrcAlpha CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" sampler2D _MainTex,_AlphaTex; float4 _ClipRange0 = float4(0.0, 0.0, 1.0, 1.0); float2 _ClipArgs0 = float2(1000.0, 1000.0); struct appdata_t { float4 vertex : POSITION; half4 color : COLOR; float2 texcoord : TEXCOORD0; }; struct v2f { float4 vertex : POSITION; half4 color : COLOR; float2 texcoord : TEXCOORD0; float2 worldPos : TEXCOORD1; }; v2f o; v2f vert (appdata_t v) { o.vertex = mul(UNITY_MATRIX_MVP, v.vertex); o.color = v.color; o.texcoord = v.texcoord; o.worldPos = v.vertex.xy * _ClipRange0.zw + _ClipRange0.xy; return o; } half4 frag (v2f IN) : COLOR { // Softness factor float2 factor = (float2(1.0, 1.0) - abs(IN.worldPos)) * _ClipArgs0; // Sample the texture //half4 col = half4(tex2D(_MainTex, IN.texcoord).xyz,tex2D(_AlphaTex, IN.texcoord).r) * IN.color; half4 col = half4(tex2D(_MainTex, IN.texcoord).xyz, tex2D(_AlphaTex, IN.texcoord).r); //half a = IN.color.a; if (IN.color.a == 0) { col.rgb = Luminance(col.rgb); } else { col *= IN.color; } /* //step(a, x) x < 0 = 0, x >= 0 1 //用step 优化掉if half a = IN.color.a; half4 grayColor = Luminance(col.rgb); fixed stepV = step(0.000001, a); col = IN.color * col * stepV + half4(grayColor.rgb, col.a) * (1 - stepV); */ col.a *= clamp( min(factor.x, factor.y), 0.0, 1.0); return col; } ENDCG } } SubShader { LOD 50 Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" } Pass { Cull Off Lighting Off ZWrite Off Offset -1, -1 Fog { Mode Off } ColorMask RGB Blend SrcAlpha OneMinusSrcAlpha CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" sampler2D _MainTex,_AlphaTex; float4 _ClipRange0 = float4(0.0, 0.0, 1.0, 1.0); float2 _ClipArgs0 = float2(1000.0, 1000.0); struct appdata_t { float4 vertex : POSITION; half4 color : COLOR; float2 texcoord : TEXCOORD0; }; struct v2f { float4 vertex : POSITION; fixed4 color : COLOR; float2 texcoord : TEXCOORD0; float2 worldPos : TEXCOORD1; }; v2f o; v2f vert (appdata_t v) { o.vertex = mul(UNITY_MATRIX_MVP, v.vertex); o.color = v.color; o.texcoord = v.texcoord; o.worldPos = v.vertex.xy * _ClipRange0.zw + _ClipRange0.xy; return o; } fixed4 frag (v2f IN) : COLOR { // Softness factor float2 factor = (float2(1.0, 1.0) - abs(IN.worldPos)) * _ClipArgs0; // Sample the texture //fixed4 col = fixed4(tex2D(_MainTex, IN.texcoord).xyz,tex2D(_AlphaTex, IN.texcoord).r) * IN.color; fixed4 col = fixed4(tex2D(_MainTex, IN.texcoord).xyz, tex2D(_AlphaTex, IN.texcoord).r); if (IN.color.a == 0) { col.rgb = Luminance(col.rgb); } else { col *= IN.color; } /* half a = IN.color.a; half4 grayColor = Luminance(col.rgb); fixed stepV = step(0.000001, a); col = IN.color * col * stepV + half4(grayColor.rgb, col.a) * (1 - stepV); */ col.a *= clamp( min(factor.x, factor.y), 0.0, 1.0); return col; } ENDCG } } }
接着,在UIWidget类中,新增m_grayState变量和grayState属性
//UIWidget.cs[SerializeField]protected bool m_grayState = false;public bool grayState{ get { return m_grayState; } set { if(m_grayState != value) { m_grayState = value; MarkAsChanged(); } }}
然后再UIBasicSprite类中,修改drawingColor属性
//UIBasicSprite.csColor32 drawingColor{ get { ... if(m_grayState) colF.a =0; return colF; }}
然后再改一下UIWidgetInspetor
//UIBasicSprite.csstatic public void DrawInspectorProperties(SerializedObject so, UIWidget w, bool drawColor){ ... if(NGUIEditorTools.DrawHeader("Widget")) { ... SerializedProperty graySpPre = so.FindProperty("m_grayState"); bool preGrayValue = graySpPre.boolValue; SerializedProperty graySp = NGUIEditorTools.DrawProperty("Gray", so, "m_grayState"); if(graySp.boolValue != preGrayValue) { w.MarkAsChanged(); } NGUIEditorTools.SetLabelWidth(80f); NGUIEditorTools.EndContents(); }}
效果如下:
方法二
图集的材质球的shader使用下面的shader脚本,当精灵的颜色设置成全黑的时候,就会自动变灰色
Shader "Unlit/Transparent Colored"{ Properties { _MainTex ("Base (RGB), Alpha (A)", 2D) = "black" {} } SubShader { LOD 200 Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" } Pass { Cull Off Lighting Off ZWrite Off Fog { Mode Off } Offset -1, -1 Blend SrcAlpha OneMinusSrcAlpha CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" sampler2D _MainTex; float4 _MainTex_ST; struct appdata_t { float4 vertex : POSITION; float2 texcoord : TEXCOORD0; fixed4 color : COLOR; }; struct v2f { float4 vertex : SV_POSITION; half2 texcoord : TEXCOORD0; fixed4 color : COLOR; }; v2f o; v2f vert (appdata_t v) { o.vertex = mul(UNITY_MATRIX_MVP, v.vertex); o.texcoord = v.texcoord; o.color = v.color; return o; } fixed4 frag (v2f IN) : COLOR { fixed4 col; if (IN.color.r < 0.001) { col = tex2D(_MainTex, IN.texcoord); float grey = dot(col.rgb, float3(0.299, 0.587, 0.114)); col.rgb = float3(grey, grey, grey); } else { col = tex2D(_MainTex, IN.texcoord) * IN.color; } return col; } ENDCG } } SubShader { LOD 100 Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" } Pass { Cull Off Lighting Off ZWrite Off Fog { Mode Off } Offset -1, -1 ColorMask RGB Blend SrcAlpha OneMinusSrcAlpha ColorMaterial AmbientAndDiffuse SetTexture [_MainTex] { Combine Texture * Primary } } }}
转载地址:https://linxinfa.blog.csdn.net/article/details/86667423 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
第一次来,支持一个
[***.219.124.196]2024年05月03日 06时44分55秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
临界资源与临界区
2019-04-30
matlab中uint8,double,im2double和im2uint8的区别
2019-04-30
数字图像处理总复习
2019-04-30
图像去噪(包含修正的阿尔法均值滤波及通用滤波方法代码)
2019-04-30
SVM进行人脸检测
2019-04-30
C++学习笔记
2019-04-30
图像处理学习笔记
2019-04-30
Machine Learning Onramp on MATLAB 学习笔记
2019-04-30
Machine Learning with MATLAB 1.1 to 2.2
2019-04-30
Learning DSP with MATLAB
2019-04-30
用MATLAB实现m序列的生成(MATLAB 2021a适用)
2019-04-30
MATLAB函数备忘(定期更新)
2019-04-30
13行MATLAB代码实现网络爬虫 爬取NASA画廊星图
2019-04-30
MATLAB指定路径保存图片方法
2019-04-30
Python一键获取微信推送封面图
2019-04-30
油猴脚本:微信推送浏览功能拓展
2019-04-30
JavaScript DOM对象操作详解
2019-04-30
JavaScript 表单操作与MD5加密
2019-04-30
JAVA学习笔记4 - 循环与分支结构
2019-04-30