Lighting improvements: lightmap is drawn over all structures and blurred, flashlight item, OnContained statuseffects are also applied when the item is contained in a characterinventory

This commit is contained in:
Regalis
2016-05-07 09:13:23 +03:00
parent ef05650c01
commit d5f6fff2c2
14 changed files with 384 additions and 129 deletions
+104 -74
View File
@@ -13,9 +13,9 @@ namespace Barotrauma.Lights
public Vector2 LightPos;
public int ShadowVertexCount, PenumbraVertexCount;
public int ShadowVertexCount;//, PenumbraVertexCount;
public CachedShadow(VertexPositionColor[] shadowVertices, VertexPositionTexture[] penumbraVertices, Vector2 lightPos, int shadowVertexCount, int penumbraVertexCount)
public CachedShadow(VertexPositionColor[] shadowVertices, Vector2 lightPos, int shadowVertexCount, int penumbraVertexCount)
{
//var ShadowVertices = new VertexPositionColor [shadowVertices.Count()];
//shadowVertices.CopyTo(ShadowVertices, 0);
@@ -24,7 +24,7 @@ namespace Barotrauma.Lights
ShadowBuffer.SetData(shadowVertices, 0, shadowVertices.Length);
ShadowVertexCount = shadowVertexCount;
PenumbraVertexCount = penumbraVertexCount;
//PenumbraVertexCount = penumbraVertexCount;
LightPos = lightPos;
}
@@ -46,17 +46,18 @@ namespace Barotrauma.Lights
{
public static List<ConvexHull> list = new List<ConvexHull>();
static BasicEffect shadowEffect;
static BasicEffect penumbraEffect;
//static BasicEffect penumbraEffect;
private Dictionary<LightSource, CachedShadow> cachedShadows;
private Vector2[] vertices;
private Vector2[] losVertices;
private int primitiveCount;
private bool[] backFacing;
private VertexPositionColor[] shadowVertices;
private VertexPositionTexture[] penumbraVertices;
// private VertexPositionTexture[] penumbraVertices;
int shadowVertexCount;
@@ -88,26 +89,26 @@ namespace Barotrauma.Lights
shadowEffect = new BasicEffect(GameMain.CurrGraphicsDevice);
shadowEffect.VertexColorEnabled = true;
}
if (penumbraEffect == null)
{
penumbraEffect = new BasicEffect(GameMain.CurrGraphicsDevice);
penumbraEffect.TextureEnabled = true;
//shadowEffect.VertexColorEnabled = true;
penumbraEffect.LightingEnabled = false;
penumbraEffect.Texture = TextureLoader.FromFile("Content/Lights/penumbra.png");
}
//if (penumbraEffect == null)
//{
// penumbraEffect = new BasicEffect(GameMain.CurrGraphicsDevice);
// penumbraEffect.TextureEnabled = true;
// //shadowEffect.VertexColorEnabled = true;
// penumbraEffect.LightingEnabled = false;
// penumbraEffect.Texture = TextureLoader.FromFile("Content/Lights/penumbra.png");
//}
parentEntity = parent;
cachedShadows = new Dictionary<LightSource, CachedShadow>();
shadowVertices = new VertexPositionColor[6 * 2];
penumbraVertices = new VertexPositionTexture[6];
//penumbraVertices = new VertexPositionTexture[6];
vertices = points;
primitiveCount = vertices.Length;
CalculateDimensions();
//vertices = points;
primitiveCount = points.Length;
SetVertices(points);
//CalculateDimensions();
backFacing = new bool[primitiveCount];
@@ -139,6 +140,7 @@ namespace Barotrauma.Lights
for (int i = 0; i < vertices.Count(); i++)
{
vertices[i] += amount;
losVertices[i] += amount;
}
CalculateDimensions();
@@ -149,6 +151,26 @@ namespace Barotrauma.Lights
ClearCachedShadows();
vertices = points;
losVertices = points;
int margin = 7;
if (Math.Abs(points[0].X - points[2].X) < Math.Abs(points[0].Y - points[1].Y))
{
losVertices = new Vector2[] {
new Vector2(points[0].X+margin, points[0].Y),
new Vector2(points[1].X+margin, points[1].Y),
new Vector2(points[2].X-margin, points[2].Y),
new Vector2(points[3].X-margin, points[3].Y)};
}
else
{
losVertices = new Vector2[] {
new Vector2(points[0].X, points[0].Y +margin),
new Vector2(points[1].X, points[1].Y - margin),
new Vector2(points[2].X, points[2].Y - margin),
new Vector2(points[3].X, points[3].Y + margin)};
}
CalculateDimensions();
}
@@ -191,6 +213,8 @@ namespace Barotrauma.Lights
{
shadowVertexCount = 0;
var vertices = los ? losVertices : this.vertices;
//compute facing of each edge, using N*L
for (int i = 0; i < primitiveCount; i++)
{
@@ -205,7 +229,7 @@ namespace Barotrauma.Lights
-(secondVertex.Y - firstVertex.Y),
secondVertex.X - firstVertex.X);
backFacing[i] = (Vector2.Dot(N, L) < 0);
backFacing[i] = (Vector2.Dot(N, L) < 0) == los;
}
//find beginning and ending vertices which
@@ -239,70 +263,76 @@ namespace Barotrauma.Lights
{
Vector3 vertexPos = new Vector3(vertices[currentIndex], 0.0f);
int i = los ? svCount : svCount + 1;
int j = los ? svCount + 1 : svCount;
//one vertex on the hull
shadowVertices[svCount] = new VertexPositionColor();
shadowVertices[svCount].Color = los ? Color.Black : Color.Transparent;
shadowVertices[svCount].Position = vertexPos;
shadowVertices[i] = new VertexPositionColor();
shadowVertices[i].Color = los ? Color.Black : Color.Transparent;
shadowVertices[i].Position = vertexPos;
//one extruded by the light direction
shadowVertices[svCount + 1] = new VertexPositionColor();
shadowVertices[svCount + 1].Color = los ? Color.Black : Color.Transparent;
shadowVertices[j] = new VertexPositionColor();
shadowVertices[j].Color = shadowVertices[i].Color;
Vector3 L2P = vertexPos - new Vector3(lightSourcePos, 0);
L2P.Normalize();
shadowVertices[svCount + 1].Position = new Vector3(lightSourcePos, 0) + L2P * 9000;
shadowVertices[j].Position = new Vector3(lightSourcePos, 0) + L2P * 9000;
svCount += 2;
currentIndex = (currentIndex + 1) % primitiveCount;
}
if (los)
{
CalculatePenumbraVertices(startingIndex, endingIndex, lightSourcePos, los);
}
//if (los)
//{
// CalculatePenumbraVertices(startingIndex, endingIndex, lightSourcePos, los);
//}
}
private void CalculatePenumbraVertices(int startingIndex, int endingIndex, Vector2 lightSourcePos, bool los)
{
for (int n = 0; n < 4; n += 3)
{
Vector3 penumbraStart = new Vector3((n == 0) ? vertices[startingIndex] : vertices[endingIndex], 0.0f);
//private void CalculatePenumbraVertices(int startingIndex, int endingIndex, Vector2 lightSourcePos, bool los)
//{
// for (int n = 0; n < 4; n += 3)
// {
// Vector3 penumbraStart = new Vector3((n == 0) ? vertices[startingIndex] : vertices[endingIndex], 0.0f);
penumbraVertices[n] = new VertexPositionTexture();
penumbraVertices[n].Position = penumbraStart;
penumbraVertices[n].TextureCoordinate = new Vector2(0.0f, 1.0f);
//penumbraVertices[0].te = fow ? Color.Black : Color.Transparent;
// penumbraVertices[n] = new VertexPositionTexture();
// penumbraVertices[n].Position = penumbraStart;
// penumbraVertices[n].TextureCoordinate = new Vector2(0.0f, 1.0f);
// //penumbraVertices[0].te = fow ? Color.Black : Color.Transparent;
for (int i = 0; i < 2; i++)
{
penumbraVertices[n + i + 1] = new VertexPositionTexture();
Vector3 vertexDir = penumbraStart - new Vector3(lightSourcePos, 0);
vertexDir.Normalize();
// for (int i = 0; i < 2; i++)
// {
// penumbraVertices[n + i + 1] = new VertexPositionTexture();
// Vector3 vertexDir = penumbraStart - new Vector3(lightSourcePos, 0);
// vertexDir.Normalize();
Vector3 normal = (i == 0) ? new Vector3(-vertexDir.Y, vertexDir.X, 0.0f) : new Vector3(vertexDir.Y, -vertexDir.X, 0.0f) * 0.05f;
if (n > 0) normal = -normal;
// Vector3 normal = (i == 0) ? new Vector3(-vertexDir.Y, vertexDir.X, 0.0f) : new Vector3(vertexDir.Y, -vertexDir.X, 0.0f) * 0.05f;
// if (n > 0) normal = -normal;
vertexDir = penumbraStart - (new Vector3(lightSourcePos, 0) - normal * 20.0f);
vertexDir.Normalize();
penumbraVertices[n + i + 1].Position = new Vector3(lightSourcePos, 0) + vertexDir * 9000;
// vertexDir = penumbraStart - (new Vector3(lightSourcePos, 0) - normal * 20.0f);
// vertexDir.Normalize();
// penumbraVertices[n + i + 1].Position = new Vector3(lightSourcePos, 0) + vertexDir * 9000;
if (los)
{
penumbraVertices[n + i + 1].TextureCoordinate = (i == 0) ? new Vector2(0.05f, 0.0f) : new Vector2(1.0f, 0.0f);
}
else
{
penumbraVertices[n + i + 1].TextureCoordinate = (i == 0) ? new Vector2(1.0f, 0.0f) : Vector2.Zero;
}
}
// if (los)
// {
// penumbraVertices[n + i + 1].TextureCoordinate = (i == 0) ? new Vector2(0.05f, 0.0f) : new Vector2(1.0f, 0.0f);
// }
// else
// {
// penumbraVertices[n + i + 1].TextureCoordinate = (i == 0) ? new Vector2(1.0f, 0.0f) : Vector2.Zero;
// }
// }
if (n > 0)
{
var temp = penumbraVertices[4];
penumbraVertices[4] = penumbraVertices[5];
penumbraVertices[5] = temp;
}
}
}
// if (n > 0)
// {
// var temp = penumbraVertices[4];
// penumbraVertices[4] = penumbraVertices[5];
// penumbraVertices[5] = temp;
// }
// }
//}
public void DrawShadows(GraphicsDevice graphicsDevice, Camera cam, LightSource light, Matrix transform, bool los = true)
{
@@ -335,7 +365,7 @@ namespace Barotrauma.Lights
}
else
{
cachedShadow = new CachedShadow(shadowVertices, penumbraVertices, lightSourcePos, shadowVertexCount, 0);
cachedShadow = new CachedShadow(shadowVertices, lightSourcePos, shadowVertexCount, 0);
RemoveCachedShadow(light);
cachedShadows.Add(light, cachedShadow);
}
@@ -386,15 +416,15 @@ namespace Barotrauma.Lights
}
if (los)
{
penumbraEffect.World = shadowEffect.World;
penumbraEffect.CurrentTechnique.Passes[0].Apply();
// if (los && false)
// {
// penumbraEffect.World = shadowEffect.World;
// penumbraEffect.CurrentTechnique.Passes[0].Apply();
#if WINDOWS
graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, penumbraVertices, 0, 2, VertexPositionTexture.VertexDeclaration);
#endif
}
//#if WINDOWS
// graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, 0, 2, VertexPositionTexture.VertexDeclaration);
//#endif
// }
}