旋转图片

作者:追风剑情 发布于:2024-4-9 19:01 分类:Shader

1、工程截图

111.png

2、Shader

Shader "Custom/RotationUV"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        //旋转速度
        _Speed ("Speed", float) = 1.0
    }
    SubShader
    {
        Tags { "RenderType"="Transparent" "Queue" = "Transparent" "IgnoreProjector" = "True" "PreviewType" = "Plane" }
        LOD 100

        //Alpha混合
        Blend SrcAlpha OneMinusSrcAlpha

        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;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            float _Speed;

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

            fixed4 frag (v2f i) : SV_Target
            {
                float t = _Time.y * _Speed;
                /*-- 算法原理:通过旋转坐标基向量来旋转uv --*/
                fixed2 center = fixed2(0.5, 0.5);//坐标原点
                fixed2 v = i.uv - center;
                //乘以旋转矩阵
                fixed x = v.x * cos(t) + v.y * sin(t);
                fixed y = v.x * cos(t + UNITY_HALF_PI) + v.y * sin(t + UNITY_HALF_PI);
                //旋转后的uv
                fixed2 uv = fixed2(x, y);
                uv += center;
                //--End

                fixed4 col = tex2D(_MainTex, uv);
                return col;
            }
            ENDCG
        }
    }
}


运行效果
11111114.gif

标签: Shader

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号