(5a377a8ee) Unstable v0.9.1000.0
This commit is contained in:
@@ -93,9 +93,9 @@ namespace Barotrauma.Lights
|
||||
public static BasicEffect shadowEffect;
|
||||
public static BasicEffect penumbraEffect;
|
||||
|
||||
private Segment[] segments = new Segment[4];
|
||||
private SegmentPoint[] vertices = new SegmentPoint[4];
|
||||
private SegmentPoint[] losVertices = new SegmentPoint[4];
|
||||
private readonly Segment[] segments = new Segment[4];
|
||||
private readonly SegmentPoint[] vertices = new SegmentPoint[4];
|
||||
private readonly SegmentPoint[] losVertices = new SegmentPoint[4];
|
||||
|
||||
private readonly bool[] backFacing;
|
||||
private readonly bool[] ignoreEdge;
|
||||
@@ -106,6 +106,8 @@ namespace Barotrauma.Lights
|
||||
public VertexPositionTexture[] PenumbraVertices { get; private set; }
|
||||
public int ShadowVertexCount { get; private set; }
|
||||
|
||||
private readonly HashSet<ConvexHull> overlappingHulls = new HashSet<ConvexHull>();
|
||||
|
||||
public MapEntity ParentEntity { get; private set; }
|
||||
|
||||
private bool enabled;
|
||||
@@ -176,7 +178,7 @@ namespace Barotrauma.Lights
|
||||
if (door != null) { isHorizontal = door.IsHorizontal; }
|
||||
}
|
||||
|
||||
var chList = HullLists.Find(x => x.Submarine == parent.Submarine);
|
||||
var chList = HullLists.Find(h => h.Submarine == parent.Submarine);
|
||||
if (chList == null)
|
||||
{
|
||||
chList = new ConvexHullList(parent.Submarine);
|
||||
@@ -194,10 +196,12 @@ namespace Barotrauma.Lights
|
||||
|
||||
private void MergeOverlappingSegments(ConvexHull ch)
|
||||
{
|
||||
if (ch == this) return;
|
||||
|
||||
if (ch == this) { return; }
|
||||
|
||||
if (isHorizontal == ch.isHorizontal)
|
||||
{
|
||||
if (BoundingBox == ch.BoundingBox) { return; }
|
||||
|
||||
//hide segments that are roughly at the some position as some other segment (e.g. the ends of two adjacent wall pieces)
|
||||
float mergeDist = 32;
|
||||
float mergeDistSqr = mergeDist * mergeDist;
|
||||
@@ -206,6 +210,7 @@ namespace Barotrauma.Lights
|
||||
for (int j = 0; j < ch.segments.Length; j++)
|
||||
{
|
||||
if (segments[i].IsHorizontal != ch.segments[j].IsHorizontal) { continue; }
|
||||
if (ignoreEdge[i] || ch.ignoreEdge[j]) { continue; }
|
||||
|
||||
//the segments must be at different sides of the convex hulls to be merged
|
||||
//(e.g. the right edge of a wall piece and the left edge of another one)
|
||||
@@ -247,6 +252,7 @@ namespace Barotrauma.Lights
|
||||
p.Y >= ch.BoundingBox.Y && p.Y <= ch.BoundingBox.Bottom)
|
||||
{
|
||||
ignoreEdge[i] = true;
|
||||
overlappingHulls.Add(ch);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -283,11 +289,25 @@ namespace Barotrauma.Lights
|
||||
}
|
||||
else
|
||||
{
|
||||
losVertices[startPointIndex].Pos = segment2.ConvexHull.losVertices[startPoint2Index].Pos =
|
||||
(segment1.Start.Pos + segment2.End.Pos) / 2.0f;
|
||||
losVertices[endPointIndex].Pos = segment2.ConvexHull.losVertices[endPoint2Index].Pos =
|
||||
(segment1.End.Pos + segment2.Start.Pos) / 2.0f;
|
||||
if (Vector2.DistanceSquared(losVertices[startPointIndex].Pos, segment1.Start.Pos) <
|
||||
Vector2.DistanceSquared(losVertices[startPointIndex].Pos, segment1.End.Pos))
|
||||
{
|
||||
losVertices[startPointIndex].Pos = segment2.ConvexHull.losVertices[startPoint2Index].Pos =
|
||||
(segment1.Start.Pos + segment2.End.Pos) / 2.0f;
|
||||
losVertices[endPointIndex].Pos = segment2.ConvexHull.losVertices[endPoint2Index].Pos =
|
||||
(segment1.End.Pos + segment2.Start.Pos) / 2.0f;
|
||||
}
|
||||
else
|
||||
{
|
||||
losVertices[startPointIndex].Pos = segment2.ConvexHull.losVertices[startPoint2Index].Pos =
|
||||
(segment1.End.Pos + segment2.Start.Pos) / 2.0f;
|
||||
losVertices[endPointIndex].Pos = segment2.ConvexHull.losVertices[endPoint2Index].Pos =
|
||||
(segment1.Start.Pos + segment2.End.Pos) / 2.0f;
|
||||
}
|
||||
}
|
||||
|
||||
overlappingHulls.Add(segment2.ConvexHull);
|
||||
segment2.ConvexHull.overlappingHulls.Add(this);
|
||||
}
|
||||
|
||||
public void Rotate(Vector2 origin, float amount)
|
||||
@@ -328,7 +348,26 @@ namespace Barotrauma.Lights
|
||||
|
||||
LastVertexChangeTime = (float)Timing.TotalTime;
|
||||
|
||||
overlappingHulls.Clear();
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
ignoreEdge[i] = false;
|
||||
}
|
||||
|
||||
CalculateDimensions();
|
||||
|
||||
if (ParentEntity == null) { return; }
|
||||
|
||||
var chList = HullLists.Find(h => h.Submarine == ParentEntity.Submarine);
|
||||
if (chList != null)
|
||||
{
|
||||
overlappingHulls.Clear();
|
||||
foreach (ConvexHull ch in chList.List)
|
||||
{
|
||||
MergeOverlappingSegments(ch);
|
||||
ch.MergeOverlappingSegments(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void SetVertices(Vector2[] points, Matrix? rotationMatrix = null)
|
||||
@@ -348,6 +387,8 @@ namespace Barotrauma.Lights
|
||||
ignoreEdge[i] = false;
|
||||
}
|
||||
|
||||
overlappingHulls.Clear();
|
||||
|
||||
int margin = 0;
|
||||
if (Math.Abs(points[0].X - points[2].X) < Math.Abs(points[0].Y - points[2].Y))
|
||||
{
|
||||
@@ -381,9 +422,10 @@ namespace Barotrauma.Lights
|
||||
|
||||
if (ParentEntity == null) return;
|
||||
|
||||
var chList = HullLists.Find(x => x.Submarine == ParentEntity.Submarine);
|
||||
var chList = HullLists.Find(h => h.Submarine == ParentEntity.Submarine);
|
||||
if (chList != null)
|
||||
{
|
||||
overlappingHulls.Clear();
|
||||
foreach (ConvexHull ch in chList.List)
|
||||
{
|
||||
MergeOverlappingSegments(ch);
|
||||
@@ -484,8 +526,8 @@ namespace Barotrauma.Lights
|
||||
|
||||
//find beginning and ending vertices which
|
||||
//belong to the shadow
|
||||
int startingIndex = 0;
|
||||
int endingIndex = 0;
|
||||
int startingIndex = -1;
|
||||
int endingIndex = -1;
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
int currentEdge = i;
|
||||
@@ -498,6 +540,8 @@ namespace Barotrauma.Lights
|
||||
startingIndex = nextEdge;
|
||||
}
|
||||
|
||||
if (startingIndex == -1 || endingIndex == -1) { return; }
|
||||
|
||||
//nr of vertices that are in the shadow
|
||||
if (endingIndex > startingIndex)
|
||||
ShadowVertexCount = endingIndex - startingIndex + 1;
|
||||
@@ -663,7 +707,7 @@ namespace Barotrauma.Lights
|
||||
|
||||
public void Remove()
|
||||
{
|
||||
var chList = HullLists.Find(x => x.Submarine == ParentEntity.Submarine);
|
||||
var chList = HullLists.Find(h => h.Submarine == ParentEntity.Submarine);
|
||||
|
||||
if (chList != null)
|
||||
{
|
||||
@@ -672,8 +716,19 @@ namespace Barotrauma.Lights
|
||||
{
|
||||
HullLists.Remove(chList);
|
||||
}
|
||||
foreach (ConvexHull ch2 in overlappingHulls)
|
||||
{
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
ch2.ignoreEdge[i] = false;
|
||||
}
|
||||
ch2.overlappingHulls.Remove(this);
|
||||
foreach (ConvexHull ch in chList.List)
|
||||
{
|
||||
ch.MergeOverlappingSegments(ch2);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -11,18 +11,6 @@ namespace Barotrauma.Lights
|
||||
{
|
||||
class LightManager
|
||||
{
|
||||
private const float AmbientLightUpdateInterval = 0.2f;
|
||||
private const float AmbientLightFalloff = 0.8f;
|
||||
|
||||
/// <summary>
|
||||
/// Enables a feature that makes lights inside the hull increase the brightness of the entire hull
|
||||
/// and adjacent ones to some extent, if there are gaps for the lights to pass through.
|
||||
/// Prevents unnaturally dark looking shadows in otherwise well-lit submarines, but disabled at least for
|
||||
/// the time being because it makes the lighting behave unpredictably and may cause rooms to appear
|
||||
/// excessively bright if different lighting conditions aren't tested and accounted for.
|
||||
/// </summary>
|
||||
private static readonly bool UseHullSpecificAmbientLight = false;
|
||||
|
||||
public static Entity ViewTarget { get; set; }
|
||||
|
||||
private float currLightMapScale;
|
||||
@@ -57,7 +45,7 @@ namespace Barotrauma.Lights
|
||||
public Effect LosEffect { get; private set; }
|
||||
public Effect SolidColorEffect { get; private set; }
|
||||
|
||||
private List<LightSource> lights;
|
||||
private readonly List<LightSource> lights;
|
||||
|
||||
public bool LosEnabled = true;
|
||||
public LosMode LosMode = LosMode.Transparent;
|
||||
@@ -66,13 +54,8 @@ namespace Barotrauma.Lights
|
||||
|
||||
public bool ObstructVision;
|
||||
|
||||
private Texture2D visionCircle;
|
||||
private readonly Texture2D visionCircle;
|
||||
|
||||
private Dictionary<Hull, Color> hullAmbientLights;
|
||||
private Dictionary<Hull, Color> smoothedHullAmbientLights;
|
||||
|
||||
private float ambientLightUpdateTimer;
|
||||
|
||||
public IEnumerable<LightSource> Lights
|
||||
{
|
||||
get { return lights; }
|
||||
@@ -80,7 +63,7 @@ namespace Barotrauma.Lights
|
||||
|
||||
public LightManager(GraphicsDevice graphics, ContentManager content)
|
||||
{
|
||||
lights = new List<LightSource>();
|
||||
lights = new List<LightSource>(100);
|
||||
|
||||
AmbientLight = new Color(20, 20, 20, 255);
|
||||
|
||||
@@ -114,9 +97,6 @@ namespace Barotrauma.Lights
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
hullAmbientLights = new Dictionary<Hull, Color>();
|
||||
smoothedHullAmbientLights = new Dictionary<Hull, Color>();
|
||||
}
|
||||
|
||||
private void CreateRenderTargets(GraphicsDevice graphics)
|
||||
@@ -167,43 +147,12 @@ namespace Barotrauma.Lights
|
||||
}
|
||||
}
|
||||
|
||||
public void Update(float deltaTime)
|
||||
{
|
||||
if (UseHullSpecificAmbientLight)
|
||||
{
|
||||
if (ambientLightUpdateTimer > 0.0f)
|
||||
{
|
||||
ambientLightUpdateTimer -= deltaTime;
|
||||
}
|
||||
else
|
||||
{
|
||||
CalculateAmbientLights();
|
||||
ambientLightUpdateTimer = AmbientLightUpdateInterval;
|
||||
}
|
||||
|
||||
foreach (Hull hull in hullAmbientLights.Keys)
|
||||
{
|
||||
if (!smoothedHullAmbientLights.ContainsKey(hull))
|
||||
{
|
||||
smoothedHullAmbientLights.Add(hull, Color.TransparentBlack);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (Hull hull in smoothedHullAmbientLights.Keys.ToList())
|
||||
{
|
||||
Color targetColor = Color.TransparentBlack;
|
||||
hullAmbientLights.TryGetValue(hull, out targetColor);
|
||||
smoothedHullAmbientLights[hull] = Color.Lerp(smoothedHullAmbientLights[hull], targetColor, deltaTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private List<LightSource> activeLights = new List<LightSource>(capacity: 100);
|
||||
private readonly List<LightSource> activeLights = new List<LightSource>(capacity: 100);
|
||||
|
||||
public void UpdateLightMap(GraphicsDevice graphics, SpriteBatch spriteBatch, Camera cam, RenderTarget2D backgroundObstructor = null)
|
||||
{
|
||||
if (!LightingEnabled) return;
|
||||
|
||||
if (!LightingEnabled) { return; }
|
||||
|
||||
if (Math.Abs(currLightMapScale - GameMain.Config.LightMapScale) > 0.01f)
|
||||
{
|
||||
//lightmap scale has changed -> recreate render targets
|
||||
@@ -229,7 +178,16 @@ namespace Barotrauma.Lights
|
||||
light.Position = light.ParentBody.DrawPosition;
|
||||
if (light.ParentSub != null) { light.Position -= light.ParentSub.DrawPosition; }
|
||||
}
|
||||
if (!MathUtils.CircleIntersectsRectangle(light.WorldPosition, light.LightSourceParams.TextureRange, viewRect)) { continue; }
|
||||
|
||||
float range = light.LightSourceParams.TextureRange;
|
||||
if (light.LightSprite != null)
|
||||
{
|
||||
float spriteRange = Math.Max(
|
||||
light.LightSprite.size.X * light.SpriteScale.X * (0.5f + Math.Abs(light.LightSprite.RelativeOrigin.X - 0.5f)),
|
||||
light.LightSprite.size.Y * light.SpriteScale.Y * (0.5f + Math.Abs(light.LightSprite.RelativeOrigin.Y - 0.5f)));
|
||||
range = Math.Max(spriteRange, range);
|
||||
}
|
||||
if (!MathUtils.CircleIntersectsRectangle(light.WorldPosition, range, viewRect)) { continue; }
|
||||
activeLights.Add(light);
|
||||
}
|
||||
|
||||
@@ -252,16 +210,14 @@ namespace Barotrauma.Lights
|
||||
//draw background lights
|
||||
//---------------------------------------------------------------------------------------------------
|
||||
graphics.SetRenderTarget(LightMap);
|
||||
graphics.Clear(Color.Black);
|
||||
graphics.Clear(AmbientLight);
|
||||
graphics.BlendState = BlendState.Additive;
|
||||
bool backgroundSpritesDrawn = false;
|
||||
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive, transformMatrix: spriteBatchTransform);
|
||||
foreach (LightSource light in activeLights)
|
||||
{
|
||||
if (!light.IsBackground) { continue; }
|
||||
light.DrawSprite(spriteBatch, cam);
|
||||
if (light.Color.A > 0 && light.Range > 0.0f) { light.DrawLightVolume(spriteBatch, lightEffect, transform); }
|
||||
backgroundSpritesDrawn = true;
|
||||
}
|
||||
GameMain.ParticleManager.Draw(spriteBatch, true, null, Particles.ParticleBlendState.Additive);
|
||||
spriteBatch.End();
|
||||
@@ -269,33 +225,34 @@ namespace Barotrauma.Lights
|
||||
//draw a black rectangle on hulls to hide background lights behind subs
|
||||
//---------------------------------------------------------------------------------------------------
|
||||
|
||||
Dictionary<Hull, Rectangle> visibleHulls = null;
|
||||
if (backgroundSpritesDrawn)
|
||||
if (backgroundObstructor != null)
|
||||
{
|
||||
if (backgroundObstructor != null)
|
||||
{
|
||||
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied);
|
||||
spriteBatch.Draw(backgroundObstructor, new Rectangle(0, 0,
|
||||
(int)(GameMain.GraphicsWidth * currLightMapScale), (int)(GameMain.GraphicsHeight * currLightMapScale)), Color.Black);
|
||||
spriteBatch.End();
|
||||
}
|
||||
|
||||
visibleHulls = GetVisibleHulls(cam);
|
||||
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque, transformMatrix: spriteBatchTransform);
|
||||
foreach (Rectangle drawRect in visibleHulls.Values)
|
||||
{
|
||||
//TODO: draw some sort of smoothed rectangle
|
||||
GUI.DrawRectangle(spriteBatch,
|
||||
new Vector2(drawRect.X, -drawRect.Y),
|
||||
new Vector2(drawRect.Width, drawRect.Height),
|
||||
Color.Black, true);
|
||||
}
|
||||
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied);
|
||||
spriteBatch.Draw(backgroundObstructor, new Rectangle(0, 0,
|
||||
(int)(GameMain.GraphicsWidth * currLightMapScale), (int)(GameMain.GraphicsHeight * currLightMapScale)), Color.Black);
|
||||
spriteBatch.End();
|
||||
|
||||
|
||||
graphics.BlendState = BlendState.Additive;
|
||||
}
|
||||
|
||||
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque, transformMatrix: spriteBatchTransform);
|
||||
Dictionary<Hull, Rectangle> visibleHulls = GetVisibleHulls(cam);
|
||||
foreach (KeyValuePair<Hull, Rectangle> hull in visibleHulls)
|
||||
{
|
||||
GUI.DrawRectangle(spriteBatch,
|
||||
new Vector2(hull.Value.X, -hull.Value.Y),
|
||||
new Vector2(hull.Value.Width, hull.Value.Height),
|
||||
hull.Key.AmbientLight == Color.TransparentBlack ? Color.Black : hull.Key.AmbientLight.Multiply(hull.Key.AmbientLight.A / 255.0f), true);
|
||||
}
|
||||
spriteBatch.End();
|
||||
|
||||
SolidColorEffect.CurrentTechnique = SolidColorEffect.Techniques["SolidColor"];
|
||||
SolidColorEffect.Parameters["color"].SetValue(AmbientLight.ToVector4());
|
||||
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, transformMatrix: spriteBatchTransform, effect: SolidColorEffect);
|
||||
Submarine.DrawDamageable(spriteBatch, null);
|
||||
spriteBatch.End();
|
||||
|
||||
graphics.BlendState = BlendState.Additive;
|
||||
|
||||
|
||||
//draw the focused item and character to highlight them,
|
||||
//and light sprites (done before drawing the actual light volumes so we can make characters obstruct the highlights and sprites)
|
||||
//---------------------------------------------------------------------------------------------------
|
||||
@@ -318,33 +275,37 @@ namespace Barotrauma.Lights
|
||||
//draw characters to obstruct the highlighted items/characters and light sprites
|
||||
//---------------------------------------------------------------------------------------------------
|
||||
|
||||
SolidColorEffect.CurrentTechnique = SolidColorEffect.Techniques["SolidColor"];
|
||||
SolidColorEffect.Parameters["color"].SetValue(Color.Black.ToVector4());
|
||||
SolidColorEffect.CurrentTechnique = SolidColorEffect.Techniques["SolidVertexColor"];
|
||||
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, effect: SolidColorEffect, transformMatrix: spriteBatchTransform);
|
||||
foreach (Character character in Character.CharacterList)
|
||||
{
|
||||
if (character.CurrentHull == null || !character.Enabled) continue;
|
||||
if (Character.Controlled?.FocusedCharacter == character) continue;
|
||||
if (character.CurrentHull == null || !character.Enabled) { continue; }
|
||||
if (Character.Controlled?.FocusedCharacter == character) { continue; }
|
||||
Color lightColor = character.CurrentHull.AmbientLight == Color.TransparentBlack ?
|
||||
Color.Black :
|
||||
character.CurrentHull.AmbientLight.Multiply(character.CurrentHull.AmbientLight.A / 255.0f).Opaque();
|
||||
foreach (Limb limb in character.AnimController.Limbs)
|
||||
{
|
||||
if (limb.DeformSprite != null) continue;
|
||||
limb.Draw(spriteBatch, cam, Color.Black);
|
||||
if (limb.DeformSprite != null) { continue; }
|
||||
limb.Draw(spriteBatch, cam, lightColor);
|
||||
}
|
||||
}
|
||||
spriteBatch.End();
|
||||
|
||||
DeformableSprite.Effect.CurrentTechnique = DeformableSprite.Effect.Techniques["DeformShaderSolidColor"];
|
||||
DeformableSprite.Effect.Parameters["solidColor"].SetValue(Color.Black.ToVector4());
|
||||
DeformableSprite.Effect.CurrentTechnique = DeformableSprite.Effect.Techniques["DeformShaderSolidVertexColor"];
|
||||
DeformableSprite.Effect.CurrentTechnique.Passes[0].Apply();
|
||||
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, transformMatrix: spriteBatchTransform);
|
||||
foreach (Character character in Character.CharacterList)
|
||||
{
|
||||
if (character.CurrentHull == null || !character.Enabled) continue;
|
||||
if (Character.Controlled?.FocusedCharacter == character) continue;
|
||||
if (character.CurrentHull == null || !character.Enabled) { continue; }
|
||||
if (Character.Controlled?.FocusedCharacter == character) { continue; }
|
||||
Color lightColor = character.CurrentHull.AmbientLight == Color.TransparentBlack ?
|
||||
Color.Black :
|
||||
character.CurrentHull.AmbientLight.Multiply(character.CurrentHull.AmbientLight.A / 255.0f).Opaque();
|
||||
foreach (Limb limb in character.AnimController.Limbs)
|
||||
{
|
||||
if (limb.DeformSprite == null) continue;
|
||||
limb.Draw(spriteBatch, cam, Color.Black);
|
||||
if (limb.DeformSprite == null) { continue; }
|
||||
limb.Draw(spriteBatch, cam, lightColor);
|
||||
}
|
||||
}
|
||||
spriteBatch.End();
|
||||
@@ -355,8 +316,6 @@ namespace Barotrauma.Lights
|
||||
//---------------------------------------------------------------------------------------------------
|
||||
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive, transformMatrix: spriteBatchTransform);
|
||||
|
||||
GUI.DrawRectangle(spriteBatch, new Rectangle(cam.WorldView.X, -cam.WorldView.Y, cam.WorldView.Width, cam.WorldView.Height), AmbientLight, isFilled: true);
|
||||
|
||||
spriteBatch.Draw(LimbLightMap, new Rectangle(cam.WorldView.X, -cam.WorldView.Y, cam.WorldView.Width, cam.WorldView.Height), Color.White);
|
||||
|
||||
foreach (ElectricalDischarger discharger in ElectricalDischarger.List)
|
||||
@@ -373,24 +332,7 @@ namespace Barotrauma.Lights
|
||||
lightEffect.World = transform;
|
||||
|
||||
GameMain.ParticleManager.Draw(spriteBatch, false, null, Particles.ParticleBlendState.Additive);
|
||||
|
||||
if (UseHullSpecificAmbientLight)
|
||||
{
|
||||
if (visibleHulls == null)
|
||||
{
|
||||
visibleHulls = GetVisibleHulls(cam);
|
||||
}
|
||||
foreach (Hull hull in smoothedHullAmbientLights.Keys)
|
||||
{
|
||||
if (smoothedHullAmbientLights[hull].A < 0.01f) continue;
|
||||
if (!visibleHulls.TryGetValue(hull, out Rectangle drawRect)) continue;
|
||||
GUI.DrawRectangle(spriteBatch,
|
||||
new Vector2(drawRect.X, -drawRect.Y),
|
||||
new Vector2(hull.Rect.Width, hull.Rect.Height),
|
||||
smoothedHullAmbientLights[hull], true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (Character.Controlled != null)
|
||||
{
|
||||
Vector2 haloDrawPos = Character.Controlled.DrawPosition;
|
||||
@@ -592,7 +534,10 @@ namespace Barotrauma.Lights
|
||||
}
|
||||
}
|
||||
|
||||
penumbraVerts.AddRange(convexHull.PenumbraVertices);
|
||||
if (convexHull.ShadowVertexCount > 0)
|
||||
{
|
||||
penumbraVerts.AddRange(convexHull.PenumbraVertices);
|
||||
}
|
||||
}
|
||||
|
||||
if (shadowVerts.Count > 0)
|
||||
@@ -613,86 +558,6 @@ namespace Barotrauma.Lights
|
||||
graphics.SetRenderTarget(null);
|
||||
}
|
||||
|
||||
|
||||
private void CalculateAmbientLights()
|
||||
{
|
||||
hullAmbientLights.Clear();
|
||||
|
||||
foreach (LightSource light in lights)
|
||||
{
|
||||
if (light.Color.A < 1f || light.Range < 1.0f || light.IsBackground) continue;
|
||||
|
||||
var newAmbientLights = AmbientLightHulls(light);
|
||||
foreach (Hull hull in newAmbientLights.Keys)
|
||||
{
|
||||
if (hullAmbientLights.ContainsKey(hull))
|
||||
{
|
||||
//hull already lit by some other light source -> add the ambient lights up
|
||||
hullAmbientLights[hull] = new Color(
|
||||
hullAmbientLights[hull].R + newAmbientLights[hull].R,
|
||||
hullAmbientLights[hull].G + newAmbientLights[hull].G,
|
||||
hullAmbientLights[hull].B + newAmbientLights[hull].B,
|
||||
hullAmbientLights[hull].A + newAmbientLights[hull].A);
|
||||
}
|
||||
else
|
||||
{
|
||||
hullAmbientLights.Add(hull, newAmbientLights[hull]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add ambient light to the hull the lightsource is inside + all adjacent hulls connected by a gap
|
||||
/// </summary>
|
||||
private Dictionary<Hull, Color> AmbientLightHulls(LightSource light)
|
||||
{
|
||||
Dictionary<Hull, Color> hullAmbientLight = new Dictionary<Hull, Color>();
|
||||
|
||||
var hull = Hull.FindHull(light.WorldPosition);
|
||||
if (hull == null) return hullAmbientLight;
|
||||
|
||||
return AmbientLightHulls(hull, hullAmbientLight, light.Color * Math.Min(light.Range / 1000.0f, 1.0f));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A flood fill algorithm that adds ambient light to all hulls the starting hull is connected to
|
||||
/// </summary>
|
||||
private Dictionary<Hull, Color> AmbientLightHulls(Hull hull, Dictionary<Hull, Color> hullAmbientLight, Color currColor)
|
||||
{
|
||||
if (hullAmbientLight.ContainsKey(hull))
|
||||
{
|
||||
if (hullAmbientLight[hull].A > currColor.A)
|
||||
return hullAmbientLight;
|
||||
else
|
||||
hullAmbientLight[hull] = currColor;
|
||||
}
|
||||
else
|
||||
{
|
||||
hullAmbientLight.Add(hull, currColor);
|
||||
}
|
||||
|
||||
Color nextHullLight = currColor * AmbientLightFalloff;
|
||||
//light getting too dark to notice -> no need to spread further
|
||||
if (nextHullLight.A < 20) return hullAmbientLight;
|
||||
|
||||
//use hashset to make sure that each hull is only included once
|
||||
HashSet<Hull> hulls = new HashSet<Hull>();
|
||||
foreach (Gap g in hull.ConnectedGaps)
|
||||
{
|
||||
if (!g.IsRoomToRoom || !g.PassAmbientLight || g.Open < 0.5f) continue;
|
||||
|
||||
hulls.Add((g.linkedTo[0] == hull ? g.linkedTo[1] : g.linkedTo[0]) as Hull);
|
||||
}
|
||||
|
||||
foreach (Hull h in hulls)
|
||||
{
|
||||
hullAmbientLight = AmbientLightHulls(h, hullAmbientLight, nextHullLight);
|
||||
}
|
||||
|
||||
return hullAmbientLight;
|
||||
}
|
||||
|
||||
public void ClearLights()
|
||||
{
|
||||
lights.Clear();
|
||||
|
||||
@@ -16,7 +16,7 @@ namespace Barotrauma.Lights
|
||||
|
||||
public Dictionary<string, SerializableProperty> SerializableProperties { get; private set; } = new Dictionary<string, SerializableProperty>();
|
||||
|
||||
[Serialize("1.0,1.0,1.0,1.0", true), Editable]
|
||||
[Serialize("1.0,1.0,1.0,1.0", true, alwaysUseInstanceValues: true), Editable]
|
||||
public Color Color
|
||||
{
|
||||
get;
|
||||
@@ -25,7 +25,7 @@ namespace Barotrauma.Lights
|
||||
|
||||
private float range;
|
||||
|
||||
[Serialize(100.0f, true), Editable(MinValueFloat = 0.0f, MaxValueFloat = 2048.0f)]
|
||||
[Serialize(100.0f, true, alwaysUseInstanceValues: true), Editable(MinValueFloat = 0.0f, MaxValueFloat = 2048.0f)]
|
||||
public float Range
|
||||
{
|
||||
get { return range; }
|
||||
@@ -331,7 +331,7 @@ namespace Barotrauma.Lights
|
||||
|
||||
if (lightSourceParams.DeformableLightSpriteElement != null)
|
||||
{
|
||||
DeformableLightSprite = new DeformableSprite(lightSourceParams.DeformableLightSpriteElement);
|
||||
DeformableLightSprite = new DeformableSprite(lightSourceParams.DeformableLightSpriteElement, invert: true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -342,7 +342,7 @@ namespace Barotrauma.Lights
|
||||
lightSourceParams.Persistent = true;
|
||||
if (lightSourceParams.DeformableLightSpriteElement != null)
|
||||
{
|
||||
DeformableLightSprite = new DeformableSprite(lightSourceParams.DeformableLightSpriteElement);
|
||||
DeformableLightSprite = new DeformableSprite(lightSourceParams.DeformableLightSpriteElement, invert: true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -952,23 +952,44 @@ namespace Barotrauma.Lights
|
||||
{
|
||||
Vector2 origin = DeformableLightSprite.Origin;
|
||||
Vector2 drawPos = position;
|
||||
if (ParentSub != null) drawPos += ParentSub.DrawPosition;
|
||||
if (ParentSub != null)
|
||||
{
|
||||
drawPos += ParentSub.DrawPosition;
|
||||
}
|
||||
|
||||
if (LightSpriteEffect == SpriteEffects.FlipHorizontally)
|
||||
{
|
||||
origin.X = DeformableLightSprite.Sprite.SourceRect.Width - origin.X;
|
||||
}
|
||||
if (LightSpriteEffect == SpriteEffects.FlipVertically)
|
||||
{
|
||||
origin.Y = DeformableLightSprite.Sprite.SourceRect.Height - origin.Y;
|
||||
}
|
||||
|
||||
DeformableLightSprite.Draw(
|
||||
cam, new Vector3(drawPos, 0.0f),
|
||||
origin, -Rotation, SpriteScale,
|
||||
new Color(Color, lightSourceParams.OverrideLightSpriteAlpha ?? Color.A / 255.0f),
|
||||
LightSpriteEffect == SpriteEffects.FlipHorizontally);
|
||||
LightSpriteEffect == SpriteEffects.FlipVertically);
|
||||
}
|
||||
|
||||
if (LightSprite != null)
|
||||
{
|
||||
Vector2 origin = LightSprite.Origin;
|
||||
if (LightSpriteEffect == SpriteEffects.FlipHorizontally) origin.X = LightSprite.SourceRect.Width - origin.X;
|
||||
if (LightSpriteEffect == SpriteEffects.FlipVertically) origin.Y = LightSprite.SourceRect.Height - origin.Y;
|
||||
if (LightSpriteEffect == SpriteEffects.FlipHorizontally)
|
||||
{
|
||||
origin.X = LightSprite.SourceRect.Width - origin.X;
|
||||
}
|
||||
if (LightSpriteEffect == SpriteEffects.FlipVertically)
|
||||
{
|
||||
origin.Y = LightSprite.SourceRect.Height - origin.Y;
|
||||
}
|
||||
|
||||
Vector2 drawPos = position;
|
||||
if (ParentSub != null) drawPos += ParentSub.DrawPosition;
|
||||
if (ParentSub != null)
|
||||
{
|
||||
drawPos += ParentSub.DrawPosition;
|
||||
}
|
||||
drawPos.Y = -drawPos.Y;
|
||||
|
||||
LightSprite.Draw(
|
||||
|
||||
Reference in New Issue
Block a user