手把手带你使用Paint in 3D和Photon撸一个在线涂鸦画板

简介: Paint in 3D用于在游戏内和编辑器里绘制所有物体。所有功能已经过深度优化,在WebGL、移动端、VR 以及更多平台用起来都非常好用!它支持标准管线,以及 LWRP、HDRP 和 URP。通过使用GPU 加速,你的物体将以难以置信的速度被绘制。代码还经过深度优化来防止GC,和将所有绘制操作一起批次完成。跟贴图系统不同,它是一个纹理绘制解决方案。这意味着你可以绘制你的物体上百万次,还是无帧率丢失,让你创作难以想象的游戏。

Paint in 3D

Paint in 3D用于在游戏内和编辑器里绘制所有物体。所有功能已经过深度优化,在WebGL、移动端、VR 以及更多平台用起来都非常好用!

它支持标准管线,以及 LWRP、HDRP 和 URP。通过使用GPU 加速,你的物体将以难以置信的速度被绘制。代码还经过深度优化来防止GC,和将所有绘制操作一起批次完成。

跟贴图系统不同,它是一个纹理绘制解决方案。这意味着你可以绘制你的物体上百万次,还是无帧率丢失,让你创作难以想象的游戏。

它在Unity应用商店上的售价是60美元,地址:https://assetstore.unity.com/packages/tools/painting/paint-in-3d-26286

Photon

Photon中文翻译为“光子”,为有着15年服务器后端开发经验的德国Exit Games公司开发的高效,稳定,可拓展的网络引擎。为目前世界上用户最广泛,支持游戏类型最多的专业网络引擎之一,也是Unity应用商店里用户评价最高的网络组件。

世界多个知名游戏公司和工作室选用Photon作为其产品的网络支持引擎,其中包括WB华纳,Codemaster, 2K, Glu, 微软游戏工作室,史克威尔艾尼克斯,百代南梦宫,SandBox,雨神电竞等知名企业,也有许多工作室和新创企业正在了解和试用Photon之中。

它在Unity应用商店上有一个免费应用,地址:https://assetstore.unity.com/packages/tools/network/pun-2-free-119922

当然,Photon需要注册账号、创建应用等操作才能使用,还不了解的同学可以去官方网站查阅相关资料。

温馨提示:Photon的国外服务器在国内使用比较卡,所以最好去中国官网申请国内的服务器,申请地址:https://vibrantlink.com/chinacloudapply/

下面正式开始。

创建工程

使用Unity Hub创建一个3D项目,然后分别引入Paint in 3DPhoton Unity Networking 2,如下图:

万猫学社.png

温馨提示:在引入Photon Unity Networking 2后,记得配置AppId。

创建简易画板

为了方便演示,我们创建一个Quad作为画板,然后为其添加P3dPaintable、P3dMaterialCloner和P3dPaintableTexture组件,使用它们的默认配置即可,如下图:

万猫学社.png

然后,创建一个空的GameObject命名为OneMorePaint,然后向OneMorePaint添加P3dPaintSphere组件,修改P3dPaintSphere组件的Color为红色,其他配置保持默认不变,如下图:

万猫学社.png

再向OneMorePaint添加P3dHitScreen组件,勾选上P3dHitScreen组件的ConnectHits,其他配置保持默认不变,如下图:

万猫学社.png

这时候,创建简易画板就做好了,运行以后就可以画画了,如下图:

万猫学社.png

只不过,还是个单机版,我们加上实时在线功能。

连接PUN2服务器

创建一个C#脚本命名为Launcher,再创建一个空的GameObject命名为LauncherGameObject,把C#脚本Launcher添加到LauncherGameObject中。

编辑C#脚本Launcher为如下内容:

using Photon.Pun;
using Photon.Realtime;
using UnityEngine;

namespace One.More
{
    public class Launcher : MonoBehaviourPunCallbacks
    {
        #region Private Fields
        /// <summary>
        /// This client's version number. Users are separated from each other by gameVersion (which allows you to make breaking changes).
        /// </summary>
        string gameVersion = "1";
        /// <summary>
        /// Keep track of the current process. Since connection is asynchronous and is based on several callbacks from Photon,
        /// we need to keep track of this to properly adjust the behavior when we receive call back by Photon.
        /// Typically this is used for the OnConnectedToMaster() callback.
        /// </summary>
        bool isConnecting;
        #endregion

        void Start()
        {
            this.Connect();
        }

