Unity画线之GL

article/2025/9/15 14:27:12

上一篇中,SetPixel的方法,卡顿严重,暂未解决,又去看了原来的GL画线,自己画图思考了一下适配UI的问题,最终解决。

特此说明,GL画线功能,及Shader均为借鉴,自己做了优化。

程序代码如下:

(MouseOverController.GetOverUI(UIManager.GetInstance().GetCanvas().gameObject) == null   这句为UI判断)

/************************************************************Copyright (C), 2007-2016,BJ Rainier Tech. Co., Ltd.FileName: Painting.csAuthor:末零       Version :1.0          Date: 2018-11-10Description:画图功能
************************************************************/using UnityEngine;
using UnityEngine.UI;namespace LastZero.Utility
{public class Painting : MonoBehaviour{public Color mColor = Color.black;//画笔颜色[HideInInspector]public Texture brushTexture;//画笔[HideInInspector]public float brushScale = 0.1f;//画笔大小[HideInInspector]public bool isEraser = false;//是否使用橡皮擦private RenderTexture texRender;//接收图的RenderTextureprivate Material mat;//材质球private RawImage rImage;//RawImage,自身private RectTransform rTransform;//RectTransformprivate Vector3 startPosition = Vector3.zero;private Vector3 endPosition = Vector3.zero;private Vector2 posOffset;private void Start(){SetPainting();posOffset.Set((Screen.width - rTransform.sizeDelta.x * transform.root.localScale.x) / 2 + transform.position.x - transform.root.position.x, (Screen.height - rTransform.sizeDelta.y * transform.root.localScale.y) / 2 + transform.position.y - transform.root.position.y);}void Update(){if (MouseOverController.GetOverUI(transform.root.gameObject) == null){EndPainting();return;}else if (MouseOverController.GetOverUI(transform.root.gameObject).name != gameObject.name){EndPainting();return;}if (Input.GetMouseButton(0)){StartPainting(new Vector3(Input.mousePosition.x - posOffset.x, Input.mousePosition.y - posOffset.y, 0));}if (Input.GetMouseButtonUp(0)){EndPainting();}}/// <summary>/// 初始化设置/// </summary>public void SetPainting(){rTransform = GetComponent<RectTransform>();mat = new Material(Shader.Find("Custom/Painting"));texRender = new RenderTexture((int)rTransform.sizeDelta.x, (int)rTransform.sizeDelta.y, 24, RenderTextureFormat.ARGB32);Clear(texRender);rImage = GetComponent<RawImage>();Debug.Log(rImage);rImage.texture = texRender;}/// <summary>/// 开始画线/// </summary>/// <param name="pos"></param>public void StartPainting(Vector3 pos){endPosition = pos;if (startPosition.Equals(Vector3.zero)){startPosition = endPosition;return;}float distance = Vector3.Distance(startPosition, endPosition);if (distance > 1){int d = (int)distance;for (int i = 0; i < d; i++){float difx = endPosition.x - startPosition.x;float dify = endPosition.y - startPosition.y;float delta = (float)i / distance;if (isEraser){DrawBrush(texRender, new Vector2(startPosition.x + (difx * delta), startPosition.y + (dify * delta)), brushTexture, Color.white, brushScale);}else{DrawBrush(texRender, new Vector2(startPosition.x + (difx * delta), startPosition.y + (dify * delta)), brushTexture, mColor, brushScale);}}}startPosition = endPosition;}/// <summary>/// 结束画线/// </summary>public void EndPainting(){startPosition = Vector3.zero;}private void DrawBrush(RenderTexture destTexture, Vector2 pos, Texture sourceTexture, Color color, float scale){DrawBrush(destTexture, (int)pos.x, (int)pos.y, sourceTexture, color, scale);}private void DrawBrush(RenderTexture destTexture, int x, int y, Texture sourceTexture, Color color, float scale){DrawBrush(destTexture, new Rect(x, y, sourceTexture.width, sourceTexture.height), sourceTexture, color, scale);}private void DrawBrush(RenderTexture destTexture, Rect destRect, Texture sourceTexture, Color color, float scale){Graphics.SetRenderTarget(destTexture);float left = destRect.xMin - destRect.width * scale / 2.0f;float right = destRect.xMin + destRect.width * scale / 2.0f;float top = destRect.yMin - destRect.height * scale / 2.0f;float bottom = destRect.yMin + destRect.height * scale / 2.0f;GL.PushMatrix();GL.LoadOrtho();mat.SetTexture("_MainTex", brushTexture);mat.SetColor("_Color", color);mat.SetPass(0);GL.Begin(GL.QUADS);GL.TexCoord2(0.0f, 0.0f); GL.Vertex3(left / (int)rTransform.sizeDelta.x / transform.root.localScale.x, top / (int)rTransform.sizeDelta.y / transform.root.localScale.y, 0);GL.TexCoord2(1.0f, 0.0f); GL.Vertex3(right / (int)rTransform.sizeDelta.x / transform.root.localScale.x, top / (int)rTransform.sizeDelta.y / transform.root.localScale.y, 0);GL.TexCoord2(1.0f, 1.0f); GL.Vertex3(right / (int)rTransform.sizeDelta.x / transform.root.localScale.x, bottom / (int)rTransform.sizeDelta.y / transform.root.localScale.y, 0);GL.TexCoord2(0.0f, 1.0f); GL.Vertex3(left / (int)rTransform.sizeDelta.x / transform.root.localScale.x, bottom / (int)rTransform.sizeDelta.y / transform.root.localScale.y, 0);GL.End();GL.PopMatrix();}/// <summary>/// 重置RenderTexture/// </summary>/// <param name="destTexture"></param>private void Clear(RenderTexture destTexture){Graphics.SetRenderTarget(destTexture);GL.PushMatrix();GL.Clear(true, true, Color.white);GL.PopMatrix();}/// <summary>/// 外部可以调用的重置修改的RenderTexture/// </summary>public void ClearAll(){Clear(texRender);}}
}

Shader:

Shader "Custom/Painting"
{Properties{_MainTex("MainTex (RGB) Trans (A)", 2D) = "white" {}_Color("Color", Color) = (1,1,1,1)}SubShader{Tags{"Queue" = "Transparent""IgnoreProjector" = "True""RenderType" = "Transparent""PreviewType" = "Plane""CanUseSpriteAtlas" = "True"}Cull OffLighting OffZWrite OffFog{ Mode Off }Blend One OneMinusSrcAlphaPass{CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"struct v2f{float4 vertex : SV_POSITION;half2 texcoord : TEXCOORD0;};fixed4 _Color;v2f vert(appdata_base IN){v2f OUT;OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);OUT.texcoord = IN.texcoord;return OUT;}sampler2D _MainTex;fixed4 frag(v2f IN) : SV_Target{float4 col = _Color * tex2D(_MainTex, IN.texcoord);col.rgb *= col.a;return col;}ENDCG}}
}

效果图:

需要Demo可移步下载:

Demo下载


http://chatgpt.dhexx.cn/article/cnoK0Hvh.shtml

相关文章

GL823K

下面是另一家SD/TF解码芯片的方案 ![](https://img-blog.csdnimg.cn/20210319145313645.png?x-oss-processimage/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80OTU3MDgwNA,size_16,color_FFFFFF,t_70 13030533945 VX

GL各个表结构总结

NewProgramer EBS GL表结构学习(转) gl_code_combinations&#xff1a;科目组合 字段名 含义 备注 code_combination_id 主键&#xff0c;科目编码ID&#xff0c;自动编号 segment1 分行代码 setgment2 是受益部门 segment3 科目代码 segment4 产品…

初识mapbox GL

一、概述 最近由于项目的需求&#xff0c;借此机会对mapbox GL做了一个系统的学习&#xff0c;同时也对整个学习过程做一个记录&#xff0c;一方面留作自用&#xff0c;另一方面也希望看到此文的人在学习mapbox GL的时候&#xff0c;能够有所启发、有所收获。 二、快速认识 …

支持Genero BDL 4gl语言的编辑器

内测版本出来啦。点此下载: FglDeveloper v1.0 →此版本已下架 还有bug,分享几张截图 模板产生器&#xff1a; 画面产生器&#xff1a; 编辑器各种变量提醒 详细功能小伙伴们下载后体验。

TOPGP5.3:导入jar包并在4GL中引用

查看环境$CLASSPATH 上传引用到的JAR包到以下目录 /u1/topprod/tiptop/ds4gl2/bin/javaad/jar 以上为GP5.3目录&#xff0c;其他版本系统可根据查看到的$CLASSPATH上传到相应目录设置环境变量 GP5.3系统中&#xff0c;$CLASSPATH环境变量的设置存在下图文件中&#xff1a; …

4gl调用WEB API,实现JSON传递(Demo)

测试环境: GP5.25 , fjs版本2.32,解析json所需要的jar依赖包 (PS: 如果没有记错是fjs2.32版本及以上才支持java bridge,所以GP 5.25以下的同学就不要用这种方式去测试) 测试内容: 利用此fjs版本对java bridge的支持,实现4gl调用WEB API,实现json传递 测试步骤如下: 1.下载本…

给大家展示一下4gl编辑器

&#xff08;正式版已发布点击下载&#xff09;特地为编辑器开发内置语法解析器&#xff0c;将在代码编辑过程中实时提示代码错误&#xff0c;并且错误提示都是中文显示(楼主英文太垃圾只有做中文了),不再需要频繁上传服务器了哦&#xff0c; 经过楼主努力已经把所有的内置函数…

【实习之T100开发】Genero FGL (TIPTOP4GL) 学习笔记(1)

Genero FGL 学习 Genero FGL 简介Genero FGL 开发&#xff08;编译、连接、执行&#xff09;第一个程序 Hello World变量与运算符变量定义&#xff08;DEFINE&#xff09;预定义变量变量集合&#xff08;RECORD &#xff09;数据结构&#xff08;TYPE&#xff09;变量赋值&…

win10忘记密码重置密码,一行代码帮你解决

步骤如下&#xff1a; 1.右击windows图标&#xff0c;选择进入Windows PowerShell(管理员&#xff09; 2.敲入代码net user 加上你的用户名和新密码&#xff0c;Ok&#xff0c;问题解决&#xff0c;你就可以用你的新密码登陆了

win10 重置登录账户密码

在登陆界面按下强制关机&#xff08;重复3次&#xff09;。注意&#xff1a;不能进到登陆界面 出现下图时&#xff0c;选择“高级选项”&#xff1a; 选择“疑难解答”&#xff1a; 选择“高级选项”&#xff1a; 选择“命令提示符”&#xff1a; 在管理员窗口输入diskpart 回车…

服务器2008系统设置密码,win2008服务器设置密码

win2008服务器设置密码 内容精选 换一换 修改服务IP地址&#xff0c;并且将DNS地址指向本机&#xff0c;然后修改计算机名为server。安装AD域服务之后&#xff0c;机器名称会自动变成“主机名域名”的形式&#xff0c;例如server.huawei.com。在命令行下输入dcpromo.exe &#…

Win10系统修改开机密码

愚蠢的人才能进步&#xff0c;对于我这种记忆力差的人&#xff0c;密码这个东东随时忘&#xff0c;今天早上大无语事件&#xff0c;我自己都惊呆了&#xff0c;每天都用的办公电脑&#xff0c;今天死活想不起密码&#xff0c;死活打不开电脑&#xff0c;明明就是那些组合&#…

win10忘记密码_电脑忘记密码没关系,这招教你简单轻松改密码

相信"忘记电脑开机密码"这件事经常发生,忘记密码该怎么办呢?这个问题困扰了许多小伙伴,今天就教大家一种最简单的方法轻松重置电脑开机密码(本方法适用win10、win8、win7系统)。 划重点: 重置电脑开机密码一共分为三个步骤「 制作PE系统」「进入PE系统 」「重置密…

win10计算机默认用户名和密码是什么,win10共享的文件夹需要密码和用户名登陆...

一、共享文件夹所在电脑设置 1、右键我的电脑→管理→系统工具→本地用户和组→用户→中间空白区域右键→新用户&#xff1b; 2、输入自设的用户名和密码&#xff0c;如图勾选→创建&#xff1b; 3、右键需要共享的文件见→安全→编辑&#xff1b; 4、点击添加&#xff1b; 5、…

WIN10取消密码和休眠密码

安装Win10系统之后&#xff0c;发现每次开机都会出现登录密码&#xff1f;有些用户觉得很麻烦&#xff0c;所以想要取消Win10开机密码。那么&#xff0c;该如何操作呢&#xff1f; 按下winx组合键&#xff0c;启动快捷菜单 在弹出菜单选择运行&#xff0c;如下图所示 在运行框…

计算机共享网络的账号密码怎么设置密码,怎样才能给win10共享设置密码的操作方法...

怎样才能给win10共享设置密码呢&#xff1f;一些善于使用win10共享设置的小伙伴们&#xff0c;一定会为了隐私而设置win10的共享设置密码&#xff0c;为了可以完美的设置共享密码&#xff0c;小伙伴们找了好多方法&#xff0c;都没有解决&#xff0c;针对这一问题&#xff0c;小…

win10系统mysql重新配置密码

前言 最近捣鼓自己的破烂笔记本&#xff0c;想着写点玩具项目&#xff0c;想着&#xff1a;写项目不能没有mysql吧&#xff0c;点击mysqlbench开始上号。这个时候发现坏了&#xff0c;登录不上&#xff0c;想着估计mysql没装好呗&#xff0c;于是重装了mysql和mysqlbench&…

win10重置网络命令_Win10怎么重置网络 Win10重置网络命令使用方法

有时候Windows系统出问题的时候&#xff0c;会导致网络异常&#xff0c;无法正常上网&#xff0c;甚至是重新连接网络&#xff0c;依然会出现无法连接的现象。下面就来为大家分享2种重置网络的方法&#xff0c;有需要了解的小伙伴&#xff0c;快来涨知识吧 方法一&#xff1a;使…

win10 linux重置密码忘记了,忘记密码时如何重新设置Windows10密码

忘记了Windows10计算机的本地管理员密码&#xff0c;如果忘记密码&#xff0c;您将无法访问PC上的任何数据。为什么会这样呢&#xff1f;可能您的Microsoft帐户密码可能已被盗用&#xff0c;针对此疑问&#xff0c;接下去和大家分享忘记密码时重新设置Windows10密码的方法。 但…

win10上Redis设置密码

首先我的电脑是win10家庭版 安装redis后打开这个安装目录出现这个图上的内容就是找对地方了。先说下几个数字是什么&#xff1a;1.者是redis服务&#xff0c;如果项目用到redis要先打开这个&#xff0c;比如若依启动之前要先打开这个。2.cli是客户端。打开1之后 再打开2可以连接…