服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - Unity中 mesh生成斜坡的示例代码

Unity中 mesh生成斜坡的示例代码

2022-11-21 14:30nanyang0310 C#

Mesh是指模型的网格,3D模型是由多边形拼接而成,而多边形实际上是由多个三角形拼接而成的,今天通过本文给大家介绍Unity中 mesh生成斜坡功能,感兴趣的朋友一起看看吧

Mesh概念:

Mesh是Unity中的一个组件,称为网格组件。通俗的讲,Mesh是指模型的网格,3D模型是由多边形拼接而成,而多边形实际上是由多个三角形拼接而成的。所以一个3D模型的表面其实是由多个彼此相连的三角面构成。三维空间中,构成这些三角形的点和边的集合就是Mesh。

Mesh组成:

1、顶点坐标数组vertexes

2、顶点在uv坐标系中的位置信息数组uvs

3、三角形顶点顺时针或者逆时针索引数组triangles

4、MeshFiler组件,用于增加mesh属性

5、MeshRender组件,增加材质并渲染出来。

6、可能还需要每个顶点的法线的数组normals

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
/// 功能:
/// 时间:
/// 版本:
*/
 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class MeshCreater : MonoBehaviour
{
 
    public Vector3 eulerAngles;
 
    [Header("斜坡尺寸")]
    public float sizeX;
    public float sizeY;
    public float sizeZ;
 
    [Header("斜坡后面的立方体尺寸")]
    public float planeSize;
    public float m_angle;
    public Material material;
 
    Vector3[] vertices = new Vector3[54];
    Vector2[] uvs = new Vector2[54];
    List<int> allTris = new List<int>();
 
    Vector3[] allPoint;
 
    GameObject gameObject;
 
 
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            DrawMesh(m_angle);
        }
    }
 
    public void DrawMesh(float angle)
    {
        if (gameObject != null)
        {
            Destroy(gameObject);
        }
        sizeX = sizeY * Mathf.Tan(angle * Mathf.PI / 180);
 
        gameObject = new GameObject("Cube");
        gameObject.transform.eulerAngles = eulerAngles;
 
        var mf = gameObject.AddComponent<MeshFilter>();
        var mr = gameObject.AddComponent<MeshRenderer>();
 
        allPoint = new[]
        {
            //斜坡
             new Vector3(0, 0, 0),
             new Vector3(0, 0, sizeZ),
             new Vector3(sizeX, 0, sizeZ),
             new Vector3(sizeX, 0, 0),
             new Vector3(0, sizeY, 0),
             new Vector3(0, sizeY, sizeZ),
 
             //斜坡后的立方体
             new Vector3(0,-planeSize,0),
             new Vector3(sizeX,-planeSize,0),
             new Vector3(sizeX,-planeSize,sizeZ),
             new Vector3(0,-planeSize,sizeZ),
        };
 
        //斜坡
        vertices[0] = allPoint[0];  //正面
        vertices[1] = allPoint[4];
        vertices[2] = allPoint[3];
 
 
        vertices[3] = allPoint[0]; //底面1
        vertices[4] = allPoint[3];
        vertices[5] = allPoint[2];
 
        vertices[6] = allPoint[0];//底面1
        vertices[7] = allPoint[2];
        vertices[8] = allPoint[1];
 
        vertices[9] = allPoint[1];  //背面1
        vertices[10] = allPoint[4];
        vertices[11] = allPoint[0];
 
        vertices[12] = allPoint[4];//背面2
        vertices[13] = allPoint[1];
        vertices[14] = allPoint[5];
 
        vertices[15] = allPoint[5]; //反面
        vertices[16] = allPoint[1];
        vertices[17] = allPoint[2];
 
        vertices[18] = allPoint[2]; //斜坡1
        vertices[19] = allPoint[3];
        vertices[20] = allPoint[4];
 
        vertices[21] = allPoint[2]; //斜坡1
        vertices[22] = allPoint[4];
        vertices[23] = allPoint[5];
 
 
        //斜坡后的立方体
        vertices[24] = allPoint[6];
        vertices[25] = allPoint[0];
        vertices[26] = allPoint[3];
 
        vertices[27] = allPoint[6];
        vertices[28] = allPoint[3];
        vertices[29] = allPoint[7];
 
        vertices[30] = allPoint[6];
        vertices[31] = allPoint[9];
        vertices[32] = allPoint[0];
 
        vertices[33] = allPoint[0];
        vertices[34] = allPoint[9];
        vertices[35] = allPoint[1];
 
        vertices[36] = allPoint[7];
        vertices[37] = allPoint[3];
        vertices[38] = allPoint[8];
 
        vertices[39] = allPoint[8];
        vertices[40] = allPoint[3];
        vertices[41] = allPoint[2];
 
        vertices[42] = allPoint[7];
        vertices[43] = allPoint[8];
        vertices[44] = allPoint[9];
 
        vertices[45] = allPoint[7];
        vertices[46] = allPoint[9];
        vertices[47] = allPoint[6];
 
        vertices[48] = allPoint[1];
        vertices[49] = allPoint[9];
        vertices[50] = allPoint[8];
 
        vertices[51] = allPoint[1];
        vertices[52] = allPoint[8];
        vertices[53] = allPoint[2];
 
 
        for (int i = 0; i < vertices.Length; i++)
        {
            allTris.Add(i);
        }
 
        uvs[0] = new Vector2(0, 0);
        uvs[1] = new Vector2(0, 1);
        uvs[2] = new Vector2(1, 0);
 
        uvs[3] = new Vector2(0, 0);
        uvs[4] = new Vector2(1, 0);
        uvs[5] = new Vector2(1, 1);
 
        uvs[6] = new Vector2(0f, 0f);
        uvs[7] = new Vector2(1f, 1f);
        uvs[8] = new Vector2(0f, 1f);
 
        uvs[9] = new Vector2(0, 1);
        uvs[10] = new Vector2(1, 0);
        uvs[11] = new Vector2(0, 0);
 
        uvs[12] = new Vector2(1, 0);
        uvs[13] = new Vector2(0, 1);
        uvs[14] = new Vector2(1, 1);
 
        uvs[15] = new Vector2(0f, 1f);
        uvs[16] = new Vector2(0, 0);
        uvs[17] = new Vector2(1, 0);
 
        uvs[18] = new Vector2(1f, 1f);
        uvs[19] = new Vector2(1f, 0f);
        uvs[20] = new Vector2(0f, 0f);
 
        uvs[21] = new Vector2(1, 1);
        uvs[22] = new Vector2(0, 0);
        uvs[23] = new Vector2(0, 1);
 
 
        uvs[24] = new Vector2(1, 1);
        uvs[25] = new Vector2(0, 0);
        uvs[26] = new Vector2(0, 1);
 
        uvs[27] = new Vector2(1, 1);
        uvs[28] = new Vector2(0, 0);
        uvs[29] = new Vector2(0, 1);
 
        uvs[30] = new Vector2(1, 1);
        uvs[31] = new Vector2(0, 0);
        uvs[32] = new Vector2(0, 1);
 
        uvs[33] = new Vector2(1, 1);
        uvs[34] = new Vector2(0, 0);
        uvs[35] = new Vector2(0, 1);
 
        uvs[36] = new Vector2(1, 1);
        uvs[37] = new Vector2(0, 0);
        uvs[38] = new Vector2(0, 1);
 
        uvs[39] = new Vector2(1, 1);
        uvs[40] = new Vector2(0, 0);
        uvs[41] = new Vector2(0, 1);
 
        uvs[42] = new Vector2(1, 1);
        uvs[43] = new Vector2(0, 0);
        uvs[44] = new Vector2(0, 1);
 
        uvs[45] = new Vector2(1, 1);
        uvs[46] = new Vector2(0, 0);
        uvs[47] = new Vector2(0, 1);
 
        uvs[48] = new Vector2(1, 1);
        uvs[49] = new Vector2(0, 0);
        uvs[50] = new Vector2(0, 1);
 
        uvs[51] = new Vector2(1, 1);
        uvs[52] = new Vector2(0, 0);
        uvs[53] = new Vector2(0, 1);
 
        Mesh mesh = new Mesh();
        mesh.vertices = vertices;
        mesh.uv = uvs;
        mesh.triangles = allTris.ToArray();
        mr.material = material;
        mesh.RecalculateTangents();
        mesh.RecalculateNormals();
        mesh.RecalculateBounds();
        mf.mesh = mesh;
    }
 
    private void OnDrawGizmos()
    {
        if (allPoint != null)
        {
            for (int i = 0; i < allPoint.Length; i++)
            {
                Gizmos.color = Color.yellow;
                Gizmos.DrawSphere(allPoint[i], 0.1f);
            }
        }
    }
}