        #region MonoBehaviourPunCallbacks Callbacks
        public override void OnConnectedToMaster()
        {
            Debug.Log("PUN Basics Tutorial/Launcher: OnConnectedToMaster() was called by PUN");
            // we don't want to do anything if we are not attempting to join a room.
            // this case where isConnecting is false is typically when you lost or quit the game, when this level is loaded, OnConnectedToMaster will be called, in that case
            // we don't want to do anything.
            if (isConnecting)
            {
                // #Critical: The first we try to do is to join a potential existing room. If there is, good, else, we'll be called back with OnJoinRandomFailed()
                PhotonNetwork.JoinRandomRoom();
                isConnecting = false;
            }
        }
        public override void OnDisconnected(DisconnectCause cause)
        {
            Debug.LogWarningFormat("PUN Basics Tutorial/Launcher: OnDisconnected() was called by PUN with reason {0}", cause);
            isConnecting = false;
        }
        public override void OnJoinRandomFailed(short returnCode, string message)
        {
            Debug.Log("PUN Basics Tutorial/Launcher:OnJoinRandomFailed() was called by PUN. No random room available, so we create one.\nCalling: PhotonNetwork.CreateRoom");
            // #Critical: we failed to join a random room, maybe none exists or they are all full. No worries, we create a new room.
            PhotonNetwork.CreateRoom(null, new RoomOptions());
        }
        public override void OnJoinedRoom()
        {
            Debug.Log("PUN Basics Tutorial/Launcher: OnJoinedRoom() called by PUN. Now this client is in a room.");
        }
        #endregion

        #region Public Methods
        /// <summary>
        /// Start the connection process.
        /// - If already connected, we attempt joining a random room
        /// - if not yet connected, Connect this application instance to Photon Cloud Network
        /// </summary>
        public void Connect()
        {
            // we check if we are connected or not, we join if we are , else we initiate the connection to the server.
            if (PhotonNetwork.IsConnected)
            {
                // #Critical we need at this point to attempt joining a Random Room. If it fails, we'll get notified in OnJoinRandomFailed() and we'll create one.
                PhotonNetwork.JoinRandomRoom();
            }
            else
            {
                // #Critical, we must first and foremost connect to Photon Online Server.
                isConnecting = PhotonNetwork.ConnectUsingSettings();
                PhotonNetwork.GameVersion = gameVersion;
            }
        }
        #endregion
    }
}

这时候,就可以连接到连接PUN2服务器了,运行以后我们可以看到如下日志:

万猫学社.png

实时在线同步

向之前创建的OneMorePaint添加PhotonView组件,使用默认配置即可,如下图:

万猫学社.png

创建一个C#脚本命名为OnlinePainting,把C#脚本OnlinePainting添加到OneMorePaint中。

编辑C#脚本OnlinePainting为如下内容:

using PaintIn3D;
using Photon.Pun;
using UnityEngine;

namespace One.More
{
    public class OnlinePainting : MonoBehaviour, IHitPoint, IHitLine
    {
        private PhotonView photonView;
        private P3dPaintSphere paintSphere;

        void Start()
        {
            this.photonView = this.GetComponent<PhotonView>();
            this.paintSphere = this.GetComponent<P3dPaintSphere>();
        }

        public void HandleHitPoint(bool preview, int priority, float pressure, int seed, Vector3 position, Quaternion rotation)
        {
            if (preview)
            {
                return;
            }
            if (this.photonView == null)
            {
                Debug.LogError("PhotonView is not found.");
                return;
            }
            this.photonView.RPC("HandleHitPointRpc", RpcTarget.Others, preview, priority, pressure, seed, position, rotation);
        }

        public void HandleHitLine(bool preview, int priority, float pressure, int seed, Vector3 position, Vector3 endPosition, Quaternion rotation, bool clip)
        {
            if (preview)
            {
                return;
            }
            if (this.photonView == null)
            {
                Debug.LogError("PhotonView is not found.");
                return;
            }
            this.photonView.RPC("HandleHitLinetRpc", RpcTarget.Others, preview, priority, pressure, seed, position, endPosition, rotation, clip);
        }

        [PunRPC]
        public void HandleHitPointRpc(bool preview, int priority, float pressure, int seed, Vector3 position, Quaternion rotation)
        {
            if (this.paintSphere == null)
            {
                Debug.LogError("P3dPaintSphere is not found.");
                return;
            }
            this.paintSphere.HandleHitPoint(preview, priority, pressure, seed, position, rotation);
        }

