From 5301a571087a6ac918adaa359dcb62ebc7509b61 Mon Sep 17 00:00:00 2001 From: Regalis Date: Wed, 11 May 2016 20:20:11 +0300 Subject: [PATCH] Updating lightSource.hullsInRange when a convex hull is removed, some cleanup --- Subsurface/Source/Map/Lights/ConvexHull.cs | 87 ++------------------ Subsurface/Source/Map/Lights/LightManager.cs | 7 +- Subsurface/Source/Map/Lights/LightSource.cs | 9 +- Subsurface/Source/Map/Structure.cs | 5 ++ 4 files changed, 24 insertions(+), 84 deletions(-) diff --git a/Subsurface/Source/Map/Lights/ConvexHull.cs b/Subsurface/Source/Map/Lights/ConvexHull.cs index 0866532e9..bc37d6308 100644 --- a/Subsurface/Source/Map/Lights/ConvexHull.cs +++ b/Subsurface/Source/Map/Lights/ConvexHull.cs @@ -46,8 +46,7 @@ namespace Barotrauma.Lights { public static List list = new List(); static BasicEffect shadowEffect; - //static BasicEffect penumbraEffect; - + private Dictionary cachedShadows; private Vector2[] vertices; @@ -57,7 +56,6 @@ namespace Barotrauma.Lights private bool[] backFacing; private VertexPositionColor[] shadowVertices; - // private VertexPositionTexture[] penumbraVertices; int shadowVertexCount; @@ -191,6 +189,7 @@ namespace Barotrauma.Lights { foreach (KeyValuePair cachedShadow in cachedShadows) { + cachedShadow.Key.NeedsHullUpdate(); cachedShadow.Value.Dispose(); } cachedShadows.Clear(); @@ -284,56 +283,8 @@ namespace Barotrauma.Lights svCount += 2; 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) { if (!Enabled) return; @@ -343,18 +294,9 @@ namespace Barotrauma.Lights if (light.Submarine==null && parentEntity != null && parentEntity.Submarine != null) lightSourcePos -= parentEntity.Submarine.Position; CachedShadow cachedShadow = null; - if (cachedShadows.TryGetValue(light, out cachedShadow) && - (lightSourcePos == cachedShadow.LightPos || Vector2.DistanceSquared(lightSourcePos, cachedShadow.LightPos) < 1.0f)) + if (!cachedShadows.TryGetValue(light, out cachedShadow) || + Vector2.DistanceSquared(lightSourcePos, cachedShadow.LightPos) > 1.0f) { - - } - else - { - //if (light.Submarine!=null && parentEntity != null && parentEntity.Submarine == light.Submarine) - //{ - // lightPos = light.Position; - //} - CalculateShadowVertices(lightSourcePos, los); if (cachedShadow != null) @@ -390,8 +332,6 @@ namespace Barotrauma.Lights private void DrawShadows(GraphicsDevice graphicsDevice, Camera cam, Matrix transform, bool los = true) { - - Vector3 offset = Vector3.Zero; if (parentEntity != null && parentEntity.Submarine != null) { @@ -411,31 +351,18 @@ namespace Barotrauma.Lights { shadowEffect.CurrentTechnique.Passes[0].Apply(); 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() { + + ClearCachedShadows(); list.Remove(this); } - - } } diff --git a/Subsurface/Source/Map/Lights/LightManager.cs b/Subsurface/Source/Map/Lights/LightManager.cs index 77d1ef731..4cebf7238 100644 --- a/Subsurface/Source/Map/Lights/LightManager.cs +++ b/Subsurface/Source/Map/Lights/LightManager.cs @@ -135,8 +135,9 @@ namespace Barotrauma.Lights foreach (LightSource light in lights) { - if (!light.hullsInRange.Any() || light.Color.A < 0.01f || light.Range < 1.0f) continue; - if (!MathUtils.CircleIntersectsRectangle(light.WorldPosition, light.Range, viewRect)) continue; + if (light.Color.A < 0.01f || light.Range < 1.0f) continue; + if (light.hullsInRange == null) light.UpdateHullsInRange(); + if (!light.hullsInRange.Any() || !MathUtils.CircleIntersectsRectangle(light.WorldPosition, light.Range, viewRect)) continue; //clear alpha to 1 ClearAlphaToOne(graphics, spriteBatch); @@ -170,7 +171,7 @@ namespace Barotrauma.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; light.Draw(spriteBatch); diff --git a/Subsurface/Source/Map/Lights/LightSource.cs b/Subsurface/Source/Map/Lights/LightSource.cs index 5460e51a4..46e04bf90 100644 --- a/Subsurface/Source/Map/Lights/LightSource.cs +++ b/Subsurface/Source/Map/Lights/LightSource.cs @@ -86,7 +86,7 @@ namespace Barotrauma.Lights { 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(); prevHullUpdateRange = range; @@ -136,6 +136,8 @@ namespace Barotrauma.Lights { if (!CastShadows) return; + if (hullsInRange == null) hullsInRange = new List(); + hullsInRange.Clear(); 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) { if (range > 1.0f) diff --git a/Subsurface/Source/Map/Structure.cs b/Subsurface/Source/Map/Structure.cs index 9361dbe41..70f2f97dc 100644 --- a/Subsurface/Source/Map/Structure.cs +++ b/Subsurface/Source/Map/Structure.cs @@ -282,9 +282,13 @@ namespace Barotrauma // if there is a gap and we have sections to merge, do it. if (section.gap != null) + { GenerateMergedHull(mergedSections); + } else + { mergedSections.Add(section); + } } // take care of any leftover pieces @@ -323,6 +327,7 @@ namespace Barotrauma corners[1] = new Vector2(sectionRect.X, sectionRect.Y); corners[2] = new Vector2(sectionRect.Right, sectionRect.Y); corners[3] = new Vector2(sectionRect.Right, sectionRect.Y - sectionRect.Height); + return corners; }