diff --git a/Effects/DeferredPBREffect.cs b/Effects/DeferredPBREffect.cs index 04af0e8..97c8615 100644 --- a/Effects/DeferredPBREffect.cs +++ b/Effects/DeferredPBREffect.cs @@ -12,6 +12,7 @@ namespace Kav EffectParameter eyePositionParam; PointLightCollection pointLightCollection; + DirectionalLightCollection directionalLightCollection; public Texture2D GPosition { get; set; } public Texture2D GAlbedo { get; set; } @@ -28,6 +29,14 @@ 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(); @@ -37,6 +46,11 @@ namespace Kav Parameters["PointLightColors"], MaxPointLights ); + + DirectionalLights = new DirectionalLightCollection( + Parameters["DirectionalLightDirections"], + Parameters["DirectionalLightColors"] + ); } protected DeferredPBREffect(DeferredPBREffect cloneSource) : base(cloneSource) @@ -58,6 +72,16 @@ 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() diff --git a/Effects/GBufferEffect.cs b/Effects/DeferredPBR_GBufferEffect.cs similarity index 95% rename from Effects/GBufferEffect.cs rename to Effects/DeferredPBR_GBufferEffect.cs index ff8769d..26a9c8f 100644 --- a/Effects/GBufferEffect.cs +++ b/Effects/DeferredPBR_GBufferEffect.cs @@ -3,7 +3,7 @@ using Microsoft.Xna.Framework.Graphics; namespace Kav { - public class GBufferEffect : Effect, TransformEffect + public class DeferredPBR_GBufferEffect : Effect, TransformEffect { EffectParameter worldParam; EffectParameter worldViewProjectionParam; @@ -126,12 +126,12 @@ namespace Kav } } - public GBufferEffect(GraphicsDevice graphicsDevice) : base(graphicsDevice, Resources.GBufferEffect) + public DeferredPBR_GBufferEffect(GraphicsDevice graphicsDevice) : base(graphicsDevice, Resources.DeferredPBR_GBufferEffect) { CacheEffectParameters(); } - protected GBufferEffect(GBufferEffect cloneSource) : base(cloneSource) + protected DeferredPBR_GBufferEffect(DeferredPBR_GBufferEffect cloneSource) : base(cloneSource) { CacheEffectParameters(); @@ -150,7 +150,7 @@ namespace Kav public override Effect Clone() { - return new GBufferEffect(this); + return new DeferredPBR_GBufferEffect(this); } protected override void OnApply() diff --git a/Effects/DirectionalLightCollection.cs b/Effects/DirectionalLightCollection.cs new file mode 100644 index 0000000..10bcbda --- /dev/null +++ b/Effects/DirectionalLightCollection.cs @@ -0,0 +1,47 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; + +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]; + + readonly EffectParameter lightDirectionsParam; + readonly EffectParameter lightColorsParam; + + public DirectionalLightCollection(EffectParameter lightDirectionsParam, EffectParameter lightColorsParam) + { + this.lightDirectionsParam = lightDirectionsParam; + this.lightColorsParam = lightColorsParam; + } + + public DirectionalLight this[int i] + { + get + { + var color = colors[i] / intensities[i]; + return new DirectionalLight( + directions[i], + new Color( + color.X, + color.Y, + color.Z, + 1f + ), + intensities[i] + ); + } + set + { + directions[i] = value.Direction; + colors[i] = value.Color.ToVector3() * value.Intensity; + intensities[i] = value.Intensity; + lightDirectionsParam.SetValue(directions); + lightColorsParam.SetValue(colors); + } + } + } +} diff --git a/Effects/FXB/DeferredPBREffect.fxb b/Effects/FXB/DeferredPBREffect.fxb index b3cda6c..10298d5 100644 Binary files a/Effects/FXB/DeferredPBREffect.fxb and b/Effects/FXB/DeferredPBREffect.fxb differ diff --git a/Effects/FXB/DeferredPBR_GBufferEffect.fxb b/Effects/FXB/DeferredPBR_GBufferEffect.fxb new file mode 100644 index 0000000..9b2d684 Binary files /dev/null and b/Effects/FXB/DeferredPBR_GBufferEffect.fxb differ diff --git a/Effects/HLSL/DeferredPBREffect.fx b/Effects/HLSL/DeferredPBREffect.fx index 9f4e74e..a49f27a 100644 --- a/Effects/HLSL/DeferredPBREffect.fx +++ b/Effects/HLSL/DeferredPBREffect.fx @@ -2,6 +2,7 @@ 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); @@ -10,10 +11,13 @@ DECLARE_TEXTURE(gMetallicRoughness, 3); BEGIN_CONSTANTS - float3 EyePosition _ps(c0) _cb(c0); + float3 EyePosition _ps(c0) _cb(c0); - float3 PointLightPositions[MAX_POINT_LIGHTS] _ps(c1) _cb(c1); - float3 PointLightColors[MAX_POINT_LIGHTS] _ps(c65) _cb(c65); + 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); MATRIX_CONSTANTS @@ -123,14 +127,14 @@ float4 ComputeColor( Lo += ComputeLight(lightDir, radiance, F0, V, N, albedo, metallic, roughness); } - // // directional light - // for (int i = 0; i < 4; i++) - // { - // float3 lightDir = LightDirections[i]; - // float3 radiance = DirectionLightColors[i]; + // directional light + for (int i = 0; i < MAX_DIRECTIONAL_LIGHTS; 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); + } float3 ambient = float3(0.03, 0.03, 0.03) * albedo; // * AO; float3 color = ambient + Lo; diff --git a/Effects/HLSL/GBufferEffect.fx b/Effects/HLSL/DeferredPBR_GBufferEffect.fx similarity index 100% rename from Effects/HLSL/GBufferEffect.fx rename to Effects/HLSL/DeferredPBR_GBufferEffect.fx diff --git a/Effects/PBREffect.cs b/Effects/PBREffect.cs index 9efe2fe..db04a25 100644 --- a/Effects/PBREffect.cs +++ b/Effects/PBREffect.cs @@ -3,48 +3,6 @@ using Microsoft.Xna.Framework.Graphics; 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]; - - readonly EffectParameter lightPositionsParam; - readonly EffectParameter lightColorsParam; - - public DirectionalLightCollection(EffectParameter lightPositionsParam, EffectParameter lightColorsParam) - { - this.lightPositionsParam = lightPositionsParam; - this.lightColorsParam = lightColorsParam; - } - - public DirectionalLight this[int i] - { - get - { - var color = colors[i] / intensities[i]; - return new DirectionalLight( - directions[i], - new Color( - color.X, - color.Y, - color.Z, - 1f - ), - intensities[i] - ); - } - set - { - directions[i] = value.Direction; - colors[i] = value.Color.ToVector3() * value.Intensity; - intensities[i] = value.Intensity; - lightPositionsParam.SetValue(directions); - lightColorsParam.SetValue(colors); - } - } - } - public class PBREffect : Effect, TransformEffect, PointLightEffect, DirectionalLightEffect { EffectParameter worldParam; diff --git a/Kav.Core.csproj b/Kav.Core.csproj index 615ab97..7957739 100644 --- a/Kav.Core.csproj +++ b/Kav.Core.csproj @@ -15,8 +15,8 @@ - - Kav.Resources.GBufferEffect.fxb + + Kav.Resources.DeferredPBR_GBufferEffect.fxb Kav.Resources.DeferredPBREffect.fxb diff --git a/Kav.Framework.csproj b/Kav.Framework.csproj index c0f517d..0557675 100644 --- a/Kav.Framework.csproj +++ b/Kav.Framework.csproj @@ -15,8 +15,8 @@ - - Kav.Resources.GBufferEffect.fxb + + Kav.Resources.DeferredPBR_GBufferEffect.fxb Kav.Resources.DeferredPBREffect.fxb diff --git a/Loaders/ModelLoader.cs b/Loaders/ModelLoader.cs index 2655368..4e908a0 100644 --- a/Loaders/ModelLoader.cs +++ b/Loaders/ModelLoader.cs @@ -15,7 +15,7 @@ namespace Kav foreach (var meshPartData in meshData.MeshParts) { - var effect = new Kav.GBufferEffect( + var effect = new Kav.DeferredPBR_GBufferEffect( graphicsDevice ) { diff --git a/Renderer.cs b/Renderer.cs index 0a77640..c53f855 100644 --- a/Renderer.cs +++ b/Renderer.cs @@ -160,6 +160,14 @@ namespace Kav i++; } + i = 0; + foreach (var directionalLight in directionalLights) + { + if (i > DeferredPBREffect.MaxDirectionalLights) { break; } + DeferredPBREffect.DirectionalLights[i] = directionalLight; + i++; + } + SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, null, null, null, DeferredPBREffect); SpriteBatch.Draw(deferredRenderTarget, Vector2.Zero, Color.White); SpriteBatch.End(); diff --git a/Resources.cs b/Resources.cs index 5c50646..83d4b04 100644 --- a/Resources.cs +++ b/Resources.cs @@ -4,13 +4,13 @@ namespace Kav { internal class Resources { - public static byte[] GBufferEffect + public static byte[] DeferredPBR_GBufferEffect { get { if (gBufferEffect == null) { - gBufferEffect = GetResource("GBufferEffect"); + gBufferEffect = GetResource("DeferredPBR_GBufferEffect"); } return gBufferEffect; }