add directional light + rename GBufferEffect for clarity
parent
737cb68f8d
commit
abd6b1af91
|
@ -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()
|
||||
|
|
|
@ -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()
|
|
@ -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.
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -15,8 +15,8 @@
|
|||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="Effects\FXB\GBufferEffect.fxb">
|
||||
<LogicalName>Kav.Resources.GBufferEffect.fxb</LogicalName>
|
||||
<EmbeddedResource Include="Effects\FXB\DeferredPBR_GBufferEffect.fxb">
|
||||
<LogicalName>Kav.Resources.DeferredPBR_GBufferEffect.fxb</LogicalName>
|
||||
</EmbeddedResource>
|
||||
<EmbeddedResource Include="Effects\FXB\DeferredPBREffect.fxb">
|
||||
<LogicalName>Kav.Resources.DeferredPBREffect.fxb</LogicalName>
|
||||
|
|
|
@ -15,8 +15,8 @@
|
|||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="Effects\FXB\GBufferEffect.fxb">
|
||||
<LogicalName>Kav.Resources.GBufferEffect.fxb</LogicalName>
|
||||
<EmbeddedResource Include="Effects\FXB\DeferredPBR_GBufferEffect.fxb">
|
||||
<LogicalName>Kav.Resources.DeferredPBR_GBufferEffect.fxb</LogicalName>
|
||||
</EmbeddedResource>
|
||||
<EmbeddedResource Include="Effects\FXB\DeferredPBREffect.fxb">
|
||||
<LogicalName>Kav.Resources.DeferredPBREffect.fxb</LogicalName>
|
||||
|
|
|
@ -15,7 +15,7 @@ namespace Kav
|
|||
|
||||
foreach (var meshPartData in meshData.MeshParts)
|
||||
{
|
||||
var effect = new Kav.GBufferEffect(
|
||||
var effect = new Kav.DeferredPBR_GBufferEffect(
|
||||
graphicsDevice
|
||||
)
|
||||
{
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue