Unity UI擦除效果

2024-04-24 01:04
文章标签 ui 效果 unity 擦除

本文主要是介绍Unity UI擦除效果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

public class ScratchImage : MonoBehaviour{/// <summary>/// 蒙版贴图/// </summary>public Image maskImage;public Material maskMaterial;private Camera uiCamera;private Vector2 _maskSize;private Texture2D _rt;private Color[] spritePixels;public int brushRadius = 50;private float percent = 0;public float finishPercent = 0.95f;private int clearNum = 0;private int[] dirtyPoints;private Vector2 SCALE_FACTOR;private Vector2Int RTSize;bool bClear = false;private void Reset(){maskImage = GetComponent<Image>();maskMaterial = maskImage.material;}public void SetParams(int brushRadius,float fpercent){this.brushRadius = brushRadius;this.finishPercent = fpercent;}private void Init(){if (uiCamera == null){uiCamera = UIHelper.Instance.UICamera;}SCALE_FACTOR = maskImage.sprite.textureRect.size / maskImage.rectTransform.rect.size;_maskSize = maskImage.rectTransform.rect.size;//Debug.LogFormat("mask image size:{0}*{1}", maskSize.x, maskSize.y);RTSize = new Vector2Int((int)maskImage.sprite.textureRect.size.x, (int)maskImage.sprite.textureRect.size.y);spritePixels = maskImage.sprite.texture.GetPixels(0, 0, RTSize.x, RTSize.y);_rt = new Texture2D(RTSize.x, RTSize.y);_rt.SetPixels(spritePixels);_rt.Apply();maskMaterial.SetTexture("_TempTex", _rt);dirtyPoints = new int[RTSize.x * RTSize.y];bClear = false;percent = 0;clearNum = 0;}public void Update(){onUpdate();}void Fill(){bClear = true;for (int i = 0; i < RTSize.x; i++){for (int j = 0; j < RTSize.y; j++){if(dirtyPoints[i*RTSize.y+j]==0){_rt.SetPixel(i, j, new Color(1, 1, 1, 0));}}}_rt.Apply();}public void ResetMask(){_rt.SetPixels(spritePixels);_rt.Apply();for (int i = 0; i < dirtyPoints.Length; i++){dirtyPoints[i] = 0;}clearNum = 0;bClear = false;}private void onUpdate(){if (bClear) return;if (uiCamera == null)return;if (!Input.GetMouseButton(0)) // 移动鼠标或者处于按下状态{return;}Vector2 localPt = Vector2.zero;RectTransformUtility.ScreenPointToLocalPointInRectangle(transform as RectTransform, Input.mousePosition, uiCamera, out localPt);//Debug.Log($"pt:{localPt}, status:{mouseStatus}");if (localPt.x < 0 || localPt.y < 0 || localPt.y >= _maskSize.x || localPt.y >= _maskSize.y)return;localPt = localPt * SCALE_FACTOR;Vector2Int usePixel = new Vector2Int((int)(localPt.x), (int)localPt.y);int left = (int)(usePixel.x - brushRadius);if (left < 0) left = 0;int right = (int)(usePixel.x + brushRadius);if (right > RTSize.x) right = (int)RTSize.x;int down = (int)(usePixel.y - brushRadius);if (down < 0) down = 0;int top = (int)(usePixel.y + brushRadius);if (top > RTSize.y) top = (int)RTSize.y;bool dirty = false;for (int i = left; i < right; i++){for (int j = down; j < top; j++){int index = (int)(i * RTSize.y + j);if (dirtyPoints[index] == 1){}else{Vector2 p = new Vector2(i, j);float dis = Vector2.Distance(usePixel, p);if (dis <= brushRadius){_rt.SetPixel(i, j, new Color(1, 1, 1, 0));dirtyPoints[index] = 1;++clearNum;dirty = true;}}}}if (dirty){percent = (float)clearNum / dirtyPoints.Length;if (percent > finishPercent){Fill();GameHelp.FireEvent((ushort)DGlobalEvent.EVENT_SMALL_GAME_RESULT_OK, 0, 0, null);}else{_rt.Apply();}}}}

shader

Shader "Unlit/PaintMask"
{Properties{_MainTex ("MainTexture", 2D) = "white" {}_TempTex ("TempTexture",2D) = "white" {}}SubShader{Tags { "Queue"="Transparent""RenderType"="Transparent"}LOD 100ZWrite OffZTest OffBlend SrcAlpha OneMinusSrcAlpha Pass{CGPROGRAM#pragma vertex vert#pragma fragment frag// make fog work#pragma multi_compile_fog#include "UnityCG.cginc"struct appdata{float4 vertex : POSITION;float2 uv : TEXCOORD0;};struct v2f{float2 uv : TEXCOORD0;UNITY_FOG_COORDS(1)float4 vertex : SV_POSITION;};sampler2D _MainTex;float4 _MainTex_ST;sampler2D _TempTex;float4 _TempTex_ST;v2f vert (appdata v){v2f o;o.vertex = UnityObjectToClipPos(v.vertex);o.uv = TRANSFORM_TEX(v.uv, _MainTex);UNITY_TRANSFER_FOG(o,o.vertex);return o;}fixed4 frag (v2f i) : SV_Target{// sample the texturefixed4 col = tex2D(_MainTex, i.uv);half alpha = tex2D(_TempTex,i.uv).a;col.a = alpha;// apply fogUNITY_APPLY_FOG(i.fogCoord, col);return col;}ENDCG}}
}

最粗暴的方法,原理就是跟着鼠标的位置,以鼠标为圆心把圆内的像素点清除,遮罩变成透明了,里面的图片就显示出来了

这篇关于Unity UI擦除效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/930373

相关文章

VS配置好Qt环境之后但无法打开ui界面的问题解决

《VS配置好Qt环境之后但无法打开ui界面的问题解决》本文主要介绍了VS配置好Qt环境之后但无法打开ui界面的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目UKeLvb录找到Qt安装目录中designer.UKeLvBexe的路径找到vs中的解决方案资源

Kotlin Compose Button 实现长按监听并实现动画效果(完整代码)

《KotlinComposeButton实现长按监听并实现动画效果(完整代码)》想要实现长按按钮开始录音,松开发送的功能,因此为了实现这些功能就需要自己写一个Button来解决问题,下面小编给大... 目录Button 实现原理1. Surface 的作用(关键)2. InteractionSource3.

使用WPF实现窗口抖动动画效果

《使用WPF实现窗口抖动动画效果》在用户界面设计中,适当的动画反馈可以提升用户体验,尤其是在错误提示、操作失败等场景下,窗口抖动作为一种常见且直观的视觉反馈方式,常用于提醒用户注意当前状态,本文将详细... 目录前言实现思路概述核心代码实现1、 获取目标窗口2、初始化基础位置值3、创建抖动动画4、动画完成后

uniapp小程序中实现无缝衔接滚动效果代码示例

《uniapp小程序中实现无缝衔接滚动效果代码示例》:本文主要介绍uniapp小程序中实现无缝衔接滚动效果的相关资料,该方法可以实现滚动内容中字的不同的颜色更改,并且可以根据需要进行艺术化更改和自... 组件滚动通知只能实现简单的滚动效果,不能实现滚动内容中的字进行不同颜色的更改,下面实现一个无缝衔接的滚动

QT6中绘制UI的两种方法详解与示例代码

《QT6中绘制UI的两种方法详解与示例代码》Qt6提供了两种主要的UI绘制技术:​​QML(QtMeta-ObjectLanguage)​​和​​C++Widgets​​,这两种技术各有优势,适用于不... 目录一、QML 技术详解1.1 QML 简介1.2 QML 的核心概念1.3 QML 示例:简单按钮

Java实现图片淡入淡出效果

《Java实现图片淡入淡出效果》在现代图形用户界面和游戏开发中,**图片淡入淡出(FadeIn/Out)**是一种常见且实用的视觉过渡效果,它可以用于启动画面、场景切换、轮播图、提示框弹出等场景,通过... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细

使用animation.css库快速实现CSS3旋转动画效果

《使用animation.css库快速实现CSS3旋转动画效果》随着Web技术的不断发展,动画效果已经成为了网页设计中不可或缺的一部分,本文将深入探讨animation.css的工作原理,如何使用以及... 目录1. css3动画技术简介2. animation.css库介绍2.1 animation.cs

在 PyQt 加载 UI 三种常见方法

《在PyQt加载UI三种常见方法》在PyQt中,加载UI文件通常指的是使用QtDesigner设计的.ui文件,并将其转换为Python代码,以便在PyQt应用程序中使用,这篇文章给大家介绍在... 目录方法一:使用 uic 模块动态加载 (不推荐用于大型项目)方法二:将 UI 文件编译为 python 模

Flutter实现文字镂空效果的详细步骤

《Flutter实现文字镂空效果的详细步骤》:本文主要介绍如何使用Flutter实现文字镂空效果,包括创建基础应用结构、实现自定义绘制器、构建UI界面以及实现颜色选择按钮等步骤,并详细解析了混合模... 目录引言实现原理开始实现步骤1:创建基础应用结构步骤2:创建主屏幕步骤3:实现自定义绘制器步骤4:构建U

Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)

《Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)》文章介绍了如何使用dhtmlx-gantt组件来实现公司的甘特图需求,并提供了一个简单的Vue组件示例,文章还分享了一... 目录一、首先 npm 安装插件二、创建一个vue组件三、业务页面内 引用自定义组件:四、dhtmlx