experimenting with storing shadows in cube map

tight_frustum
cosmonaut 2020-08-28 01:12:17 -07:00
parent 3a89e9a091
commit 9f6a32b8a0
5 changed files with 17 additions and 15 deletions

View File

@ -32,7 +32,7 @@ namespace Kav
private set { pointLightCollection = value; } private set { pointLightCollection = value; }
} }
public int MaxDirectionalLights { get; } = 4; public int MaxDirectionalLights { get; } = 6;
public DirectionalLightCollection DirectionalLights public DirectionalLightCollection DirectionalLights
{ {

View File

@ -5,10 +5,10 @@ namespace Kav
{ {
public class DirectionalLightCollection public class DirectionalLightCollection
{ {
private readonly Vector3[] directions = new Vector3[4]; private readonly Vector3[] directions = new Vector3[6];
private readonly Vector3[] colors = new Vector3[4]; private readonly Vector3[] colors = new Vector3[6];
private readonly float[] intensities = new float[4]; private readonly float[] intensities = new float[6];
private readonly Matrix[] lightSpaceMatrices = new Matrix[4]; private readonly Matrix[] lightSpaceMatrices = new Matrix[6];
readonly EffectParameter lightDirectionsParam; readonly EffectParameter lightDirectionsParam;
readonly EffectParameter lightColorsParam; readonly EffectParameter lightColorsParam;

Binary file not shown.

View File

@ -2,7 +2,7 @@
static const float PI = 3.141592653589793; static const float PI = 3.141592653589793;
static const int MAX_POINT_LIGHTS = 64; 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(gPosition, 0);
DECLARE_TEXTURE(gAlbedo, 1); DECLARE_TEXTURE(gAlbedo, 1);
@ -18,11 +18,11 @@ BEGIN_CONSTANTS
float3 PointLightColors[MAX_POINT_LIGHTS] _ps(c65) _cb(c65); float3 PointLightColors[MAX_POINT_LIGHTS] _ps(c65) _cb(c65);
float3 DirectionalLightDirections[MAX_DIRECTIONAL_LIGHTS] _ps(c129) _cb(c129); 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 MATRIX_CONSTANTS
float4x4 DirectionalLightMatrices[MAX_DIRECTIONAL_LIGHTS] _ps(c137) _cb(c137); float4x4 DirectionalLightMatrices[MAX_DIRECTIONAL_LIGHTS] _ps(c141) _cb(c141);
END_CONSTANTS END_CONSTANTS
@ -96,8 +96,8 @@ float ComputeShadow(float4 positionLightSpace, int directionalLightIndex)
// maps to [-1, 1] // maps to [-1, 1]
float3 projectionCoords = positionLightSpace.xyz / positionLightSpace.w; float3 projectionCoords = positionLightSpace.xyz / positionLightSpace.w;
// map our UV sample coordinates to a cube map sample vector
float3 cubeMapSampleVector; float3 cubeMapSampleVector;
if (directionalLightIndex == 0) if (directionalLightIndex == 0)
{ {
cubeMapSampleVector = float3(1.0f, projectionCoords.y, -projectionCoords.x); 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 closestDepth = SAMPLE_CUBEMAP(shadowMap, cubeMapSampleVector).r;
float currentDepth = projectionCoords.z; 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; return shadow;
} }
@ -189,7 +191,7 @@ float4 ComputeColor(
} }
// directional light // 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]); float4 positionLightSpace = mul(float4(worldPosition, 1.0), DirectionalLightMatrices[i]);
float shadow = ComputeShadow(positionLightSpace, i); float shadow = ComputeShadow(positionLightSpace, i);

View File

@ -4,15 +4,15 @@ namespace Kav
{ {
public struct DirectionalLight public struct DirectionalLight
{ {
public Vector3 Direction { get; set; } public Vector3 Direction { get; }
public Color Color { get; set; } public Color Color { get; }
public float Intensity { get; set; } public float Intensity { get; }
public Matrix View public Matrix View
{ {
get get
{ {
return Matrix.CreateLookAt(Direction * 4.5f, Vector3.Zero, Vector3.Up); return Matrix.CreateLookAt((Direction * 5f), Vector3.Zero, Vector3.Up);
} }
} }