【Unity】运行时创建曲线(贝塞尔的运用)

[Unity]运行时创建线(贝塞尔的运用)

1. 实现的目标

在运行状态下创建一条可以使用贝塞尔方法实时编辑的网格曲线。

2. 原理介绍

2.1 曲线的创建

unity建立网格曲线可以参考Unity程序化网格体的实现方法。主要分为顶点,三角面,UV和法线。笔者有类似的文章unity 线绳管道纯代码创建方法_,详细的讲解了网格线的创建方法,这次的不同点在于法线的确立方法上。

2.2贝塞尔曲线点的确立

笔者有文章Unity 贝塞尔曲线的创建_描述了贝塞尔的创建方法。

3. 实现过程

3.1曲线的创建方法

线的组成原理
曲线由横截面圆和中心轴线组成。横截面的法线方向为前后两点向量差,如下图绿色线为中心轴线,A点为横截面所在点,横截面的法线方法为向量 A D ⃗ \vec{AD} AD 既向量 C B ⃗ \vec{CB} CB 的单位向量;终点和起点的法线方向为自身和前点或者后一点的向量。

代码源码

3.1.1 横截圆的创建
           #region 横切圆创建/// <summary>/// 得到管线横切圆/// </summary>/// <param name="Count">段数</param>/// <param name="R">半径</param>/// <returns></returns>Vector3[] CircularSection(int Count, float R){Vector3[] vector3s = new Vector3[Count];float angle = 360 / Count;Vector3 vector3 = new Vector3(R, 0, 0);for (int i = 0; i < Count; i++){//根据角度得到圆的分布点vector3s[i] = vector3.ToAngle(angle * i, Vector3.zero, Vector3.forward);}return vector3s;}#endregion

​ vector3旋转扩展方法

        /// <summary>/// 角度旋转/// </summary>/// <param name="vector3"></param>/// <param name="angle">旋转角度</param>/// <param name="center">旋转中心点</param>/// <param name="direction">旋转轴</param>/// <returns></returns>public static Vector3 ToAngle(this Vector3 vector3, float angle, Vector3 center, Vector3 direction){Vector3 pos = center;Quaternion quaternion = Quaternion.AngleAxis(angle, direction);Matrix4x4 matrix = new Matrix4x4();matrix.SetTRS(pos, quaternion, Vector3.one);vector3 = matrix.MultiplyPoint3x4(vector3);return vector3;}
3.1.2 中心线的确立
           class LinePoint{Vector3 location;Vector3 direction;public Vector3 Location { get => location; set => location = value; }public Vector3 Direction { get => direction; set => direction = value; }}/// <summary>/// 中心线的确立/// </summary>/// <param name="createPoint">曲线点</param>/// <returns></returns>List<LinePoint> SetLinePoint(Vector3[] createPoint){List<LinePoint> pipePoints = new List<LinePoint>();int length = createPoint.Length;for (int i = 0; i < length; i++){if (i == 0){Vector3 tangent = (createPoint[i + 1] - createPoint[i]).normalized;//法线AddPipePoints(createPoint[i], tangent, ref pipePoints);}else if (i == length - 1){Vector3 tangent = (createPoint[i] - createPoint[i - 1]).normalized;//法线AddPipePoints(createPoint[i], tangent, ref pipePoints);}else{Vector3 tangent = (createPoint[i+1] - createPoint[i - 1]).normalized;//法线AddPipePoints(createPoint[i], tangent, ref pipePoints);}}return pipePoints;}/// <summary>/// 增加中心轴线点/// </summary>/// <param name="location">位置</param>/// <param name="direction">法线</param>void AddPipePoints(Vector3 location, Vector3 direction,  ref List<LinePoint> pipePoints){LinePoint pipePoint = new LinePoint();pipePoint.Location = location;pipePoint.Direction = direction;pipePoints.Add(pipePoint);}
3.1.3网格创建
        /// <summary>/// 立体网格创建/// </summary>/// <param name="createPoint">创建的点数据</param>/// <param name="circularCount">圆的段数</param>/// <param name="circularR">圆的半径</param>/// <returns></returns>public Mesh CreateLine3D(Vector3[] createPoint, int circularCount, float circularR){//截面圆Vector3[] circul = CircularSection(circularCount, circularR);//中心线List<LinePoint> centreLine = SetLinePoint(createPoint);//网格点数据Vector3[] meshPoint = CreateMeshPoint(centreLine, circul);float uvX = Vector3.Distance(circul[0], circul[1]);//返回网格return CreatMesh(centreLine, meshPoint, circul.Length, uvX);}
