Basic functions of UI Shader in Unity


Preface

Basic functions of UI Shader in Unity

1. Implementation ideas

1. Expose a 2D type attribute to accept the texture of the UI

//The naming must be according to the standard, so that this property can be associated with the properties in the Unity component
//For example, when changing the source picture of the Image, change this
[PerRendererData]_MainTex("MainTex",2D) = "white" at the same time "{}

2. Set the shader level to the TransParent translucent rendering level. Generally, the UI is at this rendering level.

//Change the rendering queue (the rendering queue of the UI is generally a translucent layer)
Tags {"Queue" = "TransParent"}

3. Change the blending mode, which is the texture used by the UI. Make the transparent places transparent.

//Blend modeBlend
SrcAlpha OneMinusSrcAlpha

2. Code implementation

Shader"MyShader/P1_1_1"
{
    Properties
    {
        //命名要按标准来,这个属性才可以和Unity组件中的属性产生关联
        //比如说,在更改 Image 的源图片时,同时更改这个
        
        [PerRendererData]_MainTex("MainTex",2D) = "white"{}
    }
    SubShader
    {
        //更改渲染队列(UI的渲染队列一般是半透明层的)
        Tags {"Queue" = "TransParent"}
        //混合模式
        Blend SrcAlpha OneMinusSrcAlpha
        Pass
        {
            CGPROGRAM
            #pragma vertex  vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            //存储 应用程序输入到顶点着色器的信息
            struct appdata
            {
                //顶点信息
                float4 vertex:POSITION;

                float2 uv : TEXCOORD;
            };
            //存储 顶点着色器输入到片元着色器的信息
            struct v2f
            {
                //裁剪空间下的位置信息
                float4 pos:SV_POSITION;
                float2 uv : TEXCOORD;
            };
            
            sampler2D _MainTex;
            
            v2f vert(appdata v)
            {
                v2f o;
                //把顶点信息转化到裁剪坐标下
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }
            fixed4 frag(v2f i) : SV_Target
            {
                fixed4 mainTex = tex2D(_MainTex,i.uv);
                return  mainTex;
            }
            
            ENDCG
        }
    }
}

Property settings:
Insert image description here

Effect:
Insert image description here

Guess you like

Origin blog.csdn.net/qq_51603875/article/details/132861304