Unity Shader : My Simplest shader(1)

141 篇文章 34 订阅
49 篇文章 0 订阅

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 测试效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值