This is a reference to do this:
https://blog.csdn.net/tracyzly/article/details/80279692
Shader "UI/ImageWithHole" { Properties { [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {} _TintColor ("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 [KeywordEnum(ROUND, RECTANGLE, NULL)] _MaskMode("Mask mode", Float) = 0 _Center("Center", vector) = (0, 0, 0, 0) _Radius("Radius", Range(0,1000)) = 100 // 圆半径 _RectangleSize("Rectangle Size", vector) = (0, 0, 0, 0) // 矩形边长 _TransitionRange("Transition Range", Range(0, 100)) = 10 } 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 Blend SrcAlpha OneMinusSrcAlpha ColorMask RGBA 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 _MASKMODE_ROUND _MASKMODE_RECTANGLE _MASKMODE_NULL 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 _TintColor; fixed4 _TextureSampleAdd; float4 _ClipRect; float2 _Center; half _Radius; float2 _RectangleSize; half _TransitionRange; 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 * _TintColor; return OUT; } _MainTex sampler2D; the frag fixed4 (V2f I): SV_Target { half4 Color = (tex2D (_MainTex, i.texcoord) + _TextureSampleAdd) * i.color; #ifdef UNITY_UI_CLIP_RECT color.a * = UnityGet2DClipping (IN.worldPosition.xy, _ClipRect); #endif UNITY_UI_ALPHACLIP #ifdef Clip (color.a - from 0.001 ); #endif #ifdef _MASKMODE_ROUND // calculated from the sheet element and the world coordinates of the center position of the target Half DIS =Distance (i.worldPosition.xy, _Center.xy); // filter out smaller than the distance (radius - the transition range) sheet element Clip (DIS - (_radius - _TransitionRange)); // if the circular inside int Inside = STEP ( DIS, _radius); // calculate the alpha value in the transition range color.a * = ( . 1 - inside) inside * + (DIS - (_radius - _TransitionRange)) / _TransitionRange; #elif _MASKMODE_RECTANGLE // calculated world coordinates and fragments from the center of the target position Half disX = distance (i.worldPosition.x, _Center.x); Half disY =Distance (i.worldPosition.y, _Center.y); // X decides to return a pixel should be removed, without removing returns 0 int ClipX = STEP (disX, _RectangleSize.x- _TransitionRange); int ClipY = STEP (disY, _RectangleSize .y- _TransitionRange); Clip (disX - (_RectangleSize.x -_TransitionRange) * ClipY); Clip (disY - (_RectangleSize.y -_TransitionRange) * ClipX); // X in the range of 1 to return, within a range not return 0 int insideX = STEP (disX, _RectangleSize.x); int insideY = STEP (disY, _RectangleSize.y); Half alphax = ( . 1 - insideX) + insideX * (disX - (_RectangleSize.x - _TransitionRange)) / _TransitionRange; half alphaY= (1 - insideY) + insideY * (disY - (_RectangleSize.y - _TransitionRange)) / _TransitionRange; color.a *= max(alphaX, alphaY); #endif return color; } ENDCG } } }