Calculating light volumes works now. Very poorly optimized at the moment, todo: cache volumes and only recalculate when needed

This commit is contained in:
Regalis
2017-02-26 18:44:18 +02:00
parent abfe2261d2
commit f3f875e641
4 changed files with 255 additions and 41 deletions

View File

@@ -74,20 +74,30 @@ namespace Barotrauma.Lights
{
Start = start;
End = end;
start.Segment = this;
end.Segment = this;
}
}
class SegmentPoint
struct SegmentPoint
{
public Vector2 Pos;
public Segment[] Segments;
public Vector2 Pos;
public Vector2 WorldPos;
public SegmentPoint(Vector2 pos, Segment[] segments)
public Segment Segment;
public SegmentPoint(Vector2 pos)
{
Pos = pos;
Segments = segments;
WorldPos = pos;
Segment = null;
}
public override string ToString()
{
return Pos.ToString();
}
}
@@ -225,6 +235,9 @@ namespace Barotrauma.Lights
{
vertices[i].Pos += amount;
losVertices[i].Pos += amount;
segments[i].Start.Pos += amount;
segments[i].End.Pos += amount;
}
CalculateDimensions();
@@ -236,16 +249,16 @@ namespace Barotrauma.Lights
for (int i = 0; i < 4; i++)
{
vertices[i] = new SegmentPoint(points[i], new Segment[2]);
losVertices[i] = new SegmentPoint(points[i], new Segment[2]);
vertices[i] = new SegmentPoint(points[i]);
losVertices[i] = new SegmentPoint(points[i]);
}
for (int i = 0; i < 4; i++)
{
segments[i] = new Segment(vertices[i], vertices[(i + 1) % 4]);
vertices[i].Segments[1] = segments[i];
vertices[(i + 1) % 4].Segments[0] = segments[i];
//vertices[i].Segments[1] = segments[i];
//vertices[(i + 1) % 4].Segments[0] = segments[i];
}
@@ -315,15 +328,18 @@ namespace Barotrauma.Lights
{
if (ignoreEdge[i]) continue;
Vector2 middle = (segments[i].Start.Pos + segments[i].End.Pos) / 2;
Vector2 pos1 = vertices[i].WorldPos;
Vector2 pos2 = vertices[(i + 1) % 4].WorldPos;
Vector2 middle = (pos1 + pos2) / 2;
Vector2 L = viewPosition - middle;
Vector2 N = new Vector2(
-(segments[i].End.Pos.Y - segments[i].Start.Pos.Y),
segments[i].End.Pos.X - segments[i].Start.Pos.X);
-(pos2.Y - pos1.Y),
pos2.X - pos1.X);
if (Vector2.Dot(N, L) < 0)
if (Vector2.Dot(N, L) > 0)
{
visibleFaces.Add(segments[i]);
}
@@ -339,6 +355,9 @@ namespace Barotrauma.Lights
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;
}
}

View File

@@ -25,6 +25,8 @@ namespace Barotrauma.Lights
public Color AmbientLight;
RenderTarget2D lightMap, losTexture;
BasicEffect lightEffect;
private static Texture2D alphaClearTexture;
@@ -60,6 +62,15 @@ namespace Barotrauma.Lights
losTexture = new RenderTarget2D(graphics, GameMain.GraphicsWidth, GameMain.GraphicsHeight);
if (lightEffect == null)
{
lightEffect = new BasicEffect(GameMain.CurrGraphicsDevice);
lightEffect.VertexColorEnabled = false;
lightEffect.TextureEnabled = true;
lightEffect.Texture = LightSource.LightTexture;
}
hullAmbientLights = new Dictionary<Hull, Color>();
smoothedHullAmbientLights = new Dictionary<Hull, Color>();
@@ -132,16 +143,23 @@ namespace Barotrauma.Lights
//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 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);
lightEffect.World = Matrix.CreateTranslation(offset) * transform;
foreach (LightSource light in lights)
{
if (light.Color.A < 1 || light.Range < 1.0f || !light.CastShadows) continue;
if (!MathUtils.CircleIntersectsRectangle(light.WorldPosition, light.Range, viewRect)) continue;
light.Draw(spriteBatch);
light.Draw(spriteBatch, lightEffect);
}
GameMain.ParticleManager.Draw(spriteBatch, false, Particles.ParticleBlendState.Additive);
@@ -178,9 +196,10 @@ namespace Barotrauma.Lights
spriteBatch.End();
//clear alpha, to avoid messing stuff up later
ClearAlphaToOne(graphics, spriteBatch);
//ClearAlphaToOne(graphics, spriteBatch);
graphics.SetRenderTarget(null);
graphics.BlendState = BlendState.AlphaBlend;
}
public void UpdateObstructVision(GraphicsDevice graphics, SpriteBatch spriteBatch, Camera cam, Vector2 lookAtPosition)

