Unity Shader 擾動效果
阿新 • • 發佈:2019-02-05
之前寫的擾動shader找不到了,重寫了一個。
效果圖:
C#程式碼很簡單:
然後是對應的shaderusing System.Collections; using System.Collections.Generic; using UnityEngine; public class Distortion : MonoBehaviour { public Material mat; void OnRenderImage(RenderTexture src, RenderTexture dst) { Graphics.Blit(src, dst, mat); } }
Shader "Custom/Distortion" { Properties { _MainTex("Texture", 2D) = "white" {} _R("R",Range(-0.5,1)) = 0 _Width("Width",Range(0,0.5)) = 0 [Toggle]_Show("Show",Range(0,1)) = 0 _OriginPos("Origin",Vector) = (0,0,0,0) } 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; half4 _MainTex_TexelSize; float _R; float _Width; bool _Show; float4 _OriginPos; fixed4 frag(v2f i) : SV_Target { //中心偏移 float2 uv = i.uv - _OriginPos; uv.x *= _MainTex_TexelSize.y / _MainTex_TexelSize.x; //半徑位置 float2 center = float2(1,1)*_R; float dis = sqrt(pow(uv.x, 2) + pow(uv.y, 2)); //半個寬度 float halfWid = _Width / 2; float centerR = _R + halfWid; fixed4 col = float4(0,0,0,0); //強度 float power = 1; if (dis > _R&&dis < _R + _Width) { power = 1 - abs(dis - centerR) / halfWid; col.r = power; } //是否顯示紅黑圖 if (_Show == false) { if (dis > _R&&dis < _R + _Width) { power *= 10; col = tex2D(_MainTex, i.uv + float2(_MainTex_TexelSize.x* power, _MainTex_TexelSize.y*power)); } else { col = tex2D(_MainTex, i.uv); } } return col; } ENDCG } } }
效果圖: