Hull-specific ambient lighting: light sources increase the amount of ambient light in rooms and the light can spread to adjacent rooms through gaps. Allows making the default ambient light much darker without making shadows look too dark in fully lit subs.

This commit is contained in:
Regalis
2016-10-10 21:13:22 +03:00
parent 14447062a9
commit d8476d9371
10 changed files with 198 additions and 35 deletions
Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.8 KiB

After

Width:  |  Height:  |  Size: 3.7 KiB

@@ -10,8 +10,8 @@
<Sprite texture="lamp.png" sourcerect="0,0,16,32" depth="0.8"/> <Sprite texture="lamp.png" sourcerect="0,0,16,32" depth="0.8"/>
<LightComponent color="1.0,1.0,1.0,1.0" range ="800.0" powerconsumption="5"> <LightComponent lightcolor="1.0,1.0,1.0,1.0" range ="800.0" powerconsumption="5">
<sprite texture="Content/Items/Electricity/lamp.png" sourcerect="33,0,31,39"/> <sprite texture="Content/Items/Electricity/lamp.png" sourcerect="33,0,31,37"/>
</LightComponent> </LightComponent>
<ConnectionPanel selectkey="Action" canbeselected = "true" msg="Rewire [Screwdriver]"> <ConnectionPanel selectkey="Action" canbeselected = "true" msg="Rewire [Screwdriver]">
@@ -20,8 +20,25 @@
<input name="toggle"/> <input name="toggle"/>
<input name="set_state"/> <input name="set_state"/>
</ConnectionPanel> </ConnectionPanel>
</Item> </Item>
<Item
name="Emergency Light"
category="Electrical"
Tags="smallitem"
pickdistance="150">
<Sprite texture="lamp.png" sourcerect="0,48,48,16" depth="0.8"/>
<LightComponent lightcolor="1.0,0.0,0.0,0.2" range="500.0" IsOn="true">
<sprite texture="Content/Items/Electricity/lamp.png" sourcerect="0,48,48,16"/>
</LightComponent>
<ConnectionPanel selectkey="Action" canbeselected = "true" msg="Rewire [Screwdriver]">
<requireditem name="Screwdriver,Wire" type="Equipped"/>
<input name="toggle"/>
<input name="set_state"/>
</ConnectionPanel>
</Item>
</Items> </Items>
+7 -1
View File
@@ -56,10 +56,15 @@ namespace Barotrauma.Items.Components
get get
{ {
if (linkedGap != null) return linkedGap; if (linkedGap != null) return linkedGap;
foreach (MapEntity e in item.linkedTo) foreach (MapEntity e in item.linkedTo)
{ {
linkedGap = e as Gap; linkedGap = e as Gap;
if (linkedGap != null) return linkedGap; if (linkedGap != null)
{
linkedGap.PassAmbientLight = window != Rectangle.Empty;
return linkedGap;
}
} }
Rectangle rect = item.Rect; Rectangle rect = item.Rect;
if (isHorizontal) if (isHorizontal)
@@ -75,6 +80,7 @@ namespace Barotrauma.Items.Components
linkedGap = new Gap(rect, Item.Submarine); linkedGap = new Gap(rect, Item.Submarine);
linkedGap.Submarine = item.Submarine; linkedGap.Submarine = item.Submarine;
linkedGap.PassAmbientLight = window != Rectangle.Empty;
linkedGap.Open = openState; linkedGap.Open = openState;
item.linkedTo.Add(linkedGap); item.linkedTo.Add(linkedGap);
return linkedGap; return linkedGap;
+5 -2
View File
@@ -30,9 +30,14 @@ namespace Barotrauma
private float higherSurface; private float higherSurface;
private float lowerSurface; private float lowerSurface;
private Vector2 lerpedFlowForce;
//if set to true, hull connections of this gap won't be updated when changes are being done to hulls //if set to true, hull connections of this gap won't be updated when changes are being done to hulls
public bool DisableHullRechecks; public bool DisableHullRechecks;
//can ambient light get through the gap even if it's not open
public bool PassAmbientLight;
public float Open public float Open
{ {
get { return open; } get { return open; }
@@ -48,8 +53,6 @@ namespace Barotrauma
get { return lerpedFlowForce; } get { return lerpedFlowForce; }
} }
private Vector2 lerpedFlowForce;
public Hull FlowTargetHull public Hull FlowTargetHull
{ {
get { return flowTargetHull; } get { return flowTargetHull; }
-1
View File
@@ -723,7 +723,6 @@ namespace Barotrauma
} }
var entities = EntityGrid.GetEntities(entityGrids, position, useWorldCoordinates); var entities = EntityGrid.GetEntities(entityGrids, position, useWorldCoordinates);
foreach (Hull hull in entities) foreach (Hull hull in entities)
{ {
if (Submarine.RectContains(useWorldCoordinates ? hull.WorldRect : hull.rect, position)) return hull; if (Submarine.RectContains(useWorldCoordinates ? hull.WorldRect : hull.rect, position)) return hull;
+1 -1
View File
@@ -181,7 +181,7 @@ namespace Barotrauma
backgroundColor = generationParams.BackgroundColor; backgroundColor = generationParams.BackgroundColor;
float avgValue = (backgroundColor.R + backgroundColor.G + backgroundColor.G) / 3; float avgValue = (backgroundColor.R + backgroundColor.G + backgroundColor.G) / 3;
GameMain.LightManager.AmbientLight = new Color(backgroundColor * (60.0f / avgValue), 1.0f); GameMain.LightManager.AmbientLight = new Color(backgroundColor * (10.0f / avgValue), 1.0f);
float minWidth = Submarine.MainSub == null ? 0.0f : Math.Max(Submarine.MainSub.Borders.Width, Submarine.MainSub.Borders.Height); float minWidth = Submarine.MainSub == null ? 0.0f : Math.Max(Submarine.MainSub.Borders.Width, Submarine.MainSub.Borders.Height);
minWidth = Math.Max(minWidth, 6500.0f); minWidth = Math.Max(minWidth, 6500.0f);
+151 -15
View File
@@ -8,7 +8,9 @@ namespace Barotrauma.Lights
{ {
class LightManager class LightManager
{ {
//public static Vector2 ViewPos; private const float AmbientLightUpdateInterval = 0.2f;
private const float AmbientLightFalloff = 0.8f;
private static Entity viewTarget; private static Entity viewTarget;
public static Entity ViewTarget public static Entity ViewTarget
@@ -36,11 +38,16 @@ namespace Barotrauma.Lights
private Texture2D visionCircle; private Texture2D visionCircle;
private Dictionary<Hull, Color> hullAmbientLights = new Dictionary<Hull, Color>();
private Dictionary<Hull, Color> smoothedHullAmbientLights = new Dictionary<Hull, Color>();
private float ambientLightUpdateTimer;
public LightManager(GraphicsDevice graphics) public LightManager(GraphicsDevice graphics)
{ {
lights = new List<LightSource>(); lights = new List<LightSource>();
AmbientLight = new Color(60, 60, 60, 255); AmbientLight = new Color(20, 20, 20, 255);
visionCircle = Sprite.LoadTexture("Content/Lights/visioncircle.png"); visionCircle = Sprite.LoadTexture("Content/Lights/visioncircle.png");
@@ -53,7 +60,10 @@ namespace Barotrauma.Lights
losTexture = new RenderTarget2D(graphics, GameMain.GraphicsWidth, GameMain.GraphicsHeight); losTexture = new RenderTarget2D(graphics, GameMain.GraphicsWidth, GameMain.GraphicsHeight);
if (alphaClearTexture==null) hullAmbientLights = new Dictionary<Hull, Color>();
smoothedHullAmbientLights = new Dictionary<Hull, Color>();
if (alphaClearTexture == null)
{ {
alphaClearTexture = TextureLoader.FromFile("Content/Lights/alphaOne.png"); alphaClearTexture = TextureLoader.FromFile("Content/Lights/alphaOne.png");
} }
@@ -77,7 +87,38 @@ namespace Barotrauma.Lights
} }
} }
public void UpdateLightMap(GraphicsDevice graphics, SpriteBatch spriteBatch, Camera cam) public void Update(float deltaTime)
{
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);
}
}
public void UpdateLightMap(GraphicsDevice graphics, SpriteBatch spriteBatch, Camera cam, Effect blur)
{ {
if (!LightingEnabled) return; if (!LightingEnabled) return;
@@ -94,7 +135,7 @@ namespace Barotrauma.Lights
foreach (LightSource light in lights) foreach (LightSource light in lights)
{ {
if (light.Color.A < 0.01f || light.Range < 1.0f || !light.CastShadows) continue; if (light.Color.A < 1 || light.Range < 1.0f || !light.CastShadows) continue;
if (!MathUtils.CircleIntersectsRectangle(light.WorldPosition, light.Range, viewRect)) continue; if (!MathUtils.CircleIntersectsRectangle(light.WorldPosition, light.Range, viewRect)) continue;
//clear alpha to 1 //clear alpha to 1
@@ -118,7 +159,6 @@ namespace Barotrauma.Lights
ClearAlphaToOne(graphics, spriteBatch); ClearAlphaToOne(graphics, spriteBatch);
spriteBatch.Begin(SpriteSortMode.Deferred, CustomBlendStates.MultiplyWithAlpha, null, null, null, null, cam.Transform); spriteBatch.Begin(SpriteSortMode.Deferred, CustomBlendStates.MultiplyWithAlpha, null, null, null, null, cam.Transform);
GameMain.ParticleManager.Draw(spriteBatch, false, Particles.ParticleBlendState.Additive); GameMain.ParticleManager.Draw(spriteBatch, false, Particles.ParticleBlendState.Additive);
@@ -126,7 +166,7 @@ namespace Barotrauma.Lights
foreach (LightSource light in lights) foreach (LightSource light in lights)
{ {
if (light.Color.A < 0.01f || light.Range < 1.0f || light.CastShadows) continue; if (light.Color.A < 1 || light.Range < 1.0f || light.CastShadows) 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);
@@ -147,6 +187,27 @@ namespace Barotrauma.Lights
} }
spriteBatch.End(); spriteBatch.End();
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive, null, null, null, null, cam.Transform);
foreach (Hull hull in smoothedHullAmbientLights.Keys)
{
if (smoothedHullAmbientLights[hull].A < 0.01f) continue;
var drawRect =
hull.Submarine == null ?
hull.Rect :
new Rectangle((int)(hull.Submarine.DrawPosition.X + hull.Rect.X), (int)(hull.Submarine.DrawPosition.Y + hull.Rect.Y), hull.Rect.Width, hull.Rect.Height);
GUI.DrawRectangle(spriteBatch,
new Vector2(drawRect.X, -drawRect.Y),
new Vector2(hull.Rect.Width, hull.Rect.Height),
smoothedHullAmbientLights[hull] * 0.5f, true);
}
spriteBatch.End();
//clear alpha, to avoid messing stuff up later //clear alpha, to avoid messing stuff up later
ClearAlphaToOne(graphics, spriteBatch); ClearAlphaToOne(graphics, spriteBatch);
@@ -173,8 +234,6 @@ namespace Barotrauma.Lights
spriteBatch.Draw(visionCircle, new Vector2(ViewTarget.WorldPosition.X, -ViewTarget.WorldPosition.Y), null, Color.White, rotation, spriteBatch.Draw(visionCircle, new Vector2(ViewTarget.WorldPosition.X, -ViewTarget.WorldPosition.Y), null, Color.White, rotation,
new Vector2(LightSource.LightTexture.Width*0.2f, LightSource.LightTexture.Height/2), scale, SpriteEffects.None, 0.0f); new Vector2(LightSource.LightTexture.Width*0.2f, LightSource.LightTexture.Height/2), scale, SpriteEffects.None, 0.0f);
} }
else else
{ {
@@ -210,6 +269,86 @@ namespace Barotrauma.Lights
graphics.SetRenderTarget(null); graphics.SetRenderTarget(null);
} }
private void CalculateAmbientLights()
{
hullAmbientLights.Clear();
foreach (LightSource light in lights)
{
if (light.Color.A < 1f || light.Range < 1.0f) 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 * (light.Range/2000.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);
}
foreach (Gap g in hull.ConnectedGaps)
{
for (int i = 0; i < g.linkedTo.Count;i++ )
{
if (g.linkedTo[i] is Hull)
{
if (g.linkedTo[i] == hull) continue;
Color nextHullLight = currColor * AmbientLightFalloff;
if (!g.PassAmbientLight) nextHullLight *= g.Open;
if (nextHullLight.A < 10) continue;
hullAmbientLight = AmbientLightHulls((Hull)g.linkedTo[i], hullAmbientLight, nextHullLight);
}
}
}
return hullAmbientLight;
}
private void ClearAlphaToOne(GraphicsDevice graphics, SpriteBatch spriteBatch) private void ClearAlphaToOne(GraphicsDevice graphics, SpriteBatch spriteBatch)
{ {
spriteBatch.Begin(SpriteSortMode.Deferred, CustomBlendStates.WriteToAlpha); spriteBatch.Begin(SpriteSortMode.Deferred, CustomBlendStates.WriteToAlpha);
@@ -217,7 +356,7 @@ namespace Barotrauma.Lights
spriteBatch.End(); spriteBatch.End();
} }
public void DrawLightMap(SpriteBatch spriteBatch, Camera cam, Effect effect) public void DrawLightMap(SpriteBatch spriteBatch, Effect effect)
{ {
if (!LightingEnabled) return; if (!LightingEnabled) return;
@@ -227,11 +366,11 @@ namespace Barotrauma.Lights
spriteBatch.End(); spriteBatch.End();
} }
public void DrawLOS(GraphicsDevice graphics, SpriteBatch spriteBatch, Camera cam, Effect effect) public void DrawLOS(SpriteBatch spriteBatch, Effect effect)
{ {
if (!LosEnabled || ViewTarget == null) return; if (!LosEnabled || ViewTarget == null) return;
spriteBatch.Begin(SpriteSortMode.Deferred, CustomBlendStates.Multiplicative, null, null, null); spriteBatch.Begin(SpriteSortMode.Deferred, CustomBlendStates.Multiplicative, null, null, null, effect);
spriteBatch.Draw(losTexture, Vector2.Zero, Color.White); spriteBatch.Draw(losTexture, Vector2.Zero, Color.White);
spriteBatch.End(); spriteBatch.End();
@@ -264,9 +403,6 @@ namespace Barotrauma.Lights
public static BlendState Multiplicative { get; private set; } public static BlendState Multiplicative { get; private set; }
public static BlendState WriteToAlpha { get; private set; } public static BlendState WriteToAlpha { get; private set; }
public static BlendState MultiplyWithAlpha { get; private set; } public static BlendState MultiplyWithAlpha { get; private set; }
} }
} }
+4 -3
View File
@@ -306,20 +306,21 @@ namespace Barotrauma.Lights
Vector2 center = new Vector2(LightTexture.Width / 2, LightTexture.Height / 2); Vector2 center = new Vector2(LightTexture.Width / 2, LightTexture.Height / 2);
float scale = range / (lightTexture.Width / 2.0f); float scale = range / (lightTexture.Width / 2.0f);
spriteBatch.Draw(lightTexture, new Vector2(WorldPosition.X, -WorldPosition.Y), null, color, 0, center, scale, SpriteEffects.None, 1); spriteBatch.Draw(lightTexture, new Vector2(WorldPosition.X, -WorldPosition.Y), null, color * (color.A / 255.0f), 0, center, scale, SpriteEffects.None, 1);
} }
else else
{ {
overrideLightTexture.Draw(spriteBatch, overrideLightTexture.Draw(spriteBatch,
new Vector2(WorldPosition.X, -WorldPosition.Y), Color, new Vector2(WorldPosition.X, -WorldPosition.Y), color * (color.A / 255.0f),
overrideLightTexture.Origin, -Rotation, overrideLightTexture.Origin, -Rotation,
new Vector2(overrideLightTexture.size.X/overrideLightTexture.SourceRect.Width, overrideLightTexture.size.Y/overrideLightTexture.SourceRect.Height)); new Vector2(overrideLightTexture.size.X / overrideLightTexture.SourceRect.Width, overrideLightTexture.size.Y / overrideLightTexture.SourceRect.Height));
} }
} }
if (LightSprite != null) if (LightSprite != null)
{ {
LightSprite.Draw(spriteBatch, new Vector2(WorldPosition.X, -WorldPosition.Y), Color, LightSprite.Origin); LightSprite.Draw(spriteBatch, new Vector2(WorldPosition.X, -WorldPosition.Y), Color, LightSprite.Origin);
} }
} }
+5 -4
View File
@@ -128,6 +128,8 @@ namespace Barotrauma
{ {
cam.TargetPos = Lights.LightManager.ViewTarget.WorldPosition; cam.TargetPos = Lights.LightManager.ViewTarget.WorldPosition;
} }
GameMain.LightManager.Update((float)deltaTime);
cam.MoveCamera((float)deltaTime); cam.MoveCamera((float)deltaTime);
foreach (Submarine sub in Submarine.Loaded) foreach (Submarine sub in Submarine.Loaded)
@@ -205,7 +207,7 @@ namespace Barotrauma
GameMain.LightManager.ObstructVision = Character.Controlled != null && Character.Controlled.ObstructVision; GameMain.LightManager.ObstructVision = Character.Controlled != null && Character.Controlled.ObstructVision;
GameMain.LightManager.UpdateLightMap(graphics, spriteBatch, cam); GameMain.LightManager.UpdateLightMap(graphics, spriteBatch, cam, lightBlur.Effect);
if (Character.Controlled != null) if (Character.Controlled != null)
{ {
GameMain.LightManager.UpdateObstructVision(graphics, spriteBatch, cam, Character.Controlled.CursorWorldPosition); GameMain.LightManager.UpdateObstructVision(graphics, spriteBatch, cam, Character.Controlled.CursorWorldPosition);
@@ -339,8 +341,7 @@ namespace Barotrauma
spriteBatch.End(); spriteBatch.End();
GameMain.LightManager.DrawLightMap(spriteBatch, lightBlur.Effect);
GameMain.LightManager.DrawLightMap(spriteBatch, cam, lightBlur.Effect);
spriteBatch.Begin(SpriteSortMode.BackToFront, spriteBatch.Begin(SpriteSortMode.BackToFront,
BlendState.AlphaBlend, SamplerState.LinearWrap, BlendState.AlphaBlend, SamplerState.LinearWrap,
@@ -355,7 +356,7 @@ namespace Barotrauma
if (Character.Controlled != null) if (Character.Controlled != null)
{ {
GameMain.LightManager.DrawLOS(graphics, spriteBatch, cam, lightBlur.Effect); GameMain.LightManager.DrawLOS(spriteBatch, lightBlur.Effect);
} }
} }
Binary file not shown.