Unity3D生成一段隧道网格的方法
作者:三体问题 时间:2022-02-22 23:46:27
本文实例为大家分享了Unity3D生成一段隧道网格的具体代码,供大家参考,具体内容如下
一、需求
最近有一个需求,生成段隧道的骨架网格。目前想到的方法就是,获取隧道网格,通过一个算法分离内框和外框的点:
然后通过外框和内框上的点画线,可以通过深度优先搜索得到内外两个环的序列, 从而实现骨架网格
二、生成隧道算法
隧道由段圆弧和一条直线组成,所以算法如下:
1、取圆心为0,0,0,和圆上半径的向量 \underset{OB}{\rightarrow} ,绕z轴,旋转向量\underset{OB}{\rightarrow},取到圆上的点,外框上的点同理可得
2、平移内外框上的点,z轴加上偏离,得到隧道另一端的点
3、取相邻外框上的点和平移后的四个点,生成两个三角形。内框同理可得。
三、效果如下:
四、实现:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MeshCreater : MonoBehaviour
{
Mesh mesh;
public Material mat;//mesh材质
public GameObject game;
// Start is called before the first frame update
void Start()
{
mesh = new Mesh();
mesh.Clear();
SetVertivesUV();
SetTriangles();
mesh.vertices = vertices.ToArray();
mesh.triangles = triangles;
GameObject obj_cell = new GameObject();
obj_cell.name = "cell";
mesh.RecalculateNormals();//重置法线
mesh.RecalculateBounds(); //重置范围
obj_cell.AddComponent<MeshFilter>().mesh = mesh;
obj_cell.AddComponent<MeshRenderer>();
obj_cell.GetComponent<MeshRenderer>().material = mat;
MeshCaluate mesh_caluate = new MeshCaluate();
mesh_caluate.CalculateMesh(mesh);
}
// Update is called once per frame
void Update()
{
}
public List<Vector3> vertices = new List<Vector3>();
private float angle = 10;
private float max_angle = 120;
// 设置顶点信息
void SetVertivesUV()
{
Vector3 dir1 = new Vector3(Mathf.Sqrt(3f), -1, 0);
Vector3 dir2 = dir1 * 0.8f;
List<Vector3> points1 = new List<Vector3>();
List<Vector3> points2 = new List<Vector3>();
int count = (int)((360 - max_angle) / angle);
for (int i = 0; i < count; i++)
{
Quaternion q= Quaternion.AngleAxis(i * angle, Vector3.forward);
Vector3 point1 = q* dir1;
Vector3 point2 = q* dir2;
points1.Add(point1);
points2.Add(point2);
}
points1.Add(points1[0]);
points2.Add(points2[0]);
for (int i = 0; i < points1.Count; i++)
{
var v1 = points1[i];
var v2 = points2[i];
var v3 = points1[i];
v3.z = 10;
var v4 = points2[i];
v4.z = 10;
vertices.Add(v1);
vertices.Add(v3);
vertices.Add(v2);
vertices.Add(v4);
}
}
private int[] triangles;//索引
// 设置索引
void SetTriangles()
{
triangles = new int[vertices.Count * 3];
int c = 0;
for (int i = 0; i < triangles.Length -12 ; i += 12)
{
var v1 = c;
var v2 = c + 1;
var v3 = c + 4;
var v4 = c + 5;
var v5 = c + 2;
var v6 = c + 3;
var v7 = c + 6;
var v8 = c + 7;
triangles[i] = v4;
triangles[i + 1] = v2;
triangles[i + 2] = v1;
triangles[i + 3] = v3;
triangles[i + 4] = v4;
triangles[i + 5] = v1;
triangles[i + 6] = v5;
triangles[i + 7] = v6;
triangles[i + 8] = v8;
triangles[i + 9] = v5;
triangles[i + 10] = v8;
triangles[i + 11] = v7;
c += 4;
}
}
}
五、缺陷
UV未计算,所以使用贴图时有问题
来源:https://blog.csdn.net/q943520218/article/details/108031765
标签:Unity3D,网格
0
投稿
猜你喜欢
Android 自定义布局竖向的ViewPager的实现
2022-12-30 19:56:17
springboot2.5.6集成RabbitMq实现Topic主题模式(推荐)
2021-10-03 22:01:22
elasticsearch索引index之engine读写控制结构实现
2021-12-22 00:08:07
java实现十六进制字符unicode与中英文转换示例
2021-05-29 18:19:29
论C++的lambda是函数还是对象
2022-09-24 05:16:06
Android简单实现圆盘抽奖界面
2022-07-25 08:35:27
详解Java线程-守护线程与用户线程
2023-11-25 00:17:29
Android 5.0+ 屏幕录制实现的示例代码
2023-11-19 13:02:30
C#中Dynamic和Dictionary性能比较
2022-05-11 12:16:33
SpringBoot分页查询功能的实现方法
2023-07-14 02:22:21
java实现推箱子小游戏
2023-02-09 04:57:52
c#实现一元二次方程求解器示例分享
2023-01-03 21:15:30
Java中不可或缺的关键字volatile详析
2023-07-22 12:11:29
设置Android系统永不锁屏永不休眠的方法
2021-06-28 12:01:21
IDEA利用自带Axis工具和wsdl文件反向生成服务端客户端代码图文详解
2021-12-06 20:02:12
Java中switch的三种用法方式小结
2023-11-24 03:40:21
Java实现抢红包功能
2021-08-05 07:47:58
C#集合之队列的用法
2022-11-24 16:15:38
Java雇员管理小项目
2023-02-12 00:39:01
HTTP中get和post的区别详解
2023-04-19 11:42:18