Kav/Effects/LinearDepthEffect.cs

91 lines
2.5 KiB
C#
Raw Normal View History

2020-10-19 10:01:37 +00:00
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Kav
{
2020-12-08 04:10:27 +00:00
public class LinearDepthEffect : Effect, IHasWorldMatrix
2020-10-19 10:01:37 +00:00
{
2020-12-08 04:10:27 +00:00
EffectParameter worldParam;
EffectParameter worldViewProjectionParam;
2020-10-19 10:01:37 +00:00
EffectParameter lightPositionParam;
EffectParameter farPlaneParam;
EffectDirtyFlags dirtyFlags = EffectDirtyFlags.All;
2020-12-08 04:10:27 +00:00
Matrix world;
2020-10-19 10:01:37 +00:00
Matrix view;
Matrix projection;
public Vector3 LightPosition { get; set; }
public float FarPlane { get; set; }
2020-12-08 04:10:27 +00:00
public Matrix World
2020-10-19 10:01:37 +00:00
{
2020-12-08 04:10:27 +00:00
get { return world; }
2020-10-19 10:01:37 +00:00
set
{
2020-12-08 04:10:27 +00:00
world = value;
2020-10-19 10:01:37 +00:00
dirtyFlags |= EffectDirtyFlags.World;
dirtyFlags |= EffectDirtyFlags.WorldViewProj;
}
}
public Matrix View
{
get { return view; }
set
{
view = value;
dirtyFlags |= EffectDirtyFlags.WorldViewProj;
}
}
public Matrix Projection
{
get { return projection; }
set
{
projection = value;
dirtyFlags |= EffectDirtyFlags.WorldViewProj;
}
}
public LinearDepthEffect(GraphicsDevice graphicsDevice) : base(graphicsDevice, Resources.LinearDepthEffect)
{
CacheEffectParameters();
}
protected override void OnApply()
{
if ((dirtyFlags & EffectDirtyFlags.WorldViewProj) != 0)
{
Matrix.Multiply(ref view, ref projection, out Matrix viewProjection);
2020-12-08 04:10:27 +00:00
Matrix.Multiply(ref world, ref viewProjection, out Matrix worldViewProj);
2020-10-19 10:01:37 +00:00
2020-12-08 04:10:27 +00:00
worldViewProjectionParam.SetValue(worldViewProj);
2020-10-19 10:01:37 +00:00
dirtyFlags &= ~EffectDirtyFlags.WorldViewProj;
}
if ((dirtyFlags & EffectDirtyFlags.World) != 0)
{
2020-12-08 04:10:27 +00:00
worldParam.SetValue(world);
2020-10-19 10:01:37 +00:00
dirtyFlags &= ~EffectDirtyFlags.World;
}
lightPositionParam.SetValue(LightPosition);
farPlaneParam.SetValue(FarPlane);
}
private void CacheEffectParameters()
{
2020-12-08 04:10:27 +00:00
worldParam = Parameters["Model"];
worldViewProjectionParam = Parameters["ModelViewProjection"];
2020-10-19 10:01:37 +00:00
lightPositionParam = Parameters["LightPosition"];
farPlaneParam = Parameters["FarPlane"];
}
}
}