使用UnityShader实现2D流光效果
实现原理------------------------------------------------------------------------------------
1. 使用纹理偏移,注意设置纹理为 Repeat模式
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
//流光UV
float2 flowUV = i.uv;
//UV 延X轴偏移
flowUV.x += _Time.y;
fixed4 flowCol = tex2D(_FlowTex, flowUV);
//像素叠加
col = col + flowCol;
return col;
}
-
也可以修改 Tiling and offset 值
-
使用uv坐标变换生成流光纹理
Shader "zclShader/流光公式版Shader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Percent("_Percent",Range(-1.2,0.2))=2 //流光的位置
_Power("_Power",Range(0,20)) = 2
_Angle("_Angle",Range(0,6.28))=0.5 //流光的方向
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
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;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
float _Percent;
float _Power;
float _Angle;
fixed4 frag (v2f i) : SV_Target
{
float2 tempUV = i.uv;
float2x2 MAT = {cos(_Angle),sin(_Angle),-sin(_Angle),cos(_Angle)};
tempUV = mul(MAT, tempUV);
fixed4 col = tex2D(_MainTex, i.uv);
fixed4 y = tempUV.x;
fixed4 flowCol = y + _Percent; // (-0.5,0.5)
flowCol = abs(flowCol); // (0.5,0,0.5)
flowCol = flowCol * _Power; //( 1,0.09,1)
flowCol = lerp(1, 0, saturate(flowCol)); //反色(0,0.99,0)
return col+fixed4(flowCol.x,0,0,1);
}
ENDCG
}
}
}
源码下载:QQ群:808297975
免责声明:图片素材来源于Unity资源商店免费资源包,仅供学习使用
参考资料:https://edu.manew.com/my/course/443/material?type=material
参考书籍:冯乐乐《unityshader入门精要》
参考书籍:郭浩瑜,张鹤《unity3D shaderLab开发实战详解》