使用UnityShader实现河流流动或岩浆流动效果
实现原理------------------------------------------------------------------------------------
1. 使用纹理偏移,注意设置纹理为 Repeat模式
fixed4 frag (v2f i) : SV_Target
{
float2 UV = i.uv;
float offsetX = _SpeedX * _Time.y; //x 偏移
float offsetY = _SpeedY * _Time.y; //y 偏移
UV += i.uv + float2(offsetX, offsetY);
fixed4 col = tex2D(_MainTex, UV);
return col;
}
2.使用sin或cos函数偏移UV
_Swing("_Swing 振幅",Range(0,20)) = 0.2
_Frequency("_Frequency 频率",Range(0,20))=3
_Speed("_Speed",Range(0,20)) = 3
fixed4 frag (v2f i) : SV_Target
{
// y=Acos(wt+&) A 振幅 _Swing, w影响频率 _Frequency
float offset = cos(_Frequency*i.uv.x + _Time.y * _Speed) * _Swing;
float2 UV = float2(i.uv.x,i.uv.y+offset);
fixed4 col = tex2D(_MainTex, UV);
return col;
}
源码下载:QQ群:808297975
免责声明:图片素材来源于Unity资源商店免费资源包,仅供学习使用
参考资料:https://edu.manew.com/my/course/443/material?type=material
参考书籍:冯乐乐《unityshader入门精要》
参考书籍:郭浩瑜,张鹤《unity3D shaderLab开发实战详解》