[游戏开发][渲染]实现新手引导蒙板遮罩裁剪的几种方式

//扣圆
Shader "GameEffects/GuideCircleMaskClip"
{
    Properties
    {
		_Color("Color",Color) = (0,0,0,1)
        _Center("Center",Vector) = (0,0,0,0)
        _MaxRadius("_MaxRadius",Range(0,500)) = 100
		_MinRadius("_MinRadius",Range(0,700)) = 200
    }

    SubShader
    {
        Tags
        {
            "Queue"="Transparent"
            "IgnoreProjector"="True"
            "RenderType"="Transparent"
        }

        Pass
        {
			Lighting Off
			ZWrite Off
			AlphaTest Off
			Fog { Mode Off }
			Offset -1, -1
			Blend One OneMinusSrcAlpha

			CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            struct appdata_t
            {
                half4 vertex   : POSITION;
            };

            struct v2f
            {
				half4 vertex   : SV_POSITION;
				half4 screenPos : TEXCOORD0;
            };

			half4 _Color;
			half2 _Center;
			half _MaxRadius;
			half _MinRadius;

            v2f vert(appdata_t v)
            {
                v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.screenPos = ComputeScreenPos(o.vertex);
                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
				half2 screenPos = i.screenPos.xy / i.screenPos.w * _ScreenParams.xy;
				half screenDis = min(_MaxRadius,max(_MinRadius,distance(screenPos.xy, _Center.xy)));
				half percent = (screenDis - _MinRadius) / (_MaxRadius - _MinRadius);
				fixed4 color = _Color;
				color.a = lerp(0, _Color.a, percent);
                return color;
            }
			ENDCG
        }
    }
}

第二种方式:

实现原理:实际上就是先利用渲染队列渲染,然后再利用ZTest,改变渲染的遮挡关系。

PS:Depth Testing:深度测试,也叫深度缓冲。只有最靠近观察者的物体会被绘制。深度即Z,该值越小表示离观察者越近,该值越大表示离观察者越远。

具体操作步骤:

1、修改NGUI的Unlit - Transparent Colored Shader,将ZWrite改为On。

2、创建3个Panel,分别为叫GameObject、Mask、Cutout,里面分别创建一个Sprite,

3、将Panel的渲染方式改为Explicit,渲染队列分别改为3000(GameObject)、3002(Mask)、3001(Cutout),这一步会是渲染按照GameObject->Cutout->Mask的顺序渲染。

4、调整他们的Z轴:GameObject为300,Mask为200,Cutout为100。这一步会使渲染的遮挡关系发生改变,会使Cutout挡住Mask。我们再把Mask下面的Sprite透明度调为1。最后神奇的事情出现了!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,下面是一个简单的Android代码实现新手引导蒙板页的示例: 1. 首先,在布局文件中创建一个FrameLayout,并在其中添加需要展示的View,例如: ```xml <FrameLayout android:id="@+id/guide_container" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/image_view" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/image" /> </FrameLayout> ``` 2. 在Activity中,创建一个View用于展示蒙板,并在其上绘制需要展示的区域,例如: ```java public class GuideActivity extends AppCompatActivity { private View mMaskView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_guide); // 获取布局容器 FrameLayout container = findViewById(R.id.guide_container); // 创建蒙板View mMaskView = new View(this); mMaskView.setBackgroundColor(Color.parseColor("#80000000")); // 绘制圆形区域,需要展示的View是ImageView int radius = getResources().getDimensionPixelSize(R.dimen.guide_radius); int[] location = new int[2]; ImageView imageView = findViewById(R.id.image_view); imageView.getLocationOnScreen(location); int centerX = location[0] + imageView.getWidth() / 2; int centerY = location[1] + imageView.getHeight() / 2; Canvas canvas = new Canvas(); Bitmap bitmap = Bitmap.createBitmap(container.getWidth(), container.getHeight(), Bitmap.Config.ARGB_8888); canvas.setBitmap(bitmap); canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); canvas.drawCircle(centerX, centerY, radius, new Paint()); // 在蒙板View上绘制Bitmap BitmapDrawable drawable = new BitmapDrawable(getResources(), bitmap); mMaskView.setBackground(drawable); // 添加蒙板View到布局容器中 container.addView(mMaskView); } } ``` 3. 在关闭蒙板页时,移除蒙板View并销毁Activity,例如: ```java public void onCloseClick(View view) { // 移除蒙板View FrameLayout container = findViewById(R.id.guide_container); container.removeView(mMaskView); // 销毁Activity finish(); } ``` 以上就是一个简单的Android代码实现新手引导蒙板页的示例,你可以根据需要进行调整和完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Little丶Seven

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值