Compare commits
4 Commits
main
...
tight_frus
Author | SHA1 | Date |
---|---|---|
cosmonaut | 4d562f545b | |
cosmonaut | 9f6a32b8a0 | |
cosmonaut | 3a89e9a091 | |
cosmonaut | 28bc8c79e7 |
|
@ -10,15 +10,25 @@ namespace Kav
|
|||
EffectParameter gNormalParam;
|
||||
EffectParameter gMetallicRoughnessParam;
|
||||
|
||||
EffectParameter directionalShadowMapParam;
|
||||
EffectParameter directionalLightDirectionParam;
|
||||
EffectParameter directionalLightColorParam;
|
||||
EffectParameter directionalLightMatrixParam;
|
||||
|
||||
EffectParameter eyePositionParam;
|
||||
|
||||
PointLightCollection pointLightCollection;
|
||||
DirectionalLightCollection directionalLightCollection;
|
||||
|
||||
public Texture2D GPosition { get; set; }
|
||||
public Texture2D GAlbedo { get; set; }
|
||||
public Texture2D GNormal { get; set; }
|
||||
public Texture2D GMetallicRoughness { get; set; }
|
||||
|
||||
public Texture2D DirectionalShadowMap { get; set; }
|
||||
public Vector3 DirectionalLightDirection { get; set; }
|
||||
public Vector3 DirectionalLightColor { get; set; }
|
||||
public Matrix DirectionalLightMatrix { get; set; }
|
||||
|
||||
public Vector3 EyePosition { get; set; }
|
||||
|
||||
public int MaxPointLights { get; } = 64;
|
||||
|
@ -29,14 +39,6 @@ namespace Kav
|
|||
private set { pointLightCollection = value; }
|
||||
}
|
||||
|
||||
public int MaxDirectionalLights { get; } = 4;
|
||||
|
||||
public DirectionalLightCollection DirectionalLights
|
||||
{
|
||||
get { return directionalLightCollection; }
|
||||
private set { directionalLightCollection = value; }
|
||||
}
|
||||
|
||||
public DeferredPBREffect(GraphicsDevice graphicsDevice) : base(graphicsDevice, Resources.DeferredPBREffect)
|
||||
{
|
||||
CacheEffectParameters();
|
||||
|
@ -46,11 +48,6 @@ namespace Kav
|
|||
Parameters["PointLightColors"],
|
||||
MaxPointLights
|
||||
);
|
||||
|
||||
DirectionalLights = new DirectionalLightCollection(
|
||||
Parameters["DirectionalLightDirections"],
|
||||
Parameters["DirectionalLightColors"]
|
||||
);
|
||||
}
|
||||
|
||||
protected DeferredPBREffect(DeferredPBREffect cloneSource) : base(cloneSource)
|
||||
|
@ -72,16 +69,6 @@ namespace Kav
|
|||
{
|
||||
PointLights[i] = cloneSource.PointLights[i];
|
||||
}
|
||||
|
||||
DirectionalLights = new DirectionalLightCollection(
|
||||
Parameters["DirectionalLightDirections"],
|
||||
Parameters["DirectionalLightColors"]
|
||||
);
|
||||
|
||||
for (int i = 0; i < MaxDirectionalLights; i++)
|
||||
{
|
||||
DirectionalLights[i] = cloneSource.DirectionalLights[i];
|
||||
}
|
||||
}
|
||||
|
||||
public override Effect Clone()
|
||||
|
@ -96,6 +83,11 @@ namespace Kav
|
|||
gNormalParam.SetValue(GNormal);
|
||||
gMetallicRoughnessParam.SetValue(GMetallicRoughness);
|
||||
|
||||
directionalShadowMapParam.SetValue(DirectionalShadowMap);
|
||||
directionalLightDirectionParam.SetValue(DirectionalLightDirection);
|
||||
directionalLightColorParam.SetValue(DirectionalLightColor);
|
||||
directionalLightMatrixParam.SetValue(DirectionalLightMatrix);
|
||||
|
||||
eyePositionParam.SetValue(EyePosition);
|
||||
}
|
||||
|
||||
|
@ -105,6 +97,11 @@ namespace Kav
|
|||
gAlbedoParam = Parameters["gAlbedo"];
|
||||
gNormalParam = Parameters["gNormal"];
|
||||
gMetallicRoughnessParam = Parameters["gMetallicRoughness"];
|
||||
directionalShadowMapParam = Parameters["directionalShadowMap"];
|
||||
|
||||
directionalLightDirectionParam = Parameters["DirectionalLightDirection"];
|
||||
directionalLightColorParam = Parameters["DirectionalLightColor"];
|
||||
directionalLightMatrixParam = Parameters["DirectionalLightMatrix"];
|
||||
|
||||
eyePositionParam = Parameters["EyePosition"];
|
||||
}
|
||||
|
|
|
@ -5,17 +5,41 @@ 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 Vector3[] directions = new Vector3[6];
|
||||
private readonly Vector3[] colors = new Vector3[6];
|
||||
private readonly float[] intensities = new float[6];
|
||||
private readonly Matrix[] viewMatrices = new Matrix[6];
|
||||
private readonly Matrix[] projectionMatrices = new Matrix[6];
|
||||
private readonly Matrix[] viewProjectionMatrices = new Matrix[6];
|
||||
|
||||
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 void SetMatrices(int index, Matrix view, Matrix projection)
|
||||
{
|
||||
viewMatrices[index] = view;
|
||||
projectionMatrices[index] = projection;
|
||||
}
|
||||
|
||||
public void Apply()
|
||||
{
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
viewProjectionMatrices[i] = viewMatrices[i] * projectionMatrices[i];
|
||||
}
|
||||
|
||||
lightSpaceMatricesParam.SetValue(viewProjectionMatrices);
|
||||
}
|
||||
|
||||
public DirectionalLight this[int i]
|
||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -2,12 +2,12 @@
|
|||
|
||||
static const float PI = 3.141592653589793;
|
||||
static const int MAX_POINT_LIGHTS = 64;
|
||||
static const int MAX_DIRECTIONAL_LIGHTS = 4;
|
||||
|
||||
DECLARE_TEXTURE(gPosition, 0);
|
||||
DECLARE_TEXTURE(gAlbedo, 1);
|
||||
DECLARE_TEXTURE(gNormal, 2);
|
||||
DECLARE_TEXTURE(gMetallicRoughness, 3);
|
||||
DECLARE_TEXTURE(directionalShadowMap, 4);
|
||||
|
||||
BEGIN_CONSTANTS
|
||||
|
||||
|
@ -16,19 +16,37 @@ BEGIN_CONSTANTS
|
|||
float3 PointLightPositions[MAX_POINT_LIGHTS] _ps(c1) _cb(c1);
|
||||
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 DirectionalLightDirection _ps(c129) _cb(c129);
|
||||
float3 DirectionalLightColor _ps(c130) _cb(c130);
|
||||
|
||||
MATRIX_CONSTANTS
|
||||
|
||||
float4x4 DirectionalLightMatrix _ps(c131) _cb(c131);
|
||||
|
||||
END_CONSTANTS
|
||||
|
||||
struct VertexInput
|
||||
{
|
||||
float4 Position : POSITION;
|
||||
float2 TexCoord : TEXCOORD;
|
||||
};
|
||||
|
||||
struct PixelInput
|
||||
{
|
||||
float4 Position : SV_POSITION;
|
||||
float2 TexCoord : TEXCOORD0;
|
||||
};
|
||||
|
||||
PixelInput main_vs(VertexInput input)
|
||||
{
|
||||
PixelInput output;
|
||||
|
||||
output.Position = input.Position;
|
||||
output.TexCoord = input.TexCoord;
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
// Pixel Shader
|
||||
|
||||
float3 FresnelSchlick(float cosTheta, float3 F0)
|
||||
|
@ -71,6 +89,26 @@ float GeometrySmith(float3 N, float3 V, float3 L, float roughness)
|
|||
return ggx1 * ggx2;
|
||||
}
|
||||
|
||||
float ComputeShadow(float4 positionLightSpace)
|
||||
{
|
||||
float bias = 0.001;
|
||||
|
||||
// maps to [-1, 1]
|
||||
float3 projectionCoords = positionLightSpace.xyz / positionLightSpace.w;
|
||||
|
||||
//transform to [0, 1] range
|
||||
projectionCoords = projectionCoords * 0.5 + 0.5;
|
||||
|
||||
float closestDepth = SAMPLE_TEXTURE(directionalShadowMap, positionLightSpace.xy).r;
|
||||
float currentDepth = projectionCoords.z;
|
||||
|
||||
if (projectionCoords.z > 1.0) { return 0.0; }
|
||||
|
||||
float shadow = currentDepth - bias > closestDepth ? 1.0 : 0.0;
|
||||
|
||||
return shadow;
|
||||
}
|
||||
|
||||
float3 ComputeLight(
|
||||
float3 lightDir,
|
||||
float3 radiance,
|
||||
|
@ -79,7 +117,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 +137,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(
|
||||
|
@ -116,7 +155,7 @@ float4 ComputeColor(
|
|||
|
||||
float3 Lo = float3(0.0, 0.0, 0.0);
|
||||
|
||||
// point light
|
||||
// point lights
|
||||
for (int i = 0; i < MAX_POINT_LIGHTS; i++)
|
||||
{
|
||||
float3 lightDir = PointLightPositions[i] - worldPosition;
|
||||
|
@ -124,17 +163,17 @@ 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++)
|
||||
{
|
||||
float3 lightDir = DirectionalLightDirections[i];
|
||||
float3 radiance = DirectionalLightColors[i];
|
||||
float4 positionLightSpace = mul(float4(worldPosition, 1.0), DirectionalLightMatrix);
|
||||
float shadow = ComputeShadow(positionLightSpace);
|
||||
|
||||
Lo += ComputeLight(lightDir, radiance, F0, V, N, albedo, metallic, roughness);
|
||||
}
|
||||
float3 lightDir = DirectionalLightDirection;
|
||||
float3 radiance = DirectionalLightColor;
|
||||
|
||||
Lo += ComputeLight(lightDir, radiance, F0, V, N, albedo, metallic, roughness, (1.0 - shadow));
|
||||
|
||||
float3 ambient = float3(0.03, 0.03, 0.03) * albedo; // * AO;
|
||||
float3 color = ambient + Lo;
|
||||
|
@ -148,13 +187,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,
|
||||
|
@ -166,6 +205,7 @@ Technique DeferredPBR
|
|||
{
|
||||
Pass
|
||||
{
|
||||
VertexShader = compile vs_3_0 main_vs();
|
||||
PixelShader = compile ps_3_0 main_ps();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -52,6 +52,8 @@
|
|||
sampler Name##Sampler : register(s##index) = sampler_state { Texture = (Name); };
|
||||
|
||||
#define SAMPLE_TEXTURE(Name, texCoord) tex2D(Name##Sampler, texCoord)
|
||||
#define SAMPLE_TEXTURE_LOD(Name, texCoord) tex2Dlod(Name##Sampler, texCoord)
|
||||
#define SAMPLE_VERTEX_TEXTURE(Name, texCoord) tex2Dlod(Name##Sampler, float4(texCoord, 0, 0))
|
||||
#define SAMPLE_CUBEMAP(Name, texCoord) texCUBE(Name##Sampler, texCoord)
|
||||
#define SAMPLE_CUBEMAP_LOD(Name, texCoord) texCUBElod(Name##Sampler, texCoord)
|
||||
#endif
|
||||
|
|
|
@ -16,21 +16,29 @@ 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;
|
||||
|
||||
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++)
|
||||
|
|
|
@ -42,6 +42,7 @@ namespace Kav
|
|||
dirtyFlags |= EffectDirtyFlags.WorldViewProj;
|
||||
}
|
||||
}
|
||||
|
||||
public SimpleDepthEffect(GraphicsDevice graphicsDevice) : base(graphicsDevice, Resources.SimpleDepthEffect)
|
||||
{
|
||||
CacheEffectParameters();
|
||||
|
@ -58,6 +59,7 @@ namespace Kav
|
|||
|
||||
dirtyFlags &= ~EffectDirtyFlags.WorldViewProj;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void CacheEffectParameters()
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
using Microsoft.Xna.Framework;
|
||||
|
||||
namespace Kav.Extensions
|
||||
{
|
||||
public static class Vector3Extensions
|
||||
{
|
||||
public static Vector3 Floor(this Vector3 input)
|
||||
{
|
||||
var x = (float)System.Math.Floor(input.X);
|
||||
var y = (float)System.Math.Floor(input.Y);
|
||||
var z = (float)System.Math.Floor(input.Z);
|
||||
|
||||
return new Vector3(x, y, z);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -7,11 +7,13 @@
|
|||
<Copyright>Evan Hemsley 2020</Copyright>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<AssemblyName>Kav</AssemblyName>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="../FNA/FNA.Core.csproj" />
|
||||
<ProjectReference Include="../Smuggler/Smuggler.Core.csproj" />
|
||||
<ProjectReference Include="../MoonTools.Bonk/Bonk/Bonk.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
@ -7,11 +7,13 @@
|
|||
<Copyright>Evan Hemsley 2020</Copyright>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<AssemblyName>Kav</AssemblyName>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="../FNA/FNA.csproj" />
|
||||
<ProjectReference Include="../Smuggler/Smuggler.Framework.csproj" />
|
||||
<ProjectReference Include="../MoonTools.Bonk/Bonk/Bonk.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
@ -4,25 +4,9 @@ namespace Kav
|
|||
{
|
||||
public struct DirectionalLight
|
||||
{
|
||||
public Vector3 Direction { get; set; }
|
||||
public Color Color { get; set; }
|
||||
public float Intensity { get; set; }
|
||||
|
||||
public Matrix View
|
||||
{
|
||||
get
|
||||
{
|
||||
return Matrix.CreateLookAt(-Direction * 100f, Vector3.Zero, Vector3.Up);
|
||||
}
|
||||
}
|
||||
|
||||
public Matrix Projection
|
||||
{
|
||||
get
|
||||
{
|
||||
return Matrix.CreateOrthographic(20f, 20f, 1f, 101f);
|
||||
}
|
||||
}
|
||||
public Vector3 Direction { get; }
|
||||
public Color Color { get; }
|
||||
public float Intensity { get; }
|
||||
|
||||
public DirectionalLight(Vector3 direction, Color color, float intensity = 1f)
|
||||
{
|
||||
|
|
108
Renderer.cs
108
Renderer.cs
|
@ -1,4 +1,5 @@
|
|||
using System.Collections.Generic;
|
||||
using Kav.Extensions;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
|
||||
|
@ -10,7 +11,7 @@ namespace Kav
|
|||
private int RenderDimensionsX { get; }
|
||||
private int RenderDimensionsY { get; }
|
||||
|
||||
private RenderTarget2D DepthRenderTarget { get; }
|
||||
private RenderTarget2D DirectionalLightDepthTarget { get; }
|
||||
private SimpleDepthEffect SimpleDepthEffect { get; }
|
||||
|
||||
private RenderTarget2D gPosition { get; }
|
||||
|
@ -19,6 +20,8 @@ namespace Kav
|
|||
private RenderTarget2D gMetallicRoughness { get; }
|
||||
private RenderTarget2D deferredRenderTarget { get; }
|
||||
|
||||
private VertexBuffer fullscreenTriangle { get; }
|
||||
|
||||
private RenderTargetBinding[] GBuffer { get; }
|
||||
|
||||
private DeferredPBREffect DeferredPBREffect { get; }
|
||||
|
@ -31,12 +34,12 @@ namespace Kav
|
|||
RenderDimensionsX = renderDimensionsX;
|
||||
RenderDimensionsY = renderDimensionsY;
|
||||
|
||||
DepthRenderTarget = new RenderTarget2D(
|
||||
DirectionalLightDepthTarget = new RenderTarget2D(
|
||||
GraphicsDevice,
|
||||
renderDimensionsX,
|
||||
renderDimensionsY,
|
||||
1024,
|
||||
1024,
|
||||
false,
|
||||
SurfaceFormat.HalfSingle, // unused
|
||||
SurfaceFormat.Single,
|
||||
DepthFormat.Depth24
|
||||
);
|
||||
|
||||
|
@ -92,10 +95,21 @@ namespace Kav
|
|||
SimpleDepthEffect = new SimpleDepthEffect(GraphicsDevice);
|
||||
DeferredPBREffect = new DeferredPBREffect(GraphicsDevice);
|
||||
|
||||
fullscreenTriangle = new VertexBuffer(GraphicsDevice, typeof(VertexPositionTexture), 3, BufferUsage.WriteOnly);
|
||||
fullscreenTriangle.SetData(new VertexPositionTexture[3] {
|
||||
new VertexPositionTexture(new Vector3(-1, -1, 0), new Vector2(0, 0)),
|
||||
new VertexPositionTexture(new Vector3(-1, 3, 0), new Vector2(0, -2)),
|
||||
new VertexPositionTexture(new Vector3(3, -1, 0), new Vector2(2, 0))
|
||||
});
|
||||
|
||||
SpriteBatch = new SpriteBatch(GraphicsDevice);
|
||||
|
||||
GraphicsDevice.SetRenderTarget(deferredRenderTarget);
|
||||
graphicsDevice.Clear(Color.White);
|
||||
|
||||
GraphicsDevice.SetRenderTarget(DirectionalLightDepthTarget);
|
||||
GraphicsDevice.Clear(Color.White);
|
||||
|
||||
GraphicsDevice.SetRenderTarget(null);
|
||||
}
|
||||
|
||||
|
@ -103,8 +117,16 @@ namespace Kav
|
|||
Camera camera,
|
||||
IEnumerable<(Model, Matrix)> modelTransforms,
|
||||
IEnumerable<PointLight> pointLights,
|
||||
IEnumerable<DirectionalLight> directionalLights
|
||||
DirectionalLight directionalLight
|
||||
) {
|
||||
var cameraViewProjectionMatrix = camera.View * camera.Projection;
|
||||
|
||||
ShadowMapRender(
|
||||
modelTransforms,
|
||||
directionalLight,
|
||||
cameraViewProjectionMatrix
|
||||
);
|
||||
|
||||
GraphicsDevice.SetRenderTargets(GBuffer);
|
||||
GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1f, 0);
|
||||
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
|
||||
|
@ -160,36 +182,53 @@ namespace Kav
|
|||
i++;
|
||||
}
|
||||
|
||||
i = 0;
|
||||
foreach (var directionalLight in directionalLights)
|
||||
DeferredPBREffect.DirectionalShadowMap = DirectionalLightDepthTarget;
|
||||
DeferredPBREffect.DirectionalLightDirection = directionalLight.Direction;
|
||||
DeferredPBREffect.DirectionalLightColor = directionalLight.Color.ToVector3() * directionalLight.Intensity;
|
||||
|
||||
foreach (EffectPass pass in DeferredPBREffect.CurrentTechnique.Passes)
|
||||
{
|
||||
if (i > DeferredPBREffect.MaxDirectionalLights) { break; }
|
||||
DeferredPBREffect.DirectionalLights[i] = directionalLight;
|
||||
i++;
|
||||
pass.Apply();
|
||||
GraphicsDevice.SetVertexBuffer(fullscreenTriangle);
|
||||
GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
|
||||
}
|
||||
|
||||
SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, null, null, null, DeferredPBREffect);
|
||||
SpriteBatch.Draw(deferredRenderTarget, Vector2.Zero, Color.White);
|
||||
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)
|
||||
{
|
||||
GraphicsDevice.SetRenderTarget(DepthRenderTarget);
|
||||
GraphicsDevice.Clear(ClearOptions.DepthBuffer, Color.Black, 1, 0);
|
||||
public void ShadowMapRender(
|
||||
IEnumerable<(Model, Matrix)> modelTransforms,
|
||||
DirectionalLight directionalLight,
|
||||
Matrix cameraViewProjectionMatrix
|
||||
) {
|
||||
GraphicsDevice.SetRenderTarget(DirectionalLightDepthTarget);
|
||||
GraphicsDevice.Clear(Color.White);
|
||||
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
|
||||
GraphicsDevice.BlendState = BlendState.Opaque;
|
||||
|
||||
SimpleDepthEffect.View = directionalLight.View;
|
||||
SimpleDepthEffect.Projection = directionalLight.Projection;
|
||||
var right = Vector3.Cross(Vector3.Up, directionalLight.Direction);
|
||||
var up = Vector3.Cross(directionalLight.Direction, right);
|
||||
|
||||
var lightRotation = Matrix.CreateLookAt(Vector3.Zero, -directionalLight.Direction, up);
|
||||
|
||||
var cameraBoundingFrustum = new BoundingFrustum(cameraViewProjectionMatrix);
|
||||
|
||||
Vector3[] frustumCorners = cameraBoundingFrustum.GetCorners();
|
||||
for (var i = 0; i < frustumCorners.Length; i++)
|
||||
{
|
||||
frustumCorners[i] = Vector3.Transform(frustumCorners[i], lightRotation);
|
||||
}
|
||||
|
||||
BoundingBox lightBox = BoundingBox.CreateFromPoints(frustumCorners);
|
||||
Vector3 boxSize = lightBox.Max - lightBox.Min;
|
||||
Vector3 halfBoxSize = boxSize * 0.5f;
|
||||
|
||||
Vector3 lightPosition = lightBox.Min + halfBoxSize;
|
||||
lightPosition.Z = lightBox.Min.Z;
|
||||
lightPosition = Vector3.Transform(lightPosition, Matrix.Invert(lightRotation));
|
||||
|
||||
SimpleDepthEffect.View = Matrix.CreateLookAt(lightPosition, lightPosition - directionalLight.Direction, up);
|
||||
SimpleDepthEffect.Projection = Matrix.CreateOrthographic(boxSize.X, boxSize.Y, -boxSize.Z, boxSize.Z);
|
||||
DeferredPBREffect.DirectionalLightMatrix = SimpleDepthEffect.View * SimpleDepthEffect.Projection;
|
||||
|
||||
foreach (var (model, transform) in modelTransforms)
|
||||
{
|
||||
|
@ -220,6 +259,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