Kav/Effects/SimpleDepthEffect.cs

105 lines
2.8 KiB
C#
Raw Normal View History

2020-08-07 00:58:50 +00:00
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Kav
{
2020-12-08 04:10:27 +00:00
public class SimpleDepthEffect : Effect, IHasWorldMatrix
2020-08-07 00:58:50 +00:00
{
2020-12-08 04:10:27 +00:00
EffectParameter worldParam;
EffectParameter viewProjectionParam;
EffectParameter vertexShaderIndexParam;
2020-08-07 00:58:50 +00:00
2020-12-08 04:10:27 +00:00
Matrix world;
2020-08-07 00:58:50 +00:00
Matrix view;
Matrix projection;
2020-12-08 04:10:27 +00:00
bool hardwareInstancingEnabled = false;
2020-08-07 00:58:50 +00:00
EffectDirtyFlags dirtyFlags = EffectDirtyFlags.All;
2020-12-08 04:10:27 +00:00
public Matrix World
2020-08-07 00:58:50 +00:00
{
2020-12-08 04:10:27 +00:00
get { return world; }
2020-08-07 00:58:50 +00:00
set
{
2020-12-08 04:10:27 +00:00
world = value;
dirtyFlags |= EffectDirtyFlags.World;
2020-08-07 00:58:50 +00:00
}
}
public Matrix View
{
get { return view; }
set
{
view = value;
2020-12-08 04:10:27 +00:00
dirtyFlags |= EffectDirtyFlags.ViewProj;
2020-08-07 00:58:50 +00:00
}
}
public Matrix Projection
{
get { return projection; }
set
{
projection = value;
2020-12-08 04:10:27 +00:00
dirtyFlags |= EffectDirtyFlags.ViewProj;
}
}
public bool HardwareInstancingEnabled
{
get { return hardwareInstancingEnabled; }
set
{
if (value != hardwareInstancingEnabled)
{
hardwareInstancingEnabled = value;
dirtyFlags |= EffectDirtyFlags.VertexShaderIndex;
}
2020-08-07 00:58:50 +00:00
}
}
2020-08-07 00:58:50 +00:00
public SimpleDepthEffect(GraphicsDevice graphicsDevice) : base(graphicsDevice, Resources.SimpleDepthEffect)
{
CacheEffectParameters();
}
protected override void OnApply()
{
2020-12-08 04:10:27 +00:00
if ((dirtyFlags & EffectDirtyFlags.World) != 0)
{
worldParam.SetValue(world);
dirtyFlags &= ~EffectDirtyFlags.World;
}
if ((dirtyFlags & EffectDirtyFlags.ViewProj) != 0)
2020-08-07 00:58:50 +00:00
{
Matrix.Multiply(ref view, ref projection, out Matrix viewProjection);
2020-12-08 04:10:27 +00:00
viewProjectionParam.SetValue(viewProjection);
dirtyFlags &= ~EffectDirtyFlags.ViewProj;
}
if ((dirtyFlags & EffectDirtyFlags.VertexShaderIndex) != 0)
{
int vertexShaderIndex = 0;
2020-08-07 00:58:50 +00:00
2020-12-08 04:10:27 +00:00
if (hardwareInstancingEnabled)
{
vertexShaderIndex = 1;
}
2020-08-07 00:58:50 +00:00
2020-12-08 04:10:27 +00:00
vertexShaderIndexParam.SetValue(vertexShaderIndex);
2020-08-07 00:58:50 +00:00
}
}
private void CacheEffectParameters()
{
2020-12-08 04:10:27 +00:00
worldParam = Parameters["World"];
viewProjectionParam = Parameters["ViewProjection"];
vertexShaderIndexParam = Parameters["VertexShaderIndex"];
2020-08-07 00:58:50 +00:00
}
}
}