代码:
前面的属性不用管,算法都在片段处理器上
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
}
}
}
数学差的人是不是感到了这个世界深深的恶意。。