experimenting with storing shadows in cube map
parent
3a89e9a091
commit
9f6a32b8a0
|
@ -32,7 +32,7 @@ namespace Kav
|
|||
private set { pointLightCollection = value; }
|
||||
}
|
||||
|
||||
public int MaxDirectionalLights { get; } = 4;
|
||||
public int MaxDirectionalLights { get; } = 6;
|
||||
|
||||
public DirectionalLightCollection DirectionalLights
|
||||
{
|
||||
|
|
|
@ -5,10 +5,10 @@ namespace Kav
|
|||
{
|
||||
public class DirectionalLightCollection
|
||||
{
|
||||
private readonly Vector3[] directions = new Vector3[4];
|
||||
private readonly Vector3[] colors = new Vector3[4];
|
||||
private readonly float[] intensities = new float[4];
|
||||
private readonly Matrix[] lightSpaceMatrices = new Matrix[4];
|
||||
private readonly Vector3[] directions = new Vector3[6];
|
||||
private readonly Vector3[] colors = new Vector3[6];
|
||||
private readonly float[] intensities = new float[6];
|
||||
private readonly Matrix[] lightSpaceMatrices = new Matrix[6];
|
||||
|
||||
readonly EffectParameter lightDirectionsParam;
|
||||
readonly EffectParameter lightColorsParam;
|
||||
|
|
Binary file not shown.
|
@ -2,7 +2,7 @@
|
|||
|
||||
static const float PI = 3.141592653589793;
|
||||
static const int MAX_POINT_LIGHTS = 64;
|
||||
static const int MAX_DIRECTIONAL_LIGHTS = 4;
|
||||
static const int MAX_DIRECTIONAL_LIGHTS = 6;
|
||||
|
||||
DECLARE_TEXTURE(gPosition, 0);
|
||||
DECLARE_TEXTURE(gAlbedo, 1);
|
||||
|
@ -18,11 +18,11 @@ BEGIN_CONSTANTS
|
|||
float3 PointLightColors[MAX_POINT_LIGHTS] _ps(c65) _cb(c65);
|
||||
|
||||
float3 DirectionalLightDirections[MAX_DIRECTIONAL_LIGHTS] _ps(c129) _cb(c129);
|
||||
float3 DirectionalLightColors[MAX_DIRECTIONAL_LIGHTS] _ps(c133) _cb(c133);
|
||||
float3 DirectionalLightColors[MAX_DIRECTIONAL_LIGHTS] _ps(c135) _cb(c135);
|
||||
|
||||
MATRIX_CONSTANTS
|
||||
|
||||
float4x4 DirectionalLightMatrices[MAX_DIRECTIONAL_LIGHTS] _ps(c137) _cb(c137);
|
||||
float4x4 DirectionalLightMatrices[MAX_DIRECTIONAL_LIGHTS] _ps(c141) _cb(c141);
|
||||
|
||||
END_CONSTANTS
|
||||
|
||||
|
@ -96,8 +96,8 @@ float ComputeShadow(float4 positionLightSpace, int directionalLightIndex)
|
|||
// maps to [-1, 1]
|
||||
float3 projectionCoords = positionLightSpace.xyz / positionLightSpace.w;
|
||||
|
||||
// map our UV sample coordinates to a cube map sample vector
|
||||
float3 cubeMapSampleVector;
|
||||
|
||||
if (directionalLightIndex == 0)
|
||||
{
|
||||
cubeMapSampleVector = float3(1.0f, projectionCoords.y, -projectionCoords.x);
|
||||
|
@ -126,7 +126,9 @@ float ComputeShadow(float4 positionLightSpace, int directionalLightIndex)
|
|||
float closestDepth = SAMPLE_CUBEMAP(shadowMap, cubeMapSampleVector).r;
|
||||
float currentDepth = projectionCoords.z;
|
||||
|
||||
float shadow = currentDepth - bias > closestDepth ? 1.0 : 0.0;
|
||||
if (projectionCoords.z > 1.0) { return 0.0; }
|
||||
|
||||
float shadow = currentDepth > closestDepth ? 1.0 : 0.0;
|
||||
|
||||
return shadow;
|
||||
}
|
||||
|
@ -189,7 +191,7 @@ float4 ComputeColor(
|
|||
}
|
||||
|
||||
// directional light
|
||||
for (int i = 0; i < 1; i++)
|
||||
for (int i = 0; i < MAX_DIRECTIONAL_LIGHTS; i++)
|
||||
{
|
||||
float4 positionLightSpace = mul(float4(worldPosition, 1.0), DirectionalLightMatrices[i]);
|
||||
float shadow = ComputeShadow(positionLightSpace, i);
|
||||
|
|
|
@ -4,15 +4,15 @@ namespace Kav
|
|||
{
|
||||
public struct DirectionalLight
|
||||
{
|
||||
public Vector3 Direction { get; set; }
|
||||
public Color Color { get; set; }
|
||||
public float Intensity { get; set; }
|
||||
public Vector3 Direction { get; }
|
||||
public Color Color { get; }
|
||||
public float Intensity { get; }
|
||||
|
||||
public Matrix View
|
||||
{
|
||||
get
|
||||
{
|
||||
return Matrix.CreateLookAt(Direction * 4.5f, Vector3.Zero, Vector3.Up);
|
||||
return Matrix.CreateLookAt((Direction * 5f), Vector3.Zero, Vector3.Up);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue