From f3f875e641e7ea0e470456b81a1c60c7ad9adb7a Mon Sep 17 00:00:00 2001 From: Regalis Date: Sun, 26 Feb 2017 18:44:18 +0200 Subject: [PATCH] Calculating light volumes works now. Very poorly optimized at the moment, todo: cache volumes and only recalculate when needed --- Subsurface/Source/Map/Lights/ConvexHull.cs | 47 +++-- Subsurface/Source/Map/Lights/LightManager.cs | 27 ++- Subsurface/Source/Map/Lights/LightSource.cs | 174 ++++++++++++++++--- Subsurface/Source/Utils/MathUtils.cs | 48 ++++- 4 files changed, 255 insertions(+), 41 deletions(-) diff --git a/Subsurface/Source/Map/Lights/ConvexHull.cs b/Subsurface/Source/Map/Lights/ConvexHull.cs index 5a56766e4..7ac0d6c67 100644 --- a/Subsurface/Source/Map/Lights/ConvexHull.cs +++ b/Subsurface/Source/Map/Lights/ConvexHull.cs @@ -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; + } } diff --git a/Subsurface/Source/Map/Lights/LightManager.cs b/Subsurface/Source/Map/Lights/LightManager.cs index 4040be2ab..21364e4f9 100644 --- a/Subsurface/Source/Map/Lights/LightManager.cs +++ b/Subsurface/Source/Map/Lights/LightManager.cs @@ -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(); smoothedHullAmbientLights = new Dictionary(); @@ -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) diff --git a/Subsurface/Source/Map/Lights/LightSource.cs b/Subsurface/Source/Map/Lights/LightSource.cs index f37ed9fa9..cd303ff82 100644 --- a/Subsurface/Source/Map/Lights/LightSource.cs +++ b/Subsurface/Source/Map/Lights/LightSource.cs @@ -239,43 +239,156 @@ namespace Barotrauma.Lights return chList.List; }*/ - private List CalculateFanVertices() + private List 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 visibleSegments = new List(); List points = new List(); 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 { + 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 output = new List(); + + //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 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 encounters, + out VertexPositionTexture[] vertexArray, out short[] indexArray) + { + List vertices = new List(); + 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 indices = new List(); + 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(); + indexArray = indices.ToArray(); + } + + 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 + ( + PrimitiveType.TriangleList, vertices, 0, vertices.Length, indices, 0, indices.Length / 3 + ); } public void FlipX() diff --git a/Subsurface/Source/Utils/MathUtils.cs b/Subsurface/Source/Utils/MathUtils.cs index 8215b79b9..279fc3088 100644 --- a/Subsurface/Source/Utils/MathUtils.cs +++ b/Subsurface/Source/Utils/MathUtils.cs @@ -208,6 +208,48 @@ namespace Barotrauma return a1 + t * b; } + /// + /// Get the point where line segment (a1, a2) intersects the axis-aligned line segment (axisAligned1, axisAligned2) + /// + 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 + class CompareSegmentPointCW : IComparer { 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); } } }