Build 1.1.4.0
This commit is contained in:
@@ -5,6 +5,7 @@ using System.Linq;
|
||||
using System;
|
||||
using Barotrauma.Items.Components;
|
||||
using Barotrauma.Extensions;
|
||||
using System.Threading;
|
||||
|
||||
namespace Barotrauma.Lights
|
||||
{
|
||||
@@ -22,6 +23,9 @@ namespace Barotrauma.Lights
|
||||
/// </summary>
|
||||
const float ObstructLightsBehindCharactersZoomThreshold = 0.5f;
|
||||
|
||||
private Thread rayCastThread;
|
||||
private Queue<RayCastTask> pendingRayCasts = new Queue<RayCastTask>();
|
||||
|
||||
public static Entity ViewTarget { get; set; }
|
||||
|
||||
private float currLightMapScale;
|
||||
@@ -58,6 +62,8 @@ namespace Barotrauma.Lights
|
||||
|
||||
private readonly List<LightSource> lights;
|
||||
|
||||
public bool DebugLos;
|
||||
|
||||
public bool LosEnabled = true;
|
||||
public float LosAlpha = 1f;
|
||||
public LosMode LosMode = LosMode.Transparent;
|
||||
@@ -68,6 +74,8 @@ namespace Barotrauma.Lights
|
||||
|
||||
private readonly Texture2D visionCircle;
|
||||
|
||||
private readonly Texture2D gapGlowTexture;
|
||||
|
||||
private Vector2 losOffset;
|
||||
|
||||
private int recalculationCount;
|
||||
@@ -85,8 +93,16 @@ namespace Barotrauma.Lights
|
||||
|
||||
AmbientLight = new Color(20, 20, 20, 255);
|
||||
|
||||
rayCastThread = new Thread(UpdateRayCasts)
|
||||
{
|
||||
Name = "LightManager Raycast thread",
|
||||
IsBackground = true //this should kill the thread if the game crashes
|
||||
};
|
||||
rayCastThread.Start();
|
||||
|
||||
visionCircle = Sprite.LoadTexture("Content/Lights/visioncircle.png");
|
||||
highlightRaster = Sprite.LoadTexture("Content/UI/HighlightRaster.png");
|
||||
gapGlowTexture = Sprite.LoadTexture("Content/Lights/pointlight_rays.png");
|
||||
|
||||
GameMain.Instance.ResolutionChanged += () =>
|
||||
{
|
||||
@@ -100,15 +116,12 @@ namespace Barotrauma.Lights
|
||||
LosEffect = EffectLoader.Load("Effects/losshader");
|
||||
SolidColorEffect = EffectLoader.Load("Effects/solidcolor");
|
||||
|
||||
if (lightEffect == null)
|
||||
{
|
||||
lightEffect = new BasicEffect(GameMain.Instance.GraphicsDevice)
|
||||
lightEffect ??= new BasicEffect(GameMain.Instance.GraphicsDevice)
|
||||
{
|
||||
VertexColorEnabled = true,
|
||||
TextureEnabled = true,
|
||||
Texture = LightSource.LightTexture
|
||||
};
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -155,7 +168,7 @@ namespace Barotrauma.Lights
|
||||
{
|
||||
foreach (LightSource light in lights)
|
||||
{
|
||||
light.NeedsHullCheck = true;
|
||||
light.HullsUpToDate.Clear();
|
||||
light.NeedsRecalculation = true;
|
||||
}
|
||||
}
|
||||
@@ -176,6 +189,51 @@ namespace Barotrauma.Lights
|
||||
}
|
||||
}
|
||||
|
||||
private class RayCastTask
|
||||
{
|
||||
public LightSource LightSource;
|
||||
public Vector2 DrawPos;
|
||||
public float Rotation;
|
||||
|
||||
public RayCastTask(LightSource lightSource, Vector2 drawPos, float rotation)
|
||||
{
|
||||
LightSource = lightSource;
|
||||
DrawPos = drawPos;
|
||||
Rotation = rotation;
|
||||
}
|
||||
|
||||
public void Calculate()
|
||||
{
|
||||
LightSource.RayCastTask(DrawPos, Rotation);
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly object mutex = new object();
|
||||
|
||||
public void AddRayCastTask(LightSource lightSource, Vector2 drawPos, float rotation)
|
||||
{
|
||||
lock (mutex)
|
||||
{
|
||||
if (pendingRayCasts.Any(p => p.LightSource == lightSource)) { return; }
|
||||
pendingRayCasts.Enqueue(new RayCastTask(lightSource, drawPos, rotation));
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateRayCasts()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
lock (mutex)
|
||||
{
|
||||
while (pendingRayCasts.Count > 0)
|
||||
{
|
||||
pendingRayCasts.Dequeue().Calculate();
|
||||
}
|
||||
}
|
||||
Thread.Sleep(10);
|
||||
}
|
||||
}
|
||||
|
||||
public void RenderLightMap(GraphicsDevice graphics, SpriteBatch spriteBatch, Camera cam, RenderTarget2D backgroundObstructor = null)
|
||||
{
|
||||
if (!LightingEnabled) { return; }
|
||||
@@ -287,8 +345,8 @@ namespace Barotrauma.Lights
|
||||
foreach (LightSource light in activeLights)
|
||||
{
|
||||
if (!light.IsBackground || light.CurrentBrightness <= 0.0f) { continue; }
|
||||
light.DrawSprite(spriteBatch, cam);
|
||||
light.DrawLightVolume(spriteBatch, lightEffect, transform, recalculationCount < MaxLightVolumeRecalculationsPerFrame, ref recalculationCount);
|
||||
light.DrawSprite(spriteBatch, cam);
|
||||
}
|
||||
GameMain.ParticleManager.Draw(spriteBatch, true, null, Particles.ParticleBlendState.Additive);
|
||||
spriteBatch.End();
|
||||
@@ -307,15 +365,46 @@ namespace Barotrauma.Lights
|
||||
}
|
||||
spriteBatch.End();
|
||||
|
||||
SolidColorEffect.CurrentTechnique = SolidColorEffect.Techniques["SolidColor"];
|
||||
SolidColorEffect.Parameters["color"].SetValue(AmbientLight.Opaque().ToVector4());
|
||||
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, transformMatrix: spriteBatchTransform, effect: SolidColorEffect);
|
||||
Submarine.DrawDamageable(spriteBatch, null);
|
||||
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive, transformMatrix: spriteBatchTransform);
|
||||
Vector3 glowColorHSV = ToolBox.RGBToHSV(AmbientLight);
|
||||
glowColorHSV.Z = Math.Max(glowColorHSV.Z, 0.4f);
|
||||
Color glowColor = ToolBox.HSVToRGB(glowColorHSV.X, glowColorHSV.Y, glowColorHSV.Z);
|
||||
Vector2 glowSpriteSize = new Vector2(gapGlowTexture.Width, gapGlowTexture.Height);
|
||||
foreach (var gap in Gap.GapList)
|
||||
{
|
||||
if (gap.IsRoomToRoom || gap.Open <= 0.0f || gap.ConnectedWall == null) { continue; }
|
||||
|
||||
float a = MathHelper.Lerp(0.5f, 1.0f,
|
||||
PerlinNoise.GetPerlin((float)Timing.TotalTime * 0.05f, gap.GlowEffectT));
|
||||
|
||||
float scale = MathHelper.Lerp(0.5f, 2.0f,
|
||||
PerlinNoise.GetPerlin((float)Timing.TotalTime * 0.01f, gap.GlowEffectT));
|
||||
|
||||
float rot = PerlinNoise.GetPerlin((float)Timing.TotalTime * 0.001f, gap.GlowEffectT) * MathHelper.TwoPi;
|
||||
|
||||
Vector2 spriteScale = new Vector2(gap.Rect.Width, gap.Rect.Height) / glowSpriteSize;
|
||||
Vector2 drawPos = new Vector2(gap.DrawPosition.X, -gap.DrawPosition.Y);
|
||||
|
||||
spriteBatch.Draw(gapGlowTexture,
|
||||
drawPos,
|
||||
null,
|
||||
glowColor * a,
|
||||
rot,
|
||||
glowSpriteSize / 2,
|
||||
scale: Math.Max(spriteScale.X, spriteScale.Y) * scale,
|
||||
SpriteEffects.None,
|
||||
layerDepth: 0);
|
||||
}
|
||||
spriteBatch.End();
|
||||
|
||||
GameMain.GameScreen.DamageEffect.CurrentTechnique = GameMain.GameScreen.DamageEffect.Techniques["StencilShaderSolidColor"];
|
||||
GameMain.GameScreen.DamageEffect.Parameters["solidColor"].SetValue(Color.Black.ToVector4());
|
||||
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.NonPremultiplied, SamplerState.LinearWrap, transformMatrix: spriteBatchTransform, effect: GameMain.GameScreen.DamageEffect);
|
||||
Submarine.DrawDamageable(spriteBatch, GameMain.GameScreen.DamageEffect);
|
||||
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)
|
||||
//---------------------------------------------------------------------------------------------------
|
||||
@@ -449,9 +538,9 @@ namespace Barotrauma.Lights
|
||||
{
|
||||
highlightedEntities.Add(Character.Controlled.FocusedCharacter);
|
||||
}
|
||||
foreach (Item item in Item.ItemList)
|
||||
foreach (MapEntity me in MapEntity.HighlightedEntities)
|
||||
{
|
||||
if ((item.IsHighlighted || item.IconStyle != null) && !highlightedEntities.Contains(item))
|
||||
if (me is Item item && item != Character.Controlled.FocusedItem)
|
||||
{
|
||||
highlightedEntities.Add(item);
|
||||
}
|
||||
@@ -565,7 +654,7 @@ namespace Barotrauma.Lights
|
||||
|
||||
public void UpdateObstructVision(GraphicsDevice graphics, SpriteBatch spriteBatch, Camera cam, Vector2 lookAtPosition)
|
||||
{
|
||||
if ((!LosEnabled || LosMode == LosMode.None) && !ObstructVision) return;
|
||||
if ((!LosEnabled || LosMode == LosMode.None) && !ObstructVision) { return; }
|
||||
if (ViewTarget == null) return;
|
||||
|
||||
graphics.SetRenderTarget(LosTexture);
|
||||
@@ -597,23 +686,30 @@ namespace Barotrauma.Lights
|
||||
if (LosEnabled && LosMode != LosMode.None && ViewTarget != null)
|
||||
{
|
||||
Vector2 pos = ViewTarget.DrawPosition;
|
||||
if (ViewTarget is Character character &&
|
||||
character.AnimController?.GetLimb(LimbType.Head) is Limb head &&
|
||||
!head.IsSevered && !head.Removed)
|
||||
{
|
||||
pos = head.body.DrawPosition;
|
||||
}
|
||||
|
||||
Rectangle camView = new Rectangle(cam.WorldView.X, cam.WorldView.Y - cam.WorldView.Height, cam.WorldView.Width, cam.WorldView.Height);
|
||||
|
||||
Matrix shadowTransform = cam.ShaderTransform
|
||||
* Matrix.CreateOrthographic(GameMain.GraphicsWidth, GameMain.GraphicsHeight, -1, 1) * 0.5f;
|
||||
|
||||
var convexHulls = ConvexHull.GetHullsInRange(ViewTarget.Position, cam.WorldView.Width*0.75f, ViewTarget.Submarine);
|
||||
var convexHulls = ConvexHull.GetHullsInRange(ViewTarget.Position, cam.WorldView.Width * 0.75f, ViewTarget.Submarine);
|
||||
if (convexHulls != null)
|
||||
{
|
||||
List<VertexPositionColor> shadowVerts = new List<VertexPositionColor>();
|
||||
List<VertexPositionTexture> penumbraVerts = new List<VertexPositionTexture>();
|
||||
foreach (ConvexHull convexHull in convexHulls)
|
||||
{
|
||||
if (!convexHull.Enabled || !convexHull.Intersects(camView)) continue;
|
||||
if (!convexHull.Enabled || !convexHull.Intersects(camView)) { continue; }
|
||||
if (LosMode == LosMode.BlockOutsideView && !convexHull.IsExteriorWall) { continue; };
|
||||
|
||||
Vector2 relativeLightPos = pos;
|
||||
if (convexHull.ParentEntity?.Submarine != null) relativeLightPos -= convexHull.ParentEntity.Submarine.Position;
|
||||
if (convexHull.ParentEntity?.Submarine != null) { relativeLightPos -= convexHull.ParentEntity.Submarine.Position; }
|
||||
|
||||
convexHull.CalculateLosVertices(relativeLightPos);
|
||||
|
||||
@@ -646,6 +742,20 @@ namespace Barotrauma.Lights
|
||||
graphics.SetRenderTarget(null);
|
||||
}
|
||||
|
||||
public void DebugDrawLos(SpriteBatch spriteBatch, Camera cam)
|
||||
{
|
||||
if (ViewTarget == null) { return; }
|
||||
spriteBatch.Begin(SpriteSortMode.Deferred, transformMatrix: cam.Transform);
|
||||
var convexHulls = ConvexHull.GetHullsInRange(ViewTarget.Position, cam.WorldView.Width * 0.75f, ViewTarget?.Submarine);
|
||||
Rectangle camView = new Rectangle(cam.WorldView.X, cam.WorldView.Y - cam.WorldView.Height, cam.WorldView.Width, cam.WorldView.Height);
|
||||
foreach (ConvexHull convexHull in convexHulls)
|
||||
{
|
||||
if (!convexHull.Enabled || !convexHull.Intersects(camView)) { continue; }
|
||||
convexHull.DebugDraw(spriteBatch);
|
||||
}
|
||||
spriteBatch.End();
|
||||
}
|
||||
|
||||
public void ClearLights()
|
||||
{
|
||||
lights.Clear();
|
||||
|
||||
Reference in New Issue
Block a user