-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeshUtils.cs
107 lines (92 loc) · 2.91 KB
/
MeshUtils.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
using StereoKit;
namespace RDR
{
class MeshUtils
{
static public Mesh createArrow(float dx, float dy, float dz)
{
Vertex[] verts = new Vertex[5];
verts[0] = new Vertex(new Vec3(0, 0, dz), new Vec3(0, 0, dz));
verts[1] = new Vertex(new Vec3(0, dy, 0), new Vec3(0, dy, 0));
verts[2] = new Vertex(new Vec3(dx, 0, 0), new Vec3(dx, 0, 0));
verts[3] = new Vertex(new Vec3(0, -dy, 0), new Vec3(0, -dy, 0));
verts[4] = new Vertex(new Vec3(-dx, 0, 0), new Vec3(-dx, 0, 0));
uint[] inds = new uint[] { 0, 2, 1, 0, 1, 4, 0, 4, 3, 0, 3, 2, 4, 1, 2, 4, 2, 3 };
Mesh m = new Mesh();
m.SetVerts(verts);
m.SetInds(inds);
return m;
}
static public Mesh generateUVSphere(float diameter, uint segments, uint rings)
{
// rings >= 3
Vertex[] verts = new Vertex[2 + (rings - 1) * (segments+1)];
verts[0] = new Vertex(new Vec3(0, diameter / 2.0f, 0 ), Vec3.Up, new Vec2(.5f,0));
verts[1 + (rings - 1) * (segments+1)] = new Vertex(new Vec3(0, -diameter / 2.0f,0), -Vec3.Up, new Vec2(.5f, 1));
int ind = 1;
float beta;
Vec3 p;
for (int r = 1; r < rings ; r++)
{
float alpha = SKMath.Pi / rings * r;
for (int s = 0; s < segments; s++)
{
beta = (2.0f * SKMath.Pi / segments) * s;
p = new Vec3(
SKMath.Sin(alpha) * SKMath.Cos(beta),
SKMath.Cos(alpha),
SKMath.Sin(alpha) * SKMath.Sin(beta)
);
// Normal set vertice and normal
verts[ind] = new Vertex(diameter /2.0f * p, p, new Vec2(1.0f - (1.0f/segments)*s,1.0f/rings * r));
ind += 1;
}
// add a final point at the same physical location of the first one but different UV map.
beta = 0f;
p = new Vec3(
SKMath.Sin(alpha) * SKMath.Cos(beta),
SKMath.Cos(alpha),
SKMath.Sin(alpha) * SKMath.Sin(beta)
);
verts[ind] = new Vertex(diameter / 2.0f * p, p, new Vec2(0f, (1.0f / rings) * r));
ind += 1;
}
// segments facets on poles and 2 facets per square for each segments on rest of rings.
uint[] inds = new uint[(2 * (segments+1) + (rings - 2) * (segments+1) * 2) * 3];
int j = 0;
for (uint i = 0; i < segments ; i++)
{
inds[j] = 0;
inds[j + 1] = i + 2;
inds[j + 2] = i+1;
j += 3;
}
for (uint r = 1; r < rings-1 ; r++)
{
for (uint i = 0; i < segments ; i++)
{
inds[j] = 1 + (r - 1) * (segments+1) + i;
inds[j + 1] = 1 + (r) * (segments+1) + i + 1;
inds[j + 2] = 1 + (r) * (segments+1) + i;
j += 3;
inds[j] = 1 + (r - 1) * (segments+1) + i;
inds[j + 1] = 1 + (r - 1) * (segments+1) + i + 1;
inds[j + 2] = 1 + (r) * (segments+1) + i + 1;
j += 3;
}
}
uint lastIndex = 1 + (rings - 1) * (segments+1);
for (uint i = 0; i < segments ; i++)
{
inds[j] = lastIndex;
inds[j + 2] = lastIndex-i-1;
inds[j + 1] = lastIndex-i-2;
j += 3;
}
Mesh m = new Mesh();
m.SetVerts(verts);
m.SetInds(inds);
return m;
}
}
}