This repository has been archived by the owner on Sep 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Entity.cs
254 lines (235 loc) · 10.6 KB
/
Entity.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
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using Sledge.Common;
using Sledge.DataStructures.GameData;
using Sledge.DataStructures.Geometric;
using Sledge.DataStructures.Transformations;
namespace Sledge.DataStructures.MapObjects
{
public class Entity : MapObject
{
public GameDataObject GameData { get; set; }
public EntityData EntityData { get; set; }
public Coordinate Origin { get; set; }
public ITexture Sprite { get; set; }
public ITexture Decal { get; set; }
public Entity(long id) : base(id)
{
Origin = new Coordinate(0, 0, 0);
}
public override MapObject Clone(IDGenerator generator)
{
var e = new Entity(generator.GetNextObjectID())
{
GameData = GameData,
EntityData = EntityData.Clone(),
Origin = Origin.Clone()
};
CloneBase(e, generator);
return e;
}
public override void Unclone(MapObject o, IDGenerator generator)
{
UncloneBase(o, generator);
var e = o as Entity;
if (e == null) return;
GameData = e.GameData;
Origin = e.Origin.Clone();
EntityData = e.EntityData.Clone();
}
public override void UpdateBoundingBox(bool cascadeToParent = true)
{
if (GameData == null && !Children.Any())
{
var sub = new Coordinate(-16, -16, -16);
var add = new Coordinate(16, 16, 16);
BoundingBox = new Box(Origin + sub, Origin + add);
}
else if (GameData != null && GameData.ClassType == ClassType.Point)
{
var sub = new Coordinate(-16, -16, -16);
var add = new Coordinate(16, 16, 16);
var behav = GameData.Behaviours.SingleOrDefault(x => x.Name == "size");
if (behav != null && behav.Values.Count >= 6)
{
sub = behav.GetCoordinate(0);
add = behav.GetCoordinate(1);
}
else if (GameData.Name == "infodecal")
{
sub = Coordinate.One * -4;
add = Coordinate.One * 4;
}
BoundingBox = new Box(Origin + sub, Origin + add);
}
else if (Children.Any())
{
BoundingBox = new Box(Children.SelectMany(x => new[] {x.BoundingBox.Start, x.BoundingBox.End}));
}
else
{
BoundingBox = new Box(Origin, Origin);
}
base.UpdateBoundingBox(cascadeToParent);
}
public new Color Colour
{
get
{
if (GameData != null && GameData.ClassType == ClassType.Point)
{
var behav = GameData.Behaviours.SingleOrDefault(x => x.Name == "color");
if (behav != null && behav.Values.Count == 3)
{
return behav.GetColour(0);
}
}
return base.Colour;
}
set { base.Colour = value; }
}
public IEnumerable<Face> GetBoxFaces()
{
var faces = new List<Face>();
if (Children.Any()) return faces;
var box = BoundingBox.GetBoxFaces();
foreach (var ca in box)
{
var face = new Face(0)
{
Plane = new Plane(ca[0], ca[1], ca[2]),
Colour = Colour,
IsSelected = IsSelected
};
face.Vertices.AddRange(ca.Select(x => new Vertex(x, face)));
face.UpdateBoundingBox();
faces.Add(face);
}
return faces;
}
private List<Face> _decalGeometry;
public IEnumerable<Face> GetTexturedFaces()
{
return _decalGeometry ?? (_decalGeometry = new List<Face>());
}
public void CalculateDecalGeometry()
{
_decalGeometry = new List<Face>();
if (Decal == null) return; // Texture not found
var boxRadius = Coordinate.One * 4;
// Decals apply to all faces that intersect within an 8x8x8 bounding box
// centered at the origin of the decal
var box = new Box(Origin - boxRadius, Origin + boxRadius);
var root = GetRoot(Parent);
// Get the faces that intersect with the decal's radius
var faces = root.GetAllNodesIntersectingWith(box).OfType<Solid>()
.SelectMany(x => x.Faces).Where(x => x.IntersectsWithBox(box));
var idg = new IDGenerator(); // Dummy generator
foreach (var face in faces)
{
// Project the decal onto the face
var center = face.Plane.Project(Origin);
var decalFace = new Face(idg.GetNextFaceID())
{
Colour = Colour,
IsSelected = IsSelected,
IsHidden = IsCodeHidden,
Plane = face.Plane,
Texture =
{
Name = Decal.Name,
Texture = Decal,
UAxis = face.Texture.UAxis,
VAxis = face.Texture.VAxis,
XScale = face.Texture.XScale,
YScale = face.Texture.YScale,
XShift = -Decal.Width / 2m,
YShift = -Decal.Height / 2m
}
};
// Re-project the vertices in case the texture axes are not on the face plane
var xShift = face.Texture.UAxis * face.Texture.XScale * Decal.Width / 2;
var yShift = face.Texture.VAxis * face.Texture.YScale * Decal.Height / 2;
decalFace.Vertices.Add(new Vertex(face.Plane.Project(center + xShift - yShift), decalFace)); // Bottom Right
decalFace.Vertices.Add(new Vertex(face.Plane.Project(center + xShift + yShift), decalFace)); // Top Right
decalFace.Vertices.Add(new Vertex(face.Plane.Project(center - xShift + yShift), decalFace)); // Top Left
decalFace.Vertices.Add(new Vertex(face.Plane.Project(center - xShift - yShift), decalFace)); // Bottom Left
decalFace.UpdateBoundingBox();
// TODO: verify this covers all situations and I don't have to manually calculate the texture coordinates
decalFace.FitTextureToPointCloud(new Cloud(decalFace.Vertices.Select(x => x.Location)));
// Next, the decal geometry needs to be clipped to the face so it doesn't spill into the void
// Create a fake solid out of the decal geometry and clip it against all the brush planes
var fake = CreateFakeDecalSolid(decalFace);
foreach (var f in face.Parent.Faces.Except(new[] { face }))
{
Solid back, front;
fake.Split(f.Plane, out back, out front, idg);
fake = back ?? fake;
}
// Extract out the original face
decalFace = fake.Faces.First(x => x.Plane.EquivalentTo(face.Plane));
// Add a tiny bit to the normal axis to ensure the decal is rendered in front of the face
var normalAdd = face.Plane.Normal * 0.2m;
decalFace.Transform(new UnitTranslate(normalAdd));
_decalGeometry.Add(decalFace);
}
}
private static Solid CreateFakeDecalSolid(Face face)
{
var s = new Solid(0)
{
Colour = face.Colour,
IsVisgroupHidden = face.IsHidden,
IsSelected = face.IsSelected
};
s.Faces.Add(face);
var p = face.BoundingBox.Center - face.Plane.Normal * 10; // create a new point underneath the face
var p1 = face.Vertices[0].Location;
var p2 = face.Vertices[1].Location;
var p3 = face.Vertices[2].Location;
var p4 = face.Vertices[3].Location;
var faces = new[]
{
new[] { p2, p1, p},
new[] { p3, p2, p},
new[] { p4, p3, p},
new[] { p1, p4, p}
};
foreach (var ff in faces)
{
var f = new Face(-1)
{
Colour = face.Colour,
IsSelected = face.IsSelected,
IsHidden = face.IsHidden,
Plane = new Plane(ff[0], ff[1], ff[2])
};
f.Vertices.AddRange(ff.Select(x => new Vertex(x, f)));
f.UpdateBoundingBox();
s.Faces.Add(f);
}
s.UpdateBoundingBox(false);
return s;
}
public override void Transform(IUnitTransformation transform)
{
Origin = transform.Transform(Origin);
base.Transform(transform);
}
/// <summary>
/// Returns the intersection point closest to the start of the line.
/// </summary>
/// <param name="line">The intersection line</param>
/// <returns>The closest intersecting point, or null if the line doesn't intersect.</returns>
public override Coordinate GetIntersectionPoint(Line line)
{
var faces = GetBoxFaces();
if (_decalGeometry != null) faces = faces.Union(_decalGeometry);
return faces.Select(x => x.GetIntersectionPoint(line))
.Where(x => x != null)
.OrderBy(x => (x - line.Start).VectorMagnitude())
.FirstOrDefault();
}
}
}