-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMesh.cs
More file actions
155 lines (140 loc) · 5.85 KB
/
Copy pathMesh.cs
File metadata and controls
155 lines (140 loc) · 5.85 KB
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
using SixLabors.ImageSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
namespace ProjectEstrada.Graphics.Core
{
public struct Mesh
{
public List<Vector3> VertexPositions;
public List<Vector3> VertexColors;
public List<Vector3> VertexNormals;
public List<Tuple<short, short, short>> Triangles;
public Mesh(List<Vector3> positions, List<Vector3> colors, List<Vector3> normals, List<Tuple<short, short, short>> tris)
{
VertexPositions = positions;
VertexColors = colors;
VertexNormals = normals;
Triangles = tris;
}
public static Mesh FromObj(Stream stream)
{
var mesh = new Mesh();
var reader = new StreamReader(stream);
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
string[] parts = line.Split(' ');
switch (parts[0])
{
// Geometric vertices
case "v":
mesh.VertexPositions.Add(new Vector3(
float.Parse(parts[1]), float.Parse(parts[2]), float.Parse(parts[3])
));
break;
// Vertex normals
case "vn":
mesh.VertexNormals.Add(new Vector3(
float.Parse(parts[1]), float.Parse(parts[2]), float.Parse(parts[3])
));
break;
// Polygonal face element
case "f":
// For each vertex referenced...
for (int i = 1; i <= 3; i++)
{
string[] vIdxStr = parts[i].Split('/');
if (vIdxStr.Length == 1)
{
mesh.Triangles.Add(new Tuple<short, short, short>(
short.Parse(parts[1]), short.Parse(parts[2]), short.Parse(parts[3])
));
}
else if (vIdxStr.Length == 3)
{
}
}
break;
}
}
return mesh;
}
public static Mesh CreateSquarePlane((float width, float height) size, int subdivisions)
{
var mesh = new Mesh();
int sideResolution = (int)Math.Pow(2, subdivisions);
int verticesPerSide = sideResolution + 1;
if (sideResolution == 1)
{
// This mesh has only two triangles. It's almost certainly faster
// to just hard code it than run through the entire algorithm
mesh.VertexPositions = new List<Vector3>()
{
new Vector3(-size.width, 0f, size.height),
new Vector3( size.width, 0f, size.height),
new Vector3(-size.width, 0f, -size.height),
new Vector3( size.width, 0f, -size.height)
};
mesh.Triangles = new List<Tuple<short, short, short>>()
{
new Tuple<short, short, short>(0, 1, 2),
new Tuple<short, short, short>(1, 2, 3)
};
}
else
{
// Generate the list of vertices
// This loop generates a grid whose outer vertices form a square with radius 1,
// and then scales it up by the requested width and height.
mesh.VertexPositions = new List<Vector3>(verticesPerSide * verticesPerSide);
float delta = 2f / sideResolution;
int radius = sideResolution / 2;
for (int y = radius; y >= -radius; y--)
{
for (int x = -radius; x <= radius; x++)
{
mesh.VertexPositions.Add(new Vector3(
x * delta * size.width, 0, y * delta * size.height
));
}
}
// Create the list of triangles
mesh.Triangles = new List<Tuple<short, short, short>>(sideResolution * sideResolution * 2);
// Loop through the top left corner of each quad, which contains two triangles.
// Ignore the last row and last column of vertices, since they aren't the top
// left corner of triangles.
int numTopLeftCorners = (sideResolution - 1) * (sideResolution - 1);
for (int i = 0; i < numTopLeftCorners; i++)
{
// Top triangle
mesh.Triangles.Add(new Tuple<short, short, short>(
(short)i, (short)(i + 1), (short)(i + verticesPerSide)
));
// Bottom triangle
mesh.Triangles.Add(new Tuple<short, short, short>(
(short)(i + 1), (short)(i + verticesPerSide), (short)(i + verticesPerSide + 1)
));
}
}
return mesh;
}
public static Mesh CreateSquarePlane(Vector3 vectorA, Vector3 vectorB, Vector3 vectorC, Rectangle size)
{
throw new NotImplementedException();
}
public void Extrude(Vector3 direction, float amount)
{
throw new NotImplementedException();
}
public void ExtrudeAlongNormals(float amount)
{
throw new NotImplementedException();
}
public VertexPositionColor[] ToVertexPositionColor()
{
throw new NotImplementedException();
}
}
}