2020-08-27 18:14:17 +00:00
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
|
|
|
|
namespace Kav
|
|
|
|
{
|
|
|
|
public class DeferredPBREffect : Effect
|
|
|
|
{
|
|
|
|
EffectParameter gPositionParam;
|
|
|
|
EffectParameter gAlbedoParam;
|
|
|
|
EffectParameter gNormalParam;
|
|
|
|
EffectParameter gMetallicRoughnessParam;
|
|
|
|
|
2020-10-01 19:46:25 +00:00
|
|
|
EffectParameter shadowMapOneParam;
|
|
|
|
EffectParameter shadowMapTwoParam;
|
|
|
|
EffectParameter shadowMapThreeParam;
|
|
|
|
EffectParameter shadowMapFourParam;
|
|
|
|
|
|
|
|
EffectParameter lightSpaceMatrixOneParam;
|
|
|
|
EffectParameter lightSpaceMatrixTwoParam;
|
|
|
|
EffectParameter lightSpaceMatrixThreeParam;
|
|
|
|
EffectParameter lightSpaceMatrixFourParam;
|
|
|
|
|
|
|
|
EffectParameter viewMatrixParam;
|
|
|
|
EffectParameter cascadeFarPlanesParam;
|
|
|
|
|
|
|
|
EffectParameter directionalLightColorParam;
|
|
|
|
EffectParameter directionalLightDirectionParam;
|
|
|
|
|
2020-08-27 18:14:17 +00:00
|
|
|
EffectParameter eyePositionParam;
|
2020-10-01 19:46:25 +00:00
|
|
|
|
2020-08-27 18:14:17 +00:00
|
|
|
PointLightCollection pointLightCollection;
|
|
|
|
|
|
|
|
public Texture2D GPosition { get; set; }
|
|
|
|
public Texture2D GAlbedo { get; set; }
|
|
|
|
public Texture2D GNormal { get; set; }
|
|
|
|
public Texture2D GMetallicRoughness { get; set; }
|
|
|
|
|
2020-10-01 19:46:25 +00:00
|
|
|
public Texture2D ShadowMapOne { get; set; }
|
|
|
|
public Texture2D ShadowMapTwo { get; set; }
|
|
|
|
public Texture2D ShadowMapThree { get; set; }
|
|
|
|
public Texture2D ShadowMapFour { get; set; }
|
|
|
|
|
|
|
|
public Matrix LightSpaceMatrixOne { get; set; }
|
|
|
|
public Matrix LightSpaceMatrixTwo { get; set; }
|
|
|
|
public Matrix LightSpaceMatrixThree { get; set; }
|
|
|
|
public Matrix LightSpaceMatrixFour { get; set; }
|
|
|
|
|
|
|
|
public Matrix ViewMatrix { get; set; }
|
|
|
|
public readonly float[] CascadeFarPlanes;
|
|
|
|
|
|
|
|
public Vector3 DirectionalLightColor { get; set; }
|
|
|
|
public Vector3 DirectionalLightDirection { get; set; }
|
|
|
|
|
2020-08-27 18:14:17 +00:00
|
|
|
public Vector3 EyePosition { get; set; }
|
|
|
|
|
|
|
|
public int MaxPointLights { get; } = 64;
|
|
|
|
|
|
|
|
public PointLightCollection PointLights
|
|
|
|
{
|
|
|
|
get { return pointLightCollection; }
|
|
|
|
private set { pointLightCollection = value; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public DeferredPBREffect(GraphicsDevice graphicsDevice) : base(graphicsDevice, Resources.DeferredPBREffect)
|
|
|
|
{
|
2020-10-01 19:46:25 +00:00
|
|
|
CascadeFarPlanes = new float[4];
|
|
|
|
|
2020-08-27 18:14:17 +00:00
|
|
|
CacheEffectParameters();
|
|
|
|
|
|
|
|
pointLightCollection = new PointLightCollection(
|
|
|
|
Parameters["PointLightPositions"],
|
|
|
|
Parameters["PointLightColors"],
|
|
|
|
MaxPointLights
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected DeferredPBREffect(DeferredPBREffect cloneSource) : base(cloneSource)
|
|
|
|
{
|
|
|
|
GPosition = cloneSource.GPosition;
|
|
|
|
GAlbedo = cloneSource.GAlbedo;
|
|
|
|
GNormal = cloneSource.GNormal;
|
|
|
|
GMetallicRoughness = cloneSource.GMetallicRoughness;
|
|
|
|
|
|
|
|
EyePosition = cloneSource.EyePosition;
|
|
|
|
|
|
|
|
PointLights = new PointLightCollection(
|
|
|
|
Parameters["LightPositions"],
|
|
|
|
Parameters["PositionLightColors"],
|
|
|
|
MaxPointLights
|
|
|
|
);
|
|
|
|
|
|
|
|
for (int i = 0; i < MaxPointLights; i++)
|
|
|
|
{
|
|
|
|
PointLights[i] = cloneSource.PointLights[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override Effect Clone()
|
|
|
|
{
|
|
|
|
return new DeferredPBREffect(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnApply()
|
|
|
|
{
|
|
|
|
gPositionParam.SetValue(GPosition);
|
|
|
|
gAlbedoParam.SetValue(GAlbedo);
|
|
|
|
gNormalParam.SetValue(GNormal);
|
|
|
|
gMetallicRoughnessParam.SetValue(GMetallicRoughness);
|
|
|
|
|
2020-10-01 19:46:25 +00:00
|
|
|
shadowMapOneParam.SetValue(ShadowMapOne);
|
|
|
|
shadowMapTwoParam.SetValue(ShadowMapTwo);
|
|
|
|
shadowMapThreeParam.SetValue(ShadowMapThree);
|
|
|
|
shadowMapFourParam.SetValue(ShadowMapFour);
|
|
|
|
|
|
|
|
lightSpaceMatrixOneParam.SetValue(LightSpaceMatrixOne);
|
|
|
|
lightSpaceMatrixTwoParam.SetValue(LightSpaceMatrixTwo);
|
|
|
|
lightSpaceMatrixThreeParam.SetValue(LightSpaceMatrixThree);
|
|
|
|
lightSpaceMatrixFourParam.SetValue(LightSpaceMatrixFour);
|
|
|
|
|
|
|
|
viewMatrixParam.SetValue(ViewMatrix);
|
|
|
|
cascadeFarPlanesParam.SetValue(CascadeFarPlanes);
|
|
|
|
|
|
|
|
directionalLightColorParam.SetValue(DirectionalLightColor);
|
|
|
|
directionalLightDirectionParam.SetValue(DirectionalLightDirection);
|
|
|
|
|
2020-08-27 18:14:17 +00:00
|
|
|
eyePositionParam.SetValue(EyePosition);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheEffectParameters()
|
|
|
|
{
|
2020-10-01 19:46:25 +00:00
|
|
|
gPositionParam = Parameters["gPosition"];
|
|
|
|
gAlbedoParam = Parameters["gAlbedo"];
|
|
|
|
gNormalParam = Parameters["gNormal"];
|
|
|
|
gMetallicRoughnessParam = Parameters["gMetallicRoughness"];
|
|
|
|
|
|
|
|
shadowMapOneParam = Parameters["shadowMapOne"];
|
|
|
|
shadowMapTwoParam = Parameters["shadowMapTwo"];
|
|
|
|
shadowMapThreeParam = Parameters["shadowMapThree"];
|
|
|
|
shadowMapFourParam = Parameters["shadowMapFour"];
|
|
|
|
|
|
|
|
lightSpaceMatrixOneParam = Parameters["LightSpaceMatrixOne"];
|
|
|
|
lightSpaceMatrixTwoParam = Parameters["LightSpaceMatrixTwo"];
|
|
|
|
lightSpaceMatrixThreeParam = Parameters["LightSpaceMatrixThree"];
|
|
|
|
lightSpaceMatrixFourParam = Parameters["LightSpaceMatrixFour"];
|
|
|
|
|
|
|
|
viewMatrixParam = Parameters["ViewMatrix"];
|
|
|
|
cascadeFarPlanesParam = Parameters["CascadeFarPlanes"];
|
|
|
|
|
|
|
|
directionalLightDirectionParam = Parameters["DirectionalLightDirection"];
|
|
|
|
directionalLightColorParam = Parameters["DirectionalLightColor"];
|
2020-08-27 18:14:17 +00:00
|
|
|
|
2020-10-01 19:46:25 +00:00
|
|
|
eyePositionParam = Parameters["EyePosition"];
|
2020-08-27 18:14:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|