Unity中 mesh生成斜坡的示例代码

以上就是Unity中 mesh生成斜坡的示例代码的详细内容,更多关于Unity mesh斜坡的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/nanyang0310/p/14821993.html

延伸 · 阅读

精彩推荐
  • C#C#中多态性的实现

    C#中多态性的实现

    这篇文章主要介绍了C#中多态性的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编...

    LLLLL__5702022-08-24
  • C#C#编程实现获取文件夹中所有文件的文件名

    C#编程实现获取文件夹中所有文件的文件名

    这篇文章主要介绍了C#编程实现获取文件夹中所有文件的文件名,可实现获取特定目录下制定类型文件名称的功能,涉及C#针对文件与目录的遍历、查询等操作...

    Jan.David7532021-11-02
  • C#详解C#中的session用法

    详解C#中的session用法

    这篇文章主要介绍了C#中的session用法 ,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下...

    咸鱼翻身①4562022-07-19
  • C#C#调用AForge实现摄像头录像的示例代码

    C#调用AForge实现摄像头录像的示例代码

    这篇文章主要介绍了C#调用AForge实现摄像头录像的示例代码,非常具有实用价值,需要的朋友可以参考下...

    asml11502022-01-22
  • C#C#用户控件之温度计设计

    C#用户控件之温度计设计

    这篇文章主要为大家详细介绍了C#用户控件之温度计的设计方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    Alan.hsiang8172022-02-21
  • C#Unity屏幕雪花另类实现方式示例

    Unity屏幕雪花另类实现方式示例

    这篇文章主要介绍了Unity屏幕雪花另类实现方式示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们...

    海洋_3752022-08-07
  • C#C#编程中常见数据结构的比较(Unity3D游戏开发)

    C#编程中常见数据结构的比较(Unity3D游戏开发)

    在本篇内容里我们给大家整理了关于Unity3D游戏开发中C#编程中常见数据结构的比较相关知识点内容,需要的朋友们参考下。...

    laozhang4232022-07-22
  • C#C# 使用HttpClient模拟请求的案例

    C# 使用HttpClient模拟请求的案例

    这篇文章主要介绍了C# 使用HttpClient模拟请求的案例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    丨背水丨8002022-10-12