convexhull shadow caching attempt

This commit is contained in:
Regalis
2015-12-19 16:33:32 +02:00
parent e10bffde9b
commit 50cba53f25
+51 -23
View File
@@ -14,11 +14,16 @@ namespace Barotrauma.Lights
public Vector2 LightPos; public Vector2 LightPos;
public CachedShadow(VertexPositionColor[] shadowVertices, VertexPositionTexture[] penumbraVertices, Vector2 lightPos) public int ShadowVertexCount, PenumbraVertexCount;
public CachedShadow(VertexPositionColor[] shadowVertices, VertexPositionTexture[] penumbraVertices, Vector2 lightPos, int shadowVertexCount, int penumbraVertexCount)
{ {
ShadowVertices = shadowVertices; ShadowVertices = shadowVertices;
PenumbraVertices = penumbraVertices; PenumbraVertices = penumbraVertices;
ShadowVertexCount = shadowVertexCount;
PenumbraVertexCount = penumbraVertexCount;
LightPos = lightPos; LightPos = lightPos;
} }
} }
@@ -39,6 +44,8 @@ namespace Barotrauma.Lights
private VertexPositionColor[] shadowVertices; private VertexPositionColor[] shadowVertices;
private VertexPositionTexture[] penumbraVertices; private VertexPositionTexture[] penumbraVertices;
private VertexBuffer shadowBuffer, penumbraBuffer;
int shadowVertexCount; int shadowVertexCount;
private Entity parentEntity; private Entity parentEntity;
@@ -79,6 +86,8 @@ namespace Barotrauma.Lights
shadowVertices = new VertexPositionColor[6 * 2]; shadowVertices = new VertexPositionColor[6 * 2];
penumbraVertices = new VertexPositionTexture[6]; penumbraVertices = new VertexPositionTexture[6];
shadowBuffer = new VertexBuffer(GameMain.CurrGraphicsDevice, VertexPositionColor.VertexDeclaration, 6*2, BufferUsage.WriteOnly);
vertices = points; vertices = points;
primitiveCount = vertices.Length; primitiveCount = vertices.Length;
@@ -216,6 +225,10 @@ namespace Barotrauma.Lights
{ {
CalculatePenumbraVertices(startingIndex, endingIndex, lightSourcePos, los); CalculatePenumbraVertices(startingIndex, endingIndex, lightSourcePos, los);
} }
else
{
shadowBuffer.SetData(shadowVertices);
}
} }
private void CalculatePenumbraVertices(int startingIndex, int endingIndex, Vector2 lightSourcePos, bool los) private void CalculatePenumbraVertices(int startingIndex, int endingIndex, Vector2 lightSourcePos, bool los)
@@ -266,35 +279,39 @@ namespace Barotrauma.Lights
if (!Enabled) return; if (!Enabled) return;
CachedShadow cachedShadow = null; CachedShadow cachedShadow = null;
if (cachedShadows.TryGetValue(light, out cachedShadow) && false) if (cachedShadows.TryGetValue(light, out cachedShadow) &&
(light.Position == cachedShadow.LightPos || Vector2.DistanceSquared(light.Position, cachedShadow.LightPos) < 1.0f))
{ {
if (light.Position == cachedShadow.LightPos || //{
Vector2.DistanceSquared(light.WorldPosition, cachedShadow.LightPos) < 1.0f) shadowVertices = cachedShadow.ShadowVertices;
{ penumbraVertices = cachedShadow.PenumbraVertices;
shadowVertices = cachedShadow.ShadowVertices;
penumbraVertices = cachedShadow.PenumbraVertices;
} shadowVertexCount = cachedShadow.ShadowVertexCount;
else
{
CalculateShadowVertices(light.WorldPosition, los);
cachedShadow.LightPos = light.WorldPosition;
cachedShadow.ShadowVertices = shadowVertices;
cachedShadow.PenumbraVertices = penumbraVertices;
}
//}
//else
//CalculateShadowVertices(light.Position, los);
//cachedShadow.LightPos = light.Position;
//cachedShadow.ShadowVertices = shadowVertices;
//cachedShadow.PenumbraVertices = penumbraVertices;
} }
else else
{ {
Vector2 lightPos = light.WorldPosition; Vector2 lightPos = light.Position;
if (light.Submarine!=null && parentEntity != null && parentEntity.Submarine == light.Submarine) //if (light.Submarine!=null && parentEntity != null && parentEntity.Submarine == light.Submarine)
{ //{
lightPos = light.Position; // lightPos = light.Position;
} //}
CalculateShadowVertices(lightPos, los); CalculateShadowVertices(lightPos, los);
// cachedShadow = new CachedShadow(shadowVertices, penumbraVertices, light.WorldPosition);
// cachedShadows.Add(light, cachedShadow); if (cachedShadows.ContainsKey(light)) cachedShadows.Remove(light);
cachedShadow = new CachedShadow(shadowVertices, penumbraVertices, light.Position, shadowVertexCount, 0);
cachedShadows.Add(light, cachedShadow);
} }
DrawShadows(graphicsDevice, cam, transform, los); DrawShadows(graphicsDevice, cam, transform, los);
@@ -323,10 +340,21 @@ namespace Barotrauma.Lights
if (shadowVertexCount>0) if (shadowVertexCount>0)
{ {
shadowEffect.World = Matrix.CreateTranslation(offset) * transform; shadowEffect.World = Matrix.CreateTranslation(offset) * transform;
shadowEffect.CurrentTechnique.Passes[0].Apply(); shadowEffect.CurrentTechnique.Passes[0].Apply();
graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleStrip, shadowVertices, 0, shadowVertexCount*2 - 2); if (los || true)
{
graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleStrip, shadowVertices, 0, shadowVertexCount * 2 - 2);
}
else
{
graphicsDevice.SetVertexBuffer(shadowBuffer);
graphicsDevice.DrawPrimitives(PrimitiveType.TriangleStrip, 0, shadowVertexCount);
}
} }