-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoundedCuboidRenderer.cs
318 lines (268 loc) · 10.9 KB
/
RoundedCuboidRenderer.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//
// Cuboid (http://github.com/arjonagelhout/cuboid)
// Copyright (c) 2023 Arjo Nagelhout
//
using System;
using System.Collections;
using System.Collections.Generic;
using Cuboid.Utils;
using UnityEngine;
using Unity.Mathematics;
namespace Cuboid
{
/// <summary>
/// </summary>
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class RoundedCuboidRenderer : MonoBehaviour
{
internal Guid _associatedShapeGuid;
private float _cornerRadius;
private int _cornerDivisions;
private Vector3 _size;
private float _correctedCornerRadius;
private Mesh _mesh;
private Vector3[] _vertices;
private Vector3[] _normals;
private void Awake()
{
Initialize();
}
/*
private void OnDrawGizmos()
{
if (_vertices == null) { return; }
Gizmos.color = Color.black;
for (int i = 0; i < _vertices.Length; i++)
{
Gizmos.DrawSphere(_vertices[i], 0.01f);
}
Gizmos.color = new Color(1, 0, 0, 0.1f);
Gizmos.DrawCube(transform.position + size / 2, size * 0.99f);
}*/
private void Initialize()
{
_mesh = new Mesh();
GetComponent<MeshFilter>().mesh = _mesh;
_mesh.name = $"mesh_{_associatedShapeGuid}"; // Guid set by the RealityShapeObjectData on instantiation
}
public void GenerateMesh(Vector3 size, float cornerRadius, int cornerDivisions)
{
_size = size;
_cornerRadius = cornerRadius;
_cornerDivisions = cornerDivisions;
_size = _size.Clamp(Vector3.zero, _size);
transform.localScale = _size.Inverse();
_mesh.triangles = new int[0];
ObjectFitUtils.GetSmallestComponent(_size, out int index, out float smallestSizeComponent);
_correctedCornerRadius = math.clamp(_cornerRadius, 0, smallestSizeComponent / 2);
_cornerDivisions = math.clamp(_cornerDivisions, 0, 10);
CreateVertices();
CreateTriangles();
_mesh.bounds = new Bounds(Vector3.zero, _size);
//_mesh.RecalculateBounds();
}
/// <summary>
///
/// </summary>
private void CreateVertices()
{
int cornerVertices = _cornerDivisions + 2;
//int totalVertices = (cornerVertices * cornerVertices) * 3 * 8;
int ring = cornerVertices * 8 - 4;
int around = ring * (cornerVertices * 2);
int rowVertices = (cornerVertices * 2) - 2;
int planeVertices = rowVertices * rowVertices;
int totalVertices = 2 * planeVertices + around;
//int topBottom = (cornerVertices * 2) - 1;
_vertices = new Vector3[totalVertices];
_normals = new Vector3[_vertices.Length];
int v = 0;
float positionPerIndex = _correctedCornerRadius / (cornerVertices - 1);
for (int y = 0; y < cornerVertices; y++)
{
DrawRow(y * positionPerIndex);
}
for (int y = 0; y < cornerVertices; y++)
{
DrawRow(_size.y - _correctedCornerRadius + y * positionPerIndex);
}
// Draw row
void DrawRow(float y)
{
// Side 1
for (int x = 0; x < cornerVertices; x++)
{
SetVertex(v++, new Vector3(x * positionPerIndex, y, 0));
}
for (int x = 0; x < cornerVertices; x++)
{
SetVertex(v++, new Vector3(_size.x - _correctedCornerRadius + x * positionPerIndex, y, 0));
}
// Side 2
for (int z = 1; z < cornerVertices; z++)
{
SetVertex(v++, new Vector3(_size.x, y, z * positionPerIndex));
}
for (int z = 0; z < cornerVertices; z++)
{
SetVertex(v++, new Vector3(_size.x, y, _size.z - _correctedCornerRadius + z * positionPerIndex));
}
// Side 3
for (int x = 1; x < cornerVertices; x++)
{
SetVertex(v++, new Vector3(_size.x - x * positionPerIndex, y, _size.z));
}
for (int x = 0; x < cornerVertices; x++)
{
SetVertex(v++, new Vector3(_correctedCornerRadius - x * positionPerIndex, y, _size.z));
}
// Side 4
for (int z = 1; z < cornerVertices; z++)
{
SetVertex(v++, new Vector3(0, y, _size.z - z * positionPerIndex));
}
for (int z = 0; z < cornerVertices - 1; z++)
{
SetVertex(v++, new Vector3(0, y, _correctedCornerRadius - z * positionPerIndex));
}
}
DrawHorizontalPlane(_size.y); // Top plane
DrawHorizontalPlane(0); // Bottom plane
void DrawHorizontalPlane(float y)
{
for (int z = 1; z < cornerVertices; z++)
{
DrawHorizontalRow(y, z * positionPerIndex);
}
for (int z = 0; z < cornerVertices - 1; z++)
{
DrawHorizontalRow(y, _size.z - _correctedCornerRadius + z * positionPerIndex);
}
}
void DrawHorizontalRow(float y, float z)
{
for (int x = 1; x < cornerVertices; x++)
{
SetVertex(v++, new Vector3(x * positionPerIndex, y, z));
}
for (int x = 0; x < cornerVertices - 1; x++)
{
SetVertex(v++, new Vector3(_size.x - _correctedCornerRadius + x * positionPerIndex, y, z));
}
}
_mesh.vertices = _vertices;
_mesh.normals = _normals;
}
private void SetVertex(int i, Vector3 position)
{
position -= _size / 2;
Vector3 inner = position;
_vertices[i] = position;
inner = inner.Clamp(-_size / 2 + Vector3.one * _correctedCornerRadius, _size / 2 - Vector3.one * _correctedCornerRadius);
_normals[i] = (_vertices[i] - inner).normalized;
_vertices[i] = inner + _normals[i] * _correctedCornerRadius;
}
/// <summary>
///
/// </summary>
private void CreateTriangles()
{
int quadsPerCornerOneAxis = _cornerDivisions + 1;
int quadsPerRowOneFace = quadsPerCornerOneAxis * 2 + 1;
int quadsPerFace = quadsPerRowOneFace * quadsPerRowOneFace;
int quads = quadsPerFace * 6;
int[] triangles = new int[quads * 6];
int cornerVertices = _cornerDivisions + 2;
int ring = cornerVertices * 8 - 4;
//int ring = cornerVertices * 8 - 4;
//int ring = //quadsPerRowOneFace * 4;
int t = 0, v = 0;
// Do the sides of the cube
for (int y = 0; y < quadsPerRowOneFace; y++, v++)
{
for (int q = 0; q < ring - 1; q++, v++)
{
t = SetQuad(triangles, t, v, v + 1, v + ring, v + ring + 1);
}
t = SetQuad(triangles, t, v, v - ring + 1, v + ring, v + 1);
}
t = CreateTopFace(triangles, t, ring);
t = CreateBottomFace(triangles, t, ring);
_mesh.triangles = triangles;
}
private int CreateTopFace(int[] triangles, int t, int ring)
{
int sizePerAxis = (_cornerDivisions + 2) * 2 - 1;
int v = ring * sizePerAxis;
for (int x = 0; x < (sizePerAxis) - 1; x++, v++)
{
t = SetQuad(triangles, t, v, v + 1, v + ring - 1, v + ring);
}
t = SetQuad(triangles, t, v, v + 1, v + ring - 1, v + 2);
int vMin = ring * (sizePerAxis + 1) - 1;
int vMid = vMin + 1;
int vMax = v + 2;
for (int z = 1; z < sizePerAxis - 1; z++, vMin--, vMid++, vMax++)
{
t = SetQuad(triangles, t, vMin, vMid, vMin - 1, vMid + sizePerAxis - 1);
for (int x = 1; x < sizePerAxis - 1; x++, vMid++)
{
t = SetQuad(
triangles, t,
vMid, vMid + 1, vMid + sizePerAxis - 1, vMid + sizePerAxis);
}
t = SetQuad(triangles, t, vMid, vMax, vMid + sizePerAxis - 1, vMax + 1);
}
int vTop = vMin - 2;
t = SetQuad(triangles, t, vMin, vMid, vTop + 1, vTop);
for (int x = 1; x < sizePerAxis - 1; x++, vTop--, vMid++)
{
t = SetQuad(triangles, t, vMid, vMid + 1, vTop, vTop - 1);
}
t = SetQuad(triangles, t, vMid, vTop - 2, vTop, vTop - 1);
return t;
}
private int CreateBottomFace(int[] triangles, int t, int ring)
{
int sizePerAxis = (_cornerDivisions + 2) * 2 - 1;
int v = 1;
int vMid = _vertices.Length - (sizePerAxis - 1) * (sizePerAxis - 1);
t = SetQuad(triangles, t, ring - 1, vMid, 0, 1);
for (int x = 1; x < sizePerAxis - 1; x++, v++, vMid++)
{
t = SetQuad(triangles, t, vMid, vMid + 1, v, v + 1);
}
t = SetQuad(triangles, t, vMid, v + 2, v, v + 1);
int vMin = ring - 2;
vMid -= sizePerAxis - 2;
int vMax = v + 2;
for (int z = 1; z < sizePerAxis - 1; z++, vMin--, vMid++, vMax++)
{
t = SetQuad(triangles, t, vMin, vMid + sizePerAxis - 1, vMin + 1, vMid);
for (int x = 1; x < sizePerAxis - 1; x++, vMid++)
{
t = SetQuad(
triangles, t,
vMid + sizePerAxis - 1, vMid + sizePerAxis, vMid, vMid + 1);
}
t = SetQuad(triangles, t, vMid + sizePerAxis - 1, vMax + 1, vMid, vMax);
}
int vTop = vMin - 1;
t = SetQuad(triangles, t, vTop + 1, vTop, vTop + 2, vMid);
for (int x = 1; x < sizePerAxis - 1; x++, vTop--, vMid++)
{
t = SetQuad(triangles, t, vTop, vTop - 1, vMid, vMid + 1);
}
t = SetQuad(triangles, t, vTop, vTop - 1, vMid, vTop - 2);
return t;
}
private static int SetQuad(int[] triangles, int i, int v00, int v10, int v01, int v11)
{
triangles[i] = v00;
triangles[i + 1] = triangles[i + 4] = v01;
triangles[i + 2] = triangles[i + 3] = v10;
triangles[i + 5] = v11;
return i + 6;
}
}
}