zoukankan      html  css  js  c++  java
  • NGUI 修改Shader支持灰色滤镜

    之前有人做过,不过效率不高: 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)中都会有问题,求解。

  • 相关阅读:
    IL指令详细
    读懂IL代码就这么简单(三)完结篇
    读懂IL代码就这么简单(二)
    读懂IL代码就这么简单(一)
    在 .NET Core Logging中使用 Trace和TraceSource
    使用JWT创建安全的ASP.NET Core Web API
    闭环思维
    一行代码,百万人民币打水漂
    网络接口库函数mpr.dll动态库
    使用MSF利用永恒之蓝漏洞远程控制主机——直接使用MSF即可RCE,我++,在docker里没有完成,GG!
  • 原文地址:https://www.cnblogs.com/dabiaoge/p/4130694.html
Copyright © 2011-2022 走看看