add directional light + rename GBufferEffect for clarity

pull/1/head
cosmonaut 2020-08-26 17:02:45 -07:00
parent 737cb68f8d
commit abd6b1af91
13 changed files with 104 additions and 63 deletions

View File

@ -12,6 +12,7 @@ namespace Kav
EffectParameter eyePositionParam; EffectParameter eyePositionParam;
PointLightCollection pointLightCollection; PointLightCollection pointLightCollection;
DirectionalLightCollection directionalLightCollection;
public Texture2D GPosition { get; set; } public Texture2D GPosition { get; set; }
public Texture2D GAlbedo { get; set; } public Texture2D GAlbedo { get; set; }
@ -28,6 +29,14 @@ namespace Kav
private set { pointLightCollection = value; } 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) public DeferredPBREffect(GraphicsDevice graphicsDevice) : base(graphicsDevice, Resources.DeferredPBREffect)
{ {
CacheEffectParameters(); CacheEffectParameters();
@ -37,6 +46,11 @@ namespace Kav
Parameters["PointLightColors"], Parameters["PointLightColors"],
MaxPointLights MaxPointLights
); );
DirectionalLights = new DirectionalLightCollection(
Parameters["DirectionalLightDirections"],
Parameters["DirectionalLightColors"]
);
} }
protected DeferredPBREffect(DeferredPBREffect cloneSource) : base(cloneSource) protected DeferredPBREffect(DeferredPBREffect cloneSource) : base(cloneSource)
@ -58,6 +72,16 @@ namespace Kav
{ {
PointLights[i] = cloneSource.PointLights[i]; 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() public override Effect Clone()

View File

@ -3,7 +3,7 @@ using Microsoft.Xna.Framework.Graphics;
namespace Kav namespace Kav
{ {
public class GBufferEffect : Effect, TransformEffect public class DeferredPBR_GBufferEffect : Effect, TransformEffect
{ {
EffectParameter worldParam; EffectParameter worldParam;
EffectParameter worldViewProjectionParam; 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(); CacheEffectParameters();
} }
protected GBufferEffect(GBufferEffect cloneSource) : base(cloneSource) protected DeferredPBR_GBufferEffect(DeferredPBR_GBufferEffect cloneSource) : base(cloneSource)
{ {
CacheEffectParameters(); CacheEffectParameters();
@ -150,7 +150,7 @@ namespace Kav
public override Effect Clone() public override Effect Clone()
{ {
return new GBufferEffect(this); return new DeferredPBR_GBufferEffect(this);
} }
protected override void OnApply() protected override void OnApply()

View File

@ -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);
}
}
}
}

Binary file not shown.

Binary file not shown.

View File

@ -2,6 +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;
DECLARE_TEXTURE(gPosition, 0); DECLARE_TEXTURE(gPosition, 0);
DECLARE_TEXTURE(gAlbedo, 1); DECLARE_TEXTURE(gAlbedo, 1);
@ -10,10 +11,13 @@ DECLARE_TEXTURE(gMetallicRoughness, 3);
BEGIN_CONSTANTS BEGIN_CONSTANTS
float3 EyePosition _ps(c0) _cb(c0); float3 EyePosition _ps(c0) _cb(c0);
float3 PointLightPositions[MAX_POINT_LIGHTS] _ps(c1) _cb(c1); float3 PointLightPositions[MAX_POINT_LIGHTS] _ps(c1) _cb(c1);
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 DirectionalLightColors[MAX_DIRECTIONAL_LIGHTS] _ps(c133) _cb(c133);
MATRIX_CONSTANTS MATRIX_CONSTANTS
@ -123,14 +127,14 @@ float4 ComputeColor(
Lo += ComputeLight(lightDir, radiance, F0, V, N, albedo, metallic, roughness); Lo += ComputeLight(lightDir, radiance, F0, V, N, albedo, metallic, roughness);
} }
// // directional light // directional light
// for (int i = 0; i < 4; i++) for (int i = 0; i < MAX_DIRECTIONAL_LIGHTS; i++)
// { {
// float3 lightDir = LightDirections[i]; float3 lightDir = DirectionalLightDirections[i];
// float3 radiance = DirectionLightColors[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 ambient = float3(0.03, 0.03, 0.03) * albedo; // * AO;
float3 color = ambient + Lo; float3 color = ambient + Lo;

View File

@ -3,48 +3,6 @@ using Microsoft.Xna.Framework.Graphics;
namespace Kav 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 public class PBREffect : Effect, TransformEffect, PointLightEffect, DirectionalLightEffect
{ {
EffectParameter worldParam; EffectParameter worldParam;

View File

@ -15,8 +15,8 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<EmbeddedResource Include="Effects\FXB\GBufferEffect.fxb"> <EmbeddedResource Include="Effects\FXB\DeferredPBR_GBufferEffect.fxb">
<LogicalName>Kav.Resources.GBufferEffect.fxb</LogicalName> <LogicalName>Kav.Resources.DeferredPBR_GBufferEffect.fxb</LogicalName>
</EmbeddedResource> </EmbeddedResource>
<EmbeddedResource Include="Effects\FXB\DeferredPBREffect.fxb"> <EmbeddedResource Include="Effects\FXB\DeferredPBREffect.fxb">
<LogicalName>Kav.Resources.DeferredPBREffect.fxb</LogicalName> <LogicalName>Kav.Resources.DeferredPBREffect.fxb</LogicalName>

View File

@ -15,8 +15,8 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<EmbeddedResource Include="Effects\FXB\GBufferEffect.fxb"> <EmbeddedResource Include="Effects\FXB\DeferredPBR_GBufferEffect.fxb">
<LogicalName>Kav.Resources.GBufferEffect.fxb</LogicalName> <LogicalName>Kav.Resources.DeferredPBR_GBufferEffect.fxb</LogicalName>
</EmbeddedResource> </EmbeddedResource>
<EmbeddedResource Include="Effects\FXB\DeferredPBREffect.fxb"> <EmbeddedResource Include="Effects\FXB\DeferredPBREffect.fxb">
<LogicalName>Kav.Resources.DeferredPBREffect.fxb</LogicalName> <LogicalName>Kav.Resources.DeferredPBREffect.fxb</LogicalName>

View File

@ -15,7 +15,7 @@ namespace Kav
foreach (var meshPartData in meshData.MeshParts) foreach (var meshPartData in meshData.MeshParts)
{ {
var effect = new Kav.GBufferEffect( var effect = new Kav.DeferredPBR_GBufferEffect(
graphicsDevice graphicsDevice
) )
{ {

View File

@ -160,6 +160,14 @@ namespace Kav
i++; 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.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, null, null, null, DeferredPBREffect);
SpriteBatch.Draw(deferredRenderTarget, Vector2.Zero, Color.White); SpriteBatch.Draw(deferredRenderTarget, Vector2.Zero, Color.White);
SpriteBatch.End(); SpriteBatch.End();

View File

@ -4,13 +4,13 @@ namespace Kav
{ {
internal class Resources internal class Resources
{ {
public static byte[] GBufferEffect public static byte[] DeferredPBR_GBufferEffect
{ {
get get
{ {
if (gBufferEffect == null) if (gBufferEffect == null)
{ {
gBufferEffect = GetResource("GBufferEffect"); gBufferEffect = GetResource("DeferredPBR_GBufferEffect");
} }
return gBufferEffect; return gBufferEffect;
} }