LinesOfSight: draw outline separately, with a different color

This commit is contained in:
Colin McMillen 2020-03-18 15:56:33 -04:00
parent 2aef0e26f5
commit 90ca6c4b58

View File

@ -14,16 +14,19 @@ namespace SemiColinGames {
readonly VertexBuffer vertexBuffer;
readonly IndexBuffer indexBuffer;
readonly bool[] coneEnabled = new bool[MAX_NPCS];
// coneVertices[i][0] is the eye position; the rest are the edge vertices.
readonly VertexPositionColor[][] coneVertices = new VertexPositionColor[MAX_NPCS][];
// coneFillVertices[i][0] is the eye position; the rest are the edge vertices.
readonly VertexPositionColor[][] coneFillVertices = new VertexPositionColor[MAX_NPCS][];
readonly VertexPositionColor[][] coneOutlineVertices = new VertexPositionColor[MAX_NPCS][];
// The number of total triangles drawn is one less than the number of edge points.
readonly int[] indices = new int[(NUM_EDGE_VERTICES - 1) * 3];
Color color = Color.FromNonPremultiplied(new Vector4(1, 0, 0, 0.25f));
Color fill = Color.FromNonPremultiplied(new Vector4(1, 1, 1, 0.1f));
Color outline = Color.FromNonPremultiplied(new Vector4(1, 0, 0, 0.5f));
public LinesOfSight(GraphicsDevice graphics) {
for (int i = 0; i < MAX_NPCS; i++) {
coneVertices[i] = new VertexPositionColor[NUM_EDGE_VERTICES + 1];
coneFillVertices[i] = new VertexPositionColor[NUM_EDGE_VERTICES + 1];
coneOutlineVertices[i] = new VertexPositionColor[NUM_EDGE_VERTICES + 1];
}
vertexBuffer = new VertexBuffer(
@ -61,7 +64,10 @@ namespace SemiColinGames {
float visionRangeSq = visionRange * visionRange;
float fovStep = fov / (NUM_EDGE_VERTICES - 1);
coneVertices[index][0] = new VertexPositionColor(new Vector3(npc.EyePosition, 0), color);
coneFillVertices[index][0] =
new VertexPositionColor(new Vector3(npc.EyePosition, 0), fill);
coneOutlineVertices[index][0] =
new VertexPositionColor(new Vector3(npc.EyePosition, 0), outline);
for (int i = 0; i < NUM_EDGE_VERTICES; i++) {
float angle = -fov / 2 + fovStep * i;
Vector2 rotated = ray.Rotate(angle);
@ -87,8 +93,10 @@ namespace SemiColinGames {
}
}
coneVertices[index][i + 1] = new VertexPositionColor(
new Vector3((int) closestHit.X, (int) closestHit.Y, 0), color);
coneFillVertices[index][i + 1] = new VertexPositionColor(
new Vector3((int) closestHit.X, (int) closestHit.Y, 0), fill);
coneOutlineVertices[index][i + 1] = new VertexPositionColor(
new Vector3((int) closestHit.X, (int) closestHit.Y, 0), outline);
}
}
@ -104,7 +112,7 @@ namespace SemiColinGames {
if (!coneEnabled[npcIndex]) {
continue;
}
vertexBuffer.SetData(coneVertices[npcIndex]);
vertexBuffer.SetData(coneFillVertices[npcIndex]);
indexBuffer.SetData(indices);
graphics.SetVertexBuffer(vertexBuffer);
graphics.Indices = indexBuffer;
@ -125,18 +133,15 @@ namespace SemiColinGames {
if (!coneEnabled[npcIndex]) {
continue;
}
vertexBuffer.SetData(coneVertices[npcIndex]);
vertexBuffer.SetData(coneOutlineVertices[npcIndex]);
indexBuffer.SetData(indices);
graphics.SetVertexBuffer(vertexBuffer);
graphics.Indices = indexBuffer;
foreach (EffectPass pass in lightingEffect.CurrentTechnique.Passes) {
pass.Apply();
// TODO: just draw a single opaque outline.
for (int i = 0; i < 4; i++) {
graphics.DrawIndexedPrimitives(
PrimitiveType.LineStrip, 0, 0, NUM_EDGE_VERTICES + 1);
}
graphics.DrawIndexedPrimitives(
PrimitiveType.LineStrip, 0, 0, NUM_EDGE_VERTICES + 1);
}
}
}