View File

@@ -239,43 +239,156 @@ namespace Barotrauma.Lights
return chList.List;
}*/
private List<Vector2> CalculateFanVertices()
private List<Vector2> FindRaycastHits()
{
if (!CastShadows) return null;
if (range < 1.0f || color.A < 0.01f) return null;
Vector2 drawPos = position;
if (ParentSub != null) drawPos += ParentSub.DrawPosition;
var hulls = ConvexHull.GetHullsInRange(position, range, ParentSub);
//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();
points.AddRange(hull.GetVisibleSegments(position).Select(s => s.End));
var visibleHullSegments = hull.GetVisibleSegments(drawPos);
visibleSegments.AddRange(visibleHullSegments);
foreach (Segment s in visibleHullSegments)
{
points.Add(s.Start);
points.Add(s.End);
}
}
//add a square-shaped boundary to make sure we've got something to construct the triangles from
//even if there aren't enough hull segments around the light source
//(might be more effective to calculate if we actually need these extra points)
var boundaryCorners = new List<SegmentPoint> {
new SegmentPoint(new Vector2(drawPos.X + range*2, drawPos.Y + range*2)),
new SegmentPoint(new Vector2(drawPos.X + range*2, drawPos.Y - range*2)),
new SegmentPoint(new Vector2(drawPos.X - range*2, drawPos.Y - range*2)),
new SegmentPoint(new Vector2(drawPos.X - range*2, drawPos.Y + range*2))
};
points.AddRange(boundaryCorners);
var compareCCW = new CompareSegmentPointCW(drawPos);
points.Sort(compareCCW);
List<Vector2> output = new List<Vector2>();
//remove points that are very close to each other
for (int i = 0; i < points.Count - 1; i++)
{
if (Math.Abs(points[i].WorldPos.X - points[i + 1].WorldPos.X) < 3 &&
Math.Abs(points[i].WorldPos.Y - points[i + 1].WorldPos.Y) < 3)
{
points.RemoveAt(i + 1);
}
}
foreach (SegmentPoint p in points)
{
Vector2 dir = Vector2.Normalize(p.WorldPos - drawPos);
Vector2 dirNormal = new Vector2(-dir.Y, dir.X)*3;
//do two slightly offset raycasts to hit the segment itself and whatever's behind it
Vector2 intersection1 = RayCast(drawPos, drawPos + dir * range * 2 - dirNormal, visibleSegments);
Vector2 intersection2 = RayCast(drawPos, drawPos + dir * range * 2 + dirNormal, visibleSegments);
//hit almost the same position -> only add one vertex to output
if ((Math.Abs(intersection1.X - intersection2.X) < 5 &&
Math.Abs(intersection1.Y - intersection2.Y) < 5))
{
output.Add(intersection1);
}
else
{
output.Add(intersection1);
output.Add(intersection2);
}
}
return output;
}
private Vector2 RayCast(Vector2 rayStart, Vector2 rayEnd, List<Segment> segments)
{
float closestDist = 0.0f;
Vector2? closestIntersection = null;
foreach (Segment s in segments)
{
Vector2? intersection = MathUtils.GetAxisAlignedLineIntersection(rayStart, rayEnd, s.Start.WorldPos, s.End.WorldPos);
if (intersection != null)
{
float dist = Vector2.Distance((Vector2)intersection, rayStart);
if (closestIntersection == null || dist < closestDist)
{
closestDist = dist;
closestIntersection = intersection;
}
}
}
return closestIntersection == null ? rayEnd : (Vector2)closestIntersection;
}
private void CalculateVertices(List<Vector2> encounters,
out VertexPositionTexture[] vertexArray, out short[] indexArray)
{
List<VertexPositionTexture> vertices = new List<VertexPositionTexture>();
Vector2 drawPos = position;
if (ParentSub != null) drawPos += ParentSub.DrawPosition;
//sort points counter-clockwise
var compareCCW = new CompareSegmentPointCCW(drawPos);
points.Sort(compareCCW);
// Add a vertex for the center of the mesh
vertices.Add(new VertexPositionTexture(new Vector3(drawPos.X, drawPos.Y, 0),
new Vector2(0.5f, 0.5f)));
//TODO: calculate triangles from points
//http://www.redblobgames.com/articles/visibility/
//http://roy-t.nl/index.php/2014/02/27/2d-lighting-and-shadows-preview/
// Add all the other encounter points as vertices
// storing their world position as UV coordinates
foreach (Vector2 vertex in encounters)
{
Vector2 diff = vertex - drawPos;
return points.Select(p => p.WorldPos).ToList();
}
public void Draw(SpriteBatch spriteBatch)
vertices.Add(new VertexPositionTexture(new Vector3(vertex.X, vertex.Y, 0),
new Vector2(0.5f, 0.5f) + diff / range / 2));
}
// Compute the indices to form triangles
List<short> indices = new List<short>();
for (int i = 0; i < encounters.Count - 1; i++)
{
indices.Add(0);
indices.Add((short)((i + 2) % vertices.Count));
indices.Add((short)((i + 1) % vertices.Count));
}
indices.Add(0);
indices.Add((short)(1));
indices.Add((short)(vertices.Count - 1));
vertexArray = vertices.ToArray<VertexPositionTexture>();
indexArray = indices.ToArray<short>();
}
public void Draw(SpriteBatch spriteBatch, BasicEffect lightEffect)
{
Vector2 drawPos = position;
if (ParentSub != null) drawPos += ParentSub.DrawPosition;
drawPos.Y = -drawPos.Y;
if (range > 1.0f)
if (range > 1.0f && false)
{
if (overrideLightTexture == null)
{
@@ -296,15 +409,36 @@ namespace Barotrauma.Lights
if (LightSprite != null)
{
LightSprite.Draw(spriteBatch, drawPos, Color, LightSprite.Origin, -Rotation, 1, SpriteEffect);
//LightSprite.Draw(spriteBatch, drawPos, Color, LightSprite.Origin, -Rotation, 1, SpriteEffect);
}
var verts = CalculateFanVertices();
var verts = FindRaycastHits();
foreach (var vert in verts)
/*for (int i = 0; i < verts.Count; i++)
{
GUI.DrawLine(spriteBatch, drawPos, new Vector2(vert.X, -vert.Y), Color.White);
}
Color[] clrs = new Color[] { Color.Green, Color.Cyan, Color.Red, Color.White, Color.Magenta };
Color clr = clrs[i % clrs.Length];
// 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;
lightEffect.DiffuseColor = (new Vector3(color.R, color.G, color.B) * (color.A / 255.0f)) / 255.0f;// color.ToVector3();
lightEffect.CurrentTechnique.Passes[0].Apply();
GameMain.CurrGraphicsDevice.DrawUserIndexedPrimitives<VertexPositionTexture>
(
PrimitiveType.TriangleList, vertices, 0, vertices.Length, indices, 0, indices.Length / 3
);
}
public void FlipX()

View File

@@ -208,6 +208,48 @@ namespace Barotrauma
return a1 + t * b;
}
/// <summary>
/// Get the point where line segment (a1, a2) intersects the axis-aligned line segment (axisAligned1, axisAligned2)
/// </summary>
public static Vector2? GetAxisAlignedLineIntersection(Vector2 a1, Vector2 a2, Vector2 axisAligned1, Vector2 axisAligned2)
{
if (Math.Abs(axisAligned1.X - axisAligned2.X) < 1.0f)
{
if (Math.Sign(a1.X - axisAligned1.X) == Math.Sign(a2.X - axisAligned1.X))
return null;
if (Math.Max(a1.Y, a2.Y) < Math.Min(axisAligned1.Y, axisAligned2.Y))
return null;
if (Math.Min(a1.Y, a2.Y) > Math.Max(axisAligned1.Y, axisAligned2.Y))
return null;
}
else
{
if (Math.Sign(a1.Y - axisAligned1.Y) == Math.Sign(a2.Y - axisAligned1.Y))
return null;
if (Math.Max(a1.X, a2.X) < Math.Min(axisAligned1.X, axisAligned2.X))
return null;
if (Math.Min(a1.X, a2.X) > Math.Max(axisAligned1.X, axisAligned2.X))
return null;
}
Vector2 b = a2 - a1;
Vector2 d = axisAligned2 - axisAligned1;
float bDotDPerp = b.X * d.Y - b.Y * d.X;
Vector2 c = axisAligned1 - a1;
float t = (c.X * d.Y - c.Y * d.X) / bDotDPerp;
if (t < 0 || t > 1) return null;
float u = (c.X * b.Y - c.Y * b.X) / bDotDPerp;
if (u < 0 || u > 1) return null;
return a1 + t * b;
}
public static Vector2? GetLineRectangleIntersection(Vector2 a1, Vector2 a2, Rectangle rect)
{
Vector2? intersection = GetLineIntersection(a1, a2,
@@ -477,17 +519,17 @@ namespace Barotrauma
}
}
class CompareSegmentPointCCW : IComparer<Lights.SegmentPoint>
class CompareSegmentPointCW : IComparer<Lights.SegmentPoint>
{
private Vector2 center;
public CompareSegmentPointCCW(Vector2 center)
public CompareSegmentPointCW(Vector2 center)
{
this.center = center;
}
public int Compare(Lights.SegmentPoint a, Lights.SegmentPoint b)
{
return CompareCCW.Compare(a.WorldPos, b.WorldPos, center);
return -CompareCCW.Compare(a.WorldPos, b.WorldPos, center);
}
}
}