38f1ddb...178a853: v0.8.9.1, removed content folder

This commit is contained in:
Joonas Rikkonen
2019-03-18 19:46:58 +02:00
parent 38f1ddb6fe
commit 6c0679c297
1054 changed files with 151673 additions and 144931 deletions
@@ -41,9 +41,17 @@ namespace Barotrauma.Lights
public ConvexHull ConvexHull;
public bool IsHorizontal;
public bool IsAxisAligned;
public Segment(SegmentPoint start, SegmentPoint end, ConvexHull convexHull)
{
if (start.Pos.Y > end.Pos.Y)
{
var temp = start;
start = end;
end = temp;
}
Start = start;
End = end;
ConvexHull = convexHull;
@@ -52,6 +60,7 @@ namespace Barotrauma.Lights
end.ConvexHull = convexHull;
IsHorizontal = Math.Abs(start.Pos.X - end.Pos.X) > Math.Abs(start.Pos.Y - end.Pos.Y);
IsAxisAligned = Math.Abs(start.Pos.X - end.Pos.X) < 0.1f || Math.Abs(start.Pos.Y - end.Pos.Y) < 0.001f;
}
}
@@ -151,11 +160,12 @@ namespace Barotrauma.Lights
}
if (penumbraEffect == null)
{
penumbraEffect = new BasicEffect(GameMain.Instance.GraphicsDevice);
penumbraEffect.TextureEnabled = true;
//shadowEffect.VertexColorEnabled = true;
penumbraEffect.LightingEnabled = false;
penumbraEffect.Texture = TextureLoader.FromFile("Content/Lights/penumbra.png");
penumbraEffect = new BasicEffect(GameMain.Instance.GraphicsDevice)
{
TextureEnabled = true,
LightingEnabled = false,
Texture = TextureLoader.FromFile("Content/Lights/penumbra.png")
};
}
parentEntity = parent;
@@ -206,10 +216,23 @@ namespace Barotrauma.Lights
{
ignoreEdge[i] = true;
}
}
}
}
}
}
public void Rotate(Vector2 origin, float amount)
{
Matrix rotationMatrix = Matrix.CreateRotationZ(amount);
Vector2[] newVerts = new Vector2[vertices.Length];
for (int i = 0; i < vertices.Length; i++)
{
newVerts[i] = Vector2.Transform(vertices[i].Pos - origin, rotationMatrix) + origin;
}
SetVertices(newVerts);
}
private void CalculateDimensions()
{
float minX = vertices[0].Pos.X, minY = vertices[0].Pos.Y, maxX = vertices[0].Pos.X, maxY = vertices[0].Pos.Y;
@@ -332,10 +355,8 @@ namespace Barotrauma.Lights
/// <summary>
/// Returns the segments that are facing towards viewPosition
/// </summary>
public List<Segment> GetVisibleSegments(Vector2 viewPosition)
{
List<Segment> visibleFaces = new List<Segment>();
public void GetVisibleSegments(Vector2 viewPosition, List<Segment> visibleSegments)
{
for (int i = 0; i < 4; i++)
{
if (ignoreEdge[i]) continue;
@@ -353,23 +374,26 @@ namespace Barotrauma.Lights
if (Vector2.Dot(N, L) > 0)
{
visibleFaces.Add(segments[i]);
visibleSegments.Add(segments[i]);
}
}
return visibleFaces;
}
public void RefreshWorldPositions()
{
if (parentEntity == null || parentEntity.Submarine == null) return;
for (int i = 0; i < 4; i++)
{
vertices[i].WorldPos = vertices[i].Pos + parentEntity.Submarine.DrawPosition;
segments[i].Start.WorldPos = segments[i].Start.Pos + parentEntity.Submarine.DrawPosition;
segments[i].End.WorldPos = segments[i].End.Pos + parentEntity.Submarine.DrawPosition;
vertices[i].WorldPos = vertices[i].Pos;
segments[i].Start.WorldPos = segments[i].Start.Pos;
segments[i].End.WorldPos = segments[i].End.Pos;
}
if (parentEntity == null || parentEntity.Submarine == null) { return; }
for (int i = 0; i < 4; i++)
{
vertices[i].WorldPos += parentEntity.Submarine.DrawPosition;
segments[i].Start.WorldPos += parentEntity.Submarine.DrawPosition;
segments[i].End.WorldPos += parentEntity.Submarine.DrawPosition;
}
}
@@ -3,6 +3,8 @@ using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System.Collections.Generic;
using System.Linq;
using System;
using Barotrauma.Items.Components;
namespace Barotrauma.Lights
{
@@ -11,6 +13,15 @@ namespace Barotrauma.Lights
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 bool UseHullSpecificAmbientLight = false;
private static Entity viewTarget;
public static Entity ViewTarget
@@ -22,38 +33,44 @@ namespace Barotrauma.Lights
}
}
private float currLightMapScale;
public Color AmbientLight;
private float lightmapScale = 0.5f;
public RenderTarget2D lightMap
public RenderTarget2D LightMap
{
get;
private set;
}
public RenderTarget2D losTexture
public RenderTarget2D SpecularMap
{
get;
private set;
}
public RenderTarget2D LosTexture
{
get;
private set;
}
BasicEffect lightEffect;
private BasicEffect lightEffect;
public Effect LosEffect
{
get; private set;
}
private static Texture2D alphaClearTexture;
private List<LightSource> lights;
public bool LosEnabled = true;
public LosMode LosMode = LosMode.Transparent;
public bool LightingEnabled = true;
public bool ObstructVision;
private Texture2D visionCircle;
private Dictionary<Hull, Color> hullAmbientLights;
private Dictionary<Hull, Color> smoothedHullAmbientLights;
@@ -67,22 +84,17 @@ namespace Barotrauma.Lights
AmbientLight = new Color(20, 20, 20, 255);
visionCircle = Sprite.LoadTexture("Content/Lights/visioncircle.png");
//visionCircle = new Sprite("Content/Lights/visioncircle.png", new Vector2(0.2f, 0.5f));
var pp = graphics.PresentationParameters;
lightMap = new RenderTarget2D(graphics,
(int)(GameMain.GraphicsWidth*lightmapScale), (int)(GameMain.GraphicsHeight*lightmapScale), false,
pp.BackBufferFormat, pp.DepthStencilFormat, pp.MultiSampleCount,
RenderTargetUsage.DiscardContents);
losTexture = new RenderTarget2D(graphics, (int)(GameMain.GraphicsWidth*lightmapScale), (int)(GameMain.GraphicsHeight*lightmapScale), false, SurfaceFormat.Color, DepthFormat.None);
CreateRenderTargets(graphics);
GameMain.Instance.OnResolutionChanged += () =>
{
CreateRenderTargets(graphics);
};
#if WINDOWS
LosEffect = content.Load<Effect>("losshader");
LosEffect = content.Load<Effect>("Effects/losshader");
#else
LosEffect = content.Load<Effect>("losshader_opengl");
LosEffect = content.Load<Effect>("Effects/losshader_opengl");
#endif
if (lightEffect == null)
@@ -96,16 +108,33 @@ namespace Barotrauma.Lights
hullAmbientLights = new Dictionary<Hull, Color>();
smoothedHullAmbientLights = new Dictionary<Hull, Color>();
}
if (alphaClearTexture == null)
{
alphaClearTexture = TextureLoader.FromFile("Content/Lights/alphaOne.png");
}
private void CreateRenderTargets(GraphicsDevice graphics)
{
var pp = graphics.PresentationParameters;
currLightMapScale = GameMain.Config.LightMapScale;
LightMap?.Dispose();
LightMap = new RenderTarget2D(graphics,
(int)(GameMain.GraphicsWidth * GameMain.Config.LightMapScale), (int)(GameMain.GraphicsHeight * GameMain.Config.LightMapScale), false,
pp.BackBufferFormat, pp.DepthStencilFormat, pp.MultiSampleCount,
RenderTargetUsage.DiscardContents);
SpecularMap?.Dispose();
SpecularMap = new RenderTarget2D(graphics,
(int)(GameMain.GraphicsWidth * GameMain.Config.LightMapScale), (int)(GameMain.GraphicsHeight * GameMain.Config.LightMapScale), false,
pp.BackBufferFormat, pp.DepthStencilFormat, pp.MultiSampleCount,
RenderTargetUsage.DiscardContents);
LosTexture?.Dispose();
LosTexture = new RenderTarget2D(graphics, (int)(GameMain.GraphicsWidth * GameMain.Config.LightMapScale), (int)(GameMain.GraphicsHeight * GameMain.Config.LightMapScale), false, SurfaceFormat.Color, DepthFormat.None);
}
public void AddLight(LightSource light)
{
lights.Add(light);
if (!lights.Contains(light)) lights.Add(light);
}
public void RemoveLight(LightSource light)
@@ -124,82 +153,126 @@ namespace Barotrauma.Lights
public void Update(float deltaTime)
{
if (ambientLightUpdateTimer > 0.0f)
if (UseHullSpecificAmbientLight)
{
ambientLightUpdateTimer -= deltaTime;
}
else
{
CalculateAmbientLights();
ambientLightUpdateTimer = AmbientLightUpdateInterval;
}
foreach (Hull hull in hullAmbientLights.Keys)
{
if (!smoothedHullAmbientLights.ContainsKey(hull))
if (ambientLightUpdateTimer > 0.0f)
{
smoothedHullAmbientLights.Add(hull, Color.TransparentBlack);
ambientLightUpdateTimer -= deltaTime;
}
else
{
CalculateAmbientLights();
ambientLightUpdateTimer = AmbientLightUpdateInterval;
}
}
foreach (Hull hull in smoothedHullAmbientLights.Keys.ToList())
{
Color targetColor = Color.TransparentBlack;
foreach (Hull hull in hullAmbientLights.Keys)
{
if (!smoothedHullAmbientLights.ContainsKey(hull))
{
smoothedHullAmbientLights.Add(hull, Color.TransparentBlack);
}
}
hullAmbientLights.TryGetValue(hull, out targetColor);
smoothedHullAmbientLights[hull] = Color.Lerp(smoothedHullAmbientLights[hull], targetColor, deltaTime);
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)
private List<LightSource> activeLights = new List<LightSource>(capacity: 100);
public void UpdateLightMap(GraphicsDevice graphics, SpriteBatch spriteBatch, Camera cam, RenderTarget2D backgroundObstructor = null)
{
if (!LightingEnabled) return;
graphics.SetRenderTarget(lightMap);
Rectangle viewRect = cam.WorldView;
viewRect.Y -= cam.WorldView.Height;
//clear to some small ambient light
graphics.Clear(AmbientLight);
graphics.BlendState = BlendState.Additive;
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive, null, null, null, null, cam.Transform * Matrix.CreateScale(new Vector3(lightmapScale, lightmapScale, 1.0f)));
if (Math.Abs(currLightMapScale - GameMain.Config.LightMapScale) > 0.01f)
{
//lightmap scale has changed -> recreate render targets
CreateRenderTargets(graphics);
}
Matrix spriteBatchTransform = cam.Transform * Matrix.CreateScale(new Vector3(GameMain.Config.LightMapScale, GameMain.Config.LightMapScale, 1.0f));
Matrix transform = cam.ShaderTransform
* Matrix.CreateOrthographic(GameMain.GraphicsWidth, GameMain.GraphicsHeight, -1, 1) * 0.5f;
Vector3 offset = Vector3.Zero;// new Vector3(Submarine.MainSub.DrawPosition.X, Submarine.MainSub.DrawPosition.Y, 0.0f);
if (GameMain.Config.SpecularityEnabled)
{
UpdateSpecularMap(graphics, spriteBatch, spriteBatchTransform, cam, backgroundObstructor);
}
graphics.SetRenderTarget(LightMap);
Rectangle viewRect = cam.WorldView;
viewRect.Y -= cam.WorldView.Height;
//check which lights need to be drawn
activeLights.Clear();
foreach (LightSource light in lights)
{
if (light.Color.A < 1 || light.Range < 1.0f || !light.Enabled) continue;
if (!MathUtils.CircleIntersectsRectangle(light.WorldPosition, light.Range, viewRect)) continue;
light.Draw(spriteBatch, lightEffect, transform);
activeLights.Add(light);
}
lightEffect.World = Matrix.CreateTranslation(offset) * transform;
GameMain.ParticleManager.Draw(spriteBatch, false, null, Particles.ParticleBlendState.Additive);
foreach (Hull hull in smoothedHullAmbientLights.Keys)
//clear the lightmap
graphics.Clear(Color.Black);
graphics.BlendState = BlendState.Additive;
//draw background lights
//---------------------------------------------------------------------------------------------------
bool backgroundSpritesDrawn = false;
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive, transformMatrix: spriteBatchTransform);
foreach (LightSource light in activeLights)
{
if (smoothedHullAmbientLights[hull].A < 0.01f) continue;
if (!light.IsBackground) continue;
light.DrawSprite(spriteBatch, cam);
light.DrawLightVolume(spriteBatch, lightEffect, transform);
backgroundSpritesDrawn = true;
}
GameMain.ParticleManager.Draw(spriteBatch, true, null, Particles.ParticleBlendState.Additive);
spriteBatch.End();
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);
//draw a black rectangle on hulls to hide background lights behind subs
//---------------------------------------------------------------------------------------------------
GUI.DrawRectangle(spriteBatch,
new Vector2(drawRect.X, -drawRect.Y),
new Vector2(hull.Rect.Width, hull.Rect.Height),
smoothedHullAmbientLights[hull], true);
Dictionary<Hull, Rectangle> visibleHulls = null;
if (backgroundSpritesDrawn)
{
if (backgroundObstructor != null)
{
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
spriteBatch.Draw(backgroundObstructor, new Rectangle(0, 0,
(int)(GameMain.GraphicsWidth * currLightMapScale), (int)(GameMain.GraphicsHeight * currLightMapScale)), Color.Black);
spriteBatch.End();
}
else
{
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.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)
//---------------------------------------------------------------------------------------------------
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive, transformMatrix: spriteBatchTransform);
if (Character.Controlled != null)
{
if (Character.Controlled.FocusedItem != null)
@@ -208,7 +281,7 @@ namespace Barotrauma.Lights
}
if (Character.Controlled.FocusedCharacter != null)
{
Character.Controlled.FocusedCharacter.Draw(spriteBatch);
Character.Controlled.FocusedCharacter.Draw(spriteBatch, cam);
}
foreach (Item item in Item.ItemList)
@@ -218,33 +291,182 @@ namespace Barotrauma.Lights
item.Draw(spriteBatch, false, true);
}
}
}
Vector2 drawPos = Character.Controlled.DrawPosition;
drawPos.Y = -drawPos.Y;
foreach (LightSource light in activeLights)
{
if (light.IsBackground) continue;
light.DrawSprite(spriteBatch, cam);
}
spriteBatch.End();
//draw characters to obstruct the highlighted items/characters and light sprites
//---------------------------------------------------------------------------------------------------
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, transformMatrix: spriteBatchTransform);
foreach (Character character in Character.CharacterList)
{
if (character.CurrentHull == null || !character.Enabled) continue;
if (Character.Controlled?.FocusedCharacter == character) continue;
foreach (Limb limb in character.AnimController.Limbs)
{
limb.Draw(spriteBatch, cam, Color.Black);
}
}
spriteBatch.End();
graphics.BlendState = BlendState.Additive;
//draw the actual light volumes, additive particles, hull ambient lights and the halo around the player
//---------------------------------------------------------------------------------------------------
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);
foreach (ElectricalDischarger discharger in ElectricalDischarger.List)
{
discharger.DrawElectricity(spriteBatch);
}
foreach (LightSource light in activeLights)
{
if (light.IsBackground) continue;
light.DrawLightVolume(spriteBatch, lightEffect, transform);
}
Vector3 offset = Vector3.Zero;// new Vector3(Submarine.MainSub.DrawPosition.X, Submarine.MainSub.DrawPosition.Y, 0.0f);
lightEffect.World = Matrix.CreateTranslation(Vector3.Zero) * 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;
haloDrawPos.Y = -haloDrawPos.Y;
//ambient light decreases the brightness of the halo (no need for a bright halo if the ambient light is bright enough)
float ambientBrightness = (AmbientLight.R + AmbientLight.B + AmbientLight.G) / 255.0f / 3.0f;
Color haloColor = Color.White * (1.0f - ambientBrightness);
spriteBatch.Draw(
LightSource.LightTexture, drawPos, null, haloColor * 0.4f, 0.0f,
new Vector2(LightSource.LightTexture.Width / 2, LightSource.LightTexture.Height / 2), 1.0f, SpriteEffects.None, 0.0f);
Color haloColor = Color.White * (0.4f - ambientBrightness);
if (haloColor.A > 0)
{
spriteBatch.Draw(
LightSource.LightTexture, haloDrawPos, null, haloColor, 0.0f,
new Vector2(LightSource.LightTexture.Width / 2, LightSource.LightTexture.Height / 2), 1.0f, SpriteEffects.None, 0.0f);
}
}
spriteBatch.End();
if (GameMain.Config.SpecularityEnabled)
{
spriteBatch.Begin(blendState: CustomBlendStates.Multiplicative);
spriteBatch.Draw(SpecularMap, Vector2.Zero, Color.White);
//spriteBatch.Draw(SpecularMap, Vector2.Zero, Color.White);
spriteBatch.End();
}
//draw the actual light volumes, additive particles, hull ambient lights and the halo around the player
//---------------------------------------------------------------------------------------------------
graphics.SetRenderTarget(null);
graphics.BlendState = BlendState.AlphaBlend;
}
public void UpdateSpecularMap(GraphicsDevice graphics, SpriteBatch spriteBatch, Matrix spriteBatchTransform, Camera cam, RenderTarget2D backgroundObstructor = null)
{
graphics.SetRenderTarget(SpecularMap);
//clear the lightmap
graphics.Clear(Color.Gray);
graphics.BlendState = BlendState.AlphaBlend;
spriteBatch.Begin(sortMode: SpriteSortMode.Deferred, blendState: BlendState.AlphaBlend, transformMatrix: spriteBatchTransform);
if (Level.Loaded != null)
{
Level.Loaded.LevelObjectManager.DrawObjects(spriteBatch, cam, drawFront: false, specular: true);
}
Dictionary<Hull, Rectangle> visibleHulls = new Dictionary<Hull, Rectangle>();
foreach (Hull hull in Hull.hullList)
{
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);
if (drawRect.Right < cam.WorldView.X || drawRect.X > cam.WorldView.Right ||
drawRect.Y - drawRect.Height > cam.WorldView.Y || drawRect.Y < cam.WorldView.Y - cam.WorldView.Height)
{
continue;
}
visibleHulls.Add(hull, drawRect);
}
foreach (Rectangle drawRect in visibleHulls.Values)
{
GUI.DrawRectangle(spriteBatch,
new Vector2(drawRect.X, -drawRect.Y),
new Vector2(drawRect.Width, drawRect.Height),
Color.Gray, true);
}
spriteBatch.End();
//TODO: use renderTargetFront to obstruct the things behind the sub (has to be drawn with a solid gray color)
/*spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
spriteBatch.Draw(renderTargetFront, new Rectangle(0, 0,
(int)(GameMain.GraphicsWidth * currLightMapScale), (int)(GameMain.GraphicsHeight * currLightMapScale)), Color.White);
spriteBatch.End();*/
//TODO: specular maps for level walls
Level.Loaded?.Renderer?.RenderWalls(graphics, cam, specular: true);
graphics.SetRenderTarget(null);
graphics.BlendState = BlendState.AlphaBlend;
}
private Dictionary<Hull, Rectangle> GetVisibleHulls(Camera cam)
{
Dictionary<Hull, Rectangle> visibleHulls = new Dictionary<Hull, Rectangle>();
foreach (Hull hull in Hull.hullList)
{
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);
if (drawRect.Right < cam.WorldView.X || drawRect.X > cam.WorldView.Right ||
drawRect.Y - drawRect.Height > cam.WorldView.Y || drawRect.Y < cam.WorldView.Y - cam.WorldView.Height)
{
continue;
}
visibleHulls.Add(hull, drawRect);
}
return visibleHulls;
}
public void UpdateObstructVision(GraphicsDevice graphics, SpriteBatch spriteBatch, Camera cam, Vector2 lookAtPosition)
{
if (!LosEnabled && !ObstructVision) return;
if ((!LosEnabled || LosMode == LosMode.None) && !ObstructVision) return;
if (ViewTarget == null) return;
graphics.SetRenderTarget(losTexture);
spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, cam.Transform * Matrix.CreateScale(new Vector3(lightmapScale, lightmapScale, 1.0f)));
graphics.SetRenderTarget(LosTexture);
spriteBatch.Begin(SpriteSortMode.Deferred, transformMatrix: cam.Transform * Matrix.CreateScale(new Vector3(GameMain.Config.LightMapScale, GameMain.Config.LightMapScale, 1.0f)));
if (ObstructVision)
{
graphics.Clear(Color.Black);
@@ -256,7 +478,7 @@ namespace Barotrauma.Lights
MathHelper.Clamp(diff.Length() / 256.0f, 2.0f, 5.0f), 2.0f);
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(visionCircle.Width * 0.2f, visionCircle.Height / 2), scale, SpriteEffects.None, 0.0f);
}
else
{
@@ -267,7 +489,7 @@ namespace Barotrauma.Lights
//--------------------------------------
if (LosEnabled && ViewTarget != null)
if (LosEnabled && LosMode != LosMode.None && ViewTarget != null)
{
Vector2 pos = ViewTarget.WorldPosition;
@@ -335,7 +557,7 @@ namespace Barotrauma.Lights
foreach (LightSource light in lights)
{
if (light.Color.A < 1f || light.Range < 1.0f) continue;
if (light.Color.A < 1f || light.Range < 1.0f || light.IsBackground) continue;
var newAmbientLights = AmbientLightHulls(light);
foreach (Hull hull in newAmbientLights.Keys)
@@ -367,7 +589,7 @@ namespace Barotrauma.Lights
var hull = Hull.FindHull(light.WorldPosition);
if (hull == null) return hullAmbientLight;
return AmbientLightHulls(hull, hullAmbientLight, light.Color * (light.Range / 2000.0f));
return AmbientLightHulls(hull, hullAmbientLight, light.Color * Math.Min(light.Range / 1000.0f, 1.0f));
}
/// <summary>
@@ -419,27 +641,14 @@ namespace Barotrauma.Lights
{
static CustomBlendStates()
{
Multiplicative = new BlendState();
Multiplicative.ColorSourceBlend = Multiplicative.AlphaSourceBlend = Blend.Zero;
Multiplicative.ColorDestinationBlend = Multiplicative.AlphaDestinationBlend = Blend.SourceColor;
Multiplicative.ColorBlendFunction = Multiplicative.AlphaBlendFunction = BlendFunction.Add;
WriteToAlpha = new BlendState();
WriteToAlpha.ColorWriteChannels = ColorWriteChannels.Alpha;
MultiplyWithAlpha = new BlendState();
MultiplyWithAlpha.ColorDestinationBlend = MultiplyWithAlpha.AlphaDestinationBlend = Blend.One;
MultiplyWithAlpha.ColorSourceBlend = MultiplyWithAlpha.AlphaSourceBlend = Blend.DestinationAlpha;
LOS = new BlendState();
LOS.ColorSourceBlend = LOS.AlphaSourceBlend = Blend.Zero;
LOS.ColorDestinationBlend = LOS.AlphaDestinationBlend = Blend.InverseSourceColor;
LOS.ColorBlendFunction = LOS.AlphaBlendFunction = BlendFunction.Add;
Multiplicative = new BlendState
{
ColorSourceBlend = Blend.DestinationColor,
ColorDestinationBlend = Blend.SourceColor,
ColorBlendFunction = BlendFunction.Add
};
}
public static BlendState Multiplicative { get; private set; }
public static BlendState WriteToAlpha { get; private set; }
public static BlendState MultiplyWithAlpha { get; private set; }
public static BlendState LOS { get; private set; }
}
}
@@ -8,31 +8,121 @@ using System.Xml.Linq;
namespace Barotrauma.Lights
{
class LightSource
class LightSourceParams : ISerializableEntity
{
private static Texture2D lightTexture;
public string Name => "LightSource";
private List<ConvexHullList> hullsInRange;
public bool Persistent;
public Dictionary<string, SerializableProperty> SerializableProperties
{
get;
private set;
} = new Dictionary<string, SerializableProperty>();
[Serialize("1.0,1.0,1.0,1.0", true), Editable]
public Color Color
{
get;
set;
}
private Color color;
private float range;
[Serialize(100.0f, true), Editable(MinValueFloat = 0.0f, MaxValueFloat = 2048.0f)]
public float Range
{
get { return range; }
set
{
range = MathHelper.Clamp(value, 0.0f, 2048.0f);
}
}
public Sprite overrideLightTexture;
public Texture2D texture;
public Sprite OverrideLightTexture
{
get;
private set;
}
//Additional sprite drawn on top of the lightsource. Ignores shadows.
//Can be used to make lamp sprites glow for example.
public Sprite LightSprite;
public Sprite LightSprite
{
get;
private set;
}
public XElement DeformableLightSpriteElement
{
get;
private set;
}
//Override the alpha value of the light sprite (if not set, the alpha of the light color is used)
//Can be used to make lamp sprites glow at full brightness even if the light itself is dim.
public float? OverrideLightSpriteAlpha;
public LightSourceParams(XElement element)
{
SerializableProperties = SerializableProperty.DeserializeProperties(this, element);
foreach (XElement subElement in element.Elements())
{
switch (subElement.Name.ToString().ToLowerInvariant())
{
case "sprite":
{
LightSprite = new Sprite(subElement);
float spriteAlpha = subElement.GetAttributeFloat("alpha", -1.0f);
if (spriteAlpha >= 0.0f)
{
OverrideLightSpriteAlpha = spriteAlpha;
}
}
break;
case "deformablesprite":
{
DeformableLightSpriteElement = subElement;
float spriteAlpha = subElement.GetAttributeFloat("alpha", -1.0f);
if (spriteAlpha >= 0.0f)
{
OverrideLightSpriteAlpha = spriteAlpha;
}
}
break;
case "lighttexture":
OverrideLightTexture = new Sprite(subElement);
break;
}
}
}
public LightSourceParams(float range, Color color)
{
SerializableProperties = SerializableProperty.DeserializeProperties(this);
Range = range;
Color = color;
}
}
class LightSource
{
private static Texture2D lightTexture;
private List<ConvexHullList> hullsInRange;
public Texture2D texture;
public SpriteEffects LightSpriteEffect;
public Submarine ParentSub;
public bool CastShadows;
private bool castShadows;
public bool CastShadows
{
get { return castShadows && !IsBackground; }
set { castShadows = value; }
}
//what was the range of the light when lightvolumes were last calculated
private float prevCalculatedRange;
@@ -69,13 +159,17 @@ namespace Barotrauma.Lights
private int vertexCount;
private int indexCount;
private readonly LightSourceParams lightSourceParams;
public LightSourceParams LightSourceParams => lightSourceParams;
private Vector2 position;
public Vector2 Position
{
get { return position; }
set
{
if (position == value) return;
if (Math.Abs(position.X - value.X) < 0.1f && Math.Abs(position.Y - value.Y) < 0.1f) return;
position = value;
if (Vector2.DistanceSquared(prevCalculatedPosition, position) < 5.0f * 5.0f) return;
@@ -92,7 +186,7 @@ namespace Barotrauma.Lights
get { return rotation; }
set
{
if (rotation == value) return;
if (Math.Abs(rotation - value) < 0.01f) return;
rotation = value;
NeedsHullCheck = true;
@@ -100,6 +194,12 @@ namespace Barotrauma.Lights
}
}
public Vector2 SpriteScale
{
get;
set;
} = Vector2.One;
public Vector2 WorldPosition
{
get { return (ParentSub == null) ? position : position + ParentSub.Position; }
@@ -111,81 +211,93 @@ namespace Barotrauma.Lights
{
if (lightTexture == null)
{
lightTexture = TextureLoader.FromFile("Content/Lights/light.png");
lightTexture = TextureLoader.FromFile("Content/Lights/pointlight_bright.png");
}
return lightTexture;
}
}
public Color Color
public Sprite OverrideLightTexture
{
get { return color; }
set { color = value; }
get { return lightSourceParams.OverrideLightTexture; }
}
public Sprite LightSprite
{
get { return lightSourceParams.LightSprite; }
}
public Color Color
{
get { return lightSourceParams.Color; }
set { lightSourceParams.Color = value; }
}
public float Range
{
get { return range; }
get { return lightSourceParams.Range; }
set
{
range = MathHelper.Clamp(value, 0.0f, 2048.0f);
if (Math.Abs(prevCalculatedRange - range) < 10.0f) return;
lightSourceParams.Range = value;
if (Math.Abs(prevCalculatedRange - lightSourceParams.Range) < 10.0f) return;
NeedsHullCheck = true;
NeedsRecalculation = true;
prevCalculatedRange = range;
prevCalculatedRange = lightSourceParams.Range;
}
}
/// <summary>
/// Background lights are drawn behind submarines and they don't cast shadows.
/// </summary>
public bool IsBackground
{
get;
set;
}
public DeformableSprite DeformableLightSprite
{
get;
private set;
}
public bool Enabled = true;
public LightSource (XElement element)
: this(Vector2.Zero, 100.0f, Color.White, null)
{
range = element.GetAttributeFloat("range", 100.0f);
color = new Color(element.GetAttributeVector4("color", Vector4.One));
lightSourceParams = new LightSourceParams(element);
CastShadows = element.GetAttributeBool("castshadows", true);
foreach (XElement subElement in element.Elements())
if (lightSourceParams.DeformableLightSpriteElement != null)
{
switch (subElement.Name.ToString().ToLowerInvariant())
{
case "sprite":
LightSprite = new Sprite(subElement);
DeformableLightSprite = new DeformableSprite(lightSourceParams.DeformableLightSpriteElement);
}
}
float spriteAlpha = subElement.GetAttributeFloat("alpha", -1.0f);
if (spriteAlpha >= 0.0f)
{
OverrideLightSpriteAlpha = spriteAlpha;
}
break;
case "lighttexture":
overrideLightTexture = new Sprite(subElement);
break;
}
public LightSource(LightSourceParams lightSourceParams)
: this(Vector2.Zero, 100.0f, Color.White, null)
{
this.lightSourceParams = lightSourceParams;
lightSourceParams.Persistent = true;
if (lightSourceParams.DeformableLightSpriteElement != null)
{
DeformableLightSprite = new DeformableSprite(lightSourceParams.DeformableLightSpriteElement);
}
}
public LightSource(Vector2 position, float range, Color color, Submarine submarine, bool addLight=true)
{
hullsInRange = new List<ConvexHullList>();
this.ParentSub = submarine;
this.position = position;
this.range = range;
this.color = color;
CastShadows = true;
lightSourceParams = new LightSourceParams(range, color);
CastShadows = true;
texture = LightTexture;
diffToSub = new Dictionary<Submarine, Vector2>();
if (addLight) GameMain.LightManager.AddLight(this);
}
@@ -197,7 +309,7 @@ namespace Barotrauma.Lights
var fullChList = ConvexHull.HullLists.Find(x => x.Submarine == sub);
if (fullChList == null) return;
chList.List = fullChList.List.FindAll(ch => ch.Enabled && MathUtils.CircleIntersectsRectangle(lightPos, range, ch.BoundingBox));
chList.List = fullChList.List.FindAll(ch => ch.Enabled && MathUtils.CircleIntersectsRectangle(lightPos, Range, ch.BoundingBox));
NeedsHullCheck = true;
}
@@ -249,7 +361,7 @@ namespace Barotrauma.Lights
subBorders.Location += sub.HiddenSubPosition.ToPoint() - new Point(0, sub.Borders.Height);
//only draw if the light overlaps with the sub
if (!MathUtils.CircleIntersectsRectangle(lightPos, range, subBorders))
if (!MathUtils.CircleIntersectsRectangle(lightPos, Range, subBorders))
{
if (chList.List.Count > 0) NeedsRecalculation = true;
chList.List.Clear();
@@ -283,7 +395,7 @@ namespace Barotrauma.Lights
subBorders.Location += sub.HiddenSubPosition.ToPoint() - new Point(0, sub.Borders.Height);
//don't draw any shadows if the light doesn't overlap with the borders of the sub
if (!MathUtils.CircleIntersectsRectangle(lightPos, range, subBorders))
if (!MathUtils.CircleIntersectsRectangle(lightPos, Range, subBorders))
{
if (chList.List.Count > 0) NeedsRecalculation = true;
chList.List.Clear();
@@ -316,19 +428,17 @@ namespace Barotrauma.Lights
{
return null;
}
if (range < 1.0f || color.A < 0.01f) return null;
if (Range < 1.0f || Color.A < 0.01f) return null;
Vector2 drawPos = position;
if (ParentSub != null) drawPos += ParentSub.DrawPosition;
var hulls = new List<ConvexHull>();// ConvexHull.GetHullsInRange(position, range, ParentSub);
var hulls = new List<ConvexHull>();
foreach (ConvexHullList chList in hullsInRange)
{
//hulls.AddRange(chList.List);
foreach (ConvexHull hull in chList.List)
{
if (!chList.IsHidden.Contains(hull)) hulls.Add(hull);
//hulls.Add(hull);
}
foreach (ConvexHull hull in chList.List)
{
@@ -336,16 +446,14 @@ namespace Barotrauma.Lights
}
}
float bounds = range * 2;
float bounds = Range * 2;
//find convexhull segments that are close enough and facing towards the light source
List<Segment> visibleSegments = new List<Segment>();
List<SegmentPoint> points = new List<SegmentPoint>();
foreach (ConvexHull hull in hulls)
{
hull.RefreshWorldPositions();
var visibleHullSegments = hull.GetVisibleSegments(drawPos);
visibleSegments.AddRange(visibleHullSegments);
hull.GetVisibleSegments(drawPos, visibleSegments);
}
//Generate new points at the intersections between segments
@@ -355,9 +463,15 @@ namespace Barotrauma.Lights
Vector2 p1a = visibleSegments[i].Start.WorldPos;
Vector2 p1b = visibleSegments[i].End.WorldPos;
for (int j = 0; j < visibleSegments.Count; j++)
for (int j = i + 1; j < visibleSegments.Count; j++)
{
if (j == i) continue;
//ignore intersections between parallel axis-aligned segments
if (visibleSegments[i].IsAxisAligned && visibleSegments[j].IsAxisAligned &&
visibleSegments[i].IsHorizontal == visibleSegments[j].IsHorizontal)
{
continue;
}
Vector2 p2a = visibleSegments[j].Start.WorldPos;
Vector2 p2b = visibleSegments[j].End.WorldPos;
@@ -369,15 +483,30 @@ namespace Barotrauma.Lights
continue;
}
Vector2? intersection = MathUtils.GetLineIntersection(p1a, p1b, p2a, p2b);
if (intersection != null)
bool intersects;
Vector2 intersection = Vector2.Zero;
if (visibleSegments[i].IsAxisAligned)
{
Vector2 intersectionVal = intersection.Value;
intersects = MathUtils.GetAxisAlignedLineIntersection(p2a, p2b, p1a, p1b, visibleSegments[i].IsHorizontal, out intersection);
}
else if (visibleSegments[j].IsAxisAligned)
{
intersects = MathUtils.GetAxisAlignedLineIntersection(p1a, p1b, p2a, p2b, visibleSegments[j].IsHorizontal, out intersection);
}
else
{
intersects = MathUtils.GetLineIntersection(p1a, p1b, p2a, p2b, out intersection);
}
if (intersects)
{
SegmentPoint start = visibleSegments[i].Start;
SegmentPoint end = visibleSegments[i].End;
SegmentPoint mid = new SegmentPoint(intersectionVal, null);
SegmentPoint mid = new SegmentPoint(intersection, null);
if (visibleSegments[i].ConvexHull?.ParentEntity?.Submarine != null)
{
mid.Pos -= visibleSegments[i].ConvexHull.ParentEntity.Submarine.DrawPosition;
}
if (Vector2.DistanceSquared(start.WorldPos, mid.WorldPos) < 25.0f ||
Vector2.DistanceSquared(end.WorldPos, mid.WorldPos) < 25.0f)
@@ -385,10 +514,17 @@ namespace Barotrauma.Lights
continue;
}
Segment seg1 = new Segment(start, mid, visibleSegments[i].ConvexHull); seg1.IsHorizontal = visibleSegments[i].IsHorizontal;
Segment seg2 = new Segment(mid, end, visibleSegments[i].ConvexHull); seg2.IsHorizontal = visibleSegments[i].IsHorizontal;
Segment seg1 = new Segment(start, mid, visibleSegments[i].ConvexHull)
{
IsHorizontal = visibleSegments[i].IsHorizontal,
};
Segment seg2 = new Segment(mid, end, visibleSegments[i].ConvexHull)
{
IsHorizontal = visibleSegments[i].IsHorizontal
};
visibleSegments[i] = seg1;
visibleSegments.Insert(i+1,seg2);
visibleSegments.Insert(i + 1, seg2);
i--;
break;
}
@@ -430,14 +566,14 @@ namespace Barotrauma.Lights
}
catch (Exception e)
{
StringBuilder sb = new StringBuilder("Constructing light volumes failed! Light pos: "+drawPos+", Hull verts:\n");
StringBuilder sb = new StringBuilder("Constructing light volumes failed! Light pos: " + drawPos + ", Hull verts:\n");
foreach (SegmentPoint sp in points)
{
sb.AppendLine(sp.Pos.ToString());
}
DebugConsole.ThrowError(sb.ToString(), e);
}
List<Vector2> output = new List<Vector2>();
//List<Pair<int, Vector2>> preOutput = new List<Pair<int, Vector2>>();
@@ -514,21 +650,47 @@ namespace Barotrauma.Lights
return output;
}
private Pair<int,Vector2> RayCast(Vector2 rayStart, Vector2 rayEnd, List<Segment> segments)
private Pair<int, Vector2> RayCast(Vector2 rayStart, Vector2 rayEnd, List<Segment> segments)
{
float closestDist = 0.0f;
float closestDist = float.PositiveInfinity;
Vector2? closestIntersection = null;
int segment = -1;
for (int i=0;i<segments.Count;i++)
float minX = Math.Min(rayStart.X, rayEnd.X);
float maxX = Math.Max(rayStart.X, rayEnd.X);
float minY = Math.Min(rayStart.Y, rayEnd.Y);
float maxY = Math.Max(rayStart.Y, rayEnd.Y);
for (int i = 0; i < segments.Count; i++)
{
Segment s = segments[i];
Vector2? intersection = MathUtils.GetAxisAlignedLineIntersection(rayStart, rayEnd, s.Start.WorldPos, s.End.WorldPos, s.IsHorizontal);
if (intersection != null)
//segment's end position always has a higher or equal y coordinate than the start position
//so we can do this comparison and skip segments that are at the wrong side of the ray
if (s.End.WorldPos.Y < s.Start.WorldPos.Y)
{
float dist = Vector2.DistanceSquared((Vector2)intersection, rayStart);
if (closestIntersection == null || dist < closestDist)
System.Diagnostics.Debug.Assert(s.End.WorldPos.Y >= s.Start.WorldPos.Y,
"LightSource raycast failed. Segment's end positions should never be below the start position. Parent entity: " + (s.ConvexHull?.ParentEntity == null ? "null" : s.ConvexHull.ParentEntity.ToString()));
}
if (s.Start.WorldPos.Y > maxY || s.End.WorldPos.Y < minY) { continue; }
//same for the x-axis
if (s.Start.WorldPos.X > s.End.WorldPos.X)
{
if (s.Start.WorldPos.X < minX) continue;
if (s.End.WorldPos.X > maxX) continue;
}
else
{
if (s.End.WorldPos.X < minX) continue;
if (s.Start.WorldPos.X > maxX) continue;
}
if (s.IsAxisAligned ?
MathUtils.GetAxisAlignedLineIntersection(rayStart, rayEnd, s.Start.WorldPos, s.End.WorldPos, s.IsHorizontal, out Vector2 intersection) :
MathUtils.GetLineIntersection(rayStart, rayEnd, s.Start.WorldPos, s.End.WorldPos, out intersection))
{
float dist = Vector2.DistanceSquared(intersection, rayStart);
if (dist < closestDist)
{
closestDist = dist;
closestIntersection = intersection;
@@ -536,10 +698,8 @@ namespace Barotrauma.Lights
}
}
}
Pair<int,Vector2> retVal = new Pair<int,Vector2>();
retVal.Second = closestIntersection == null ? rayEnd : (Vector2)closestIntersection;
retVal.First = segment;
Pair<int, Vector2> retVal = new Pair<int, Vector2>(segment, closestIntersection == null ? rayEnd : (Vector2)closestIntersection);
return retVal;
}
@@ -555,15 +715,27 @@ namespace Barotrauma.Lights
Vector2 uvOffset = Vector2.Zero;
Vector2 overrideTextureDims = Vector2.One;
if (overrideLightTexture != null)
if (OverrideLightTexture != null)
{
overrideTextureDims = new Vector2(overrideLightTexture.SourceRect.Width, overrideLightTexture.SourceRect.Height);
uvOffset = (overrideLightTexture.Origin / overrideTextureDims) - new Vector2(0.5f, 0.5f);
overrideTextureDims = new Vector2(OverrideLightTexture.SourceRect.Width, OverrideLightTexture.SourceRect.Height);
uvOffset = (OverrideLightTexture.Origin / overrideTextureDims) - new Vector2(0.5f, 0.5f);
}
// Add a vertex for the center of the mesh
vertices.Add(new VertexPositionColorTexture(new Vector3(position.X, position.Y, 0),
Color.White,new Vector2(0.5f, 0.5f) + uvOffset));
Color.White, new Vector2(0.5f, 0.5f) + uvOffset));
//hacky fix to exc excessively large light volumes (they used to be up to 4x the range of the light if there was nothing to block the rays).
//might want to tweak the raycast logic in a way that this isn't necessary
float boundRadius = Range * 1.1f / (1.0f - Math.Max(Math.Abs(uvOffset.X), Math.Abs(uvOffset.Y)));
Rectangle boundArea = new Rectangle((int)(drawPos.X - boundRadius), (int)(drawPos.Y + boundRadius), (int)(boundRadius * 2), (int)(boundRadius * 2));
for (int i = 0; i < rayCastHits.Count; i++)
{
if (MathUtils.GetLineRectangleIntersection(drawPos, rayCastHits[i], boundArea, out Vector2 intersection))
{
rayCastHits[i] = intersection;
}
}
// Add all the other encounter points as vertices
// storing their world position as UV coordinates
@@ -578,19 +750,6 @@ namespace Barotrauma.Lights
Vector2 nextVertex = rayCastHits[i < rayCastHits.Count - 1 ? i + 1 : 0];
Vector2 rawDiff = vertex - drawPos;
Vector2 diff = rawDiff;
diff /= range * 2.0f;
if (overrideLightTexture != null)
{
//calculate texture coordinates based on the light's rotation
Vector2 originDiff = diff;
diff.X = originDiff.X * cosAngle - originDiff.Y * sinAngle;
diff.Y = originDiff.X * sinAngle + originDiff.Y * cosAngle;
diff *= (overrideTextureDims / overrideLightTexture.size) * 2.0f;
diff += uvOffset;
}
//calculate normal of first segment
Vector2 nDiff1 = vertex - nextVertex;
@@ -614,6 +773,19 @@ namespace Barotrauma.Lights
nDiff /= Math.Max(Math.Abs(nDiff.X), Math.Abs(nDiff.Y));
nDiff *= 50.0f;
Vector2 diff = rawDiff;
diff /= Range * 2.0f;
if (OverrideLightTexture != null)
{
//calculate texture coordinates based on the light's rotation
Vector2 originDiff = diff;
diff.X = originDiff.X * cosAngle - originDiff.Y * sinAngle;
diff.Y = originDiff.X * sinAngle + originDiff.Y * cosAngle;
diff *= (overrideTextureDims / OverrideLightTexture.size);// / (1.0f - Math.Max(Math.Abs(uvOffset.X), Math.Abs(uvOffset.Y)));
diff += uvOffset;
}
//finally, create the vertices
VertexPositionColorTexture fullVert = new VertexPositionColorTexture(new Vector3(position.X + rawDiff.X, position.Y + rawDiff.Y, 0),
Color.White, new Vector2(0.5f, 0.5f) + diff);
@@ -678,50 +850,90 @@ namespace Barotrauma.Lights
lightVolumeBuffer.SetData<VertexPositionColorTexture>(vertices.ToArray());
lightVolumeIndexBuffer.SetData<short>(indices.ToArray());
}
}
public void Draw(SpriteBatch spriteBatch, BasicEffect lightEffect, Matrix transform)
/// <summary>
/// Draws the optional "light sprite", just a simple sprite with no shadows
/// </summary>
/// <param name="spriteBatch"></param>
public void DrawSprite(SpriteBatch spriteBatch, Camera cam)
{
if (CastShadows)
if (DeformableLightSprite != null)
{
CheckHullsInRange();
Vector2 origin = DeformableLightSprite.Origin;
Vector2 drawPos = position;
if (ParentSub != null) drawPos += ParentSub.DrawPosition;
DeformableLightSprite.Draw(
cam, new Vector3(drawPos, 0.0f),
origin, -Rotation, SpriteScale,
new Color(Color, lightSourceParams.OverrideLightSpriteAlpha ?? Color.A / 255.0f),
LightSpriteEffect == SpriteEffects.FlipHorizontally);
}
Vector3 offset = ParentSub == null ? Vector3.Zero :
new Vector3(ParentSub.DrawPosition.X, ParentSub.DrawPosition.Y, 0.0f);
lightEffect.World = Matrix.CreateTranslation(offset) * transform;
Vector2 drawPos = position;
if (ParentSub != null) drawPos += ParentSub.DrawPosition;
drawPos.Y = -drawPos.Y;
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;
Vector2 drawPos = position;
if (ParentSub != null) drawPos += ParentSub.DrawPosition;
drawPos.Y = -drawPos.Y;
LightSprite.Draw(
spriteBatch, drawPos,
new Color(Color, OverrideLightSpriteAlpha ?? Color.A / 255.0f),
origin, -Rotation, 1, LightSpriteEffect);
new Color(Color, lightSourceParams.OverrideLightSpriteAlpha ?? Color.A / 255.0f),
origin, -Rotation, SpriteScale, LightSpriteEffect);
}
if (GameMain.DebugDraw)
{
//visualize light recalculations
float timeSinceRecalculation = (float)Timing.TotalTime - lastRecalculationTime;
if (timeSinceRecalculation < 0.1f)
{
Vector2 drawPos = position;
if (ParentSub != null) drawPos += ParentSub.DrawPosition;
drawPos.Y = -drawPos.Y;
GUI.DrawRectangle(spriteBatch, drawPos - Vector2.One * 10, Vector2.One * 20, Color.Red * (1.0f - timeSinceRecalculation * 10.0f), isFilled: true);
GUI.DrawLine(spriteBatch, drawPos - Vector2.One * Range, drawPos + Vector2.One * Range, Color);
GUI.DrawLine(spriteBatch, drawPos - new Vector2(1.0f, -1.0f) * Range, drawPos + new Vector2(1.0f, -1.0f) * Range, Color);
}
}
}
public void DrawLightVolume(SpriteBatch spriteBatch, BasicEffect lightEffect, Matrix transform)
{
if (CastShadows)
{
CheckHullsInRange();
}
//if the light doesn't cast shadows, we can simply render the texture without having to calculate the light volume
if (!CastShadows)
{
Texture2D currentTexture = texture ?? LightTexture;
if (overrideLightTexture != null) currentTexture = overrideLightTexture.Texture;
if (OverrideLightTexture != null) { currentTexture = OverrideLightTexture.Texture; }
Vector2 center = new Vector2(currentTexture.Width / 2, currentTexture.Height / 2);
float scale = range / (currentTexture.Width / 2.0f);
Vector2 center = OverrideLightTexture == null ?
new Vector2(currentTexture.Width / 2, currentTexture.Height / 2) :
OverrideLightTexture.Origin;
float scale = Range / (currentTexture.Width / 2.0f);
spriteBatch.Draw(currentTexture, drawPos, null, color * (color.A / 255.0f), 0, center, scale, SpriteEffects.None, 1);
Vector2 drawPos = position;
if (ParentSub != null) drawPos += ParentSub.DrawPosition;
drawPos.Y = -drawPos.Y;
spriteBatch.Draw(currentTexture, drawPos, null, Color, -rotation, center, scale, SpriteEffects.None, 1);
return;
}
Vector3 offset = ParentSub == null ?
Vector3.Zero : new Vector3(ParentSub.DrawPosition.X, ParentSub.DrawPosition.Y, 0.0f);
lightEffect.World = Matrix.CreateTranslation(offset) * transform;
if (NeedsRecalculation)
{
var verts = FindRaycastHits();
@@ -733,10 +945,10 @@ namespace Barotrauma.Lights
if (vertexCount == 0) return;
lightEffect.DiffuseColor = (new Vector3(color.R, color.G, color.B) * (color.A / 255.0f)) / 255.0f;
if (overrideLightTexture != null)
lightEffect.DiffuseColor = (new Vector3(Color.R, Color.G, Color.B) * (Color.A / 255.0f)) / 255.0f;
if (OverrideLightTexture != null)
{
lightEffect.Texture = overrideLightTexture.Texture;
lightEffect.Texture = OverrideLightTexture.Texture;
}
else
{
@@ -777,19 +989,20 @@ namespace Barotrauma.Lights
public void Remove()
{
if (LightSprite != null) LightSprite.Remove();
if (lightVolumeBuffer != null)
if (!lightSourceParams.Persistent)
{
lightVolumeBuffer.Dispose();
lightVolumeBuffer = null;
LightSprite?.Remove();
OverrideLightTexture?.Remove();
}
if (lightVolumeIndexBuffer != null)
{
lightVolumeIndexBuffer.Dispose();
lightVolumeIndexBuffer = null;
}
DeformableLightSprite?.Remove();
DeformableLightSprite = null;
lightVolumeBuffer?.Dispose();
lightVolumeBuffer = null;
lightVolumeIndexBuffer?.Dispose();
lightVolumeIndexBuffer = null;
GameMain.LightManager.RemoveLight(this);
}