Unity Shader 笔记


Sprite:


纵向 进度条效果

Shader "Custom/URP/SpriteVerticalProgress"
{
    Properties
    {
        [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
        _Color ("Tint", Color) = (1,1,1,1)
        _Fill ("Fill (0-1)", Range(0,1)) = 1.0
        _Invert ("Invert (0: bottom->top, 1: top->bottom)", Range(0,1)) = 0
        _Feather ("Feather (0-0.5)", Range(0,0.5)) = 0.0
    }

    SubShader
    {
        Tags
        {
            "Queue"="Transparent"
            "RenderType"="Transparent"
            "IgnoreProjector"="True"
            "RenderPipeline"="UniversalPipeline"
        }

        Pass
        {
            Name "ForwardLit"
            Tags { "LightMode"="UniversalForward" }

            Blend SrcAlpha OneMinusSrcAlpha
            ZWrite Off
            Cull Off

            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

            struct Attributes
            {
                float4 positionOS   : POSITION;
                float4 color        : COLOR;
                float2 uv           : TEXCOORD0;
            };

            struct Varyings
            {
                float4 positionHCS  : SV_POSITION;
                float4 color        : COLOR;
                float2 uv           : TEXCOORD0;
            };

            TEXTURE2D(_MainTex);
            SAMPLER(sampler_MainTex);

            float4 _MainTex_ST;
            float4 _Color;
            float _Fill;
            float _Invert;
            float _Feather;

            Varyings vert (Attributes IN)
            {
                Varyings OUT;
                OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
                OUT.uv = TRANSFORM_TEX(IN.uv, _MainTex);
                OUT.color = IN.color * _Color;
                return OUT;
            }

            half4 frag (Varyings IN) : SV_Target
            {
                half4 tex = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.uv);

                float uvY = IN.uv.y;
                if (_Invert > 0.5) uvY = 1.0 - uvY;

                float f = saturate(_Fill);
                float feather = saturate(_Feather);

                float lower = f - feather;
                float upper = f + feather;
                float mask = smoothstep(lower, upper, uvY);

                float alpha = tex.a * mask * _Color.a;
                float3 rgb = tex.rgb * IN.color.rgb;

                return half4(rgb, alpha);
            }
            ENDHLSL
        }
    }
}

Image:





C# string.Format 字符串格式化速查表

Unity ScriptableObject 数据被重置Bug记录

评 论