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

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
// }
}

View File

@@ -69,30 +69,42 @@ namespace Barotrauma.Lights
lights.Remove(light);
}
public void DrawLOS(GraphicsDevice graphics, SpriteBatch spriteBatch, Camera cam)
public void DrawLOS(GraphicsDevice graphics, SpriteBatch spriteBatch, Camera cam, Effect effect)
{
if (!LosEnabled || ViewTarget==null) return;
//if (!LosEnabled || ViewTarget==null) return;
Vector2 pos = ViewTarget.WorldPosition;
//Vector2 pos = ViewTarget.WorldPosition;
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);
Matrix shadowTransform = cam.ShaderTransform
* Matrix.CreateOrthographic(GameMain.GraphicsWidth, GameMain.GraphicsHeight, -1, 1) * 0.5f;
//Matrix shadowTransform = cam.ShaderTransform
// * Matrix.CreateOrthographic(GameMain.GraphicsWidth, GameMain.GraphicsHeight, -1, 1) * 0.5f;
foreach (ConvexHull convexHull in ConvexHull.list)
{
if (!convexHull.Intersects(camView)) continue;
//if (!camView.Intersects(convexHull.BoundingBox)) continue;
//graphics.SetRenderTarget(losTexture);
//graphics.Clear(Color.Transparent);
convexHull.DrawShadows(graphics, cam, pos, shadowTransform);
}
//foreach (ConvexHull convexHull in ConvexHull.list)
//{
// if (!convexHull.Intersects(camView)) continue;
// //if (!camView.Intersects(convexHull.BoundingBox)) continue;
// convexHull.DrawShadows(graphics, cam, pos, shadowTransform);
//}
//graphics.SetRenderTarget(null);
if (!ObstructVision) return;
//if (!ObstructVision) return;
spriteBatch.Begin(SpriteSortMode.Deferred, CustomBlendStates.Multiplicative);
spriteBatch.Draw(losTexture, Vector2.Zero);
spriteBatch.End();
//spriteBatch.Begin(SpriteSortMode.Deferred, CustomBlendStates.Multiplicative);
//spriteBatch.Draw(losTexture, Vector2.Zero);
//spriteBatch.End();
spriteBatch.Begin(SpriteSortMode.Deferred, ObstructVision ? CustomBlendStates.Multiplicative : BlendState.AlphaBlend, null, null, null, effect);
//effect.CurrentTechnique.Passes[0].Apply();
spriteBatch.Draw(losTexture, Vector2.Zero, Color.White);
spriteBatch.End();
ObstructVision = false;
@@ -148,9 +160,14 @@ namespace Barotrauma.Lights
spriteBatch.End();
}
ClearAlphaToOne(graphics, spriteBatch);
spriteBatch.Begin(SpriteSortMode.Deferred, CustomBlendStates.MultiplyWithAlpha, null, null, null, null, cam.Transform);
GameMain.ParticleManager.Draw(spriteBatch, false, Particles.ParticleBlendState.Additive);
foreach (LightSource light in lights)
{
if (light.hullsInRange.Any() || light.Color.A < 0.01f) continue;
@@ -163,37 +180,59 @@ namespace Barotrauma.Lights
//clear alpha, to avoid messing stuff up later
ClearAlphaToOne(graphics, spriteBatch);
graphics.SetRenderTarget(null);
}
public void UpdateObstructVision(GraphicsDevice graphics, SpriteBatch spriteBatch, Camera cam, Vector2 lookAtPosition)
{
if (!LosEnabled && !ObstructVision) return;
if (!ObstructVision) return;
graphics.SetRenderTarget(losTexture);
graphics.Clear(Color.Black);
spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, cam.Transform);
if (ObstructVision)
{
graphics.Clear(Color.Black);
Vector2 diff = lookAtPosition - ViewTarget.WorldPosition;
diff.Y = -diff.Y;
float rotation = MathUtils.VectorToAngle(diff);
spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, cam.Transform);
Vector2 diff = lookAtPosition - ViewTarget.WorldPosition;
diff.Y = -diff.Y;
float rotation = MathUtils.VectorToAngle(diff);
Vector2 scale = new Vector2(MathHelper.Clamp(diff.Length()/256.0f, 2.0f, 5.0f), 2.0f);
Vector2 scale = new Vector2(MathHelper.Clamp(diff.Length()/256.0f, 2.0f, 5.0f), 2.0f);
spriteBatch.Draw(visionCircle, new Vector2(ViewTarget.WorldPosition.X, -ViewTarget.WorldPosition.Y), null, Color.White, rotation,
new Vector2(LightSource.LightTexture.Width*0.2f, LightSource.LightTexture.Height/2), scale, SpriteEffects.None, 0.0f);
spriteBatch.End();
spriteBatch.Draw(visionCircle, new Vector2(ViewTarget.WorldPosition.X, -ViewTarget.WorldPosition.Y), null, Color.White, rotation,
new Vector2(LightSource.LightTexture.Width*0.2f, LightSource.LightTexture.Height/2), scale, SpriteEffects.None, 0.0f);
spriteBatch.End();
//ClearAlphaToOne(graphics, spriteBatch);
}
else
{
graphics.Clear(Color.Transparent);
}
graphics.SetRenderTarget(null);
//--------------------------------------
//spriteBatch.Begin();
//spriteBatch.Draw(lightMap, Vector2.Zero, Color.White);
//spriteBatch.End();
if (LosEnabled && ViewTarget != null)
{
Vector2 pos = ViewTarget.WorldPosition;
Rectangle camView = new Rectangle(cam.WorldView.X, cam.WorldView.Y - cam.WorldView.Height, cam.WorldView.Width, cam.WorldView.Height);
Matrix shadowTransform = cam.ShaderTransform
* Matrix.CreateOrthographic(GameMain.GraphicsWidth, GameMain.GraphicsHeight, -1, 1) * 0.5f;
foreach (ConvexHull convexHull in ConvexHull.list)
{
if (!convexHull.Intersects(camView)) continue;
//if (!camView.Intersects(convexHull.BoundingBox)) continue;
convexHull.DrawShadows(graphics, cam, pos, shadowTransform);
}
}
graphics.SetRenderTarget(null);
}
private void ClearAlphaToOne(GraphicsDevice graphics, SpriteBatch spriteBatch)
@@ -203,16 +242,18 @@ namespace Barotrauma.Lights
spriteBatch.End();
}
public void DrawLightMap(SpriteBatch spriteBatch, Camera cam)
public void DrawLightMap(SpriteBatch spriteBatch, Camera cam, Effect effect)
{
if (!LightingEnabled) return;
//multiply scene with lightmap
spriteBatch.Begin(SpriteSortMode.Deferred, CustomBlendStates.Multiplicative);
spriteBatch.Draw(lightMap, Vector2.Zero, Color.White);
spriteBatch.End();
spriteBatch.Begin(SpriteSortMode.Deferred, CustomBlendStates.Multiplicative, null, null, null, effect);
//effect.CurrentTechnique.Passes[0].Apply();
spriteBatch.Draw(lightMap, Vector2.Zero, Color.White);
spriteBatch.End();
}
public void ClearLights()
{
lights.Clear();