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