/
BoxMesh.cs
194 lines (173 loc) · 7.88 KB
/
BoxMesh.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
using DeltaEngine.Content;
using DeltaEngine.Datatypes;
using DeltaEngine.Graphics;
using DeltaEngine.Graphics.Vertices;
namespace DeltaEngine.Rendering3D.Shapes
{
/// <summary>
/// Entity 3D representing a simple box.
/// </summary>
public class BoxMesh : Mesh
{
public BoxMesh(Vector3D size, Color color)
: this(size, new Material(ShaderFlags.Colored, "") { DefaultColor = color }) {}
public BoxMesh(Vector3D size, Material material)
: base(CreateGeometry(size, material), material)
{
this.size = size;
}
private static Geometry CreateGeometry(Vector3D size, Material material)
{
var isColored = material.Shader.Flags == ShaderFlags.Colored;
var format = isColored
? VertexFormat.Position3DColor
: material.Shader.Flags == ShaderFlags.LitTextured
? VertexFormat.Position3DNormalUV : VertexFormat.Position3DUV;
var creationData = new GeometryCreationData(format, isColored ? 8 : 24, 36);
var geometry = ContentLoader.Create<Geometry>(creationData);
ComputeVertices(size, material, geometry);
return geometry;
}
private static void ComputeVertices(Vector3D size, Material material, Geometry geometry)
{
if (material.Shader.Flags == ShaderFlags.Colored)
geometry.SetData(ComputeColorVertices(size, material.DefaultColor), ColorBoxIndices);
else if (material.Shader.Flags == ShaderFlags.LitTextured)
geometry.SetData(ComputeNormalUVVertices(size), UVBoxIndices);
else
geometry.SetData(ComputeUVVertices(size), UVBoxIndices);
}
private static Vertex[] ComputeColorVertices(Vector3D size, Color color)
{
float x1 = -size.X / 2.0f;
float x2 = size.X / 2.0f;
float y1 = -size.Y / 2.0f;
float y2 = size.Y / 2.0f;
float z1 = -size.Z / 2.0f;
float z2 = size.Z / 2.0f;
var vertices = new Vertex[]
{
new VertexPosition3DColor(new Vector3D(x1, y2, z1), color),
new VertexPosition3DColor(new Vector3D(x2, y2, z1), color),
new VertexPosition3DColor(new Vector3D(x1, y1, z1), color),
new VertexPosition3DColor(new Vector3D(x2, y1, z1), color),
new VertexPosition3DColor(new Vector3D(x2, y2, z2), color),
new VertexPosition3DColor(new Vector3D(x1, y2, z2), color),
new VertexPosition3DColor(new Vector3D(x2, y1, z2), color),
new VertexPosition3DColor(new Vector3D(x1, y1, z2), color)
};
return vertices;
}
public static readonly short[] ColorBoxIndices =
{
0, 1, 2, 2, 1, 3,
4, 5, 6, 6, 5, 7,
5, 0, 7, 7, 0, 2,
1, 4, 3, 3, 4, 6,
5, 4, 0, 0, 4, 1,
6, 7, 3, 3, 7, 2
};
private static Vertex[] ComputeNormalUVVertices(Vector3D size)
{
float x1 = -size.X / 2.0f;
float x2 = size.X / 2.0f;
float y1 = -size.Y / 2.0f;
float y2 = size.Y / 2.0f;
float z1 = -size.Z / 2.0f;
float z2 = size.Z / 2.0f;
var vertices = new Vertex[]
{
new VertexPosition3DNormalUV(new Vector3D(x1, y2, z1), -Vector3D.UnitZ, new Vector2D(0, 1)),
new VertexPosition3DNormalUV(new Vector3D(x2, y2, z1), -Vector3D.UnitZ, new Vector2D(1, 1)),
new VertexPosition3DNormalUV(new Vector3D(x1, y1, z1), -Vector3D.UnitZ, new Vector2D(0, 0)),
new VertexPosition3DNormalUV(new Vector3D(x2, y1, z1), -Vector3D.UnitZ, new Vector2D(1, 0)),
new VertexPosition3DNormalUV(new Vector3D(x2, y2, z2), Vector3D.UnitZ, new Vector2D(1, 1)),
new VertexPosition3DNormalUV(new Vector3D(x1, y2, z2), Vector3D.UnitZ, new Vector2D(0, 1)),
new VertexPosition3DNormalUV(new Vector3D(x2, y1, z2), Vector3D.UnitZ, new Vector2D(1, 0)),
new VertexPosition3DNormalUV(new Vector3D(x1, y1, z2), Vector3D.UnitZ, new Vector2D(0, 0)),
new VertexPosition3DNormalUV(new Vector3D(x1, y2, z2), Vector3D.UnitY, new Vector2D(1, 0)),
new VertexPosition3DNormalUV(new Vector3D(x2, y2, z2), Vector3D.UnitY, new Vector2D(0, 0)),
new VertexPosition3DNormalUV(new Vector3D(x1, y2, z1), Vector3D.UnitY, new Vector2D(1, 1)),
new VertexPosition3DNormalUV(new Vector3D(x2, y2, z1), Vector3D.UnitY, new Vector2D(0, 1)),
new VertexPosition3DNormalUV(new Vector3D(x2, y1, z2), -Vector3D.UnitY, new Vector2D(1, 0)),
new VertexPosition3DNormalUV(new Vector3D(x1, y1, z2), -Vector3D.UnitY, new Vector2D(0, 0)),
new VertexPosition3DNormalUV(new Vector3D(x2, y1, z1), -Vector3D.UnitY, new Vector2D(1, 1)),
new VertexPosition3DNormalUV(new Vector3D(x1, y1, z1), -Vector3D.UnitY, new Vector2D(0, 1)),
new VertexPosition3DNormalUV(new Vector3D(x1, y1, z2), -Vector3D.UnitX, new Vector2D(1, 0)),
new VertexPosition3DNormalUV(new Vector3D(x1, y2, z2), -Vector3D.UnitX, new Vector2D(0, 0)),
new VertexPosition3DNormalUV(new Vector3D(x1, y1, z1), -Vector3D.UnitX, new Vector2D(1, 1)),
new VertexPosition3DNormalUV(new Vector3D(x1, y2, z1), -Vector3D.UnitX, new Vector2D(0, 1)),
new VertexPosition3DNormalUV(new Vector3D(x2, y2, z2), Vector3D.UnitX, new Vector2D(1, 0)),
new VertexPosition3DNormalUV(new Vector3D(x2, y1, z2), Vector3D.UnitX, new Vector2D(0, 0)),
new VertexPosition3DNormalUV(new Vector3D(x2, y2, z1), Vector3D.UnitX, new Vector2D(1, 1)),
new VertexPosition3DNormalUV(new Vector3D(x2, y1, z1), Vector3D.UnitX, new Vector2D(0, 1))
};
return vertices;
}
private static Vertex[] ComputeUVVertices(Vector3D size)
{
float x1 = -size.X / 2.0f;
float x2 = size.X / 2.0f;
float y1 = -size.Y / 2.0f;
float y2 = size.Y / 2.0f;
float z1 = -size.Z / 2.0f;
float z2 = size.Z / 2.0f;
var vertices = new Vertex[]
{
new VertexPosition3DUV(new Vector3D(x1, y2, z1), new Vector2D(0, 1)),
new VertexPosition3DUV(new Vector3D(x2, y2, z1), new Vector2D(1, 1)),
new VertexPosition3DUV(new Vector3D(x1, y1, z1), new Vector2D(0, 0)),
new VertexPosition3DUV(new Vector3D(x2, y1, z1), new Vector2D(1, 0)),
new VertexPosition3DUV(new Vector3D(x2, y2, z2), new Vector2D(1, 1)),
new VertexPosition3DUV(new Vector3D(x1, y2, z2), new Vector2D(0, 1)),
new VertexPosition3DUV(new Vector3D(x2, y1, z2), new Vector2D(1, 0)),
new VertexPosition3DUV(new Vector3D(x1, y1, z2), new Vector2D(0, 0)),
new VertexPosition3DUV(new Vector3D(x1, y2, z2), new Vector2D(1, 0)),
new VertexPosition3DUV(new Vector3D(x2, y2, z2), new Vector2D(0, 0)),
new VertexPosition3DUV(new Vector3D(x1, y2, z1), new Vector2D(1, 1)),
new VertexPosition3DUV(new Vector3D(x2, y2, z1), new Vector2D(0, 1)),
new VertexPosition3DUV(new Vector3D(x2, y1, z2), new Vector2D(1, 0)),
new VertexPosition3DUV(new Vector3D(x1, y1, z2), new Vector2D(0, 0)),
new VertexPosition3DUV(new Vector3D(x2, y1, z1), new Vector2D(1, 1)),
new VertexPosition3DUV(new Vector3D(x1, y1, z1), new Vector2D(0, 1)),
new VertexPosition3DUV(new Vector3D(x1, y1, z2), new Vector2D(1, 0)),
new VertexPosition3DUV(new Vector3D(x1, y2, z2), new Vector2D(0, 0)),
new VertexPosition3DUV(new Vector3D(x1, y1, z1), new Vector2D(1, 1)),
new VertexPosition3DUV(new Vector3D(x1, y2, z1), new Vector2D(0, 1)),
new VertexPosition3DUV(new Vector3D(x2, y2, z2), new Vector2D(1, 0)),
new VertexPosition3DUV(new Vector3D(x2, y1, z2), new Vector2D(0, 0)),
new VertexPosition3DUV(new Vector3D(x2, y2, z1), new Vector2D(1, 1)),
new VertexPosition3DUV(new Vector3D(x2, y1, z1), new Vector2D(0, 1))
};
return vertices;
}
public static readonly short[] UVBoxIndices =
{
0+0, 0+1, 0+2, 0+2, 0+1, 0+3,
4+0, 4+1, 4+2, 4+2, 4+1, 4+3,
8+0, 8+1, 8+2, 8+2, 8+1, 8+3,
12+0, 12+1, 12+2, 12+2, 12+1, 12+3,
16+0, 16+1, 16+2, 16+2, 16+1, 16+3,
20+0, 20+1, 20+2, 20+2, 20+1, 20+3,
};
public Vector3D Size
{
get { return size; }
set
{
size = value;
ComputeVertices(size, Material, Geometry);
}
}
private Vector3D size;
public Color Color
{
get { return Material.DefaultColor; }
set
{
Material.DefaultColor = value;
ComputeVertices(size, Material, Geometry);
}
}
}
}