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
+87 -6
View File
@@ -4,6 +4,7 @@ using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Barotrauma.Lights;
using System.Diagnostics;
using Microsoft.Xna.Framework.Content;
namespace Barotrauma
{
@@ -15,6 +16,8 @@ namespace Barotrauma
readonly RenderTarget2D renderTargetWater;
readonly RenderTarget2D renderTargetAir;
Effect blurEffect;
public BackgroundCreatureManager BackgroundCreatureManager;
public Camera Cam
@@ -22,7 +25,7 @@ namespace Barotrauma
get { return cam; }
}
public GameScreen(GraphicsDevice graphics)
public GameScreen(GraphicsDevice graphics, ContentManager content)
{
cam = new Camera();
cam.Translate(new Vector2(-10.0f, 50.0f));
@@ -32,6 +35,9 @@ namespace Barotrauma
renderTargetAir = new RenderTarget2D(graphics, GameMain.GraphicsWidth, GameMain.GraphicsHeight);
BackgroundCreatureManager = new BackgroundCreatureManager("Content/BackgroundSprites/BackgroundCreaturePrefabs.xml");
blurEffect = content.Load<Effect>("blurshader");
SetBlurEffectParameters(0.001f, 0.001f);
}
public override void Select()
@@ -47,7 +53,6 @@ namespace Barotrauma
cam.Position = Submarine.Loaded.WorldPosition;
}
foreach (MapEntity entity in MapEntity.mapEntityList)
entity.IsHighlighted = false;
}
@@ -200,7 +205,6 @@ namespace Barotrauma
Level.Loaded.DrawBack(graphics, spriteBatch, cam, BackgroundCreatureManager);
}
spriteBatch.Begin(SpriteSortMode.BackToFront,
BlendState.AlphaBlend,
null, null, null, null,
@@ -212,8 +216,6 @@ namespace Barotrauma
spriteBatch.End();
GameMain.LightManager.DrawLightMap(spriteBatch, cam);
//----------------------------------------------------------------------------------------
//draw the rendertarget and particles that are only supposed to be drawn in water into renderTargetWater
graphics.SetRenderTarget(renderTargetWater);
@@ -304,12 +306,18 @@ namespace Barotrauma
foreach (Character c in Character.CharacterList) c.DrawFront(spriteBatch);
Submarine.DrawFront(spriteBatch);
if (Level.Loaded!=null) Level.Loaded.DrawFront(spriteBatch);
spriteBatch.End();
GameMain.LightManager.DrawLOS(graphics, spriteBatch, cam);
GameMain.LightManager.DrawLightMap(spriteBatch, cam, blurEffect);
GameMain.LightManager.DrawLOS(graphics, spriteBatch, cam, blurEffect);
}
private void DrawSubmarineIndicator(SpriteBatch spriteBatch, Submarine submarine)
@@ -331,5 +339,78 @@ namespace Barotrauma
GUI.Arrow.Draw(spriteBatch, iconPos + arrowOffset, Color.LightBlue * 0.5f, MathUtils.VectorToAngle(arrowOffset) + MathHelper.PiOver2);
}
}
/// <summary>
/// Computes sample weightings and texture coordinate offsets
/// for one pass of a separable gaussian blur filter.
/// </summary>
void SetBlurEffectParameters(float dx, float dy)
{
EffectParameter weightsParameter = blurEffect.Parameters["SampleWeights"];
EffectParameter offsetsParameter = blurEffect.Parameters["SampleOffsets"];
// Look up how many samples our gaussian blur effect supports.
int sampleCount = weightsParameter.Elements.Count;
// Create temporary arrays for computing our filter settings.
float[] sampleWeights = new float[sampleCount];
Vector2[] sampleOffsets = new Vector2[sampleCount];
sampleWeights[0] = ComputeGaussian(0);
sampleOffsets[0] = new Vector2(0);
float totalWeights = sampleWeights[0];
// Add pairs of additional sample taps, positioned
// along a line in both directions from the center.
for (int i = 0; i < sampleCount / 2; i++)
{
// Store weights for the positive and negative taps.
float weight = ComputeGaussian(i + 1);
sampleWeights[i * 2 + 1] = weight;
sampleWeights[i * 2 + 2] = weight;
totalWeights += weight * 2;
// To get the maximum amount of blurring from a limited number of
// pixel shader samples, we take advantage of the bilinear filtering
// hardware inside the texture fetch unit. If we position our texture
// coordinates exactly halfway between two texels, the filtering unit
// will average them for us, giving two samples for the price of one.
// This allows us to step in units of two texels per sample, rather
// than just one at a time. The 1.5 offset kicks things off by
// positioning us nicely in between two texels.
float sampleOffset = i * 2 + 1.5f;
Vector2 delta = new Vector2(dx, dy) * sampleOffset;
// Store texture coordinate offsets for the positive and negative taps.
sampleOffsets[i * 2 + 1] = delta;
sampleOffsets[i * 2 + 2] = -delta;
}
// Normalize the list of sample weightings, so they will always sum to one.
for (int i = 0; i < sampleWeights.Length; i++)
{
sampleWeights[i] /= totalWeights;
}
weightsParameter.SetValue(sampleWeights);
offsetsParameter.SetValue(sampleOffsets);
}
/// <summary>
/// Evaluates a single point on the gaussian falloff curve.
/// Used for setting up the blur filter weightings.
/// </summary>
float ComputeGaussian(float n)
{
float theta = 4.0f;
return (float)((1.0 / Math.Sqrt(2 * Math.PI * theta)) *
Math.Exp(-(n * n) / (2 * theta * theta)));
}
}
}