Updating lightSource.hullsInRange when a convex hull is removed, some cleanup

This commit is contained in:
Regalis
2016-05-11 20:20:11 +03:00
parent 5e951bc225
commit 5301a57108
4 changed files with 24 additions and 84 deletions
+7 -80
View File
@@ -46,8 +46,7 @@ namespace Barotrauma.Lights
{ {
public static List<ConvexHull> list = new List<ConvexHull>(); public static List<ConvexHull> list = new List<ConvexHull>();
static BasicEffect shadowEffect; static BasicEffect shadowEffect;
//static BasicEffect penumbraEffect;
private Dictionary<LightSource, CachedShadow> cachedShadows; private Dictionary<LightSource, CachedShadow> cachedShadows;
private Vector2[] vertices; private Vector2[] vertices;
@@ -57,7 +56,6 @@ namespace Barotrauma.Lights
private bool[] backFacing; private bool[] backFacing;
private VertexPositionColor[] shadowVertices; private VertexPositionColor[] shadowVertices;
// private VertexPositionTexture[] penumbraVertices;
int shadowVertexCount; int shadowVertexCount;
@@ -191,6 +189,7 @@ namespace Barotrauma.Lights
{ {
foreach (KeyValuePair<LightSource, CachedShadow> cachedShadow in cachedShadows) foreach (KeyValuePair<LightSource, CachedShadow> cachedShadow in cachedShadows)
{ {
cachedShadow.Key.NeedsHullUpdate();
cachedShadow.Value.Dispose(); cachedShadow.Value.Dispose();
} }
cachedShadows.Clear(); cachedShadows.Clear();
@@ -284,56 +283,8 @@ namespace Barotrauma.Lights
svCount += 2; svCount += 2;
currentIndex = (currentIndex + 1) % primitiveCount; currentIndex = (currentIndex + 1) % primitiveCount;
} }
//if (los)
//{
// CalculatePenumbraVertices(startingIndex, endingIndex, lightSourcePos, los);
//}
} }
//private void CalculatePenumbraVertices(int startingIndex, int endingIndex, Vector2 lightSourcePos, bool los)
//{
// 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, LightSource light, Matrix transform, bool los = true) public void DrawShadows(GraphicsDevice graphicsDevice, Camera cam, LightSource light, Matrix transform, bool los = true)
{ {
if (!Enabled) return; if (!Enabled) return;
@@ -343,18 +294,9 @@ namespace Barotrauma.Lights
if (light.Submarine==null && parentEntity != null && parentEntity.Submarine != null) lightSourcePos -= parentEntity.Submarine.Position; if (light.Submarine==null && parentEntity != null && parentEntity.Submarine != null) lightSourcePos -= parentEntity.Submarine.Position;
CachedShadow cachedShadow = null; CachedShadow cachedShadow = null;
if (cachedShadows.TryGetValue(light, out cachedShadow) && if (!cachedShadows.TryGetValue(light, out cachedShadow) ||
(lightSourcePos == cachedShadow.LightPos || Vector2.DistanceSquared(lightSourcePos, cachedShadow.LightPos) < 1.0f)) Vector2.DistanceSquared(lightSourcePos, cachedShadow.LightPos) > 1.0f)
{ {
}
else
{
//if (light.Submarine!=null && parentEntity != null && parentEntity.Submarine == light.Submarine)
//{
// lightPos = light.Position;
//}
CalculateShadowVertices(lightSourcePos, los); CalculateShadowVertices(lightSourcePos, los);
if (cachedShadow != null) if (cachedShadow != null)
@@ -390,8 +332,6 @@ namespace Barotrauma.Lights
private void DrawShadows(GraphicsDevice graphicsDevice, Camera cam, Matrix transform, bool los = true) private void DrawShadows(GraphicsDevice graphicsDevice, Camera cam, Matrix transform, bool los = true)
{ {
Vector3 offset = Vector3.Zero; Vector3 offset = Vector3.Zero;
if (parentEntity != null && parentEntity.Submarine != null) if (parentEntity != null && parentEntity.Submarine != null)
{ {
@@ -411,31 +351,18 @@ namespace Barotrauma.Lights
{ {
shadowEffect.CurrentTechnique.Passes[0].Apply(); shadowEffect.CurrentTechnique.Passes[0].Apply();
graphicsDevice.DrawPrimitives(PrimitiveType.TriangleStrip, 0, shadowVertexCount * 2 - 2); graphicsDevice.DrawPrimitives(PrimitiveType.TriangleStrip, 0, shadowVertexCount * 2 - 2);
} }
} }
// if (los && false)
// {
// penumbraEffect.World = shadowEffect.World;
// penumbraEffect.CurrentTechnique.Passes[0].Apply();
//#if WINDOWS
// graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, 0, 2, VertexPositionTexture.VertexDeclaration);
//#endif
// }
} }
public void Remove() public void Remove()
{ {
ClearCachedShadows(); ClearCachedShadows();
list.Remove(this); list.Remove(this);
} }
} }
} }
+4 -3
View File
@@ -135,8 +135,9 @@ namespace Barotrauma.Lights
foreach (LightSource light in lights) foreach (LightSource light in lights)
{ {
if (!light.hullsInRange.Any() || light.Color.A < 0.01f || light.Range < 1.0f) continue; if (light.Color.A < 0.01f || light.Range < 1.0f) continue;
if (!MathUtils.CircleIntersectsRectangle(light.WorldPosition, light.Range, viewRect)) continue; if (light.hullsInRange == null) light.UpdateHullsInRange();
if (!light.hullsInRange.Any() || !MathUtils.CircleIntersectsRectangle(light.WorldPosition, light.Range, viewRect)) continue;
//clear alpha to 1 //clear alpha to 1
ClearAlphaToOne(graphics, spriteBatch); ClearAlphaToOne(graphics, spriteBatch);
@@ -170,7 +171,7 @@ namespace Barotrauma.Lights
foreach (LightSource light in lights) foreach (LightSource light in lights)
{ {
if (light.hullsInRange.Any() || light.Color.A < 0.01f) continue; if (light.hullsInRange==null || light.hullsInRange.Any() || light.Color.A < 0.01f) continue;
//if (!MathUtils.CircleIntersectsRectangle(light.WorldPosition, light.Range, viewRect)) continue; //if (!MathUtils.CircleIntersectsRectangle(light.WorldPosition, light.Range, viewRect)) continue;
light.Draw(spriteBatch); light.Draw(spriteBatch);
+8 -1
View File
@@ -86,7 +86,7 @@ namespace Barotrauma.Lights
{ {
range = MathHelper.Clamp(value, 0.0f, 2048.0f); range = MathHelper.Clamp(value, 0.0f, 2048.0f);
if (Math.Abs(prevHullUpdateRange - range)<5.0f) return; if (Math.Abs(prevHullUpdateRange - range) < 5.0f) return;
UpdateHullsInRange(); UpdateHullsInRange();
prevHullUpdateRange = range; prevHullUpdateRange = range;
@@ -136,6 +136,8 @@ namespace Barotrauma.Lights
{ {
if (!CastShadows) return; if (!CastShadows) return;
if (hullsInRange == null) hullsInRange = new List<ConvexHull>();
hullsInRange.Clear(); hullsInRange.Clear();
if (range < 1.0f || color.A < 0.01f) return; if (range < 1.0f || color.A < 0.01f) return;
@@ -156,6 +158,11 @@ namespace Barotrauma.Lights
} }
} }
public void NeedsHullUpdate()
{
hullsInRange = null;
}
public void Draw(SpriteBatch spriteBatch) public void Draw(SpriteBatch spriteBatch)
{ {
if (range > 1.0f) if (range > 1.0f)
+5
View File
@@ -282,9 +282,13 @@ namespace Barotrauma
// if there is a gap and we have sections to merge, do it. // if there is a gap and we have sections to merge, do it.
if (section.gap != null) if (section.gap != null)
{
GenerateMergedHull(mergedSections); GenerateMergedHull(mergedSections);
}
else else
{
mergedSections.Add(section); mergedSections.Add(section);
}
} }
// take care of any leftover pieces // take care of any leftover pieces
@@ -323,6 +327,7 @@ namespace Barotrauma
corners[1] = new Vector2(sectionRect.X, sectionRect.Y); corners[1] = new Vector2(sectionRect.X, sectionRect.Y);
corners[2] = new Vector2(sectionRect.Right, sectionRect.Y); corners[2] = new Vector2(sectionRect.Right, sectionRect.Y);
corners[3] = new Vector2(sectionRect.Right, sectionRect.Y - sectionRect.Height); corners[3] = new Vector2(sectionRect.Right, sectionRect.Y - sectionRect.Height);
return corners; return corners;
} }