/// <summary>/// 创建网格点数据/// </summary>/// <param name="linePoint"></param>/// <param name="circular"></param>/// <returns></returns>Vector3[] CreateMeshPoint(List<LinePoint> linePoint, Vector3[] circular){int length = linePoint.Count;int circularCount = circular.Length;Vector3[] meshPoint = new Vector3[length * circularCount];for (int i = 0; i < length; i++){for (int j = 0; j < circularCount; j++){meshPoint[(i * circularCount) + j] = circular[j].FromToMoveRotation(linePoint[i].Location, linePoint[i].Direction);}}return meshPoint;}/// <summary>/// 网格创建/// </summary>/// <param name="linePoints">线的轴心线组</param>/// <param name="meshPoint">网格点</param>/// <param name="count">段数</param>/// <param name="uvX">uv宽度</param>/// <returns></returns>Mesh CreatMesh(List<LinePoint> linePoints, Vector3[] meshPoint, int count, float uvX){Mesh mesh = new Mesh();mesh.vertices = meshPoint;mesh.triangles = GetTriangles(linePoints.Count, count);mesh.uv = GetUV(linePoints, count, uvX);mesh.RecalculateNormals();mesh.RecalculateBounds();return mesh;}/// <param name="length">线段段数</param>/// <param name="count">横截面段数(也就是圆的段数)</param>/// <returns></returns>int[] GetTriangles(int length, int count){int[] triangles = new int[(count * (length - 1)) * 6];int k = 0;if (count == 1){for (int i = 0; i < length-1; i++){int a = i * 2;triangles[k] = a;triangles[k + 1] = a + 1;triangles[k + 2] = a + 3;triangles[k + 3] = a;triangles[k + 4] = a + 3;triangles[k + 5] = a + 2;k += 6;}}else{for (int i = 0; i < length - 1; i++){for (int j = 0; j < count; j++){if (j == count - 1){// Debug.Log("k=" + k);triangles[k] = (i * count) + j;triangles[k + 1] = (i * count) + 0;triangles[k + 2] = ((i + 1) * count) + 0;triangles[k + 3] = (i * count) + j;triangles[k + 4] = ((i + 1) * count) + 0;triangles[k + 5] = ((i + 1) * count) + j;}else{triangles[k] = (i * count) + j;triangles[k + 1] = (i * count) + j + 1;triangles[k + 2] = ((i + 1) * count) + j + 1;triangles[k + 3] = (i * count) + j;triangles[k + 4] = ((i + 1) * count) + j + 1;triangles[k + 5] = ((i + 1) * count) + j;}k += 6;}}}return triangles;}/// <summary>/// 创建uv/// </summary>/// <param name="linePoints"></param>/// <param name="count"></param>/// <param name="uvX"></param>/// <returns></returns>Vector2[] GetUV(List<LinePoint> linePoints,int count, float uvX){int length = linePoints.Count;if (count == 1) { count = 2; }Vector2[] uvs = new Vector2[(count * length)];float lineDis = 0;int k = 0;for (int i = 0; i < length; i ++){if (i != 0){lineDis += Vector3.Distance(linePoints[i].Location, linePoints[i - 1].Location);}for (int j = 0; j < count; j++){Vector2 vector2;if (j % 2 != 0){vector2 = new Vector2(uvX, lineDis);}else{vector2 = new Vector2(0, lineDis);}uvs[k] = vector2;k += 1;}}return uvs;}
3.2贝塞尔曲线的建立方法

源码

 /// <summary>/// 获取绘制点/// </summary>/// <param name="controlPoints"></param>/// <param name="segmentsPerCurve"></param>/// <returns></returns>public List<Vector3> GetDrawingPoints(List<Vector3> controlPoints, int segmentsPerCurve){List<Vector3> points = new List<Vector3>();// 下一段的起始点和上段终点是一个,所以是 i+=3for (int i = 0; i <= controlPoints.Count - 4; i += 3){var p0 = controlPoints[i];var p1 = controlPoints[i + 1];var p2 = controlPoints[i + 2];var p3 = controlPoints[i + 3];float dis = Vector3.Distance(p0, p3);int count = Mathf.CeilToInt(segmentsPerCurve * dis);if (count < segmentsPerCurve){count = segmentsPerCurve;}for (int j = 0; j <= count; j++){var t = j / (float)count;points.Add(CalculateBezierPoint(t, p0, p1, p2, p3));}}return points;}// 三阶公式Vector3 CalculateBezierPoint(float t, Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3){Vector3 result;Vector3 p0p1 = (1 - t) * p0 + t * p1;Vector3 p1p2 = (1 - t) * p1 + t * p2;Vector3 p2p3 = (1 - t) * p2 + t * p3;Vector3 p0p1p2 = (1 - t) * p0p1 + t * p1p2;Vector3 p1p2p3 = (1 - t) * p1p2 + t * p2p3;result = (1 - t) * p0p1p2 + t * p1p2p3;return result;}
3.3贝塞尔曲线应用

基于上述方法实现了贝塞尔创建,保存,读取,在编辑功能。
案例下载地址

创建曲线

保存曲线

保存方法有两种分别是,长期保存和暂时保存。长期保存是将保存数据写入本地文件,项目重启也可以读取;暂时保存是在项目运行期间保存数据,重启后丢失。demo使用暂时保存的方法

读取再编辑

