Unity Shader實(shí)現(xiàn)水波紋效果
本文實(shí)例為大家分享了Unity Shader實(shí)現(xiàn)水波紋的具體代碼,供大家參考,具體內(nèi)容如下
效果:
Shader代碼:
Shader "Custom/shuibowen"{ Properties{ _MainTex("Base (RGB)",2D)="white"{} _distanceFactor("Distancefactor",float)=1 _timeFactor("time factor",float)=2 _totalFactor("total factor",float)=3 _waveWidth("wave width",float)=4 _curWaveDis("curwave dis",float)=5 _startPos("star pos",Vector) = (1,1,1,1) _MainTex_TexelSize("Maintex_texelSize",vector)=(1,1,1,1) } CGINCLUDE #include "UnityCG.cginc" uniform sampler2D _MainTex; float4 _MainTex_TexelSize; uniform float _distanceFactor; uniform float _timeFactor; uniform float _totalFactor; uniform float _waveWidth; uniform float _curWaveDis; uniform float4 _startPos; fixed4 frag(v2f_img i) : SV_Target { //DX下紋理坐標(biāo)反向問題 #if UNITY_UV_STARTS_AT_TOP if (_MainTex_TexelSize.y < 0) _startPos.y = 1 - _startPos.y; #endif //計算uv到中間點(diǎn)的向量(向外擴(kuò),反過來就是向里縮) float2 dv = _startPos.xy - i.uv; //按照屏幕長寬比進(jìn)行縮放 dv = dv * float2(_ScreenParams.x / _ScreenParams.y, 1); //計算像素點(diǎn)距中點(diǎn)的距離 float dis = sqrt(dv.x * dv.x + dv.y * dv.y); //用sin函數(shù)計算出波形的偏移值factor //dis在這里都是小于1的,所以我們需要乘以一個比較大的數(shù),比如60,這樣就有多個波峰波谷 //sin函數(shù)是(-1,1)的值域,我們希望偏移值很小,所以這里我們縮小100倍,據(jù)說乘法比較快,so... float sinFactor = sin(dis * _distanceFactor + _Time.y * _timeFactor) * _totalFactor * 0.01; //距離當(dāng)前波紋運(yùn)動點(diǎn)的距離,如果小于waveWidth才予以保留,否則已經(jīng)出了波紋范圍,factor通過clamp設(shè)置為0 float discardFactor = clamp(_waveWidth - abs(_curWaveDis - dis), 0, 1) / _waveWidth; //歸一化 float2 dv1 = normalize(dv); //計算每個像素uv的偏移值 float2 offset = dv1 * sinFactor * discardFactor; //像素采樣時偏移offset float2 uv = offset + i.uv; return tex2D(_MainTex, uv); } ENDCG SubShader { Pass { ZTest Always Cull Off ZWrite Off Fog { Mode off } CGPROGRAM #pragma vertex vert_img #pragma fragment frag #pragma fragmentoption ARB_precision_hint_fastest ENDCG } } Fallback off }
C#代碼:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class WaterWaveEffect : PostEffectsBase { //距離系數(shù) public float distanceFactor = 60.0f; //時間系數(shù) public float timeFactor = -30.0f; //sin函數(shù)結(jié)果系數(shù) public float totalFactor = 1.0f; //波紋寬度 public float waveWidth = 0.3f; //波紋擴(kuò)散的速度 public float waveSpeed = 0.3f; private float waveStartTime; private Vector4 startPos = new Vector4(0.5f, 0.5f, 0, 0); public Material _Material; void OnRenderImage(RenderTexture source, RenderTexture destination) { //計算波紋移動的距離,根據(jù)enable到目前的時間*速度求解 float curWaveDistance = (Time.time - waveStartTime) * waveSpeed; //設(shè)置一系列參數(shù) _Material.SetFloat("_distanceFactor", distanceFactor); _Material.SetFloat("_timeFactor", timeFactor); _Material.SetFloat("_totalFactor", totalFactor); _Material.SetFloat("_waveWidth", waveWidth); _Material.SetFloat("_curWaveDis", curWaveDistance); _Material.SetVector("_startPos", startPos); Graphics.Blit(source, destination, _Material); } void Update() { if (Input.GetMouseButton(0)) { Vector2 mousePos = Input.mousePosition; //將mousePos轉(zhuǎn)化為(0,1)區(qū)間 startPos = new Vector4(mousePos.x / Screen.width, mousePos.y / Screen.height, 0, 0); waveStartTime = Time.time; } } }
新建一個材質(zhì)球,選擇此shader,并賦值給這個腳本,點(diǎn)擊屏幕即可看到效果
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
winform中的ListBox和ComboBox綁定數(shù)據(jù)用法實(shí)例
這篇文章主要介紹了winform中的ListBox和ComboBox綁定數(shù)據(jù)用法,實(shí)例分析了將集合數(shù)據(jù)綁定到ListBox和ComboBox控件的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2014-12-12一種c#深拷貝方式完勝java深拷貝(實(shí)現(xiàn)上的對比分析)
下面小編就為大家?guī)硪黄环Nc#深拷貝方式完勝java深拷貝(實(shí)現(xiàn)上的對比分析)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-07-07C# 中將數(shù)值型數(shù)據(jù)轉(zhuǎn)換為字節(jié)數(shù)組的方法
C# 中將數(shù)值型數(shù)據(jù)轉(zhuǎn)換為字節(jié)數(shù)組的方法,需要的朋友可以參考一下2013-05-05C#實(shí)現(xiàn)獲取多維數(shù)組的行數(shù)與列數(shù)
這篇文章主要為大家詳細(xì)介紹了C#如何分別使用Array.GetUpperBound方法和Array.GetLength方法實(shí)現(xiàn)獲取多維數(shù)組的行數(shù)與列數(shù),需要的可以參考下2024-02-02C#應(yīng)用XML作為數(shù)據(jù)庫的快速開發(fā)框架實(shí)現(xiàn)方法
這篇文章主要介紹了C#應(yīng)用XML作為數(shù)據(jù)庫的快速開發(fā)框架實(shí)現(xiàn)方法,詳細(xì)介紹了將XML作為數(shù)據(jù)庫的C#桌面應(yīng)用開發(fā)技巧,具有一定的參考借鑒價值,需要的朋友可以參考下2014-12-12C#通過WIN32 API實(shí)現(xiàn)嵌入程序窗體
這篇文章主要介紹了C#通過WIN32 API實(shí)現(xiàn)嵌入程序窗體的方法,涉及WIN32 API的調(diào)用及窗體的設(shè)計,具有很好的借鑒價值,需要的朋友可以參考下2014-09-09