Compare commits
2 Commits
Author | SHA1 | Date |
---|---|---|
cosmonaut | 3a89e9a091 | |
cosmonaut | 28bc8c79e7 |
|
@ -9,8 +9,10 @@ namespace Kav
|
|||
EffectParameter gAlbedoParam;
|
||||
EffectParameter gNormalParam;
|
||||
EffectParameter gMetallicRoughnessParam;
|
||||
EffectParameter shadowMapParam;
|
||||
|
||||
EffectParameter eyePositionParam;
|
||||
|
||||
PointLightCollection pointLightCollection;
|
||||
DirectionalLightCollection directionalLightCollection;
|
||||
|
||||
|
@ -18,6 +20,7 @@ namespace Kav
|
|||
public Texture2D GAlbedo { get; set; }
|
||||
public Texture2D GNormal { get; set; }
|
||||
public Texture2D GMetallicRoughness { get; set; }
|
||||
public TextureCube DirectionalShadowMap { get; set; }
|
||||
|
||||
public Vector3 EyePosition { get; set; }
|
||||
|
||||
|
@ -49,7 +52,8 @@ namespace Kav
|
|||
|
||||
DirectionalLights = new DirectionalLightCollection(
|
||||
Parameters["DirectionalLightDirections"],
|
||||
Parameters["DirectionalLightColors"]
|
||||
Parameters["DirectionalLightColors"],
|
||||
Parameters["DirectionalLightMatrices"]
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -75,7 +79,8 @@ namespace Kav
|
|||
|
||||
DirectionalLights = new DirectionalLightCollection(
|
||||
Parameters["DirectionalLightDirections"],
|
||||
Parameters["DirectionalLightColors"]
|
||||
Parameters["DirectionalLightColors"],
|
||||
Parameters["DirectionalLightMatrices"]
|
||||
);
|
||||
|
||||
for (int i = 0; i < MaxDirectionalLights; i++)
|
||||
|
@ -95,6 +100,7 @@ namespace Kav
|
|||
gAlbedoParam.SetValue(GAlbedo);
|
||||
gNormalParam.SetValue(GNormal);
|
||||
gMetallicRoughnessParam.SetValue(GMetallicRoughness);
|
||||
shadowMapParam.SetValue(DirectionalShadowMap);
|
||||
|
||||
eyePositionParam.SetValue(EyePosition);
|
||||
}
|
||||
|
@ -105,6 +111,7 @@ namespace Kav
|
|||
gAlbedoParam = Parameters["gAlbedo"];
|
||||
gNormalParam = Parameters["gNormal"];
|
||||
gMetallicRoughnessParam = Parameters["gMetallicRoughness"];
|
||||
shadowMapParam = Parameters["shadowMap"];
|
||||
|
||||
eyePositionParam = Parameters["EyePosition"];
|
||||
}
|
||||
|
|
|
@ -8,14 +8,20 @@ namespace Kav
|
|||
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];
|
||||
|
||||
readonly EffectParameter lightDirectionsParam;
|
||||
readonly EffectParameter lightColorsParam;
|
||||
readonly EffectParameter lightSpaceMatricesParam;
|
||||
|
||||
public DirectionalLightCollection(EffectParameter lightDirectionsParam, EffectParameter lightColorsParam)
|
||||
{
|
||||
public DirectionalLightCollection(
|
||||
EffectParameter lightDirectionsParam,
|
||||
EffectParameter lightColorsParam,
|
||||
EffectParameter lightSpaceMatricesParam
|
||||
) {
|
||||
this.lightDirectionsParam = lightDirectionsParam;
|
||||
this.lightColorsParam = lightColorsParam;
|
||||
this.lightSpaceMatricesParam = lightSpaceMatricesParam;
|
||||
}
|
||||
|
||||
public DirectionalLight this[int i]
|
||||
|
@ -39,8 +45,10 @@ namespace Kav
|
|||
directions[i] = value.Direction;
|
||||
colors[i] = value.Color.ToVector3() * value.Intensity;
|
||||
intensities[i] = value.Intensity;
|
||||
lightSpaceMatrices[i] = value.View * value.Projection;
|
||||
lightDirectionsParam.SetValue(directions);
|
||||
lightColorsParam.SetValue(colors);
|
||||
lightSpaceMatricesParam.SetValue(lightSpaceMatrices);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -8,6 +8,7 @@ DECLARE_TEXTURE(gPosition, 0);
|
|||
DECLARE_TEXTURE(gAlbedo, 1);
|
||||
DECLARE_TEXTURE(gNormal, 2);
|
||||
DECLARE_TEXTURE(gMetallicRoughness, 3);
|
||||
DECLARE_CUBEMAP(shadowMap, 4);
|
||||
|
||||
BEGIN_CONSTANTS
|
||||
|
||||
|
@ -21,6 +22,8 @@ BEGIN_CONSTANTS
|
|||
|
||||
MATRIX_CONSTANTS
|
||||
|
||||
float4x4 DirectionalLightMatrices[MAX_DIRECTIONAL_LIGHTS] _ps(c137) _cb(c137);
|
||||
|
||||
END_CONSTANTS
|
||||
|
||||
struct PixelInput
|
||||
|
@ -71,6 +74,63 @@ float GeometrySmith(float3 N, float3 V, float3 L, float roughness)
|
|||
return ggx1 * ggx2;
|
||||
}
|
||||
|
||||
float3 ConvertCubeUVToXYZ(int index, float u, float v)
|
||||
{
|
||||
float uc = 2.0 * u - 1.0;
|
||||
float vc = 2.0 * v - 1.0;
|
||||
|
||||
if (index == 0) { return float3(1.0, vc, -uc); }
|
||||
if (index == 1) { return float3(-1.0, vc, uc); }
|
||||
if (index == 2) { return float3(uc, 1.0, -vc); }
|
||||
if (index == 3) { return float3(uc, -1.0, vc); }
|
||||
if (index == 4) { return float3(uc, vc, -1.0); }
|
||||
if (index == 5) { return float3(-uc, vc, 1.0); }
|
||||
|
||||
return float3(1.0, 0.0, 0.5);
|
||||
}
|
||||
|
||||
float ComputeShadow(float4 positionLightSpace, int directionalLightIndex)
|
||||
{
|
||||
float bias = 0.001;
|
||||
|
||||
// maps to [-1, 1]
|
||||
float3 projectionCoords = positionLightSpace.xyz / positionLightSpace.w;
|
||||
|
||||
float3 cubeMapSampleVector;
|
||||
|
||||
if (directionalLightIndex == 0)
|
||||
{
|
||||
cubeMapSampleVector = float3(1.0f, projectionCoords.y, -projectionCoords.x);
|
||||
}
|
||||
else if (directionalLightIndex == 1)
|
||||
{
|
||||
cubeMapSampleVector = float3(-1.0f, projectionCoords.y, projectionCoords.x);
|
||||
}
|
||||
else if (directionalLightIndex == 2)
|
||||
{
|
||||
cubeMapSampleVector = float3(projectionCoords.x, 1.0f, projectionCoords.y);
|
||||
}
|
||||
else if (directionalLightIndex == 3)
|
||||
{
|
||||
cubeMapSampleVector = float3(projectionCoords.x, -1.0f, -projectionCoords.y);
|
||||
}
|
||||
else if (directionalLightIndex == 4)
|
||||
{
|
||||
cubeMapSampleVector = float3(projectionCoords.x, projectionCoords.y, 1.0);
|
||||
}
|
||||
else
|
||||
{
|
||||
cubeMapSampleVector = float3(-projectionCoords.x, projectionCoords.y, -1.0);
|
||||
}
|
||||
|
||||
float closestDepth = SAMPLE_CUBEMAP(shadowMap, cubeMapSampleVector).r;
|
||||
float currentDepth = projectionCoords.z;
|
||||
|
||||
float shadow = currentDepth - bias > closestDepth ? 1.0 : 0.0;
|
||||
|
||||
return shadow;
|
||||
}
|
||||
|
||||
float3 ComputeLight(
|
||||
float3 lightDir,
|
||||
float3 radiance,
|
||||
|
@ -79,7 +139,8 @@ float3 ComputeLight(
|
|||
float3 N,
|
||||
float3 albedo,
|
||||
float metallic,
|
||||
float roughness
|
||||
float roughness,
|
||||
float shadow
|
||||
) {
|
||||
float3 L = normalize(lightDir);
|
||||
float3 H = normalize(V + L);
|
||||
|
@ -98,7 +159,7 @@ float3 ComputeLight(
|
|||
kD *= 1.0 - metallic;
|
||||
|
||||
float NdotL = max(dot(N, L), 0.0);
|
||||
return (kD * albedo / PI + specular) * radiance * NdotL;
|
||||
return (kD * albedo / PI + specular) * radiance * NdotL * shadow;
|
||||
}
|
||||
|
||||
float4 ComputeColor(
|
||||
|
@ -124,16 +185,19 @@ float4 ComputeColor(
|
|||
float attenuation = 1.0 / (distance * distance);
|
||||
float3 radiance = PointLightColors[i] * attenuation;
|
||||
|
||||
Lo += ComputeLight(lightDir, radiance, F0, V, N, albedo, metallic, roughness);
|
||||
Lo += ComputeLight(lightDir, radiance, F0, V, N, albedo, metallic, roughness, 1.0);
|
||||
}
|
||||
|
||||
// directional light
|
||||
for (int i = 0; i < MAX_DIRECTIONAL_LIGHTS; i++)
|
||||
for (int i = 0; i < 1; i++)
|
||||
{
|
||||
float4 positionLightSpace = mul(float4(worldPosition, 1.0), DirectionalLightMatrices[i]);
|
||||
float shadow = ComputeShadow(positionLightSpace, i);
|
||||
|
||||
float3 lightDir = DirectionalLightDirections[i];
|
||||
float3 radiance = DirectionalLightColors[i];
|
||||
|
||||
Lo += ComputeLight(lightDir, radiance, F0, V, N, albedo, metallic, roughness);
|
||||
Lo += ComputeLight(lightDir, radiance, F0, V, N, albedo, metallic, roughness, (1.0 - shadow));
|
||||
}
|
||||
|
||||
float3 ambient = float3(0.03, 0.03, 0.03) * albedo; // * AO;
|
||||
|
@ -148,13 +212,13 @@ float4 ComputeColor(
|
|||
|
||||
float4 main_ps(PixelInput input) : SV_TARGET0
|
||||
{
|
||||
float3 fragPosition = SAMPLE_TEXTURE(gPosition, input.TexCoord).rgb;
|
||||
float3 worldPosition = SAMPLE_TEXTURE(gPosition, input.TexCoord).rgb;
|
||||
float3 normal = SAMPLE_TEXTURE(gNormal, input.TexCoord).xyz;
|
||||
float3 albedo = SAMPLE_TEXTURE(gAlbedo, input.TexCoord).rgb;
|
||||
float2 metallicRoughness = SAMPLE_TEXTURE(gMetallicRoughness, input.TexCoord).rg;
|
||||
|
||||
return ComputeColor(
|
||||
fragPosition,
|
||||
worldPosition,
|
||||
normal,
|
||||
albedo,
|
||||
metallicRoughness.r,
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
BEGIN_CONSTANTS
|
||||
|
||||
float4x4 ModelViewProjection _vs(c0) _cb(c0);
|
||||
float near _vs(c4) _cb(c4);
|
||||
float far _vs(c5) _cb(c5);
|
||||
|
||||
MATRIX_CONSTANTS
|
||||
|
||||
|
@ -16,21 +18,31 @@ struct VertexShaderInput
|
|||
struct VertexShaderOutput
|
||||
{
|
||||
float4 Position : SV_Position;
|
||||
float Depth : TEXCOORD0;
|
||||
};
|
||||
|
||||
VertexShaderOutput main_vs(VertexShaderInput input)
|
||||
{
|
||||
VertexShaderOutput output;
|
||||
|
||||
output.Position = mul(float4(input.Position.xyz, 1.0), ModelViewProjection);
|
||||
output.Position = mul(input.Position, ModelViewProjection);
|
||||
|
||||
output.Depth = output.Position.z / output.Position.w;
|
||||
output.Depth = (output.Depth * 0.5) + 0.5;
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
float4 main_ps(VertexShaderOutput input) : SV_TARGET0
|
||||
{
|
||||
return float4(input.Depth, 0.0, 0.0, 0.0);
|
||||
}
|
||||
|
||||
Technique SimpleDepth
|
||||
{
|
||||
Pass
|
||||
{
|
||||
VertexShader = compile vs_3_0 main_vs();
|
||||
PixelShader = compile ps_3_0 main_ps();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -207,7 +207,8 @@ namespace Kav
|
|||
|
||||
directionalLightCollection = new DirectionalLightCollection(
|
||||
Parameters["LightDirections"],
|
||||
Parameters["DirectionLightColors"]
|
||||
Parameters["DirectionLightColors"],
|
||||
Parameters["DirectionalLightMatrices"]
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -232,7 +233,8 @@ namespace Kav
|
|||
|
||||
DirectionalLights = new DirectionalLightCollection(
|
||||
Parameters["LightDirections"],
|
||||
Parameters["DirectionLightColors"]
|
||||
Parameters["DirectionLightColors"],
|
||||
Parameters["DirectionalLightMatrices"]
|
||||
);
|
||||
|
||||
for (int i = 0; i < MaxDirectionalLights; i++)
|
||||
|
|
|
@ -6,6 +6,8 @@ namespace Kav
|
|||
public class SimpleDepthEffect : Effect
|
||||
{
|
||||
EffectParameter modelViewProjectionParam;
|
||||
EffectParameter nearParam;
|
||||
EffectParameter farParam;
|
||||
|
||||
Matrix model;
|
||||
Matrix view;
|
||||
|
@ -42,6 +44,10 @@ namespace Kav
|
|||
dirtyFlags |= EffectDirtyFlags.WorldViewProj;
|
||||
}
|
||||
}
|
||||
|
||||
public float Near { get; set; }
|
||||
public float Far { get; set; }
|
||||
|
||||
public SimpleDepthEffect(GraphicsDevice graphicsDevice) : base(graphicsDevice, Resources.SimpleDepthEffect)
|
||||
{
|
||||
CacheEffectParameters();
|
||||
|
@ -58,11 +64,16 @@ namespace Kav
|
|||
|
||||
dirtyFlags &= ~EffectDirtyFlags.WorldViewProj;
|
||||
}
|
||||
|
||||
nearParam.SetValue(Near);
|
||||
farParam.SetValue(Far);
|
||||
}
|
||||
|
||||
private void CacheEffectParameters()
|
||||
{
|
||||
modelViewProjectionParam = Parameters["ModelViewProjection"];
|
||||
nearParam = Parameters["near"];
|
||||
farParam = Parameters["far"];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ namespace Kav
|
|||
{
|
||||
get
|
||||
{
|
||||
return Matrix.CreateLookAt(-Direction * 100f, Vector3.Zero, Vector3.Up);
|
||||
return Matrix.CreateLookAt(Direction * 4.5f, Vector3.Zero, Vector3.Up);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -20,7 +20,7 @@ namespace Kav
|
|||
{
|
||||
get
|
||||
{
|
||||
return Matrix.CreateOrthographic(20f, 20f, 1f, 101f);
|
||||
return Matrix.CreateOrthographic(20f, 20f, 1f, 7.5f);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
56
Renderer.cs
56
Renderer.cs
|
@ -10,7 +10,7 @@ namespace Kav
|
|||
private int RenderDimensionsX { get; }
|
||||
private int RenderDimensionsY { get; }
|
||||
|
||||
private RenderTarget2D DepthRenderTarget { get; }
|
||||
private RenderTargetCube DirectionalLightDepthTarget { get; }
|
||||
private SimpleDepthEffect SimpleDepthEffect { get; }
|
||||
|
||||
private RenderTarget2D gPosition { get; }
|
||||
|
@ -31,12 +31,11 @@ namespace Kav
|
|||
RenderDimensionsX = renderDimensionsX;
|
||||
RenderDimensionsY = renderDimensionsY;
|
||||
|
||||
DepthRenderTarget = new RenderTarget2D(
|
||||
DirectionalLightDepthTarget = new RenderTargetCube(
|
||||
GraphicsDevice,
|
||||
renderDimensionsX,
|
||||
renderDimensionsY,
|
||||
1024,
|
||||
false,
|
||||
SurfaceFormat.HalfSingle, // unused
|
||||
SurfaceFormat.Single,
|
||||
DepthFormat.Depth24
|
||||
);
|
||||
|
||||
|
@ -96,6 +95,13 @@ namespace Kav
|
|||
|
||||
GraphicsDevice.SetRenderTarget(deferredRenderTarget);
|
||||
graphicsDevice.Clear(Color.White);
|
||||
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
GraphicsDevice.SetRenderTarget(DirectionalLightDepthTarget, (CubeMapFace) i);
|
||||
GraphicsDevice.Clear(Color.White);
|
||||
}
|
||||
|
||||
GraphicsDevice.SetRenderTarget(null);
|
||||
}
|
||||
|
||||
|
@ -105,6 +111,15 @@ namespace Kav
|
|||
IEnumerable<PointLight> pointLights,
|
||||
IEnumerable<DirectionalLight> directionalLights
|
||||
) {
|
||||
var directionalLightIndex = 0;
|
||||
|
||||
foreach (var directionalLight in directionalLights)
|
||||
{
|
||||
if (directionalLightIndex > 5) { break; }
|
||||
ShadowMapRender(modelTransforms, directionalLight, (CubeMapFace) directionalLightIndex);
|
||||
directionalLightIndex += 1;
|
||||
}
|
||||
|
||||
GraphicsDevice.SetRenderTargets(GBuffer);
|
||||
GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1f, 0);
|
||||
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
|
||||
|
@ -150,6 +165,7 @@ namespace Kav
|
|||
DeferredPBREffect.GAlbedo = gAlbedo;
|
||||
DeferredPBREffect.GNormal = gNormal;
|
||||
DeferredPBREffect.GMetallicRoughness = gMetallicRoughness;
|
||||
DeferredPBREffect.DirectionalShadowMap = DirectionalLightDepthTarget;
|
||||
DeferredPBREffect.EyePosition = Matrix.Invert(camera.View).Translation;
|
||||
|
||||
int i = 0;
|
||||
|
@ -173,23 +189,18 @@ namespace Kav
|
|||
SpriteBatch.End();
|
||||
}
|
||||
|
||||
public void Render(
|
||||
Camera camera,
|
||||
IEnumerable<(Model, Matrix)> modelTransforms,
|
||||
IEnumerable<PointLight> pointLights,
|
||||
IEnumerable<DirectionalLight> directionalLights
|
||||
) {
|
||||
Render(camera.View, camera.Projection, modelTransforms, pointLights, directionalLights);
|
||||
}
|
||||
|
||||
// for shadow mapping
|
||||
public void DepthRender(IEnumerable<(Model, Matrix)> modelTransforms, DirectionalLight directionalLight)
|
||||
public void ShadowMapRender(IEnumerable<(Model, Matrix)> modelTransforms, DirectionalLight directionalLight, CubeMapFace face)
|
||||
{
|
||||
GraphicsDevice.SetRenderTarget(DepthRenderTarget);
|
||||
GraphicsDevice.Clear(ClearOptions.DepthBuffer, Color.Black, 1, 0);
|
||||
|
||||
GraphicsDevice.SetRenderTarget(DirectionalLightDepthTarget, face);
|
||||
GraphicsDevice.Clear(Color.White);
|
||||
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
|
||||
GraphicsDevice.BlendState = BlendState.Opaque;
|
||||
|
||||
SimpleDepthEffect.View = directionalLight.View;
|
||||
SimpleDepthEffect.Projection = directionalLight.Projection;
|
||||
SimpleDepthEffect.Near = 0.1f; // FIXME: this is a kludge
|
||||
SimpleDepthEffect.Far = 200f;
|
||||
|
||||
foreach (var (model, transform) in modelTransforms)
|
||||
{
|
||||
|
@ -220,6 +231,15 @@ namespace Kav
|
|||
}
|
||||
}
|
||||
|
||||
public void Render(
|
||||
Camera camera,
|
||||
IEnumerable<(Model, Matrix)> modelTransforms,
|
||||
IEnumerable<PointLight> pointLights,
|
||||
IEnumerable<DirectionalLight> directionalLights
|
||||
) {
|
||||
Render(camera.View, camera.Projection, modelTransforms, pointLights, directionalLights);
|
||||
}
|
||||
|
||||
private void Render(
|
||||
Matrix view,
|
||||
Matrix projection,
|
||||
|
|
Loading…
Reference in New Issue