LightSources cache the light vertices in vertex buffers and only recalculate if needed
This commit is contained in:
@@ -103,6 +103,15 @@ namespace Barotrauma
|
|||||||
{
|
{
|
||||||
get { return NetworkMember as GameClient; }
|
get { return NetworkMember as GameClient; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Total seconds elapsed after startup
|
||||||
|
/// </summary>
|
||||||
|
public double TotalElapsedTime
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
private set;
|
||||||
|
}
|
||||||
|
|
||||||
public GameMain()
|
public GameMain()
|
||||||
{
|
{
|
||||||
@@ -297,11 +306,12 @@ namespace Barotrauma
|
|||||||
protected override void Update(GameTime gameTime)
|
protected override void Update(GameTime gameTime)
|
||||||
{
|
{
|
||||||
Timing.Accumulator += gameTime.ElapsedGameTime.TotalSeconds;
|
Timing.Accumulator += gameTime.ElapsedGameTime.TotalSeconds;
|
||||||
|
|
||||||
bool paused = true;
|
bool paused = true;
|
||||||
|
|
||||||
while (Timing.Accumulator >= Timing.Step)
|
while (Timing.Accumulator >= Timing.Step)
|
||||||
{
|
{
|
||||||
|
TotalElapsedTime = gameTime.TotalGameTime.TotalSeconds;
|
||||||
|
|
||||||
fixedTime.IsRunningSlowly = gameTime.IsRunningSlowly;
|
fixedTime.IsRunningSlowly = gameTime.IsRunningSlowly;
|
||||||
TimeSpan addTime = new TimeSpan(0, 0, 0, 0, 16);
|
TimeSpan addTime = new TimeSpan(0, 0, 0, 0, 16);
|
||||||
fixedTime.ElapsedGameTime = addTime;
|
fixedTime.ElapsedGameTime = addTime;
|
||||||
|
|||||||
@@ -110,7 +110,7 @@ namespace Barotrauma.Lights
|
|||||||
//private Dictionary<LightSource, CachedShadow> cachedShadows;
|
//private Dictionary<LightSource, CachedShadow> cachedShadows;
|
||||||
|
|
||||||
public VertexBuffer ShadowBuffer;
|
public VertexBuffer ShadowBuffer;
|
||||||
|
|
||||||
Segment[] segments = new Segment[4];
|
Segment[] segments = new Segment[4];
|
||||||
SegmentPoint[] vertices = new SegmentPoint[4];
|
SegmentPoint[] vertices = new SegmentPoint[4];
|
||||||
SegmentPoint[] losVertices = new SegmentPoint[4];
|
SegmentPoint[] losVertices = new SegmentPoint[4];
|
||||||
@@ -136,10 +136,28 @@ namespace Barotrauma.Lights
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private bool enabled;
|
||||||
public bool Enabled
|
public bool Enabled
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return enabled;
|
||||||
|
}
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (enabled == value) return;
|
||||||
|
enabled = value;
|
||||||
|
LastVertexChangeTime = (float)GameMain.Instance.TotalElapsedTime;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The elapsed gametime when the vertices of this hull last changed
|
||||||
|
/// </summary>
|
||||||
|
public float LastVertexChangeTime
|
||||||
{
|
{
|
||||||
get;
|
get;
|
||||||
set;
|
private set;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Rectangle BoundingBox
|
public Rectangle BoundingBox
|
||||||
@@ -197,6 +215,8 @@ namespace Barotrauma.Lights
|
|||||||
|
|
||||||
private void UpdateIgnoredEdges(ConvexHull ch)
|
private void UpdateIgnoredEdges(ConvexHull ch)
|
||||||
{
|
{
|
||||||
|
if (ch == this) return;
|
||||||
|
//ignore edges that are inside some other convex hull
|
||||||
for (int i = 0; i < vertices.Length; i++)
|
for (int i = 0; i < vertices.Length; i++)
|
||||||
{
|
{
|
||||||
if (vertices[i].Pos.X >= ch.boundingBox.X && vertices[i].Pos.X <= ch.boundingBox.Right &&
|
if (vertices[i].Pos.X >= ch.boundingBox.X && vertices[i].Pos.X <= ch.boundingBox.Right &&
|
||||||
@@ -233,13 +253,15 @@ namespace Barotrauma.Lights
|
|||||||
{
|
{
|
||||||
for (int i = 0; i < vertices.Length; i++)
|
for (int i = 0; i < vertices.Length; i++)
|
||||||
{
|
{
|
||||||
vertices[i].Pos += amount;
|
vertices[i].Pos += amount;
|
||||||
losVertices[i].Pos += amount;
|
losVertices[i].Pos += amount;
|
||||||
|
|
||||||
segments[i].Start.Pos += amount;
|
segments[i].Start.Pos += amount;
|
||||||
segments[i].End.Pos += amount;
|
segments[i].End.Pos += amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LastVertexChangeTime = (float)GameMain.Instance.TotalElapsedTime;
|
||||||
|
|
||||||
CalculateDimensions();
|
CalculateDimensions();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -247,6 +269,8 @@ namespace Barotrauma.Lights
|
|||||||
{
|
{
|
||||||
Debug.Assert(points.Length == 4, "Only rectangular convex hulls are supported");
|
Debug.Assert(points.Length == 4, "Only rectangular convex hulls are supported");
|
||||||
|
|
||||||
|
LastVertexChangeTime = (float)GameMain.Instance.TotalElapsedTime;
|
||||||
|
|
||||||
for (int i = 0; i < 4; i++)
|
for (int i = 0; i < 4; i++)
|
||||||
{
|
{
|
||||||
vertices[i] = new SegmentPoint(points[i]);
|
vertices[i] = new SegmentPoint(points[i]);
|
||||||
@@ -256,10 +280,6 @@ namespace Barotrauma.Lights
|
|||||||
for (int i = 0; i < 4; i++)
|
for (int i = 0; i < 4; i++)
|
||||||
{
|
{
|
||||||
segments[i] = new Segment(vertices[i], vertices[(i + 1) % 4]);
|
segments[i] = new Segment(vertices[i], vertices[(i + 1) % 4]);
|
||||||
|
|
||||||
//vertices[i].Segments[1] = segments[i];
|
|
||||||
//vertices[(i + 1) % 4].Segments[0] = segments[i];
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int margin = 0;
|
int margin = 0;
|
||||||
@@ -280,6 +300,21 @@ namespace Barotrauma.Lights
|
|||||||
}
|
}
|
||||||
|
|
||||||
CalculateDimensions();
|
CalculateDimensions();
|
||||||
|
|
||||||
|
if (parentEntity == null || ignoreEdge == null) return;
|
||||||
|
for (int i = 0; i<4; i++)
|
||||||
|
{
|
||||||
|
ignoreEdge[i] = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var chList = HullLists.Find(x => x.Submarine == parentEntity.Submarine);
|
||||||
|
if (chList != null)
|
||||||
|
{
|
||||||
|
foreach (ConvexHull ch in chList.List)
|
||||||
|
{
|
||||||
|
UpdateIgnoredEdges(ch);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*private void RemoveCachedShadow(Lights.LightSource light)
|
/*private void RemoveCachedShadow(Lights.LightSource light)
|
||||||
|
|||||||
@@ -94,7 +94,8 @@ namespace Barotrauma.Lights
|
|||||||
{
|
{
|
||||||
foreach (LightSource light in lights)
|
foreach (LightSource light in lights)
|
||||||
{
|
{
|
||||||
light.NeedsHullUpdate = true;
|
light.NeedsHullCheck = true;
|
||||||
|
light.NeedsRecalculation = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -152,16 +153,16 @@ namespace Barotrauma.Lights
|
|||||||
|
|
||||||
Vector3 offset = Vector3.Zero;// new Vector3(Submarine.MainSub.DrawPosition.X, Submarine.MainSub.DrawPosition.Y, 0.0f);
|
Vector3 offset = Vector3.Zero;// new Vector3(Submarine.MainSub.DrawPosition.X, Submarine.MainSub.DrawPosition.Y, 0.0f);
|
||||||
|
|
||||||
lightEffect.World = Matrix.CreateTranslation(offset) * transform;
|
|
||||||
|
|
||||||
foreach (LightSource light in lights)
|
foreach (LightSource light in lights)
|
||||||
{
|
{
|
||||||
if (light.Color.A < 1 || 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, lightEffect);
|
light.Draw(spriteBatch, lightEffect, transform);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lightEffect.World = Matrix.CreateTranslation(offset) * transform;
|
||||||
|
|
||||||
GameMain.ParticleManager.Draw(spriteBatch, false, Particles.ParticleBlendState.Additive);
|
GameMain.ParticleManager.Draw(spriteBatch, false, Particles.ParticleBlendState.Additive);
|
||||||
|
|
||||||
if (Character.Controlled != null)
|
if (Character.Controlled != null)
|
||||||
|
|||||||
@@ -15,27 +15,35 @@ namespace Barotrauma.Lights
|
|||||||
private List<ConvexHullList> hullsInRange;
|
private List<ConvexHullList> hullsInRange;
|
||||||
|
|
||||||
private Color color;
|
private Color color;
|
||||||
|
|
||||||
private float range;
|
private float range;
|
||||||
|
|
||||||
public SpriteEffects SpriteEffect = SpriteEffects.None;
|
private Sprite overrideLightTexture;
|
||||||
|
|
||||||
private Texture2D texture;
|
private Texture2D texture;
|
||||||
|
|
||||||
|
public SpriteEffects SpriteEffect = SpriteEffects.None;
|
||||||
public Sprite LightSprite;
|
public Sprite LightSprite;
|
||||||
|
|
||||||
private Sprite overrideLightTexture;
|
|
||||||
|
|
||||||
public Submarine ParentSub;
|
public Submarine ParentSub;
|
||||||
|
|
||||||
public bool CastShadows;
|
public bool CastShadows;
|
||||||
|
|
||||||
//what was the range of the light when HullsInRange were last updated
|
//what was the range of the light when lightvolumes were last calculated
|
||||||
private float prevHullUpdateRange;
|
private float prevCalculatedRange;
|
||||||
|
private Vector2 prevCalculatedPosition;
|
||||||
|
|
||||||
private Vector2 prevHullUpdatePosition;
|
//do we need to recheck which convex hulls are within range
|
||||||
|
//(e.g. position or range of the lightsource has changed)
|
||||||
|
public bool NeedsHullCheck = true;
|
||||||
|
//do we need to recalculate the vertices of the light volume
|
||||||
|
public bool NeedsRecalculation = true;
|
||||||
|
|
||||||
public bool NeedsHullUpdate;
|
//when were the vertices of the light volume last calculated
|
||||||
|
private float lastRecalculationTime;
|
||||||
|
|
||||||
|
private DynamicVertexBuffer lightVolumeBuffer;
|
||||||
|
private DynamicIndexBuffer lightVolumeIndexBuffer;
|
||||||
|
private int vertexCount;
|
||||||
|
private int indexCount;
|
||||||
|
|
||||||
private Vector2 position;
|
private Vector2 position;
|
||||||
public Vector2 Position
|
public Vector2 Position
|
||||||
@@ -46,10 +54,11 @@ namespace Barotrauma.Lights
|
|||||||
if (position == value) return;
|
if (position == value) return;
|
||||||
position = value;
|
position = value;
|
||||||
|
|
||||||
if (Vector2.Distance(prevHullUpdatePosition, position) < 5.0f) return;
|
if (Vector2.Distance(prevCalculatedPosition, position) < 5.0f) return;
|
||||||
|
|
||||||
NeedsHullUpdate = true;
|
NeedsHullCheck = true;
|
||||||
prevHullUpdatePosition = position;
|
NeedsRecalculation = true;
|
||||||
|
prevCalculatedPosition = position;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,10 +99,11 @@ namespace Barotrauma.Lights
|
|||||||
{
|
{
|
||||||
|
|
||||||
range = MathHelper.Clamp(value, 0.0f, 2048.0f);
|
range = MathHelper.Clamp(value, 0.0f, 2048.0f);
|
||||||
if (Math.Abs(prevHullUpdateRange - range) < 10.0f) return;
|
if (Math.Abs(prevCalculatedRange - range) < 10.0f) return;
|
||||||
|
|
||||||
NeedsHullUpdate = true;
|
NeedsHullCheck = true;
|
||||||
prevHullUpdateRange = range;
|
NeedsRecalculation = true;
|
||||||
|
prevCalculatedRange = range;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,9 +140,9 @@ namespace Barotrauma.Lights
|
|||||||
this.color = color;
|
this.color = color;
|
||||||
|
|
||||||
CastShadows = true;
|
CastShadows = true;
|
||||||
|
|
||||||
texture = LightTexture;
|
texture = LightTexture;
|
||||||
|
|
||||||
GameMain.LightManager.AddLight(this);
|
GameMain.LightManager.AddLight(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,82 +172,112 @@ namespace Barotrauma.Lights
|
|||||||
{
|
{
|
||||||
ch.DrawShadows(graphics, cam, this, shadowTransform, false);
|
ch.DrawShadows(graphics, cam, this, shadowTransform, false);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private List<ConvexHull> GetHullsInRange(Submarine sub)
|
|
||||||
{
|
|
||||||
//find the current list of hulls in range
|
|
||||||
var chList = hullsInRange.Find(x => x.Submarine == sub);
|
|
||||||
|
|
||||||
//not found -> create one
|
|
||||||
if (chList == null)
|
|
||||||
{
|
|
||||||
chList = new ConvexHullList(sub);
|
|
||||||
hullsInRange.Add(chList);
|
|
||||||
}
|
|
||||||
|
|
||||||
Vector2 lightPos = position;
|
|
||||||
if (ParentSub == null)
|
|
||||||
{
|
|
||||||
//light and the convexhull are both outside
|
|
||||||
if (sub == null)
|
|
||||||
{
|
|
||||||
if (NeedsHullUpdate)
|
|
||||||
{
|
|
||||||
var fullChList = ConvexHull.HullLists.Find(x => x.Submarine == sub);
|
|
||||||
if (fullChList != null)
|
|
||||||
chList.List = fullChList.List.FindAll(ch => MathUtils.CircleIntersectsRectangle(lightPos, range, ch.BoundingBox));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//light is outside, convexhull inside a sub
|
|
||||||
else
|
|
||||||
{
|
|
||||||
lightPos -= sub.Position;
|
|
||||||
|
|
||||||
Rectangle subBorders = sub.Borders;
|
|
||||||
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)) return null;
|
|
||||||
|
|
||||||
var fullChList = ConvexHull.HullLists.Find(x => x.Submarine == sub);
|
|
||||||
chList.List = fullChList.List.FindAll(ch => MathUtils.CircleIntersectsRectangle(lightPos, range, ch.BoundingBox));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
//light is inside, convexhull outside
|
|
||||||
if (sub == null) return null;
|
|
||||||
|
|
||||||
//light and convexhull are both inside the same sub
|
|
||||||
if (sub == ParentSub)
|
|
||||||
{
|
|
||||||
if (NeedsHullUpdate)
|
|
||||||
{
|
|
||||||
var fullChList = ConvexHull.HullLists.Find(x => x.Submarine == sub);
|
|
||||||
chList.List = fullChList.List.FindAll(ch => MathUtils.CircleIntersectsRectangle(lightPos, range, ch.BoundingBox));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//light and convexhull are inside different subs
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (sub.DockedTo.Contains(ParentSub) && !NeedsHullUpdate) return chList.List;
|
|
||||||
|
|
||||||
lightPos -= (sub.Position - ParentSub.Position);
|
|
||||||
|
|
||||||
Rectangle subBorders = sub.Borders;
|
|
||||||
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)) return null;
|
|
||||||
|
|
||||||
var fullChList = ConvexHull.HullLists.Find(x => x.Submarine == sub);
|
|
||||||
chList.List = fullChList.List.FindAll(ch => MathUtils.CircleIntersectsRectangle(lightPos, range, ch.BoundingBox));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return chList.List;
|
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Update the contents of ConvexHullList and check if we need to recalculate vertices
|
||||||
|
/// </summary>
|
||||||
|
private void RefreshConvexHullList(ConvexHullList chList, Vector2 lightPos, Submarine sub)
|
||||||
|
{
|
||||||
|
var fullChList = ConvexHull.HullLists.Find(x => x.Submarine == sub);
|
||||||
|
chList.List = fullChList.List.FindAll(ch => ch.Enabled && MathUtils.CircleIntersectsRectangle(lightPos, range, ch.BoundingBox));
|
||||||
|
|
||||||
|
NeedsHullCheck = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Recheck which convex hulls are in range (if needed),
|
||||||
|
/// and check if we need to recalculate vertices due to changes in the convex hulls
|
||||||
|
/// </summary>
|
||||||
|
private void CheckHullsInRange()
|
||||||
|
{
|
||||||
|
List<Submarine> subs = new List<Submarine>(Submarine.Loaded);
|
||||||
|
subs.Add(null);
|
||||||
|
|
||||||
|
foreach (Submarine sub in subs)
|
||||||
|
{
|
||||||
|
//find the list of convexhulls that belong to the sub
|
||||||
|
var chList = hullsInRange.Find(x => x.Submarine == sub);
|
||||||
|
|
||||||
|
//not found -> create one
|
||||||
|
if (chList == null)
|
||||||
|
{
|
||||||
|
chList = new ConvexHullList(sub);
|
||||||
|
hullsInRange.Add(chList);
|
||||||
|
NeedsRecalculation = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (chList.List.Any(ch => ch.LastVertexChangeTime > lastRecalculationTime))
|
||||||
|
{
|
||||||
|
NeedsRecalculation = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 lightPos = position;
|
||||||
|
if (ParentSub == null)
|
||||||
|
{
|
||||||
|
//light and the convexhulls are both outside
|
||||||
|
if (sub == null)
|
||||||
|
{
|
||||||
|
if (NeedsHullCheck)
|
||||||
|
{
|
||||||
|
RefreshConvexHullList(chList, lightPos, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//light is outside, convexhulls inside a sub
|
||||||
|
else
|
||||||
|
{
|
||||||
|
lightPos -= sub.Position;
|
||||||
|
|
||||||
|
Rectangle subBorders = sub.Borders;
|
||||||
|
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 (chList.List.Count > 0) NeedsRecalculation = true;
|
||||||
|
chList.List.Clear();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
RefreshConvexHullList(chList, lightPos, sub);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//light is inside, convexhull outside
|
||||||
|
if (sub == null) continue;
|
||||||
|
|
||||||
|
//light and convexhull are both inside the same sub
|
||||||
|
if (sub == ParentSub)
|
||||||
|
{
|
||||||
|
if (NeedsHullCheck)
|
||||||
|
{
|
||||||
|
RefreshConvexHullList(chList, lightPos, sub);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//light and convexhull are inside different subs
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (sub.DockedTo.Contains(ParentSub) && !NeedsHullCheck) continue;
|
||||||
|
|
||||||
|
lightPos -= (sub.Position - ParentSub.Position);
|
||||||
|
|
||||||
|
Rectangle subBorders = sub.Borders;
|
||||||
|
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 (chList.List.Count > 0) NeedsRecalculation = true;
|
||||||
|
chList.List.Clear();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
RefreshConvexHullList(chList, lightPos, sub);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private List<Vector2> FindRaycastHits()
|
private List<Vector2> FindRaycastHits()
|
||||||
{
|
{
|
||||||
@@ -247,7 +287,11 @@ namespace Barotrauma.Lights
|
|||||||
Vector2 drawPos = position;
|
Vector2 drawPos = position;
|
||||||
if (ParentSub != null) drawPos += ParentSub.DrawPosition;
|
if (ParentSub != null) drawPos += ParentSub.DrawPosition;
|
||||||
|
|
||||||
var hulls = ConvexHull.GetHullsInRange(position, range, ParentSub);
|
var hulls = new List<ConvexHull>();// ConvexHull.GetHullsInRange(position, range, ParentSub);
|
||||||
|
foreach (ConvexHullList chList in hullsInRange)
|
||||||
|
{
|
||||||
|
hulls.AddRange(chList.List);
|
||||||
|
}
|
||||||
|
|
||||||
//find convexhull segments that are close enough and facing towards the light source
|
//find convexhull segments that are close enough and facing towards the light source
|
||||||
List<Segment> visibleSegments = new List<Segment>();
|
List<Segment> visibleSegments = new List<Segment>();
|
||||||
@@ -256,6 +300,11 @@ namespace Barotrauma.Lights
|
|||||||
{
|
{
|
||||||
hull.RefreshWorldPositions();
|
hull.RefreshWorldPositions();
|
||||||
|
|
||||||
|
if (hull.ParentEntity is Item)
|
||||||
|
{
|
||||||
|
int gfhdfgh = 1;
|
||||||
|
}
|
||||||
|
|
||||||
var visibleHullSegments = hull.GetVisibleSegments(drawPos);
|
var visibleHullSegments = hull.GetVisibleSegments(drawPos);
|
||||||
visibleSegments.AddRange(visibleHullSegments);
|
visibleSegments.AddRange(visibleHullSegments);
|
||||||
|
|
||||||
@@ -342,8 +391,7 @@ namespace Barotrauma.Lights
|
|||||||
return closestIntersection == null ? rayEnd : (Vector2)closestIntersection;
|
return closestIntersection == null ? rayEnd : (Vector2)closestIntersection;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CalculateVertices(List<Vector2> encounters,
|
private void CalculateLightVertices(List<Vector2> rayCastHits)
|
||||||
out VertexPositionTexture[] vertexArray, out short[] indexArray)
|
|
||||||
{
|
{
|
||||||
List<VertexPositionTexture> vertices = new List<VertexPositionTexture>();
|
List<VertexPositionTexture> vertices = new List<VertexPositionTexture>();
|
||||||
|
|
||||||
@@ -351,22 +399,22 @@ namespace Barotrauma.Lights
|
|||||||
if (ParentSub != null) drawPos += ParentSub.DrawPosition;
|
if (ParentSub != null) drawPos += ParentSub.DrawPosition;
|
||||||
|
|
||||||
// Add a vertex for the center of the mesh
|
// Add a vertex for the center of the mesh
|
||||||
vertices.Add(new VertexPositionTexture(new Vector3(drawPos.X, drawPos.Y, 0),
|
vertices.Add(new VertexPositionTexture(new Vector3(position.X, position.Y, 0),
|
||||||
new Vector2(0.5f, 0.5f)));
|
new Vector2(0.5f, 0.5f)));
|
||||||
|
|
||||||
// Add all the other encounter points as vertices
|
// Add all the other encounter points as vertices
|
||||||
// storing their world position as UV coordinates
|
// storing their world position as UV coordinates
|
||||||
foreach (Vector2 vertex in encounters)
|
foreach (Vector2 vertex in rayCastHits)
|
||||||
{
|
{
|
||||||
Vector2 diff = vertex - drawPos;
|
Vector2 diff = vertex - drawPos;
|
||||||
|
|
||||||
vertices.Add(new VertexPositionTexture(new Vector3(vertex.X, vertex.Y, 0),
|
vertices.Add(new VertexPositionTexture(new Vector3(position.X + diff.X, position.Y + diff.Y, 0),
|
||||||
new Vector2(0.5f, 0.5f) + diff / range / 2));
|
new Vector2(0.5f, 0.5f) + diff / range / 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compute the indices to form triangles
|
// Compute the indices to form triangles
|
||||||
List<short> indices = new List<short>();
|
List<short> indices = new List<short>();
|
||||||
for (int i = 0; i < encounters.Count - 1; i++)
|
for (int i = 0; i < rayCastHits.Count - 1; i++)
|
||||||
{
|
{
|
||||||
indices.Add(0);
|
indices.Add(0);
|
||||||
indices.Add((short)((i + 2) % vertices.Count));
|
indices.Add((short)((i + 2) % vertices.Count));
|
||||||
@@ -377,12 +425,38 @@ namespace Barotrauma.Lights
|
|||||||
indices.Add((short)(1));
|
indices.Add((short)(1));
|
||||||
indices.Add((short)(vertices.Count - 1));
|
indices.Add((short)(vertices.Count - 1));
|
||||||
|
|
||||||
vertexArray = vertices.ToArray<VertexPositionTexture>();
|
vertexCount = vertices.Count;
|
||||||
indexArray = indices.ToArray<short>();
|
indexCount = indices.Count;
|
||||||
|
|
||||||
|
//TODO: a better way to determine the size of the vertex buffer and handle changes in size?
|
||||||
|
//now we just create a buffer for 64 verts and make it larger if needed
|
||||||
|
if (lightVolumeBuffer == null)
|
||||||
|
{
|
||||||
|
lightVolumeBuffer = new DynamicVertexBuffer(GameMain.CurrGraphicsDevice, VertexPositionTexture.VertexDeclaration, Math.Max(64, (int)(vertexCount*1.5)), BufferUsage.None);
|
||||||
|
lightVolumeIndexBuffer = new DynamicIndexBuffer(GameMain.CurrGraphicsDevice, typeof(short), Math.Max(64*3, (int)(indexCount * 1.5)), BufferUsage.None);
|
||||||
|
}
|
||||||
|
else if (vertexCount > lightVolumeBuffer.VertexCount)
|
||||||
|
{
|
||||||
|
lightVolumeBuffer.Dispose();
|
||||||
|
lightVolumeIndexBuffer.Dispose();
|
||||||
|
|
||||||
|
lightVolumeBuffer = new DynamicVertexBuffer(GameMain.CurrGraphicsDevice, VertexPositionTexture.VertexDeclaration, (int)(vertexCount*1.5), BufferUsage.None);
|
||||||
|
lightVolumeIndexBuffer = new DynamicIndexBuffer(GameMain.CurrGraphicsDevice, typeof(short), (int)(indexCount * 1.5), BufferUsage.None);
|
||||||
|
}
|
||||||
|
|
||||||
|
lightVolumeBuffer.SetData<VertexPositionTexture>(vertices.ToArray());
|
||||||
|
lightVolumeIndexBuffer.SetData<short>(indices.ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Draw(SpriteBatch spriteBatch, BasicEffect lightEffect)
|
public void Draw(SpriteBatch spriteBatch, BasicEffect lightEffect, Matrix transform)
|
||||||
{
|
{
|
||||||
|
CheckHullsInRange();
|
||||||
|
|
||||||
|
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;
|
Vector2 drawPos = position;
|
||||||
if (ParentSub != null) drawPos += ParentSub.DrawPosition;
|
if (ParentSub != null) drawPos += ParentSub.DrawPosition;
|
||||||
|
|
||||||
@@ -412,32 +486,26 @@ namespace Barotrauma.Lights
|
|||||||
//LightSprite.Draw(spriteBatch, drawPos, Color, LightSprite.Origin, -Rotation, 1, SpriteEffect);
|
//LightSprite.Draw(spriteBatch, drawPos, Color, LightSprite.Origin, -Rotation, 1, SpriteEffect);
|
||||||
}
|
}
|
||||||
|
|
||||||
var verts = FindRaycastHits();
|
if (NeedsRecalculation)
|
||||||
|
|
||||||
/*for (int i = 0; i < verts.Count; i++)
|
|
||||||
{
|
{
|
||||||
Color[] clrs = new Color[] { Color.Green, Color.Cyan, Color.Red, Color.White, Color.Magenta };
|
var verts = FindRaycastHits();
|
||||||
|
CalculateLightVertices(verts);
|
||||||
|
|
||||||
Color clr = clrs[i % clrs.Length];
|
lastRecalculationTime = (float)GameMain.Instance.TotalElapsedTime;
|
||||||
|
NeedsRecalculation = false;
|
||||||
// GUI.DrawString(spriteBatch, new Vector2(verts[i].X, -verts[i].Y), verts[i].ToString(), clr);
|
}
|
||||||
GUI.DrawString(spriteBatch, new Vector2(verts[i].X, -verts[i].Y), i.ToString(), clr);
|
|
||||||
GUI.DrawLine(spriteBatch, drawPos, new Vector2(verts[i].X, -verts[i].Y), clr, 0,3);
|
|
||||||
}*/
|
|
||||||
|
|
||||||
// Generate a triangle list from the encounter points
|
|
||||||
VertexPositionTexture[] vertices;
|
|
||||||
short[] indices;
|
|
||||||
CalculateVertices(verts, out vertices, out indices);
|
|
||||||
|
|
||||||
if (vertices.Length == 0) return;
|
if (vertexCount == 0) return;
|
||||||
|
|
||||||
lightEffect.DiffuseColor = (new Vector3(color.R, color.G, color.B) * (color.A / 255.0f)) / 255.0f;// color.ToVector3();
|
lightEffect.DiffuseColor = (new Vector3(color.R, color.G, color.B) * (color.A / 255.0f)) / 255.0f;// color.ToVector3();
|
||||||
lightEffect.CurrentTechnique.Passes[0].Apply();
|
lightEffect.CurrentTechnique.Passes[0].Apply();
|
||||||
|
|
||||||
GameMain.CurrGraphicsDevice.DrawUserIndexedPrimitives<VertexPositionTexture>
|
GameMain.CurrGraphicsDevice.SetVertexBuffer(lightVolumeBuffer);
|
||||||
|
GameMain.CurrGraphicsDevice.Indices = lightVolumeIndexBuffer;
|
||||||
|
|
||||||
|
GameMain.CurrGraphicsDevice.DrawIndexedPrimitives
|
||||||
(
|
(
|
||||||
PrimitiveType.TriangleList, vertices, 0, vertices.Length, indices, 0, indices.Length / 3
|
PrimitiveType.TriangleList, 0, 0, indexCount / 3
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -463,6 +531,18 @@ namespace Barotrauma.Lights
|
|||||||
{
|
{
|
||||||
if (LightSprite != null) LightSprite.Remove();
|
if (LightSprite != null) LightSprite.Remove();
|
||||||
|
|
||||||
|
if (lightVolumeBuffer != null)
|
||||||
|
{
|
||||||
|
lightVolumeBuffer.Dispose();
|
||||||
|
lightVolumeBuffer = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lightVolumeIndexBuffer != null)
|
||||||
|
{
|
||||||
|
lightVolumeIndexBuffer.Dispose();
|
||||||
|
lightVolumeIndexBuffer = null;
|
||||||
|
}
|
||||||
|
|
||||||
GameMain.LightManager.RemoveLight(this);
|
GameMain.LightManager.RemoveLight(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user