旋转.shader

18 篇文章 0 订阅
2 篇文章 0 订阅

这里写图片描述
代码:

前面的属性不用管,算法都在片段处理器上

Shader "Custom/RotateTexShader"
{
    Properties
    {
        _RotateSpeed("Rotate Speed", float) = 1.0
        _BaseColor("Base Color", Color) = (1,1,1,1)
        _BaseTexU("BaseTexU", float) = 1.0
        _BaseTexV("BaseTexV", float) = 1.0 
        _BaseTex("Base Tex(RGB)", 2D) = "white" {}
        _DecorativeColor("Decorative Color", Color) = (1,1,1,1)
        _DecorativeTexU("DecorativeTexU", float) = 1.0
        _DecorativeTexV("DecorativeTexV", float) = 1.0
        _DecorativeTex("Decorative Tex(RGB)", 2D) = "white"{}
    }

    SubShader
    {
        tags{"Queue" = "Transparent" "RenderType" = "Transparent" "IgnoreProjector" = "True"}
        Blend SrcAlpha OneMinusSrcAlpha

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            float _RotateSpeed;

            float4 _BaseColor;
            float _BaseTexU;
            float _BaseTexV;
            sampler2D _BaseTex;

            float4 _MainTex_ST;

            float4 _DecorativeColor;
            float _DecorativeTexU;
            float _DecorativeTexV;
            sampler2D _DecorativeTex;

            struct v2f
            {
                float4 pos:POSITION;
                float4 uv:TEXCOORD0;
            };

            struct appdata{
                float4 vertex : POSITION;
                float4 texcoord : TEXCOORD0;
            };

            v2f vert(appdata v)
            {
                v2f o;
                o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                o.uv.xy = v.texcoord.xy;
                return o;
            }

            //可以选择正向旋转或者逆向旋转
            half2 rotateUV(bool direction, float2 uv){
                float2 moveUV = uv.xy - float2(0.5, 0.5);
                float2 rotateUV;
                if(direction){
                    //三角函数加上线性代数-已知旋转的角度和初始坐标求最终坐标
                    rotateUV = float2(moveUV.x * cos(_RotateSpeed * _Time.x) - moveUV.y * sin(_RotateSpeed * _Time.x), 
                            moveUV.x * sin(_RotateSpeed * _Time.x) + moveUV.y * cos(_RotateSpeed * _Time.x));
                }else{
                    rotateUV = float2(moveUV.x * cos(_RotateSpeed * _Time.x) + moveUV.y * sin(_RotateSpeed * _Time.x), 
                            moveUV.x * sin(_RotateSpeed * _Time.x) - moveUV.y * cos(_RotateSpeed * _Time.x));
                }
                return rotateUV - float2(0.5, 0.5);
            }

            half4 frag(v2f i):COLOR
            {   
                half4 baseColor = tex2D(_BaseTex, rotateUV(true, i.uv.xy)  * float2(_BaseTexU, _BaseTexV)) * _BaseColor;
                half4 decorativeColor = tex2D(_DecorativeTex, rotateUV(false, i.uv.xy) * float2(_DecorativeTexU, _DecorativeTexV)) * _DecorativeColor;
                return baseColor * decorativeColor;
            }

            ENDCG
        }
    }
}

数学差的人是不是感到了这个世界深深的恶意。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值