呈現的效果大概是這樣
設定材質顯示、加乘顏色、切除的量。這邊多一個Seed參數來改變假亂數的亂數值,Length來控制速度線/雜訊的長度,最後多一個Style來切換這兩種效果。
_MainTex ("Base (RGB)", 2D) = "white" {} _Color ("Color", Color) = (1,1,1,1) _Amount ("Amount", Range (0,1)) = 0 _Seed ("Seed", Range (0.1,1.0)) = 0.1 _Length ("Length", Range(0.1,1)) = 0.1 _Style ("Style", Range (0,1)) = 0
概念同樣也很簡單,先製作一個假的隨機,使用dot()讓原本的向量改變,dot(x,y)=x[0]*y[0]+x[1]*y[1]...。以速度線來說,使用該點的Y座標去偏移,此時的速度線會是整齊鋸齒狀排列,所以再使用個cos讓它更亂一點,或sin也可以,最後再讓該點的X座標加上偏移的Y值減去_Amount就可以有速度線效果,而不是斜切。雜訊或風吹砂的效果概念也一樣就是XY座標都偏移而已。
float random = frac(cos(dot(coord.x*floor(_Style)+coord.y ,_Seed*25 )) * 45678.54321); float4 color = tex2D(_MainTex, input.uv); clip (coord.x + random*_Length - (1+_Length)*_Amount);
完整Shader Code
Shader "Custom/Fading" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} _Color ("Color", Color) = (1,1,1,1) _Amount ("Amount", Range (0,1)) = 0 _Seed ("Seed", Range (0.1,1.0)) = 0.1 _Length ("Length", Range(0.1,1)) = 0.1 _Style ("Style", Range (0,1)) = 0 } SubShader { Pass { Cull Off Lighting Off ZWrite Off Blend SrcAlpha OneMinusSrcAlpha CGPROGRAM #pragma vertex vert #pragma fragment frag uniform sampler2D _MainTex; uniform float4 _Color; uniform float _Amount; uniform float _Seed; uniform float _Length; uniform float _Style; struct vertexInput { float4 vertex : POSITION; float4 texcoord : TEXCOORD0; }; struct vertexOutput { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; float4 screenPos : TEXCOORD5; }; vertexOutput vert(vertexInput input) { vertexOutput output; output.uv = input.texcoord; output.pos = mul(UNITY_MATRIX_MVP, input.vertex); output.screenPos = output.pos * 0.5 + 0.5; return output; } half4 frag(vertexOutput input) : COLOR { float2 coord = input.uv.xy; float random = frac(sin( dot(coord.x*floor(_Style)+coord.y ,_Seed*25 )) * 45678.54321); float4 color = tex2D(_MainTex, input.uv); clip (coord.x + random*_Length - (1+_Length)*_Amount); return color; } ENDCG } } Fallback "Diffuse" }
如果有任何想法歡迎提出。
No comments:
Post a Comment