您好,登錄后才能下訂單哦!
這兩天實現了下新手引導需要的遮罩鏤空shader效果,記錄一下。
1、圓形鏤空shader代碼:
//計算片元世界坐標和目標中心位置的距離 float dis = distance(IN.worldPosition.xy, _Center.xy); //過濾掉距離小于(半徑-過渡范圍)的片元 clip(dis - (_Radius - _TransitionRange)); //優化if條件判斷,如果距離小于半徑則執行下一步,等于if(dis < _Radius) fixed tmp = step(dis, _Radius); //計算過渡范圍內的alpha值 color.a *= (1 - tmp) + tmp * (dis - (_Radius - _TransitionRange)) / _TransitionRange;
效果:忽略漸變的蒙版,隨便找的圖片
2、橢圓鏤空shader代碼:
//計算X軸方向距離 float disX = distance(IN.worldPosition.x, _Center.x); //計算Y軸方向距離 float disY = distance(IN.worldPosition.y, _Center.y); //運用橢圓方程計算片元的alpha值,_Ellipse為橢圓系數 fixed factor = clamp(pow(abs(disX / _Width), _Ellipse) + pow(abs(disY / _Height), _Ellipse), 0.0, 1.0); //優化if條件判斷 fixed tmp = step(factor, 1.0f); //賦值橢圓外或橢圓內的alpha值 color.a *= (1 - tmp) + tmp * factor;
效果:
3、圓形目標位置聚合動畫shader代碼:
//_StartTime為效果開始時間點,Unity中對應賦值material.SetFloat("_StartTime", Time.timeSinceLevelLoad); fixed processTime = _Time.y - _StartTime; //判斷shader執行時長是否超過_TotalTime clip(_TotalTime - processTime); //優化if條件判斷 fixed tmp = step(processTime, _ReduceTime); //計算當前時間點的圓形鏤空半徑 float curRadius = (1 - tmp) * _Radius + tmp * (_MaxRadius - (_MaxRadius - _Radius) * processTime / _ReduceTime); float dis = distance(IN.worldPosition.xy, _Center.xy); //拋棄距離小于當前圓形鏤空半徑的片元 clip(dis - curRadius);
效果:不知道為啥上傳上來就有問題了,正常是沒問題的
整個shader源碼,在Unity UGUI 自帶Default shader基礎上添加:
// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) Shader "UI/Default_Mask" { Properties { [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {} _Color ("Tint", Color) = (1,1,1,1) _StencilComp ("Stencil Comparison", Float) = 8 _Stencil ("Stencil ID", Float) = 0 _StencilOp ("Stencil Operation", Float) = 0 _StencilWriteMask ("Stencil Write Mask", Float) = 255 _StencilReadMask ("Stencil Read Mask", Float) = 255 _ColorMask ("Color Mask", Float) = 15 [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0 //-------------------add---------------------- _Center("Center", vector) = (0, 0, 0, 0) _Radius("Radius", Range(0,1000)) = 1000 // sliders _TransitionRange("Transition Range", Range(0, 100)) = 10 _Width("Width", Float) = 1 _Height("Height", Float) = 1 _Ellipse("Ellipse", Float) = 4 _ReduceTime("ReduceTime", Float) = 1 _TotalTime("TotalTime", Float) = 1 _StartTime("StartTime", Float) = 0 _MaxRadius("MaxRadius", Float) = 1500 [KeywordEnum(ROUND, ELLIPSE, DYNAMIC_ROUND)] _RoundMode("Mask mode", Float) = 0 //-------------------add---------------------- } SubShader { Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "PreviewType"="Plane" "CanUseSpriteAtlas"="True" } Stencil { Ref [_Stencil] Comp [_StencilComp] Pass [_StencilOp] ReadMask [_StencilReadMask] WriteMask [_StencilWriteMask] } Cull Off Lighting Off ZWrite Off ZTest [unity_GUIZTestMode] Blend SrcAlpha OneMinusSrcAlpha ColorMask [_ColorMask] Pass { Name "Default" CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma target 2.0 #include "UnityCG.cginc" #include "UnityUI.cginc" #pragma multi_compile __ UNITY_UI_CLIP_RECT #pragma multi_compile __ UNITY_UI_ALPHACLIP #pragma multi_compile _ROUNDMODE_ROUND _ROUNDMODE_ELLIPSE _ROUNDMODE_DYNAMIC_ROUND struct appdata_t { float4 vertex : POSITION; float4 color : COLOR; float2 texcoord : TEXCOORD0; UNITY_VERTEX_INPUT_INSTANCE_ID }; struct v2f { float4 vertex : SV_POSITION; fixed4 color : COLOR; float2 texcoord : TEXCOORD0; float4 worldPosition : TEXCOORD1; UNITY_VERTEX_OUTPUT_STEREO }; fixed4 _Color; fixed4 _TextureSampleAdd; float4 _ClipRect; //-------------------add---------------------- half _Radius; float2 _Center; half _TransitionRange; half _Width; half _Height; half _Ellipse; fixed _ReduceTime; half _TotalTime; float _StartTime; half _MaxRadius; //-------------------add---------------------- v2f vert(appdata_t v) { v2f OUT; UNITY_SETUP_INSTANCE_ID(v); UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT); OUT.worldPosition = v.vertex; OUT.vertex = UnityObjectToClipPos(OUT.worldPosition); OUT.texcoord = v.texcoord; OUT.color = v.color * _Color; return OUT; } sampler2D _MainTex; fixed4 frag(v2f IN) : SV_Target { half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color; #ifdef UNITY_UI_CLIP_RECT color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect); #endif #ifdef UNITY_UI_ALPHACLIP clip (color.a - 0.001); #endif //-------------------add---------------------- #ifdef _ROUNDMODE_ROUND //計算片元世界坐標和目標中心位置的距離 float dis = distance(IN.worldPosition.xy, _Center.xy); //過濾掉距離小于(半徑-過渡范圍)的片元 clip(dis - (_Radius - _TransitionRange)); //優化if條件判斷,如果距離小于半徑則執行下一步,等于if(dis < _Radius) fixed tmp = step(dis, _Radius); //計算過渡范圍內的alpha值 color.a *= (1 - tmp) + tmp * (dis - (_Radius - _TransitionRange)) / _TransitionRange; #elif _ROUNDMODE_ELLIPSE //計算X軸方向距離 float disX = distance(IN.worldPosition.x, _Center.x); //計算Y軸方向距離 float disY = distance(IN.worldPosition.y, _Center.y); //運用橢圓方程計算片元的alpha值,_Ellipse為橢圓系數 fixed factor = clamp(pow(abs(disX / _Width), _Ellipse) + pow(abs(disY / _Height), _Ellipse), 0.0, 1.0); //優化if條件判斷 fixed tmp = step(factor, 1.0f); //賦值橢圓外或橢圓內的alpha值 color.a *= (1 - tmp) + tmp * factor; #else //_StartTime為效果開始時間點,Unity中對應賦值material.SetFloat("_StartTime", Time.timeSinceLevelLoad); fixed processTime = _Time.y - _StartTime; //判斷shader執行時長是否超過_TotalTime clip(_TotalTime - processTime); //優化if條件判斷 fixed tmp = step(processTime, _ReduceTime); //計算當前時間點的圓形鏤空半徑 float curRadius = (1 - tmp) * _Radius + tmp * (_MaxRadius - (_MaxRadius - _Radius) * processTime / _ReduceTime); float dis = distance(IN.worldPosition.xy, _Center.xy); //拋棄距離小于當前圓形鏤空半徑的片元 clip(dis - curRadius); #endif //-------------------add---------------------- return color; } ENDCG } } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。