美文网首页
unity shader UV旋转

unity shader UV旋转

作者: 独步江雪 | 来源:发表于2020-12-06 14:48 被阅读0次
a.gif
Shader "Custom/RotateUVShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _RotateSpeed ("旋转速度", Float) = 1.0
    }
    SubShader
    {
        Tags
        {
            "RenderType"="Opaque"
        }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };
 
            struct v2f
            {
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
 
            };

            sampler2D _MainTex;
            float4 _MainTex_ST; 
            half _RotateSpeed; 
            v2f vert (appdata v)
            {
                v2f o;
 
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }

            half getMold(fixed3 v)
            {
                return pow( (pow(v.x,2)+pow(v.y,2)+pow(v.z,2)),0.5);
                
            }
            
            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
        
                float2 uv = i.uv.xy - float2(0.5, 0.5);//移动坐标系原点
                uv = float2(uv.x *cos(_RotateSpeed *_Time.x) - uv.y * sin(_RotateSpeed *_Time.x),uv.x *sin(_RotateSpeed * _Time.x) + uv.y*cos(_RotateSpeed *_Time.x));//自行推导
                uv += float2(0.5, 0.5);
                uv.x = clamp(uv.x, 0, 1);
                uv.y = clamp(uv.y, 0, 1);
                float4 col = tex2D(_MainTex,  uv);

          
                // apply fog
                UNITY_APPLY_FOG(i.fogCoord, col);
                return col;
            }
            ENDCG
        }
    }
}

相关文章

网友评论

      本文标题:unity shader UV旋转

      本文链接:https://www.haomeiwen.com/subject/pipowktx.html