Light rendering optimization

This commit is contained in:
Regalis
2015-12-19 16:02:05 +02:00
parent 4234aa2094
commit e10bffde9b
6 changed files with 52 additions and 13 deletions
-2
View File
@@ -119,8 +119,6 @@ namespace Barotrauma
get { return targetPos; } get { return targetPos; }
set { set {
targetPos = value; targetPos = value;
System.Diagnostics.Debug.WriteLine(value);
} }
} }
@@ -198,8 +198,15 @@ namespace Barotrauma.Items.Components
if (pointDist > radius) continue; if (pointDist > radius) continue;
if (pointDist > prevPingRadius && pointDist < pingRadius) if (pointDist > prevPingRadius && pointDist < pingRadius)
{ {
var blip = new RadarBlip(limb.WorldPosition, 1.0f); float limbSize = limb.Mass;
radarBlips.Add(blip);
for (int i = 0; i<=limb.Mass/100.0f; i++)
{
var blip = new RadarBlip(limb.WorldPosition+Rand.Vector(limb.Mass/10.0f), 1.0f);
radarBlips.Add(blip);
}
} }
} }
} }
@@ -151,6 +151,9 @@ namespace Barotrauma
{ {
if (WallVertices == null || WallVertices.Length <= 0) return; if (WallVertices == null || WallVertices.Length <= 0) return;
Stopwatch sw = new Stopwatch();
sw.Start();
basicEffect.World = cam.ShaderTransform basicEffect.World = cam.ShaderTransform
* Matrix.CreateOrthographic(GameMain.GraphicsWidth, GameMain.GraphicsHeight, -1, 1) * 0.5f; * Matrix.CreateOrthographic(GameMain.GraphicsWidth, GameMain.GraphicsHeight, -1, 1) * 0.5f;
@@ -194,6 +197,10 @@ namespace Barotrauma
} }
} }
sw.Stop();
Debug.WriteLine("level render: "+sw.ElapsedTicks);
} }
} }
+20 -9
View File
@@ -1,6 +1,7 @@
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
namespace Barotrauma.Lights namespace Barotrauma.Lights
@@ -38,6 +39,8 @@ namespace Barotrauma.Lights
private VertexPositionColor[] shadowVertices; private VertexPositionColor[] shadowVertices;
private VertexPositionTexture[] penumbraVertices; private VertexPositionTexture[] penumbraVertices;
int shadowVertexCount;
private Entity parentEntity; private Entity parentEntity;
private Rectangle boundingBox; private Rectangle boundingBox;
@@ -73,6 +76,10 @@ namespace Barotrauma.Lights
cachedShadows = new Dictionary<LightSource, CachedShadow>(); cachedShadows = new Dictionary<LightSource, CachedShadow>();
shadowVertices = new VertexPositionColor[6 * 2];
penumbraVertices = new VertexPositionTexture[6];
vertices = points; vertices = points;
primitiveCount = vertices.Length; primitiveCount = vertices.Length;
@@ -139,6 +146,8 @@ namespace Barotrauma.Lights
private void CalculateShadowVertices(Vector2 lightSourcePos, bool los = true) private void CalculateShadowVertices(Vector2 lightSourcePos, bool los = true)
{ {
shadowVertexCount = 0;
//compute facing of each edge, using N*L //compute facing of each edge, using N*L
for (int i = 0; i < primitiveCount; i++) for (int i = 0; i < primitiveCount; i++)
{ {
@@ -172,15 +181,13 @@ namespace Barotrauma.Lights
startingIndex = nextEdge; startingIndex = nextEdge;
} }
int shadowVertexCount;
//nr of vertices that are in the shadow //nr of vertices that are in the shadow
if (endingIndex > startingIndex) if (endingIndex > startingIndex)
shadowVertexCount = endingIndex - startingIndex + 1; shadowVertexCount = endingIndex - startingIndex + 1;
else else
shadowVertexCount = primitiveCount + 1 - startingIndex + endingIndex; shadowVertexCount = primitiveCount + 1 - startingIndex + endingIndex;
shadowVertices = new VertexPositionColor[shadowVertexCount * 2]; //shadowVertices = new VertexPositionColor[shadowVertexCount * 2];
//create a triangle strip that has the shape of the shadow //create a triangle strip that has the shape of the shadow
int currentIndex = startingIndex; int currentIndex = startingIndex;
@@ -213,8 +220,6 @@ namespace Barotrauma.Lights
private void CalculatePenumbraVertices(int startingIndex, int endingIndex, Vector2 lightSourcePos, bool los) private void CalculatePenumbraVertices(int startingIndex, int endingIndex, Vector2 lightSourcePos, bool los)
{ {
penumbraVertices = new VertexPositionTexture[6];
for (int n = 0; n < 4; n += 3) for (int n = 0; n < 4; n += 3)
{ {
Vector3 penumbraStart = new Vector3((n == 0) ? vertices[startingIndex] : vertices[endingIndex], 0.0f); Vector3 penumbraStart = new Vector3((n == 0) ? vertices[startingIndex] : vertices[endingIndex], 0.0f);
@@ -309,18 +314,23 @@ namespace Barotrauma.Lights
private void DrawShadows(GraphicsDevice graphicsDevice, Camera cam, Matrix transform, bool los = true) private void DrawShadows(GraphicsDevice graphicsDevice, Camera cam, Matrix transform, bool los = true)
{ {
Vector3 offset = Vector3.Zero; Vector3 offset = Vector3.Zero;
if (parentEntity != null && parentEntity.Submarine != null) if (parentEntity != null && parentEntity.Submarine != null)
{ {
offset = new Vector3(parentEntity.Submarine.DrawPosition.X, parentEntity.Submarine.DrawPosition.Y, 0.0f); offset = new Vector3(parentEntity.Submarine.DrawPosition.X, parentEntity.Submarine.DrawPosition.Y, 0.0f);
} }
if (shadowVertexCount>0)
{
shadowEffect.World = Matrix.CreateTranslation(offset) * transform;
shadowEffect.CurrentTechnique.Passes[0].Apply();
shadowEffect.World = Matrix.CreateTranslation(offset) * transform; graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleStrip, shadowVertices, 0, shadowVertexCount*2 - 2);
shadowEffect.CurrentTechnique.Passes[0].Apply();
graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleStrip, shadowVertices, 0, shadowVertices.Length - 2);
}
if (los) if (los)
{ {
penumbraEffect.World = shadowEffect.World; penumbraEffect.World = shadowEffect.World;
@@ -330,6 +340,7 @@ namespace Barotrauma.Lights
graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, penumbraVertices, 0, 2, VertexPositionTexture.VertexDeclaration); graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, penumbraVertices, 0, 2, VertexPositionTexture.VertexDeclaration);
#endif #endif
} }
} }
public void Remove() public void Remove()
@@ -60,6 +60,10 @@ namespace Barotrauma.Lights
public void DrawLOS(GraphicsDevice graphics, SpriteBatch spriteBatch, Camera cam, Vector2 pos) public void DrawLOS(GraphicsDevice graphics, SpriteBatch spriteBatch, Camera cam, Vector2 pos)
{ {
Stopwatch sw = new Stopwatch();
sw.Start();
if (!LosEnabled) return; if (!LosEnabled) return;
Rectangle camView = new Rectangle(cam.WorldView.X, cam.WorldView.Y - cam.WorldView.Height, cam.WorldView.Width, cam.WorldView.Height); Rectangle camView = new Rectangle(cam.WorldView.X, cam.WorldView.Y - cam.WorldView.Height, cam.WorldView.Width, cam.WorldView.Height);
@@ -75,6 +79,9 @@ namespace Barotrauma.Lights
convexHull.DrawShadows(graphics, cam, pos, shadowTransform); convexHull.DrawShadows(graphics, cam, pos, shadowTransform);
} }
sw.Stop();
Debug.WriteLine("drawlos: " + sw.ElapsedTicks + " (" + sw.ElapsedMilliseconds + ")");
if (!ObstructVision) return; if (!ObstructVision) return;
spriteBatch.Begin(SpriteSortMode.Deferred, CustomBlendStates.Multiplicative); spriteBatch.Begin(SpriteSortMode.Deferred, CustomBlendStates.Multiplicative);
@@ -82,6 +89,7 @@ namespace Barotrauma.Lights
spriteBatch.End(); spriteBatch.End();
ObstructVision = false; ObstructVision = false;
} }
public void OnMapLoaded() public void OnMapLoaded()
@@ -94,6 +102,10 @@ namespace Barotrauma.Lights
public void UpdateLightMap(GraphicsDevice graphics, SpriteBatch spriteBatch, Camera cam) public void UpdateLightMap(GraphicsDevice graphics, SpriteBatch spriteBatch, Camera cam)
{ {
Stopwatch sw = new Stopwatch();
sw.Start();
if (!LightingEnabled) return; if (!LightingEnabled) return;
Matrix shadowTransform = cam.ShaderTransform Matrix shadowTransform = cam.ShaderTransform
@@ -152,6 +164,10 @@ namespace Barotrauma.Lights
graphics.SetRenderTarget(null); graphics.SetRenderTarget(null);
Debug.WriteLine("lights: " + sw.ElapsedTicks + " (" + sw.ElapsedMilliseconds + ")");
if (!ObstructVision) return;
} }
public void UpdateObstructVision(GraphicsDevice graphics, SpriteBatch spriteBatch, Camera cam, Vector2 lookAtPosition) public void UpdateObstructVision(GraphicsDevice graphics, SpriteBatch spriteBatch, Camera cam, Vector2 lookAtPosition)
Binary file not shown.