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

View File

@@ -119,8 +119,6 @@ namespace Barotrauma
get { return targetPos; }
set {
targetPos = value;
System.Diagnostics.Debug.WriteLine(value);
}
}

View File

@@ -198,8 +198,15 @@ namespace Barotrauma.Items.Components
if (pointDist > radius) continue;
if (pointDist > prevPingRadius && pointDist < pingRadius)
{
var blip = new RadarBlip(limb.WorldPosition, 1.0f);
radarBlips.Add(blip);
float limbSize = limb.Mass;
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);
}
}
}
}

View File

@@ -151,6 +151,9 @@ namespace Barotrauma
{
if (WallVertices == null || WallVertices.Length <= 0) return;
Stopwatch sw = new Stopwatch();
sw.Start();
basicEffect.World = cam.ShaderTransform
* Matrix.CreateOrthographic(GameMain.GraphicsWidth, GameMain.GraphicsHeight, -1, 1) * 0.5f;
@@ -194,6 +197,10 @@ namespace Barotrauma
}
}
sw.Stop();
Debug.WriteLine("level render: "+sw.ElapsedTicks);
}
}

View File

@@ -1,6 +1,7 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace Barotrauma.Lights
@@ -38,6 +39,8 @@ namespace Barotrauma.Lights
private VertexPositionColor[] shadowVertices;
private VertexPositionTexture[] penumbraVertices;
int shadowVertexCount;
private Entity parentEntity;
private Rectangle boundingBox;
@@ -73,6 +76,10 @@ namespace Barotrauma.Lights
cachedShadows = new Dictionary<LightSource, CachedShadow>();
shadowVertices = new VertexPositionColor[6 * 2];
penumbraVertices = new VertexPositionTexture[6];
vertices = points;
primitiveCount = vertices.Length;
@@ -139,6 +146,8 @@ namespace Barotrauma.Lights
private void CalculateShadowVertices(Vector2 lightSourcePos, bool los = true)
{
shadowVertexCount = 0;
//compute facing of each edge, using N*L
for (int i = 0; i < primitiveCount; i++)
{
@@ -172,15 +181,13 @@ namespace Barotrauma.Lights
startingIndex = nextEdge;
}
int shadowVertexCount;
//nr of vertices that are in the shadow
if (endingIndex > startingIndex)
shadowVertexCount = endingIndex - startingIndex + 1;
else
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
int currentIndex = startingIndex;
@@ -213,8 +220,6 @@ namespace Barotrauma.Lights
private void CalculatePenumbraVertices(int startingIndex, int endingIndex, Vector2 lightSourcePos, bool los)
{
penumbraVertices = new VertexPositionTexture[6];
for (int n = 0; n < 4; n += 3)
{
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)
{
Vector3 offset = Vector3.Zero;
if (parentEntity != null && parentEntity.Submarine != null)
{
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;
shadowEffect.CurrentTechnique.Passes[0].Apply();
graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleStrip, shadowVertices, 0, shadowVertices.Length - 2);
graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleStrip, shadowVertices, 0, shadowVertexCount*2 - 2);
}
if (los)
{
penumbraEffect.World = shadowEffect.World;
@@ -330,6 +340,7 @@ namespace Barotrauma.Lights
graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, penumbraVertices, 0, 2, VertexPositionTexture.VertexDeclaration);
#endif
}
}
public void Remove()

View File

@@ -60,6 +60,10 @@ namespace Barotrauma.Lights
public void DrawLOS(GraphicsDevice graphics, SpriteBatch spriteBatch, Camera cam, Vector2 pos)
{
Stopwatch sw = new Stopwatch();
sw.Start();
if (!LosEnabled) return;
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);
}
sw.Stop();
Debug.WriteLine("drawlos: " + sw.ElapsedTicks + " (" + sw.ElapsedMilliseconds + ")");
if (!ObstructVision) return;
spriteBatch.Begin(SpriteSortMode.Deferred, CustomBlendStates.Multiplicative);
@@ -82,6 +89,7 @@ namespace Barotrauma.Lights
spriteBatch.End();
ObstructVision = false;
}
public void OnMapLoaded()
@@ -94,6 +102,10 @@ namespace Barotrauma.Lights
public void UpdateLightMap(GraphicsDevice graphics, SpriteBatch spriteBatch, Camera cam)
{
Stopwatch sw = new Stopwatch();
sw.Start();
if (!LightingEnabled) return;
Matrix shadowTransform = cam.ShaderTransform
@@ -152,6 +164,10 @@ namespace Barotrauma.Lights
graphics.SetRenderTarget(null);
Debug.WriteLine("lights: " + sw.ElapsedTicks + " (" + sw.ElapsedMilliseconds + ")");
if (!ObstructVision) return;
}
public void UpdateObstructVision(GraphicsDevice graphics, SpriteBatch spriteBatch, Camera cam, Vector2 lookAtPosition)

Binary file not shown.