当前位置: 首页 > news >正文

Unity开发绘画板——01.前言

 在做这个之前其实是想研究一下在Unity中交互雪的实现,交互雪顾名思义就是可以进行交互的雪,玩家角色从雪上走过时雪被踩凹陷的效果;交互雪的一种实现方案就是将地面看做一个画板,将玩家角色的躯体看做笔刷,将角色经过的路径绘制为一张图片并作为地面雪的高度图传给shader去进行处理,在实现这个之前对如何实现轨迹的绘制比较感兴趣,故而想先以绘画板为切入点,研究研究。

不过既然研究了,就做一个尽量完整的方案,我想要实现的绘画板应包含如下基本功能:

  • 1.笔刷大小调节
  • 2.线条颜色调节
  • 3.可以自定义笔刷形状
  • 4.可自定义线条纹理
  • 5.绘制的图片可存储至本地
  • 6.带橡皮擦功能
  • 7.可一键清空画板
  • 8.带撤销和重做功能

说干就干,先介绍一下开发环境:

Unity2022.3.30f1 + Rider

用什么版本和IDE无所谓啦哈哈,就是玩!

需要具备一定的unity基础(有一些基本操作就不一一截图了),和shaderlab基础,不需要太深,感兴趣的话可以跟着我一起来做。

技术路线选择

其实要实现绘画的功能有很多种实现方式,例如,可以使用Texture2D.SetPixel, Texture2D.SetPixels,也可以使用Unity提供的图形接口Graphics.DrawTexture,从执行效率上来讲,Graphics.DrawTexture的效率更高一些,Texture2D的相关接口通过遍历像素来实现效率低一些,且不方便实现我们想要的可自定义线条纹理和笔刷形状的功能,因此本文采用Graphics.DrawTexture来实现

源代码:

/// <summary>
///   <para>Draw a texture in screen coordinates.</para>
/// </summary>
/// <param name="screenRect">Rectangle on the screen to use for the texture. In pixel coordinates with (0,0) in the upper-left corner.</param>
/// <param name="texture">Texture to draw.</param>
/// <param name="sourceRect">Region of the texture to use. In normalized coordinates with (0,0) in the bottom-left corner.</param>
/// <param name="leftBorder">Number of pixels from the left that are not affected by scale.</param>
/// <param name="rightBorder">Number of pixels from the right that are not affected by scale.</param>
/// <param name="topBorder">Number of pixels from the top that are not affected by scale.</param>
/// <param name="bottomBorder">Number of pixels from the bottom that are not affected by scale.</param>
/// <param name="color">Color that modulates the output. The neutral value is (0.5, 0.5, 0.5, 0.5). Set as vertex color for the shader.</param>
/// <param name="mat">Custom Material that can be used to draw the texture. If null is passed, a default material with the Internal-GUITexture.shader is used.</param>
/// <param name="pass">If -1 (default), draws all passes in the material. Otherwise, draws given pass only.</param>
[ExcludeFromDocs]
public static void DrawTexture(Rect screenRect, Texture texture, Material mat)
{Graphics.DrawTexture(screenRect, texture, mat, -1);
}
[ExcludeFromDocs]
public static void DrawTexture(Rect screenRect, Texture texture)
{Graphics.DrawTexture(screenRect, texture, (Material) null, -1);
}

在下面的文章中我们会详细介绍该接口的用法。

相关文章:

  • C++之String类(下)
  • TypeScript 算法手册【插入排序】
  • 五、CAN总线
  • 《NoSQL》非关系型数据库MongoDB 学习笔记!
  • 2024年3分钟手把手教你激活Guitar Pro 8破解版
  • 工业现场干扰问题及处理方法
  • 移情别恋c++ ദ്ദി˶ー̀֊ー́ ) ——15.红黑树
  • Eclipse 快捷键:提高开发效率的利器
  • 【C语言】指针详解(一)
  • 在 Kali Linux 中安装 Impacket
  • pytorch张量基础
  • 物联网将如何影响全球商业?
  • Java基础——十二、容器
  • [论文阅读] ChartInstruct: Instruction Tuning for Chart Comprehension and Reasoning
  • k8s中,服务的自动注册、自动感知、负载均衡,三个功能的含义及测试验证
  • [译] 理解数组在 PHP 内部的实现(给PHP开发者的PHP源码-第四部分)
  • [译]如何构建服务器端web组件,为何要构建?
  • 4月23日世界读书日 网络营销论坛推荐《正在爆发的营销革命》
  • C++类的相互关联
  • CentOS6 编译安装 redis-3.2.3
  • Javascript弹出层-初探
  • Java比较器对数组,集合排序
  • Laravel核心解读--Facades
  • Linux快速配置 VIM 实现语法高亮 补全 缩进等功能
  • select2 取值 遍历 设置默认值
  • springMvc学习笔记(2)
  • 初探 Vue 生命周期和钩子函数
  • 关于字符编码你应该知道的事情
  • 和 || 运算
  • 前端工程化(Gulp、Webpack)-webpack
  • 使用common-codec进行md5加密
  • 通过获取异步加载JS文件进度实现一个canvas环形loading图
  • 微信端页面使用-webkit-box和绝对定位时,元素上移的问题
  • 学习JavaScript数据结构与算法 — 树
  • 带你开发类似Pokemon Go的AR游戏
  • ​埃文科技受邀出席2024 “数据要素×”生态大会​
  • # Swust 12th acm 邀请赛# [ E ] 01 String [题解]
  • ### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
  • #NOIP 2014#Day.2 T3 解方程
  • (10)Linux冯诺依曼结构操作系统的再次理解
  • (2)STL算法之元素计数
  • (2)空速传感器
  • (20050108)又读《平凡的世界》
  • (delphi11最新学习资料) Object Pascal 学习笔记---第2章第五节(日期和时间)
  • (k8s中)docker netty OOM问题记录
  • (Matalb回归预测)PSO-BP粒子群算法优化BP神经网络的多维回归预测
  • (PWM呼吸灯)合泰开发板HT66F2390-----点灯大师
  • (板子)A* astar算法,AcWing第k短路+八数码 带注释
  • (附源码)php投票系统 毕业设计 121500
  • (附源码)spring boot儿童教育管理系统 毕业设计 281442
  • (附源码)基于ssm的模具配件账单管理系统 毕业设计 081848
  • (三)SvelteKit教程:layout 文件
  • (十)T检验-第一部分
  • (四)进入MySQL 【事务】
  • (一)Docker基本介绍