-
Notifications
You must be signed in to change notification settings - Fork 5
/
CurveTestMesh.cs
242 lines (194 loc) · 6.87 KB
/
CurveTestMesh.cs
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
using System;
using UnityEngine;
public class CurveTestMesh : MonoBehaviour
{
public float width = 1;
public float length = 1;
public float offset = 0;
public float degrees = 42;
public bool use_smooth = true;
public Material straightMaterial;
public Material curveMaterial;
float last_offset;
float last_degrees;
bool last_smooth;
float last_width;
float last_height;
MeshFilter meshFilter;
public void Start()
{
MeshRenderer meshRenderer = gameObject.AddComponent<MeshRenderer>();
meshRenderer.sharedMaterials = new Material[]{ straightMaterial, curveMaterial };
meshFilter = gameObject.AddComponent<MeshFilter>();
UdpateSmooth();
}
public void Update()
{
degrees = Mathf.Clamp(degrees, 0, 90);
offset = (offset + 1) % 1; // wrap 0 to 1
width = Mathf.Clamp(width, 0.1f, 2);
length = Mathf.Clamp(length, 0.1f, 4);
if (use_smooth != last_smooth ||
!Mathf.Approximately(last_height - length, 0) ||
!Mathf.Approximately(last_width - width, 0) ||
!Mathf.Approximately(last_offset - offset, 0) ||
!Mathf.Approximately(last_degrees - degrees, 0))
{
if(use_smooth)
{
UdpateSmooth();
}
else
{
UpdateJanky();
}
float v_offset = offset + (length-1);
curveMaterial.SetFloat("_VOffset", v_offset);
//straightMaterial.mainTextureScale = new Vector2(1, height);
//straightMaterial.mainTextureOffset = new Vector2(0, offset);
last_offset = offset;
last_degrees = degrees;
last_smooth = use_smooth;
last_height = length;
last_width = width;
}
}
private void UdpateSmooth()
{
Mesh mesh = new Mesh();
mesh.subMeshCount = 2;
float r = 0.5f;
float theta = degrees * Mathf.Deg2Rad;
float arclen = theta * r;
Quaternion edge_rot = Quaternion.Euler(0, 0, degrees - 90f);
Vector3[] vertices = new Vector3[12]
{
// first box
new Vector3(0, -length, 0),
new Vector3(width, -length, 0),
new Vector3(0, 0, 0),
new Vector3(width, 0, 0),
// second box
Quaternion.Euler(0, 0, degrees) * new Vector3(0, 0, 0),
Quaternion.Euler(0, 0, degrees) * new Vector3(width, 0, 0),
Quaternion.Euler(0, 0, degrees) * new Vector3(0, length, 0),
Quaternion.Euler(0, 0, degrees) * new Vector3(width, length, 0),
// corner cap
new Vector3(0, 0, 0),
new Vector3(width, 0, 0),
edge_rot * new Vector3(0, width, 0),
new Vector3(width, width * Mathf.Tan(theta * 0.5f), 0),
};
mesh.vertices = vertices;
Vector3[] normals = new Vector3[12];
for (int i = 0; i < normals.Length; ++i)
{
normals[i] = -Vector3.forward;
}
mesh.normals = normals;
//float offset_a = (offset + height) % 1;
float offset_b = (offset + length + arclen) % 1;
Vector2[] uv = new Vector2[12]
{
new Vector2(0, offset),
new Vector2(1, offset),
new Vector2(0, offset + length),
new Vector2(1, offset + length),
new Vector2(0, offset_b),
new Vector2(1, offset_b),
new Vector2(0, offset_b + length),
new Vector2(1, offset_b + length),
new Vector2(0, 0),
new Vector2(1, 0),
edge_rot * new Vector2(0, 1),
new Vector2(1, Mathf.Tan(theta * 0.5f)),
};
mesh.uv = uv;
int[] straightTris = new int[12]
{
0, 2, 1,
2, 3, 1,
0+4, 2+4, 1+4,
2+4, 3+4, 1+4,
};
int[] curvedTris = new int[6]
{
0+8, 2+8, 1+8,
2+8, 3+8, 1+8,
};
mesh.SetTriangles(straightTris, 0);
mesh.SetTriangles(curvedTris, 1);
meshFilter.mesh = mesh;
}
private void UpdateJanky()
{
Mesh mesh = new Mesh();
mesh.subMeshCount = 2;
float r = 0.5f;
float theta = degrees * Mathf.Deg2Rad;
float arclen = theta * r;
// 4 corner tris
float corner_cap_deg = degrees / 4;
float corner_cap_offset = arclen / 4;
Vector3[] vertices = new Vector3[14]
{
// first box
new Vector3(0, -length, 0),
new Vector3(width, -length, 0),
new Vector3(0, 0, 0),
new Vector3(width, 0, 0),
// second box
Quaternion.Euler(0, 0, degrees) * new Vector3(0, 0, 0),
Quaternion.Euler(0, 0, degrees) * new Vector3(width, 0, 0),
Quaternion.Euler(0, 0, degrees) * new Vector3(0, length, 0),
Quaternion.Euler(0, 0, degrees) * new Vector3(width, length, 0),
// corner cap
new Vector3(0, 0, 0),
Quaternion.Euler(0, 0, corner_cap_deg * 0) * new Vector3(width, 0, 0),
Quaternion.Euler(0, 0, corner_cap_deg * 1) * new Vector3(width, 0, 0),
Quaternion.Euler(0, 0, corner_cap_deg * 2) * new Vector3(width, 0, 0),
Quaternion.Euler(0, 0, corner_cap_deg * 3) * new Vector3(width, 0, 0),
Quaternion.Euler(0, 0, corner_cap_deg * 4) * new Vector3(width, 0, 0),
};
mesh.vertices = vertices;
Vector3[] normals = new Vector3[14];
for(int i = 0; i < normals.Length; ++i)
{
normals[i] = -Vector3.forward;
}
mesh.normals = normals;
float offset_a = (offset + length) % 1;
float offset_b = (offset + length + arclen) % 1;
Vector2[] uv = new Vector2[14]
{
new Vector2(0, offset),
new Vector2(1, offset),
new Vector2(0, offset + length),
new Vector2(1, offset + length),
new Vector2(0, offset_b),
new Vector2(1, offset_b),
new Vector2(0, offset_b + length),
new Vector2(1, offset_b + length),
new Vector2(0, offset_a + arclen*0.5f),
new Vector2(1, offset_a + corner_cap_offset * 0),
new Vector2(1, offset_a + corner_cap_offset * 1),
new Vector2(1, offset_a + corner_cap_offset * 2),
new Vector2(1, offset_a + corner_cap_offset * 3),
new Vector2(1, offset_a + corner_cap_offset * 4),
};
mesh.uv = uv;
int[] tris = new int[3 * 8]
{
0, 2, 1,
2, 3, 1,
0+4, 2+4, 1+4,
2+4, 3+4, 1+4,
8, 10, 9,
8, 11, 10,
8, 12, 11,
8, 13, 12,
};
mesh.SetTriangles(tris, 0);
meshFilter.mesh = mesh;
}
}