• [UnityShader基础]08.UI-Default.shader


    参考链接:

    https://zhuanlan.zhihu.com/p/32561155

    https://blog.csdn.net/WuShangLZ/article/details/80401441

    https://www.jianshu.com/p/2d98fad196d2

    内置shader下载方法:

    https://jingyan.baidu.com/article/63acb44a1f598161fcc17efa.html

    unity版本:2018.3.8f1

    内置UI-Default.shader:

      1 // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
      2 
      3 Shader "UI/Default"
      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                 return color;
    112             }
    113         ENDCG
    114         }
    115     }
    116 }

    1.模板测试

    2.

    UnityGet2DClipping的定义在UnityUI.cginc中:

    1 inline float UnityGet2DClipping (in float2 position, in float4 clipRect)
    2 {
    3     float2 inside = step(clipRect.xy, position.xy) * step(position.xy, clipRect.zw);
    4     return inside.x * inside.y;
    5 }

    step(a, x):如果a>x,返回0;否则,返回1

    clipRect:左下角为xy,右上角为zw

    因此,也就是说,如果点不在clipRect中,则返回0

  • 相关阅读:
    react-custom-scrollbars的使用
    【react】Mobx总结以及mobx和redux区别
    【React】Redux入门 & store体验
    chrome安装react-devtools开发工具
    【vue】vuex防止数据刷新数据刷掉
    搭建博客的两个工具区别
    JavaScript中的作用域
    通过JavaScript创建表格
    JavaScript中的普通for循环和 for in循环
    JavaScript中创建默认对象的方式
  • 原文地址:https://www.cnblogs.com/lyh916/p/10726812.html
Copyright © 2020-2023  润新知