之前有人做过,不过效率不高: http://blog.csdn.net/onerain88/article/details/12197277  
他的代码:

fixed4 frag (v2f i) : COLOR
{
    fixed4 col;
    if (i.color.r < 0.001) 
     {
         col = tex2D(_MainTex, i.texcoord);
         float grey = dot(col.rgb, float3(0.299, 0.587, 0.114));
         col.rgb = float3(grey, grey, grey);
     }
     else
     {
         col = tex2D(_MainTex, i.texcoord) * i.color;
     }
     return col;
}

通过修改后效率明显大幅提升:

Shader "Unlit/Transparent Colored Gray"
{
Properties
{
_MainTex ("Base (RGB) Gray, Alpha (A)", 2D) = "white" {}
}

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
AlphaTest Greater .01
Blend SrcAlpha OneMinusSrcAlpha
ColorMaterial AmbientAndDiffuse

CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest

#include "UnityCG.cginc"

sampler2D _MainTex;
fixed4 _Color;

struct appdata_t
{
float4 vertex : POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
};

struct v2f
{
float4 vertex : POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
fixed gray : TEXCOORD1;
};

float4 _MainTex_ST;

v2f vert (appdata_t v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.color = v.color;
o.gray = dot(v.color, fixed4(1,1,1,0));
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}

fixed4 frag (v2f i) : COLOR
{
fixed4 col;
if (i.gray == 0)
{
col = tex2D(_MainTex, i.texcoord);
col.rgb = dot(col.rgb, fixed3(.222,.707,.071));
}
else
{
col = tex2D(_MainTex, i.texcoord) * i.color;
}
return col;
}
ENDCG
}
}
}

设置UISprite.color = Color.black;就能使用了。
使用效果

原理是在v2f中申请一个寄存器TEXCOORD1放置数据gray,并在顶点程序vert中计算是否灰色,在片段程序frag中用i.gray == 0做if判断。

第一段代码效率低的原因是if (i.color.r < 0.001)这里访问了color的分量r,但在官方文档中明确说了这样做效率低,除非必要情况时才使用。

另外在PC中(fixed gray : TEXCOORD1;)可以改成(bool gray : TEXCOORD1;),但在移动设备(android和ios)中都会有问题,求解。

NGUI 修改Shader支持灰色滤镜的更多相关文章

  1. NGUI Material Shader SetFloat 不起作用

    通常情况下,我们在Unity3d种可以通过material.SetFloat(name, xxx) 修改参数来达到我们的Material[Shader]的效果. 但是在 NGUI 的UITexture ...

  2. Unity 3D动态修改Shader状态,使物体透明等等

    Unity动态改Shader状态透明 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- ...

  3. NGUI出现Shader wants normals, but the mesh UIAtlas doesn&#39;t have them

    NGUI出现Shader wants normals, but the mesh UIAtlas doesn't have them,没有网格法线,打开UI Root上 UIPanel组建上的 Nor ...

  4. jquery.ui.accordion的修改(支持展开多个)

    原文:jquery.ui.accordion的修改(支持展开多个) 背景:原jquery.ui.accordion插件,最多只能展开一个,不能展开多个,后来在网上找到了一个基于它的一个修改版(http ...

  5. 阅读DSSS.py 并修改成支持python3.6

    项目地址:https://github.com/stamparm/DSSS 功能:一款小型注入工具 代码如下URL:https://github.com/stamparm/DSSS/blob/mast ...

  6. dhtmlgrid修改,支持IE10

    因为项目IE升级,导致原来使用的dhtmlgrid无法正常显示,同时通过loadxml接口还有属性不支持. 花了半天时间对dhtmlgrid进行了修改,能够支持IE10正常加载显示. edit by ...

  7. 修改LVDS支持1024*600分辨率

    一.在boot中增加LVDS设置分辨率1024*600选项 1.修改文件TQIMX6_android-4.2.2\bootable\bootloader\uboot-imx\common\cmd_me ...

  8. 修改create-react-app支持多入口

    使用Facebook官方脚手架create-react-app创建React应用,默认只能生成一个SPA,入口是index.html.虽然,SPA的页面切换可以使用前台路由框架方便(比如React-R ...

  9. centos 7修改系统支持中文编码

    2019-03-14 查看系统现支持编码 }[root@web dc2-user]#locale LANG=en_US.UTF- LC_CTYPE="en_US.UTF-8" LC ...

随机推荐

  1. Autoprefixer处理CSS3属性前缀

    http://www.w3cplus.com/css3/autoprefixer-css-vender-prefixes.html

  2. hive函数 -- split 字符串分割函数

    hive字符串分割函数 split(str, regex) - Splits str around occurances that match regexTime taken: 0.769 secon ...

  3. android:configChanges属性总结

    http://blog.csdn.net/zhaokaiqiang1992/article/details/19921703 android中的组件Activity在manifest.xml文件中可以 ...

  4. 在 Xen 虚拟机下修改系统当前时间

    在 Xen 虚拟机下修改系统当前时间 Xen 虚拟机默认不允许不同的虚拟机使用不同的系统时间,因此所有虚拟机的系统时间都会同宿主机的系统时间严格同步,用 date 命令修改虚拟机系统时间时虽然提示成功 ...

  5. JVM中的垃圾收集算法和Heap分区简记

    如何判断垃圾对象? 垃圾收集的第一步就是先需要算法来标记哪些是垃圾,然后再对垃圾进行处理.   引用计数(ReferenceCounting)算法 这种方法比较简单直观,FlashPlayer/Pyt ...

  6. 解决.Net 4.0 A potentially dangerous Request.Form value was detected from the client 异常

    在web.config中加入 <httpRuntime maxRequestLength="22000" executionTimeout="43200" ...

  7. 实现Android Studio JNI开发C/C++使用__android_log_print输出Log

    相信很多人在刚开始学习Android JNI编程的时候,需要输出Log,在百度Google搜索的时候都是说需要在Android.mk中加入LOCAL_LDLIBS+= -L$(SYSROOT)/usr ...

  8. C#转C++的一点分享

    前几天看到这样一篇博客<那些年·我们读过的专业书籍>,里面列了很多大家认为很好的书,加上自己在自学C++的工程中也看了不少书,感觉并不是所有的书都值得花时间去看的,毕竟很多人一年下来也看不 ...

  9. FluentData Mysql分页的一个BUG

    开发环境 FluentData3.0.VS.NET2010.Mysql5.0 问题描述 使用FluentData对一个表(记录数28)进行分页时,突然发现一个诡异的问题,第一页返回10条数据正常,第二 ...

  10. oreData的学习记录

    1.如果想创建一个带有coreData的程序,要在项目初始化的时候勾选中 2.创建完成之后,会发现在AppDelegate里多出了几个属性,和2个方法 <span style="fon ...