读取曲线后可以继续编辑当前曲线

曲线浏览

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/285952.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

基于ssm餐饮掌上设备点餐系统论文

餐饮掌上设备点餐系统 摘要 随着信息技术在管理上越来越深入而广泛的应用&#xff0c;管理信息系统的实施在技术上已逐步成熟。本文介绍了餐饮掌上设备点餐系统的开发全过程。通过分析餐饮掌上设备点餐系统管理的不足&#xff0c;创建了一个计算机管理餐饮掌上设备点餐系统的…

【LeetCode】每日一题 2023_12_19 寻找峰值 II(二分)

文章目录 刷题前唠嗑题目&#xff1a;寻找峰值 II题目描述代码与解题思路 刷题前唠嗑 LeetCode&#xff1f;启动&#xff01;&#xff01;&#xff01; 题目&#xff1a;寻找峰值 II 题目链接&#xff1a;1901. 寻找峰值 II 题目描述 代码与解题思路 除了暴力的 O(N^2) 找这…

TypeScript 中的高级类型(联合、交叉、泛型、映射类型)

文章目录 一、联合类型&#xff08;Union Types&#xff09;二、交叉类型&#xff08;Intersection Types&#xff09;三、泛型3.1 泛型结合extends3.2 泛型结合 keyof3.3 extends keyof 和 in keyof 的区别 四、条件类型&#xff08;Conditional Types&#xff09; TypeScript…

Zookeeper-快速开始

Zookeeper介绍 简介&#xff1a;ZooKeeper 是一个开源的分布式协调框架&#xff0c;是Apache Hadoop 的一个子项目&#xff0c;主要用来解决分布式集群中应用系统的一致性问题。 设计目标&#xff1a;将那些复杂且容易出错的分布式一致性服务封装起来&#xff0c;构成一个高效…

JAVAEE初阶 多线程进阶(一)

进阶面试题 一. 锁拓展1.1 乐观锁与悲观锁1.2 轻量级锁与重量级锁1.3 自旋锁和挂起等待锁1.4 普通互斥锁与读写锁1.5 公平锁与非公平锁1.6 可重入锁和不可重入锁 二.锁的优化策略2.1 锁的自适应2.2 锁消除2.3 锁粗化 三.CAS 一. 锁拓展 1.1 乐观锁与悲观锁 乐观锁 : 加锁前,预…

Elasticsearch——快速入门

从零基础的内容开始介绍Elasticsearch&#xff0c;主要包含以下内容&#xff1a; Elasticsearch的定义、优点&#xff0c;以及典型的业务场景。Elasticsearch中重要的概念。Elasticsearch典型的接入方式。安装Elasticsearch。使用Kibana调试Elasticsearch。Elasticsearch节点的…

保护IP地址免受盗用的有效方法

IP地址是互联网通信的基础&#xff0c;然而&#xff0c;由于其重要性&#xff0c;IP地址的盗用成为一种潜在的网络威胁。本文将深入探讨防止IP地址被盗用的方法&#xff0c;以维护网络的安全性。 第一部分&#xff1a;IP地址盗用的威胁与风险 1.1 IP地址盗用的定义 IP地址盗…

软件测试必备的测试工具清单汇总

前言 据统计&#xff0c;中国软件外包市场的潜力和机会已远远超过软件王国印度&#xff0c;不过由于软件人才的严重不足致使我国软件发展遭遇“瓶颈”。国家为了大力培养软件人才&#xff0c;不断采取积极有效的措施。我国对软件测试人才的需求数量还将持续增加&#xff0c;因…

【MAC】M2 安装docker 与 mysql

一、docker下载地址 下载地址 二、安装docker完成 罗列一下docker常用命令 # 查看docker版本 docker --version# 拉取镜像 docker pull 镜像名# 查看当前所有镜像 docker images# 查看运行中的容器 docker ps -a docker ps grep| 镜像名#镜像启动操作&#xff1a; sudo dock…

使用单向链表实现栈的相关操作【1.2】

创建节点: class StackNode {private StackNode next;private int no;public StackNode getNext() {return next;}public void setNext(StackNode next) {this.next next;}public StackNode(int no) {this.no no;}public int getNo() {return no;}public void setNo(int no)…

Day65力扣打卡

打卡记录 寻找峰值 II&#xff08;二分&#xff09; 链接 class Solution:def findPeakGrid(self, mat: List[List[int]]) -> List[int]:l, r 0, len(mat) - 1while l < r:mid (l r) // 2mx max(mat[mid])if mx > mat[mid 1][mat[mid].index(mx)]:r midelse:l…

linux搭建gitlab

gitlab的介绍 区别于github&#xff0c;github是面向互联网基于git实现的代码托管平台&#xff0c;gitlab是基于Ruby语言实现的git管理平台软件&#xff0c;一般用于公司内部代码仓库。 gitlab组成 gitlab组成 Nginx 静态Web服务器Gitlab-workhorse 轻量级的反向代理服务器Git…