misc optimization & refactoring
This commit is contained in:
@@ -73,7 +73,7 @@ namespace Subsurface
|
||||
FindHulls();
|
||||
|
||||
GapList.Add(this);
|
||||
mapEntityList.Add(this);
|
||||
InsertToList();
|
||||
}
|
||||
|
||||
public static void UpdateHulls()
|
||||
|
||||
@@ -146,8 +146,7 @@ namespace Subsurface
|
||||
|
||||
Volume = 0.0f;
|
||||
|
||||
//add to list of entities as well
|
||||
mapEntityList.Add(this);
|
||||
InsertToList();
|
||||
}
|
||||
|
||||
public override bool Contains(Vector2 position)
|
||||
|
||||
@@ -10,18 +10,17 @@ namespace Subsurface.Lights
|
||||
public static List<ConvexHull> list = new List<ConvexHull>();
|
||||
static BasicEffect shadowEffect;
|
||||
static BasicEffect penumbraEffect;
|
||||
|
||||
private static VertexPositionTexture[] penumbraVertices;
|
||||
|
||||
private VertexPositionColor[] vertices;
|
||||
private short[] indices;
|
||||
int primitiveCount;
|
||||
|
||||
private Vector2[] vertices;
|
||||
private int primitiveCount;
|
||||
|
||||
bool[] backFacing;
|
||||
VertexPositionColor[] shadowVertices;
|
||||
private bool[] backFacing;
|
||||
|
||||
private VertexPositionColor[] shadowVertices;
|
||||
private VertexPositionTexture[] penumbraVertices;
|
||||
|
||||
private Rectangle boundingBox;
|
||||
|
||||
|
||||
public bool Enabled
|
||||
{
|
||||
get;
|
||||
@@ -48,86 +47,77 @@ namespace Subsurface.Lights
|
||||
penumbraEffect.LightingEnabled = false;
|
||||
penumbraEffect.Texture = TextureLoader.FromFile("Content/Lights/penumbra.png");
|
||||
}
|
||||
|
||||
vertices = points;
|
||||
primitiveCount = vertices.Length;
|
||||
|
||||
if (penumbraVertices==null)
|
||||
{
|
||||
penumbraVertices = new VertexPositionTexture[6];
|
||||
}
|
||||
CalculateDimensions();
|
||||
//indices = new short[primitiveCount * 3];
|
||||
|
||||
int vertexCount = points.Length;
|
||||
vertices = new VertexPositionColor[vertexCount + 1];
|
||||
Vector2 center = Vector2.Zero;
|
||||
|
||||
float? minX = null, minY = null, maxX = null, maxY = null;
|
||||
|
||||
for (int i = 0; i < vertexCount; i++)
|
||||
{
|
||||
vertices[i] = new VertexPositionColor(new Vector3(points[i], 0), color);
|
||||
center += points[i];
|
||||
|
||||
if (minX == null || points[i].X < minX) minX = points[i].X;
|
||||
if (minY == null || points[i].Y < minY) minY = points[i].Y;
|
||||
|
||||
if (maxX == null || points[i].X > maxX) maxX = points[i].X;
|
||||
if (maxY == null || points[i].Y > minY) maxY = points[i].Y;
|
||||
}
|
||||
center /= points.Length;
|
||||
vertices[vertexCount] = new VertexPositionColor(new Vector3(center, 0), color);
|
||||
|
||||
boundingBox = new Rectangle((int)minX, (int)minY, (int)(maxX-minX), (int)(maxY-minY));
|
||||
|
||||
primitiveCount = points.Length;
|
||||
indices = new short[primitiveCount * 3];
|
||||
|
||||
for (int i = 0; i < primitiveCount; i++)
|
||||
{
|
||||
indices[3 * i] = (short)i;
|
||||
indices[3 * i + 1] = (short)((i + 1) % vertexCount);
|
||||
indices[3 * i + 2] = (short)vertexCount;
|
||||
}
|
||||
backFacing = new bool[vertexCount];
|
||||
//for (int i = 0; i < primitiveCount; i++)
|
||||
//{
|
||||
// indices[3 * i] = (short)i;
|
||||
// indices[3 * i + 1] = (short)((i + 1) % vertexCount);
|
||||
// indices[3 * i + 2] = (short)vertexCount;
|
||||
//}
|
||||
backFacing = new bool[primitiveCount];
|
||||
|
||||
Enabled = true;
|
||||
|
||||
list.Add(this);
|
||||
}
|
||||
|
||||
private void CalculateDimensions()
|
||||
{
|
||||
Vector2 center = Vector2.Zero;
|
||||
|
||||
float? minX = null, minY = null, maxX = null, maxY = null;
|
||||
|
||||
for (int i = 0; i < vertices.Length; i++)
|
||||
{
|
||||
center += vertices[i];
|
||||
|
||||
if (minX == null || vertices[i].X < minX) minX = vertices[i].X;
|
||||
if (minY == null || vertices[i].Y < minY) minY = vertices[i].Y;
|
||||
|
||||
if (maxX == null || vertices[i].X > maxX) maxX = vertices[i].X;
|
||||
if (maxY == null || vertices[i].Y > minY) maxY = vertices[i].Y;
|
||||
}
|
||||
center /= vertices.Length;
|
||||
|
||||
boundingBox = new Rectangle((int)minX, (int)minY, (int)(maxX - minX), (int)(maxY - minY));
|
||||
}
|
||||
|
||||
public void Move(Vector2 amount)
|
||||
{
|
||||
for (int i = 0; i < vertices.Count(); i++)
|
||||
{
|
||||
vertices[i].Position = new Vector3(vertices[i].Position.X + amount.X, vertices[i].Position.Y + amount.Y, vertices[i].Position.Z);
|
||||
vertices[i] += amount;
|
||||
}
|
||||
|
||||
CalculateDimensions();
|
||||
}
|
||||
|
||||
public void SetVertices(Vector2[] points)
|
||||
{
|
||||
int vertexCount = points.Length;
|
||||
vertices = new VertexPositionColor[vertexCount + 1];
|
||||
|
||||
for (int i = 0; i < vertexCount; i++)
|
||||
{
|
||||
vertices[i] = new VertexPositionColor(new Vector3(points[i], 0), Color.Black);
|
||||
}
|
||||
vertices = points;
|
||||
}
|
||||
|
||||
public void DrawShadows(GraphicsDevice graphicsDevice, Camera cam, Vector2 lightSourcePos, Matrix transform, bool los = true)
|
||||
private void CalculateShadowVertices(Vector2 lightSourcePos, bool los = true)
|
||||
{
|
||||
if (!Enabled) return;
|
||||
|
||||
//compute facing of each edge, using N*L
|
||||
for (int i = 0; i < primitiveCount; i++)
|
||||
{
|
||||
Vector2 firstVertex = new Vector2(vertices[i].Position.X, vertices[i].Position.Y);
|
||||
Vector2 firstVertex = new Vector2(vertices[i].X, vertices[i].Y);
|
||||
int secondIndex = (i + 1) % primitiveCount;
|
||||
Vector2 secondVertex = new Vector2(vertices[secondIndex].Position.X, vertices[secondIndex].Position.Y);
|
||||
Vector2 secondVertex = new Vector2(vertices[secondIndex].X, vertices[secondIndex].Y);
|
||||
Vector2 middle = (firstVertex + secondVertex) / 2;
|
||||
|
||||
Vector2 L = lightSourcePos - middle;
|
||||
|
||||
Vector2 N = new Vector2();
|
||||
N.X = -(secondVertex.Y - firstVertex.Y);
|
||||
N.Y = secondVertex.X - firstVertex.X;
|
||||
Vector2 N = new Vector2(
|
||||
-(secondVertex.Y - firstVertex.Y),
|
||||
secondVertex.X - firstVertex.X);
|
||||
|
||||
backFacing[i] = (Vector2.Dot(N, L) < 0);
|
||||
}
|
||||
@@ -148,49 +138,6 @@ namespace Subsurface.Lights
|
||||
startingIndex = nextEdge;
|
||||
}
|
||||
|
||||
if (los)
|
||||
{
|
||||
for (int n = 0; n < 4; n+=3)
|
||||
{
|
||||
Vector3 penumbraStart = (n == 0) ? vertices[startingIndex].Position : vertices[endingIndex].Position;
|
||||
|
||||
penumbraVertices[n] = new VertexPositionTexture();
|
||||
penumbraVertices[n].Position = penumbraStart;
|
||||
penumbraVertices[n].TextureCoordinate = new Vector2(0.0f, 1.0f);
|
||||
//penumbraVertices[0].te = fow ? Color.Black : Color.Transparent;
|
||||
|
||||
for (int i = 0; i < 2; i++ )
|
||||
{
|
||||
penumbraVertices[n + i + 1] = new VertexPositionTexture();
|
||||
Vector3 vertexDir = penumbraStart - new Vector3(lightSourcePos, 0);
|
||||
vertexDir.Normalize();
|
||||
|
||||
Vector3 normal = (i == 0) ? new Vector3(-vertexDir.Y, vertexDir.X, 0.0f) : new Vector3(vertexDir.Y, -vertexDir.X, 0.0f)*0.05f;
|
||||
if (n > 0) normal = -normal;
|
||||
vertexDir = penumbraStart - (new Vector3(lightSourcePos, 0) - normal * 20.0f);
|
||||
vertexDir.Normalize();
|
||||
penumbraVertices[n + i + 1].Position = new Vector3(lightSourcePos, 0) + vertexDir * 9000;
|
||||
|
||||
if (los)
|
||||
{
|
||||
penumbraVertices[n + i + 1].TextureCoordinate = (i == 0) ? new Vector2(0.05f, 0.0f) : new Vector2(1.0f, 0.0f);
|
||||
}
|
||||
else
|
||||
{
|
||||
penumbraVertices[n + i + 1].TextureCoordinate = (i == 0) ? new Vector2(1.0f, 0.0f) : Vector2.Zero;
|
||||
}
|
||||
}
|
||||
|
||||
if (n > 0)
|
||||
{
|
||||
var temp = penumbraVertices[4];
|
||||
penumbraVertices[4] = penumbraVertices[5];
|
||||
penumbraVertices[5] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int shadowVertexCount;
|
||||
|
||||
//nr of vertices that are in the shadow
|
||||
@@ -206,7 +153,7 @@ namespace Subsurface.Lights
|
||||
int svCount = 0;
|
||||
while (svCount != shadowVertexCount * 2)
|
||||
{
|
||||
Vector3 vertexPos = vertices[currentIndex].Position;
|
||||
Vector3 vertexPos = new Vector3(vertices[currentIndex], 0.0f);
|
||||
|
||||
//one vertex on the hull
|
||||
shadowVertices[svCount] = new VertexPositionColor();
|
||||
@@ -224,10 +171,68 @@ namespace Subsurface.Lights
|
||||
currentIndex = (currentIndex + 1) % primitiveCount;
|
||||
}
|
||||
|
||||
if (los)
|
||||
{
|
||||
CalculatePenumbraVertices(startingIndex, endingIndex, lightSourcePos, los);
|
||||
}
|
||||
}
|
||||
|
||||
private void CalculatePenumbraVertices(int startingIndex, int endingIndex, Vector2 lightSourcePos, bool los)
|
||||
{
|
||||
penumbraVertices = new VertexPositionTexture[6];
|
||||
|
||||
for (int n = 0; n < 4; n += 3)
|
||||
{
|
||||
Vector3 penumbraStart = new Vector3((n == 0) ? vertices[startingIndex] : vertices[endingIndex], 0.0f);
|
||||
|
||||
penumbraVertices[n] = new VertexPositionTexture();
|
||||
penumbraVertices[n].Position = penumbraStart;
|
||||
penumbraVertices[n].TextureCoordinate = new Vector2(0.0f, 1.0f);
|
||||
//penumbraVertices[0].te = fow ? Color.Black : Color.Transparent;
|
||||
|
||||
for (int i = 0; i < 2; i++)
|
||||
{
|
||||
penumbraVertices[n + i + 1] = new VertexPositionTexture();
|
||||
Vector3 vertexDir = penumbraStart - new Vector3(lightSourcePos, 0);
|
||||
vertexDir.Normalize();
|
||||
|
||||
Vector3 normal = (i == 0) ? new Vector3(-vertexDir.Y, vertexDir.X, 0.0f) : new Vector3(vertexDir.Y, -vertexDir.X, 0.0f) * 0.05f;
|
||||
if (n > 0) normal = -normal;
|
||||
|
||||
vertexDir = penumbraStart - (new Vector3(lightSourcePos, 0) - normal * 20.0f);
|
||||
vertexDir.Normalize();
|
||||
penumbraVertices[n + i + 1].Position = new Vector3(lightSourcePos, 0) + vertexDir * 9000;
|
||||
|
||||
if (los)
|
||||
{
|
||||
penumbraVertices[n + i + 1].TextureCoordinate = (i == 0) ? new Vector2(0.05f, 0.0f) : new Vector2(1.0f, 0.0f);
|
||||
}
|
||||
else
|
||||
{
|
||||
penumbraVertices[n + i + 1].TextureCoordinate = (i == 0) ? new Vector2(1.0f, 0.0f) : Vector2.Zero;
|
||||
}
|
||||
}
|
||||
|
||||
if (n > 0)
|
||||
{
|
||||
var temp = penumbraVertices[4];
|
||||
penumbraVertices[4] = penumbraVertices[5];
|
||||
penumbraVertices[5] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void DrawShadows(GraphicsDevice graphicsDevice, Camera cam, Vector2 lightSourcePos, Matrix transform, bool los = true)
|
||||
{
|
||||
if (!Enabled) return;
|
||||
|
||||
CalculateShadowVertices(lightSourcePos, los);
|
||||
|
||||
|
||||
shadowEffect.World = transform;
|
||||
shadowEffect.CurrentTechnique.Passes[0].Apply();
|
||||
|
||||
graphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleStrip, shadowVertices, 0, shadowVertexCount * 2 - 2);
|
||||
graphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleStrip, shadowVertices, 0, shadowVertices.Length - 2);
|
||||
|
||||
if (los)
|
||||
{
|
||||
|
||||
@@ -72,7 +72,7 @@ namespace Subsurface
|
||||
set { rect = value; }
|
||||
}
|
||||
|
||||
public virtual Sprite sprite
|
||||
public virtual Sprite Sprite
|
||||
{
|
||||
get { return null; }
|
||||
}
|
||||
@@ -150,6 +150,28 @@ namespace Subsurface
|
||||
return (Submarine.RectContains(rect, position));
|
||||
}
|
||||
|
||||
protected void InsertToList()
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
if (Sprite==null)
|
||||
{
|
||||
mapEntityList.Add(this);
|
||||
return;
|
||||
}
|
||||
|
||||
while (i<mapEntityList.Count)
|
||||
{
|
||||
i++;
|
||||
|
||||
Sprite existingSprite = mapEntityList[i-1].Sprite;
|
||||
if (existingSprite == null) continue;
|
||||
if (existingSprite.Texture == this.Sprite.Texture) break;
|
||||
}
|
||||
|
||||
mapEntityList.Insert(i, this);
|
||||
}
|
||||
|
||||
public virtual void Draw(SpriteBatch spriteBatch, bool editing) {}
|
||||
|
||||
public override void Remove()
|
||||
@@ -227,8 +249,8 @@ namespace Subsurface
|
||||
|
||||
foreach (MapEntity e in mapEntityList)
|
||||
{
|
||||
if (highLightedEntity == null || e.sprite == null ||
|
||||
(highLightedEntity.sprite!=null && e.sprite.Depth < highLightedEntity.sprite.Depth))
|
||||
if (highLightedEntity == null || e.Sprite == null ||
|
||||
(highLightedEntity.Sprite!=null && e.Sprite.Depth < highLightedEntity.Sprite.Depth))
|
||||
{
|
||||
if (e.Contains(position)) highLightedEntity = e;
|
||||
}
|
||||
@@ -448,7 +470,7 @@ namespace Subsurface
|
||||
/// Has to be done after all the entities have been loaded (an entity can't
|
||||
/// be linked to some other entity that hasn't been loaded yet)
|
||||
/// </summary>
|
||||
public static void LinkAll()
|
||||
public static void OnMapLoaded()
|
||||
{
|
||||
foreach (MapEntity e in mapEntityList)
|
||||
{
|
||||
@@ -465,7 +487,15 @@ namespace Subsurface
|
||||
e.linkedTo.Add(linked);
|
||||
}
|
||||
}
|
||||
|
||||
//mapEntityList.Sort((x, y) =>
|
||||
//{
|
||||
// return x.Name.CompareTo(y.Name);
|
||||
//});
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void RemoveLinked(MapEntity e)
|
||||
{
|
||||
|
||||
@@ -57,7 +57,7 @@ namespace Subsurface
|
||||
|
||||
bool isHorizontal;
|
||||
|
||||
public override Sprite sprite
|
||||
public override Sprite Sprite
|
||||
{
|
||||
get { return prefab.sprite; }
|
||||
}
|
||||
@@ -265,7 +265,7 @@ namespace Subsurface
|
||||
convexHull = new ConvexHull(corners, Color.Black);
|
||||
}
|
||||
|
||||
mapEntityList.Add(this);
|
||||
InsertToList();
|
||||
}
|
||||
|
||||
public override void Remove()
|
||||
@@ -290,19 +290,22 @@ namespace Subsurface
|
||||
|
||||
Color color = (isHighlighted) ? Color.Green : Color.White;
|
||||
if (isSelected && editing) color = Color.Red;
|
||||
|
||||
prefab.sprite.DrawTiled(spriteBatch, new Vector2(rect.X, -rect.Y), new Vector2(rect.Width, rect.Height), Vector2.Zero, color);
|
||||
|
||||
prefab.sprite.DrawTiled(spriteBatch, new Vector2(rect.X, -rect.Y), new Vector2(rect.Width, rect.Height), Vector2.Zero, color);
|
||||
|
||||
foreach (WallSection s in sections)
|
||||
{
|
||||
if (s.isHighLighted)
|
||||
{
|
||||
GUI.DrawRectangle(spriteBatch,
|
||||
new Rectangle((int)s.rect.X, (int)-s.rect.Y, (int)s.rect.Width, (int)s.rect.Height),
|
||||
new Color((s.damage / prefab.MaxHealth), 1.0f - (s.damage / prefab.MaxHealth), 0.0f, 1.0f), true);
|
||||
}
|
||||
|
||||
|
||||
s.isHighLighted = false;
|
||||
|
||||
if (s.damage == 0.0f) continue;
|
||||
if (s.damage < 0.01f) continue;
|
||||
|
||||
GUI.DrawRectangle(spriteBatch,
|
||||
new Rectangle((int)s.rect.X, (int)-s.rect.Y, (int)s.rect.Width, (int)s.rect.Height),
|
||||
|
||||
@@ -159,7 +159,7 @@ namespace Subsurface
|
||||
{
|
||||
for (int i = 0; i < MapEntity.mapEntityList.Count(); i++)
|
||||
{
|
||||
if (MapEntity.mapEntityList[i].sprite == null || MapEntity.mapEntityList[i].sprite.Depth < 0.5f)
|
||||
if (MapEntity.mapEntityList[i].Sprite == null || MapEntity.mapEntityList[i].Sprite.Depth < 0.5f)
|
||||
MapEntity.mapEntityList[i].Draw(spriteBatch, editing);
|
||||
}
|
||||
|
||||
@@ -181,7 +181,7 @@ namespace Subsurface
|
||||
{
|
||||
for (int i = 0; i < MapEntity.mapEntityList.Count(); i++)
|
||||
{
|
||||
if (MapEntity.mapEntityList[i].sprite == null || MapEntity.mapEntityList[i].sprite.Depth >= 0.5f)
|
||||
if (MapEntity.mapEntityList[i].Sprite == null || MapEntity.mapEntityList[i].Sprite.Depth >= 0.5f)
|
||||
MapEntity.mapEntityList[i].Draw(spriteBatch, editing);
|
||||
}
|
||||
}
|
||||
@@ -603,7 +603,7 @@ namespace Subsurface
|
||||
|
||||
subBody = new SubmarineBody(this);
|
||||
|
||||
MapEntity.LinkAll();
|
||||
MapEntity.OnMapLoaded();
|
||||
|
||||
foreach (Item item in Item.itemList)
|
||||
{
|
||||
|
||||
@@ -48,7 +48,7 @@ namespace Subsurface
|
||||
linkedTo = new ObservableCollection<MapEntity>();
|
||||
idCardTags = new string[0];
|
||||
|
||||
mapEntityList.Add(this);
|
||||
InsertToList();
|
||||
WayPointList.Add(this);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user