Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the hollowing-out effect of novice guide mask by UnityShader

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "UnityShader how to achieve novice guide mask hollowing effect", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "UnityShader how to achieve novice guide mask hollowing effect" this article.

1. Circular hollowed-out shader code:

/ / calculate the distance between the world coordinates of the chip element and the center position of the target float dis = distance (IN.worldPosition.xy, _ Center.xy); / / filter out the chip element clip (dis-(_ Radius-_ TransitionRange)) whose distance is less than (radius-transition range); / / optimize the if condition judgment, and perform the next step if the distance is less than the radius, which is equal to if (dis < _ Radius) fixed tmp = step (dis, _ Radius) / / calculate the alpha value color.a * = (1-tmp) + tmp * (dis-(_ Radius-_ TransitionRange)) / _ TransitionRange within the transition range

Effect: ignore the gradient mask, randomly find the picture

2. Ellipse hollowed-out shader code:

/ / calculate the X-axis distance float disX = distance (IN.worldPosition.x, _ Center.x); / / calculate the Y-axis distance float disY = distance (IN.worldPosition.y, _ Center.y); / / use the elliptic equation to calculate the alpha value of the element, and _ Ellipse is the elliptical coefficient fixed factor = clamp (abs (disX / _ Width), _ Ellipse) + pow (abs (disY / _ Height), _ Ellipse), 0.0,1.0) / / optimize the if condition to judge fixed tmp = step (factor, 1.0f); / / assign the alpha value outside or within the ellipse color.a * = (1-tmp) + tmp * factor

Effect:

3. Circle target location aggregation animation shader code:

/ / _ StartTime is the starting time of the effect, and the corresponding assignment material.SetFloat ("_ StartTime", Time.timeSinceLevelLoad) in Unity; fixed processTime = _ Time.y-_ StartTime;// to determine whether the shader execution time exceeds _ TotalTimeclip (_ TotalTime-processTime); / / optimize the if condition to judge fixed tmp = step (processTime, _ ReduceTime) / / calculate the circular hollowing radius at the current point in time float curRadius = (1-tmp) * _ Radius + tmp * (_ MaxRadius-(_ MaxRadius-_ Radius) * processTime / _ ReduceTime); float dis = distance (IN.worldPosition.xy, _ Center.xy); / / discard the element clip (dis-curRadius) whose distance is less than the current circular hollowing radius

Effect: I don't know why it's uploaded, but there's no problem with normal.

The entire shader source code is added on the basis of Unity UGUI's own 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) StencilComp ("Stencil Comparison", Float) = 8 _ Stencil ("Stencil ID", Float) = 0 _ StencilOp ("Stencil Operation", Float) = 0 _ StencilWriteMask ("StencilWriteMask", Float) = 255 _ StencilReadMask ("StencilReadMask", Float) = 255 _ ColorMask ("ColorMask") Float) = 15 [Toggle (UNITY_UI_ALPHACLIP)] _ UseUIAlphaClip ("Use AlphaClip", Float) = 0 / /-add-- _ Center ("Center", vector) = (0,0,0,0) _ Radius ("Radius", Range (0Magne1000)) = 1000 / / sliders _ TransitionRange ("TransitionRange", Range (0) 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 / / calculate the distance between the world coordinate of the chip element and the center position of the target float dis = distance (IN.worldPosition.xy, _ Center.xy); / / filter out the chip element clip whose distance is less than (radius-transition range) (dis-(_ Radius-_ TransitionRange)) / / optimize the if condition judgment, and perform the next step if the distance is less than the radius, which is equal to if (dis < _ Radius) fixed tmp = step (dis, _ Radius); / / calculate the alpha value color.a * = (1-tmp) + tmp * (dis-(_ Radius-_ TransitionRange)) / _ TransitionRange;#elif _ ROUNDMODE_ELLIPSE / / calculate the distance in the X axis float disX = distance (IN.worldPosition.x, _ Center.x) / / calculate the Y-axis distance float disY = distance (IN.worldPosition.y, _ Center.y); / / use the elliptic equation to calculate the alpha value of the chip element, and _ Ellipse is the elliptical coefficient fixed factor = clamp (pow (disX / _ Width), _ Ellipse) + pow (abs (disY / _ Height), _ Ellipse), 0.0,1.0); / / optimize the if condition to judge fixed tmp = step (factor, 1.0f) / / the alpha value outside or within the assigned ellipse color.a * = (1-tmp) + tmp * factor;#else / / _ StartTime is the starting time of the effect, and the corresponding assigned material.SetFloat in Unity ("_ StartTime", Time.timeSinceLevelLoad); fixed processTime = _ Time.y-_ StartTime; / / determine whether the shader execution time exceeds _ TotalTime clip (_ TotalTime-processTime); / / optimize the if condition to judge fixed tmp = step (processTime, _ ReduceTime) / / calculate the circular hollowing radius at the current point in time float curRadius = (1-tmp) * _ Radius + tmp * (_ MaxRadius-(_ MaxRadius-_ Radius) * processTime / _ ReduceTime); float dis = distance (IN.worldPosition.xy, _ Center.xy); / / discard the element clip (dis-curRadius) whose distance is less than the current circular hollowing radius # endif / /-add-- return color;} ENDCG}

These are all the contents of the article "how to achieve novice guide mask hollowing out effect in UnityShader". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report