Unity Shader : My Simplest shader(1)

Code

/*
/*
author  :   Jave.Lin
date    :   2018-07-23
Testing texture, tintColor, filterAlpha(TTF) img shader

This is Simplest shader which have texture and tint color, and filter alpha, for ui(2d/3d)
*/
Shader "Testing/UI-TTF"
{
    Properties
    {
        _MainTex ("Sprite Texture", 2D) = "white" {}
        _TintColor ("Tint", Color) = (1,1,1,1)
        _FilterAlpha ("FilterAlpha", range(0.0, 1.0)) = 0.0
    }

    SubShader
    {
        Tags
        {
            "Queue" = "Transparent"
        }

        Cull Off
        Blend SrcAlpha OneMinusSrcAlpha

        Pass
        {
            Name "Default"
        CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            uniform sampler2D _MainTex;
            uniform fixed4 _MainTex_ST; // scale and translate(offset)
            uniform fixed4 _TintColor;
            uniform fixed _FilterAlpha;

            struct appdata_my
            {
                float4 vertex : POSITION0;
                fixed2 texcoord : TEXCOORD0;
            };

            struct v2f
            {
                float4 vertex   : POSITION0;
                fixed4 color    : COLOR;
                float2 texcoord  : TEXCOORD0;
            };

            v2f vert(appdata_my v)
            {
                v2f o;

                // method1 : to homogeneous coordinates
                // UnityObjectToViewPos prototype:
                // Tranforms position from object to camera space
                /*
                inline float3 UnityObjectToViewPos( in float3 pos )
                {
                    return mul(UNITY_MATRIX_V, mul(unity_ObjectToWorld, float4(pos, 1.0))).xyz;
                }
                */
                //o.vertex.xyz = UnityObjectToViewPos(v.vertex); // mv

                // UnityViewToClipPos prototype:
                // Tranforms position from view to homogeneous space
                /*
                inline float4 UnityViewToClipPos( in float3 pos )
                {
                    return mul(UNITY_MATRIX_P, float4(pos, 1.0));
                }
                */
                //o.vertex = UnityViewToClipPos(o.vertex.xyz); // p

                // method2 : to homogeneous coordinates
                // UnityObjectToClipPos prototype:
                // using UnityObjectToClipPos more efficient
                /*
                // Tranforms position from object to homogeneous space
                inline float4 UnityObjectToClipPos(in float3 pos)
                {
                    // More efficient than computing M*VP matrix product
                    return mul(UNITY_MATRIX_VP, mul(unity_ObjectToWorld, float4(pos, 1.0)));
                }
                inline float4 UnityObjectToClipPos(float4 pos) // overload for float4; avoids "implicit truncation" warning for existing shaders
                {
                    return UnityObjectToClipPos(pos.xyz);
                }
                */
                o.vertex = UnityObjectToClipPos(v.vertex);

                o.color = _TintColor;
                /*
                // TRANSFORM_TEX prototype:
                // Transforms 2D UV by scale/bias property
                #define TRANSFORM_TEX(tex,name) (tex.xy * name##_ST.xy + name##_ST.zw)
                */
                o.texcoord.xy = TRANSFORM_TEX(v.texcoord, _MainTex); // apply scale and offset
                return o;
            }

            fixed4 frag(v2f IN) : COLOR
            {
                // tex 2d mapping by filtering
                fixed4 texColor = tex2D(_MainTex, IN.texcoord.xy);
                if (texColor.a <= _FilterAlpha) discard;
                //texColor.rgb *= texColor.a;
                // output: and multiply tint-color
                return texColor * IN.color;
            }
        ENDCG
        }
    }
}

Inspector

inspector 的选项

Running

shader 测试效果

猜你喜欢

转载自blog.csdn.net/linjf520/article/details/81180363
今日推荐