zoukankan      html  css  js  c++  java
  • [UnityShader效果]02.置灰

    参考链接:

    https://blog.csdn.net/Csoap2/article/details/102772784

    在UI-Default.shader的基础上进行修改:

    UIGray.shader

      1 // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
      2 
      3 Shader "UI/Gray"
      4 {
      5     Properties
      6     {
      7         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
      8         _Color ("Tint", Color) = (1,1,1,1)
      9 
     10         _StencilComp ("Stencil Comparison", Float) = 8
     11         _Stencil ("Stencil ID", Float) = 0
     12         _StencilOp ("Stencil Operation", Float) = 0
     13         _StencilWriteMask ("Stencil Write Mask", Float) = 255
     14         _StencilReadMask ("Stencil Read Mask", Float) = 255
     15 
     16         _ColorMask ("Color Mask", Float) = 15
     17 
     18         [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
     19     }
     20 
     21     SubShader
     22     {
     23         Tags
     24         {
     25             "Queue"="Transparent"
     26             "IgnoreProjector"="True"
     27             "RenderType"="Transparent"
     28             "PreviewType"="Plane"
     29             "CanUseSpriteAtlas"="True"
     30         }
     31 
     32         Stencil
     33         {
     34             Ref [_Stencil]
     35             Comp [_StencilComp]
     36             Pass [_StencilOp]
     37             ReadMask [_StencilReadMask]
     38             WriteMask [_StencilWriteMask]
     39         }
     40 
     41         Cull Off
     42         Lighting Off
     43         ZWrite Off
     44         ZTest [unity_GUIZTestMode]
     45         Blend SrcAlpha OneMinusSrcAlpha
     46         ColorMask [_ColorMask]
     47 
     48         Pass
     49         {
     50             Name "Default"
     51         CGPROGRAM
     52             #pragma vertex vert
     53             #pragma fragment frag
     54             #pragma target 2.0
     55 
     56             #include "UnityCG.cginc"
     57             #include "UnityUI.cginc"
     58 
     59             #pragma multi_compile __ UNITY_UI_CLIP_RECT
     60             #pragma multi_compile __ UNITY_UI_ALPHACLIP
     61 
     62             struct appdata_t
     63             {
     64                 float4 vertex   : POSITION;
     65                 float4 color    : COLOR;
     66                 float2 texcoord : TEXCOORD0;
     67                 UNITY_VERTEX_INPUT_INSTANCE_ID
     68             };
     69 
     70             struct v2f
     71             {
     72                 float4 vertex   : SV_POSITION;
     73                 fixed4 color    : COLOR;
     74                 float2 texcoord  : TEXCOORD0;
     75                 float4 worldPosition : TEXCOORD1;
     76                 UNITY_VERTEX_OUTPUT_STEREO
     77             };
     78 
     79             sampler2D _MainTex;
     80             fixed4 _Color;
     81             fixed4 _TextureSampleAdd;
     82             float4 _ClipRect;
     83             float4 _MainTex_ST;
     84 
     85             v2f vert(appdata_t v)
     86             {
     87                 v2f OUT;
     88                 UNITY_SETUP_INSTANCE_ID(v);
     89                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
     90                 OUT.worldPosition = v.vertex;
     91                 OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);
     92 
     93                 OUT.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
     94 
     95                 OUT.color = v.color * _Color;
     96                 return OUT;
     97             }
     98 
     99             fixed4 frag(v2f IN) : SV_Target
    100             {
    101                 half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;
    102 
    103                 #ifdef UNITY_UI_CLIP_RECT
    104                 color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
    105                 #endif
    106 
    107                 #ifdef UNITY_UI_ALPHACLIP
    108                 clip (color.a - 0.001);
    109                 #endif
    110 
    111                 //修改处1
    112                 float gray = dot(color.xyz, float3(0.299, 0.587, 0.114));
    113                 color.xyz = float3(gray, gray, gray);
    114 
    115                 return color;
    116             }
    117         ENDCG
    118         }
    119     }
    120 }

    效果如下:

  • 相关阅读:
    毕业面试心程
    hash_map的简洁实现
    缓冲区溢出攻击
    统计一下你写过多少代码
    SecureCRT自动断开
    误操作yum导致error: rpmdb
    Foxmail7.2新建的文件夹不见了
    pycurl安装
    一起用ipython
    vi/vim多行注释和取消注释
  • 原文地址:https://www.cnblogs.com/lyh916/p/13514644.html
Copyright © 2011-2022 走看看