Yay, dynamic shadows!

This commit is contained in:
Simon Holmberg
2012-09-30 00:05:35 +02:00
parent a056127f7c
commit 7bcbe94e5f
7 changed files with 596 additions and 30 deletions
+3
View File
@@ -42,6 +42,9 @@ namespace DepthsBelow
public void Update(GameTime gameTime)
{
if (!core.IsActive)
return;
float elapsed = gameTime.ElapsedGameTime.Milliseconds / 1000.0f;
MouseState ms = Mouse.GetState();
+62 -15
View File
@@ -19,6 +19,8 @@ namespace DepthsBelow
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public Krypton.KryptonEngine KryptonEngine;
public Camera Camera;
public static MouseInput MouseInput;
@@ -39,6 +41,8 @@ namespace DepthsBelow
graphics.ApplyChanges();
this.IsMouseVisible = true;
KryptonEngine = new Krypton.KryptonEngine(this, "KryptonEffect");
}
/// <summary>
@@ -50,6 +54,13 @@ namespace DepthsBelow
protected override void Initialize()
{
// TODO: Add your initialization logic here
// Initialize lighting engine
this.KryptonEngine.Initialize();
this.KryptonEngine.SpriteBatchCompatablityEnabled = true;
this.KryptonEngine.CullMode = CullMode.CullClockwiseFace;
this.KryptonEngine.AmbientColor = new Color(35, 35, 35);
Camera = new Camera(this);
Squad = new List<Soldier>();
@@ -67,7 +78,22 @@ namespace DepthsBelow
Map = Content.Load<Map>("maps/Cave.Level1");
// Load map objects
Map.ParseObjects(this);
Map.Initialize(this, KryptonEngine);
// DEBUG: Test lights
var light = new Krypton.Lights.Light2D()
{
Texture = Krypton.LightTextureBuilder.CreatePointLight(this.GraphicsDevice, 512),
Range = (float)(200),
Color = Color.White,
Intensity = 1f,
Angle = MathHelper.TwoPi,
X = 17 * Grid.TileSize,
Y = 7 * Grid.TileSize,
Fov = MathHelper.TwoPi,
IsOn = true
};
this.KryptonEngine.Lights.Add(light);
// TODO: use this.Content to load your game content here
Soldier.LoadContent(this);
@@ -103,6 +129,9 @@ namespace DepthsBelow
foreach (var soldier in Squad)
soldier.Update(gameTime);
foreach (Krypton.Lights.Light2D light in KryptonEngine.Lights)
light.Position = Camera.ScreenToWorld(new Vector2(Mouse.GetState().X, Mouse.GetState().Y));
base.Update(gameTime);
}
@@ -112,25 +141,43 @@ namespace DepthsBelow
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
// Assign the matrix and pre-render the lightmap.
this.KryptonEngine.Matrix = Camera.Transform;
this.KryptonEngine.Bluriness = 1;
this.KryptonEngine.LightMapPrepare();
GraphicsDevice.Clear(Color.Black);
// Start drawing using the Camera transform
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null, null,
Camera.Transform);
/*
* Draw game world
*/
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null, null, Camera.Transform);
// Draw the level
Map.Draw(spriteBatch);
// Draw the level
Map.Draw(spriteBatch);
// Draw units
foreach (var soldier in Squad)
{
var sr = soldier.GetComponent<Component.SpriteRenderer>();
if (sr != null)
sr.Draw(spriteBatch);
}
// Draw units
foreach (var soldier in Squad)
{
var sr = soldier.GetComponent<Component.SpriteRenderer>();
if (sr != null)
sr.Draw(spriteBatch);
}
// Draw mouse input visuals
MouseInput.Draw(spriteBatch);
spriteBatch.End();
/*
* Draw Krypton lighting
*/
this.KryptonEngine.Draw(gameTime);
/*
* Draw HUD elements
*/
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null, null, Camera.Transform);
// Draw mouse input visuals
MouseInput.Draw(spriteBatch);
spriteBatch.End();
@@ -146,6 +146,10 @@
<Project>{CD3F949D-54AA-4D38-99DB-92905A375D84}</Project>
<Name>AStar</Name>
</ProjectReference>
<ProjectReference Include="..\..\Krypton v2.0\Krypton\Krypton.csproj">
<Project>{B1BFCB2A-C001-4DAC-BA7D-B68BF28757F3}</Project>
<Name>Krypton</Name>
</ProjectReference>
<ProjectReference Include="..\DepthsBelowContent\DepthsBelowContent.contentproj">
<Name>DepthsBelowContent %28Content%29</Name>
<XnaReferenceType>Content</XnaReferenceType>
+38 -15
View File
@@ -59,27 +59,50 @@ namespace DepthsBelow
}
public void ParseObjects(Core core)
public void Initialize(Core core, Krypton.KryptonEngine kryptonEngine)
{
foreach (var layer in Layers)
{
var objectLayer = layer as MapObjectLayer;
if (objectLayer == null)
continue;
foreach (var mapObject in objectLayer.Objects)
if (objectLayer != null)
{
if (mapObject.Type == "SquadStart")
// Parse objects
foreach (var mapObject in objectLayer.Objects)
{
var soldier = new Soldier(core);
// HACK: For some reason, the tile object coordinates are offset by one tile on the Y-axis in the Tiled map file (https://github.com/bjorn/tiled/issues/91)
var mapObjectPos = new Vector2(mapObject.Bounds.X, mapObject.Bounds.Y - Grid.TileSize);
var mapObjectGridPos = Grid.WorldToGrid(mapObjectPos);
soldier.X = mapObjectGridPos.X;
soldier.Y = mapObjectGridPos.Y;
core.Squad.Add(soldier);
if (mapObject.Type == "SquadStart")
{
var soldier = new Soldier(core);
// HACK: For some reason, the tile object coordinates are offset by one tile on the Y-axis in the Tiled map file (https://github.com/bjorn/tiled/issues/91)
var mapObjectPos = new Vector2(mapObject.Bounds.X, mapObject.Bounds.Y - Grid.TileSize);
var mapObjectGridPos = Grid.WorldToGrid(mapObjectPos);
soldier.X = mapObjectGridPos.X;
soldier.Y = mapObjectGridPos.Y;
core.Squad.Add(soldier);
}
}
}
var tileLayer = layer as MapTileLayer;
if (tileLayer != null)
{
// Prepare collision hulls for lighting engine
if (tileLayer.Name == "Collision")
{
for (int y = 0; y < tileLayer.Height; y++)
{
for (int x = 0; x < tileLayer.Width; x++)
{
MapTile mapTile = tileLayer.Tiles[y * layer.Width + x];
if (mapTile != null && mapTile.LocalId != 0)
{
var hull = Krypton.ShadowHull.CreateRectangle(new Vector2(TileWidth, TileHeight));
hull.Position.X = x * TileWidth + TileWidth/2;
hull.Position.Y = y * TileHeight + TileWidth / 2;
kryptonEngine.Hulls.Add(hull);
}
}
}
}
}
}
+4
View File
@@ -38,6 +38,10 @@ namespace DepthsBelow
public void Update(GameTime gameTime)
{
// Only capture input if the game window is in focus
if (!core.IsActive)
return;
MouseState ms = Mouse.GetState();
Vector2 mouseWorldPos = core.Camera.ScreenToWorld(new Vector2(ms.X, ms.Y));
KeyboardState ks = Keyboard.GetState();
@@ -99,6 +99,13 @@
<Processor>TextureProcessor</Processor>
</Compile>
</ItemGroup>
<ItemGroup>
<Compile Include="KryptonEffect.fx">
<Name>KryptonEffect</Name>
<Importer>EffectImporter</Importer>
<Processor>EffectProcessor</Processor>
</Compile>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\XNA Game Studio\$(XnaFrameworkVersion)\Microsoft.Xna.GameStudio.ContentPipeline.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
+478
View File
@@ -0,0 +1,478 @@
// ------------------------------------------------------------------------------------------------ //
// ----- Copyright 2011 Christopher Harris --------------------- http://krypton.codeplex.com/ ----- //
// ----------------------------------------------------------------- mailto:xixonia@gmail.com ----- //
// ------------------------------------------------------------------------------------------------ //
// ------------------------------------------------------------------------------------------------ //
// ----- Parameters ------------------------------------------------------------------------------- //
float4x4 Matrix;
texture Texture0;
texture Texture1;
float2 LightPosition;
float LightIntensityFactor = 1;
float ShadowStrech = 1000000;
float2 TexelBias;
float4 AmbientColor;
float Bluriness = 0.5f;
float BlurFactorU = 0;
float BlurFactorV = 0;
// ------------------------------------------------------------------------------------------------ //
// ----- Samplers --------------------------------------------------------------------------------- //
sampler2D tex0 = sampler_state
{
Texture = <Texture0>;
AddressU = Clamp;
AddressV = Clamp;
};
sampler2D tex1 = sampler_state
{
Texture = <Texture1>;
AddressU = Clamp;
AddressV = Clamp;
};
// ------------------------------------------------------------------------------------------------ //
// ----- Structures ------------------------------------------------------------------------------- //
struct ShadowHullVertex
{
float4 Position : POSITION0;
float2 Normal : NORMAL0;
float4 Color : COLOR0;
};
struct VertexPositionColor
{
float4 Position : POSITION0;
float4 Color : COLOR0;
};
struct VertexPositionNormalTexture
{
float4 Position : POSITION0;
float4 Normal : NORMAL0;
float2 TexCoord : TEXCOORD0;
};
struct VertexPositionColorTexture
{
float4 Position : POSITION0;
float4 Color : COLOR0;
float2 TexCoord : TEXCOORD0;
};
struct VertexPositionTexture
{
float4 Position : POSITION0;
float2 TexCoord : TEXCOORD0;
};
// ------------------------------------------------------------------------------------------------ //
// ----- Techniques ------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------------------------
// ----- Technique: TextureToTarget ---------------------------------------------------------------
VertexPositionTexture VS_ScreenCopy(VertexPositionTexture input)
{
VertexPositionTexture output;
output.Position = input.Position;
output.TexCoord = input.TexCoord;
return input;
};
float4 PS_ScreenCopy(VertexPositionTexture input) : COLOR0
{
return tex2D(tex0, input.TexCoord + TexelBias);
};
technique TextureToTarget_Add
{
pass Pass1
{
StencilEnable = False;
BlendOp = Add;
SrcBlend = One;
DestBlend = One;
AlphaBlendEnable = True;
CullMode = CCW;
VertexShader = compile vs_2_0 VS_ScreenCopy();
PixelShader = compile ps_2_0 PS_ScreenCopy();
}
};
technique TextureToTarget_Multiply
{
pass Pass1
{
StencilEnable = False;
BlendOp = Add;
SrcBlend = Zero;
DestBlend = SrcColor;
AlphaBlendEnable = True;
CullMode = CCW;
VertexShader = compile vs_2_0 VS_ScreenCopy();
PixelShader = compile ps_2_0 PS_ScreenCopy();
}
};
float4 PS_ClearTarget() : COLOR0
{
return float4(0, 0, 0, 1);
}
// ------------------------------------------------------------------------------------------------
// ----- Technique: SimpleTexture -----------------------------------------------------------------
VertexPositionColorTexture VS_SimpleTexture(VertexPositionColorTexture input)
{
VertexPositionColorTexture output;
output.Position = mul(input.Position, Matrix);
output.Color = input.Color;
output.TexCoord = input.TexCoord;
return output;
};
float4 PS_SimpleTexture(VertexPositionColorTexture input) : COLOR0
{
return tex2D(tex0, input.TexCoord) * input.Color;
};
float4 PS_LightTexture(VertexPositionColorTexture input) : COLOR0
{
return pow(tex2D(tex0, input.TexCoord) * input.Color, LightIntensityFactor);
};
technique SimpleTexture
{
pass Pass1
{
StencilEnable = False;
VertexShader = compile vs_2_0 VS_SimpleTexture();
PixelShader = compile ps_2_0 PS_SimpleTexture();
}
};
technique LightTexture
{
pass Pass1
{
StencilEnable = False;
BlendOp = Add;
DestBlend = Zero;
SrcBlend = SrcAlpha;
AlphaBlendEnable = True;
VertexShader = compile vs_2_0 VS_SimpleTexture();
PixelShader = compile ps_2_0 PS_LightTexture();
}
};
// ------------------------------------------------------------------------------------------------
// ----- Technique: PointLight_Shadow -------------------------------------------------------------
VertexPositionColor VS_PointLight_Shadow(ShadowHullVertex input)
{
float2 direction = normalize(LightPosition.xy - input.Position.xy);
if(dot(input.Normal.xy, direction) < 0)
{
// Stretch backfacing vertices
input.Position.xy -= direction * ShadowStrech;
}
VertexPositionColor output;
output.Position = mul(input.Position, Matrix);
output.Color = input.Color;
return output;
};
float4 PS_PointLight_Shadow(float4 input : COLOR0) : COLOR0
{
return input;
};
VertexPositionColor VS_Shadow_HullIllumination(ShadowHullVertex input)
{
VertexPositionColor output;
output.Position = mul(input.Position, Matrix);
output.Color = input.Color;
return output;
};
float4 PS_Shadow_HullIllumination() : COLOR0
{
return float4(1, 1, 1, 1);
};
technique PointLight_Shadow
{
pass Shadow
{
StencilEnable = False;
AlphaBlendEnable = True;
BlendOp = RevSubtract;
SrcBlend = One;
DestBlend = One;
VertexShader = compile vs_2_0 VS_PointLight_Shadow();
PixelShader = compile ps_2_0 PS_PointLight_Shadow();
}
};
// ------------------------------------------------------------------------------------------------
// ----- Technique: PointLight_ShadowWithIllumination ---------------------------------------------
technique PointLight_ShadowWithIllumination
{
pass Shadow_Illumination
{
// This outlines where our hulls are currently, so we don't draw shadows there
StencilEnable = True;
StencilFunc = Never;
StencilFail = Incr;
AlphaBlendEnable = False;
VertexShader = compile vs_2_0 VS_Shadow_HullIllumination();
PixelShader = compile ps_2_0 PS_Shadow_HullIllumination();
}
pass Shadow
{
// Only draw where the Stencil hasn't touched
StencilEnable = True;
StencilFunc = Equal;
StencilRef = 0;
StencilFail = Incr;
AlphaBlendEnable = True;
BlendOp = RevSubtract;
SrcBlend = One;
DestBlend = One;
VertexShader = compile vs_2_0 VS_PointLight_Shadow();
PixelShader = compile ps_2_0 PS_PointLight_Shadow();
}
};
// ------------------------------------------------------------------------------------------------
// ----- Technique: PointLight_ShadowWithOcclusion ------------------------------------------------
technique PointLight_ShadowWithOcclusion
{
pass Shadow_HullStencil
{
// This outlines where our hulls are currently, so we don't draw shadows there, unless the hull is occluded
StencilEnable = True;
StencilFunc = Never;
StencilFail = Incr;
VertexShader = compile vs_2_0 VS_Shadow_HullIllumination();
PixelShader = compile ps_2_0 PS_Shadow_HullIllumination();
}
pass Shadow
{
// This allows us to draw shadows on hulls behind other hulls
StencilEnable = True;
StencilFunc = NotEqual;
StencilRef = 1;
StencilPass = Keep;
StencilFail = Incr;
AlphaBlendEnable = True;
BlendOp = RevSubtract;
SrcBlend = One;
DestBlend = One;
VertexShader = compile vs_2_0 VS_PointLight_Shadow();
PixelShader = compile ps_2_0 PS_PointLight_Shadow();
}
};
// ------------------------------------------------------------------------------------------------
// ----- Technique: DebugDraw ---------------------------------------------------------------------
technique DebugDraw
{
pass Solid
{
StencilEnable = False;
AlphaBlendEnable = False;
VertexShader = compile vs_2_0 VS_Shadow_HullIllumination();
PixelShader = compile ps_2_0 PS_Shadow_HullIllumination();
}
};
// ------------------------------------------------------------------------------------------------
// ----- Technique: Blur --------------------------------------------------------------------------
float4 PS_BlurH(in float2 texCoord : TEXCOORD0) : COLOR0
{
float blurFactor = Bluriness * BlurFactorU / 4.0f;
return
tex2D(tex0, float2(texCoord.x - blurFactor * 4, texCoord.y) + TexelBias) * 0.05f +
tex2D(tex0, float2(texCoord.x - blurFactor * 3, texCoord.y) + TexelBias) * 0.09f +
tex2D(tex0, float2(texCoord.x - blurFactor * 2, texCoord.y) + TexelBias) * 0.12f +
tex2D(tex0, float2(texCoord.x - blurFactor, texCoord.y) + TexelBias) * 0.15f +
tex2D(tex0, float2(texCoord.x, texCoord.y) + TexelBias) * 0.18f +
tex2D(tex0, float2(texCoord.x + blurFactor, texCoord.y) + TexelBias) * 0.15f +
tex2D(tex0, float2(texCoord.x + blurFactor * 2, texCoord.y) + TexelBias) * 0.12f +
tex2D(tex0, float2(texCoord.x + blurFactor * 3, texCoord.y) + TexelBias) * 0.09f +
tex2D(tex0, float2(texCoord.x + blurFactor * 4, texCoord.y) + TexelBias) * 0.05f;
}
float4 PS_BlurV(in float2 texCoord : TEXCOORD0) : COLOR0
{
float blurFactor = Bluriness * BlurFactorV / 4.0f;
return
tex2D(tex0, float2(texCoord.x, texCoord.y - blurFactor * 4) + TexelBias) * 0.05f +
tex2D(tex0, float2(texCoord.x, texCoord.y - blurFactor * 3) + TexelBias) * 0.09f +
tex2D(tex0, float2(texCoord.x, texCoord.y - blurFactor * 2) + TexelBias) * 0.12f +
tex2D(tex0, float2(texCoord.x, texCoord.y - blurFactor) + TexelBias) * 0.15f +
tex2D(tex0, float2(texCoord.x, texCoord.y) + TexelBias) * 0.18f +
tex2D(tex0, float2(texCoord.x, texCoord.y + blurFactor) + TexelBias) * 0.15f +
tex2D(tex0, float2(texCoord.x, texCoord.y + blurFactor * 2) + TexelBias) * 0.12f +
tex2D(tex0, float2(texCoord.x, texCoord.y + blurFactor * 3) + TexelBias) * 0.09f +
tex2D(tex0, float2(texCoord.x, texCoord.y + blurFactor * 4) + TexelBias) * 0.05f;
}
technique Blur
{
pass HorizontalBlur
{
ScissorTestEnable = False;
StencilEnable = False;
AlphaBlendEnable = False;
CullMode = CCW;
VertexShader = compile vs_2_0 VS_ScreenCopy();
PixelShader = compile ps_2_0 PS_BlurH();
}
pass VerticalBlur
{
ScissorTestEnable = False;
StencilEnable = False;
AlphaBlendEnable = False;
CullMode = CCW;
VertexShader = compile vs_2_0 VS_ScreenCopy();
PixelShader = compile ps_2_0 PS_BlurV();
}
}
technique PointLight_Shadow_Fast
{
pass ShadowStencil
{
StencilEnable = True;
StencilFunc = Always;
StencilPass = Incr;
ScissorTestEnable = True;
AlphaBlendEnable = True;
BlendOp = Add;
SrcBlend = DestAlpha;
DestBlend = Zero;
ColorWriteEnable = Alpha;
VertexShader = compile vs_2_0 VS_PointLight_Shadow();
PixelShader = compile ps_2_0 PS_PointLight_Shadow();
}
pass Light
{
StencilEnable = True;
StencilFunc = Equal;
StencilRef = 1;
StencilPass = Keep;
ScissorTestEnable = True;
AlphaBlendEnable = True;
BlendOp = Add;
SrcBlend = One;
DestBlend = One;
ColorWriteEnable = Red | Green | Blue;
VertexShader = compile vs_2_0 VS_SimpleTexture();
PixelShader = compile ps_2_0 PS_LightTexture();
}
pass LightAlpha
{
StencilEnable = True;
StencilFunc = NotEqual;
StencilRef = 1;
StencilPass = Keep;
ScissorTestEnable = True;
AlphaBlendEnable = True;
BlendOp = Add;
SrcBlend = DestAlpha;
DestBlend = One;
VertexShader = compile vs_2_0 VS_SimpleTexture();
PixelShader = compile ps_2_0 PS_LightTexture();
}
pass ClearAlpha
{
StencilEnable = False;
AlphaBlendEnable = False;
CullMode = CCW;
ColorWriteEnable = Alpha;
VertexShader = compile vs_2_0 VS_ScreenCopy();
PixelShader = compile ps_2_0 PS_ClearTarget();
}
};