        [PunRPC]
        public void HandleHitLinetRpc(bool preview, int priority, float pressure, int seed, Vector3 position, Vector3 endPosition, Quaternion rotation, bool clip)
        {
            if (this.paintSphere == null)
            {
                Debug.LogError("P3dPaintSphere is not found.");
                return;
            }
            this.paintSphere.HandleHitLine(preview, priority, pressure, seed, position, endPosition, rotation, clip);
        }
    }
}

在线涂鸦画板就制作完成了,我们看看运行效果怎么样?

运行效果

构建以后,同时启动两个客户端,效果如下:

万猫学社

当然,这只是简单的在线涂鸦画板,你还可以再此基础上添加更丰富的功能,比如:修改画笔颜色、修改画笔大小等等。

最后,谢谢你这么帅,还给我 点赞关注
相关文章
|
算法 Python
python:判断一个数是否为质数
python:判断一个数是否为质数
|
9月前
|
人工智能 移动开发 HTML5
HTML5实现人机对战的国际象棋AI版
这是一个基于HTML5的国际象棋小游戏,它也提供人机对战,不过智商相对较低,我们称它为“Cheap AI”,像一个国际象棋初级入门的人都可以轻轻松松赢得比赛。如果你对人工智能感兴趣,你也可以改造这款国际象棋的机器智商,让它变得更为强大。
327 2
|
3月前
|
XML 前端开发 JavaScript
2025 年最新 CSS 面试题 100 道及详细答案解析
本文整理了100道CSS面试题及答案,涵盖基础概念、选择器与特性、布局(如Flexbox和Grid)、动画与过渡、响应式设计等核心内容。从CSS的基础知识如盒模型、`box-sizing`属性,到高级应用如伪类选择器(LVHA、CSS3新增伪类)和视觉格式化模型(BFC),帮助开发者系统掌握CSS技能。此外,还涉及浏览器兼容性、CSS优化及预处理器等内容,为前端工程师提供全面的学习资源。资源可从[此链接](https://pan.quark.cn/s/50438c9ee7c0)获取。
334 5
System.InvalidOperationException:“线程间操作无效: 从不是创建控件“xxx”线程它。”
System.InvalidOperationException:“线程间操作无效: 从不是创建控件“xxx”线程它。”
672 0
|
7月前
|
网络协议 图形学 Windows
unity获取本机IP地址
在 Unity 中,通过 .NET 框架的 System.Net 命名空间提供的 Dns 和 NetworkInterface 类,可以获取本机的 IPv4 和 IPv6 地址。使用 Dns.GetHostEntry 方法获取主机信息,并根据地址族(AddressFamily.InterNetwork 或 AddressFamily.InterNetworkV6)筛选出相应的 IP 地址。代码示例展示了如何分别获取 IPv4 和 IPv6 地址并输出到控制台。
344 10
|
缓存 Java
Electron V8排查问题之避免V8FatalErrorCallback崩溃问题如何解决
Electron V8排查问题之避免V8FatalErrorCallback崩溃问题如何解决
233 0
|
数据采集 机器学习/深度学习 Python
掌握XGBoost:特征工程与数据预处理
掌握XGBoost:特征工程与数据预处理
903 3
|
SQL 开发框架 .NET
代码更简洁,开发更高效:从零开始使用Entity Framework Core与传统ADO.NET构建数据持久化层的比较
【8月更文挑战第31天】在.NET平台上开发数据驱动应用时,选择合适的ORM框架至关重要。本文通过对比传统的ADO.NET和现代的Entity Framework Core (EF Core),展示了如何从零开始构建数据持久化层。ADO.NET虽强大灵活,但需要大量手写代码;EF Core则简化了数据访问,支持LINQ查询,自动生成SQL命令,提升开发效率。从创建.NET Core项目、定义数据模型、配置`DbContext`到执行数据库操作,EF Core提供了一套流畅的API,使数据持久化层的构建变得简单直接。
237 0
|
前端开发 开发者 UED
CSS进阶-CSS Sprites技术
【6月更文挑战第14天】**CSS Sprites是一种合并多个小图至大图的技术,减少HTTP请求,提升页面加载速度。本文探讨了精灵图的核心概念,常见问题(如定位不准、适应性问题、维护困难)及解决方案。建议使用工具精确计算坐标,采用媒体查询适应不同屏幕,建立图标管理机制,并提供代码示例展示如何应用。尽管有WebP、SVG等新技术,但在处理大量小图标时,CSS Sprites仍是高效选择。理解和掌握此技术对前端开发者至关重要。**
233 2