用极坐标实现简单的UI



什么是极坐标?
参考the shader of book https://thebookofshaders.com/07/?lan=ch
百度百科!!!哇,真的是从百度百科学的,很详细

1.用极坐标实现角度
2.实现uv的扭转
3.颜色板
4.UI特效算法的实现

1.计算表角度

将uv原点移动到中间,求角度
再将【0,2Π】的角度转换到【0,1】方便我们的思考和计算

float2 uv = i.uv-0.5;
float angle=atan2(uv.x,uv.y);
float angle01=frac(angle/3.14/2);

2.实现uv扭转

就是摘抄的shadergraph的源码,
可以看出他用了2d的旋转矩阵
并通过uv.length控制旋转量,这里不推理了

        float2 twirl(float2 UV , float2 Center, float Strength,float2 set)
        {
            float2 delta = UV - Center;
            float angle = Strength * length(delta);
            float x = cos(angle) * delta.x - sin(angle) * delta.y;
            float y = sin(angle) * delta.x + cos(angle) * delta.y;
            return float2(x + Center.x + set.x, y + Center.y + set.y);
        }

3.颜色板

iq的分享:具体不细说
使用方法 下面的参数,通过T【0,1】来控制颜色的变换
此篇文章 将极坐标的角度 当做T 来变换颜色
a,b,c,d可以填写第二张图片里的数字达到指定效果



4.UI特效算法的实现

1.如何画出条状线:将角度展开到2维直角坐标系


float angle=atan2(uv.x,uv.y);
float angle01=frac(angle/3.14/2);
float valueMask = sign(frac(angle01 * _number) - _black);

2.如何实现线条的长度不同:先用一个noise贴图得出求出每个放射性条纹的灰度值,然后再配合length(uv)来实现


float floorMask = floor(angle01 * _number);
float noiseValue = tex2D(_noiseMap, float2(floorMask/_number , _Time.y/10));
finalMask = step(1-finalMask , 1-length(uv0));

让我们在换个贴图验证一下



shader

Shader "Hidden/UV"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _number("number",int)=60
        _black("black",Range(0,1))=0.2
        _strength("strength",float)=0
        _swing("swing",Range(0,2))=0.5
        _noiseMap("noiseMap",2D)="white"{}
    }

    CGINCLUDE

        float2 twirl(float2 UV , float2 Center, float Strength,float2 set)
        {
            float2 delta = UV - Center;
            float angle = Strength * length(delta);
            float x = cos(angle) * delta.x - sin(angle) * delta.y;
            float y = sin(angle) * delta.x + cos(angle) * delta.y;
            return float2(x + Center.x + set.x, y + Center.y + set.y);
        }

        float2 rotation(float2 UV,float2 Center,float Rotation){
            UV -= Center;
            float s = sin(Rotation);
            float c = cos(Rotation);
            float2x2 rMatrix = float2x2(c, -s, s, c);
            rMatrix *= 0.5;
            rMatrix += 0.5;
            rMatrix = rMatrix * 2 - 1;
            UV.xy = mul(UV.xy, rMatrix);
            UV += Center;
            return UV;
        }

        float2 polar(float2 UV,float2 Center,float RadialScale,float LenghtScale){
            float2 delta = UV - Center;
            float radius = length(delta) * 2 * RadialScale;
            float angle = atan2(delta.x, delta.y) * 1.0/6.28 * LenghtScale;
            float2 uv = float2(radius,angle);
            return uv;
        }

        float3 palette( in float t, in float3 a, in float3 b, in float3 c, in float3 d )
        {
            return a + b*cos( 6.28318*(c*t+d) );
        }

    ENDCG

    SubShader
    {
        // No culling or depth
        Cull Off ZWrite Off ZTest Always

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            sampler2D _MainTex;
            int _number;
            float _black;
            float _strength;
            sampler2D _noiseMap;
            float _swing;

            fixed4 frag (v2f i) : SV_Target
            {
                
                

                float2 uv = i.uv-0.5;
                float2 uv0=uv;
            
                uv=twirl(uv,float2(0,0),_strength,float2(0,0));

                float sphere=1-step(1,length(uv*10));

                float angle=atan2(uv.x,uv.y);
                float angle01=frac(angle/3.14/2);
                
                float floorMask = floor(angle01 * _number);
                float noiseValue = tex2D(_noiseMap, float2(floorMask/_number , _Time.y/10));
                noiseValue = smoothstep(0.5-_swing,0.5+_swing,noiseValue);

                float valueMask = sign(frac(angle01 * _number) - _black);

                float finalMask = (valueMask *noiseValue );

                finalMask = step(1-finalMask , 1-length(uv0));


                float3 Color = palette(angle01,float3(0.5,0.5,0.5),float3(0.5,0.5,0.5),float3(1.0,1.0,1.0),float3(0.0,0.33,0.67));
                Color = finalMask*Color;

                fixed4 col = tex2D(_MainTex, i.uv*3);
                float mask=step(0.15,length(uv0));
                col = col*(1-mask);
                float mask2=step(0.16,length(uv0));
                col.xyz+=Color*mask2;

                return float4(col.xyz,1.0);
            }
            ENDCG
        }
    }
}

你可能感兴趣的:(用极坐标实现简单的UI)