Properties
{
_MainTex ("Texture", 2D) = "white" {}
_FlowColor ("Flow Color", Color) = (1, 1, 1, 1) // 流光颜色
_FlowFrequency ("Flow Frequency", Float) = 1.0 // 流光频率
_FlowSpeed ("Flow Speed", Float) = 1.0 // 流光速度
_FlowAmplitude ("Flow Amplitude", Float) = 0.5 // 流光振幅
_FlowWidth ("Flow Width", Float) = 0.1 // 流光宽度
_FlowRotationAngle ("Flow Rotation Angle", Float) = 90.0 // 流光旋转角度,以度为单位
_FlowPhase ("Flow Phase", Float) = 0.0 // 流光相位
}
SubShader
{
Tags {
"RenderType" = "Opaque"
"Queue" = "Geometry+10"
}
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#pragma shader_feature _ ENABLE_FLOWLIGHT
struct appdata
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
float3 normal : NORMAL;
};
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float3 pos_world : TEXCOORD1;
float3 pivot_world : TEXCOORD2;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float4 _FlowColor;
float _FlowFrequency;
float _FlowSpeed;
float _FlowAmplitude;
float _FlowWidth;
float _FlowRotationAngle;
float _FlowPhase;
v2f vert (appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
float3 pos_world = mul(unity_ObjectToWorld, v.vertex).xyz;
o.pos_world = pos_world;
o.pivot_world = mul(unity_ObjectToWorld, float4(0.0, 0.0, 0.0, 1.0)).xyz;
o.uv = v.texcoord;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
half3 baseColor = tex2D(_MainTex, i.uv).rgb;
#ifdef ENABLE_FLOWLIGHT
// 计算相对位置
half2 uv_flow = (i.pos_world.xy - i.pivot_world.xy);
// 将度数转换为弧度
float rad = radians(_FlowRotationAngle);
// 计算旋转矩阵
float cosTheta = cos(rad);
float sinTheta = sin(rad);
float2x2 rotationMatrix = float2x2(cosTheta, -sinTheta, sinTheta, cosTheta);
// 应用旋转矩阵到UV坐标
uv_flow = mul(rotationMatrix, uv_flow);
// 计算时间因子,包括相位调整
float timeFactor = _Time.y * _FlowSpeed +_FlowPhase;
// 计算流光效果
float wave = sin(_FlowFrequency * uv_flow.x + timeFactor) * _FlowAmplitude;
// 构建流光Mask
float flowMask = smoothstep(0.5 - _FlowWidth, 0.5, 0.5 + wave) - smoothstep(0.5, 0.5 + _FlowWidth, 0.5 + wave);
// 叠加流光颜色
float3 final_col = baseColor + flowMask * _FlowColor.rgb;
#else
float3 final_col = baseColor;
#endif
return float4(final_col, 1.0);
}
ENDCG
}
}
UV这样算,可以保证3D模型,流光可以沿着一个方向流动