2020-12-06 03:47:01 +00:00
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
|
|
|
|
namespace Kav
|
|
|
|
{
|
|
|
|
public class DiffuseLitSpriteEffect : Effect
|
|
|
|
{
|
|
|
|
EffectParameter ambientColorParam;
|
|
|
|
|
|
|
|
EffectParameter directionalLightDirectionParam;
|
|
|
|
EffectParameter directionalLightColorParam;
|
|
|
|
|
|
|
|
EffectParameter worldParam;
|
|
|
|
EffectParameter worldViewProjectionParam;
|
|
|
|
EffectParameter worldInverseTransposeParam;
|
|
|
|
|
2020-12-07 02:52:14 +00:00
|
|
|
EffectParameter shaderIndexParam;
|
|
|
|
|
2020-12-06 03:47:01 +00:00
|
|
|
Texture2D texture;
|
2020-12-07 02:52:14 +00:00
|
|
|
Texture2D normal;
|
|
|
|
|
|
|
|
bool normalMapEnabled = false;
|
2020-12-06 03:47:01 +00:00
|
|
|
|
|
|
|
Vector3 ambientColor;
|
|
|
|
|
|
|
|
Vector3 directionalLightDirection;
|
|
|
|
Vector3 directionalLightColor;
|
|
|
|
|
|
|
|
Matrix world = Matrix.Identity;
|
|
|
|
Matrix view = Matrix.Identity;
|
|
|
|
Matrix projection = Matrix.Identity;
|
|
|
|
|
|
|
|
EffectDirtyFlags dirtyFlags = EffectDirtyFlags.All;
|
|
|
|
|
2020-12-07 02:52:14 +00:00
|
|
|
public bool NormalMapEnabled
|
2020-12-06 03:47:01 +00:00
|
|
|
{
|
2020-12-07 02:52:14 +00:00
|
|
|
get { return normalMapEnabled; }
|
2020-12-06 03:47:01 +00:00
|
|
|
set
|
|
|
|
{
|
2020-12-07 02:52:14 +00:00
|
|
|
if (normalMapEnabled != value)
|
|
|
|
{
|
|
|
|
normalMapEnabled = value;
|
2020-12-07 09:30:09 +00:00
|
|
|
dirtyFlags |= EffectDirtyFlags.PixelShaderIndex;
|
2020-12-07 02:52:14 +00:00
|
|
|
}
|
2020-12-06 03:47:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Matrix World
|
|
|
|
{
|
|
|
|
get { return world; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
world = value;
|
|
|
|
dirtyFlags |= EffectDirtyFlags.World | EffectDirtyFlags.WorldViewProj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Matrix View
|
|
|
|
{
|
|
|
|
get { return view; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
view = value;
|
|
|
|
dirtyFlags |= EffectDirtyFlags.WorldViewProj | EffectDirtyFlags.EyePosition;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Matrix Projection
|
|
|
|
{
|
|
|
|
get { return projection; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
projection = value;
|
|
|
|
dirtyFlags |= EffectDirtyFlags.WorldViewProj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int MaxPointLights { get; } = 8;
|
|
|
|
|
|
|
|
public Vector3 AmbientColor
|
|
|
|
{
|
|
|
|
get { return ambientColor; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
ambientColor = value;
|
|
|
|
ambientColorParam.SetValue(ambientColor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public PointLightCollection PointLights { get; private set; }
|
|
|
|
|
|
|
|
public Vector3 DirectionalLightDirection
|
|
|
|
{
|
|
|
|
get { return directionalLightDirection; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
directionalLightDirection = value;
|
|
|
|
directionalLightDirectionParam.SetValue(directionalLightDirection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Vector3 DirectionalLightColor
|
|
|
|
{
|
|
|
|
get { return directionalLightColor; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
directionalLightColor = value;
|
|
|
|
directionalLightColorParam.SetValue(directionalLightColor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public DiffuseLitSpriteEffect(GraphicsDevice graphicsDevice) : base(graphicsDevice, Resources.DiffuseLitSpriteEffect)
|
|
|
|
{
|
|
|
|
CacheEffectParameters();
|
|
|
|
|
|
|
|
PointLights = new PointLightCollection(
|
|
|
|
Parameters["PointLightPositions"],
|
|
|
|
Parameters["PointLightColors"],
|
|
|
|
MaxPointLights
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnApply()
|
|
|
|
{
|
|
|
|
if ((dirtyFlags & EffectDirtyFlags.World) != 0)
|
|
|
|
{
|
|
|
|
worldParam.SetValue(world);
|
|
|
|
|
|
|
|
Matrix.Invert(ref world, out Matrix worldInverse);
|
|
|
|
Matrix.Transpose(ref worldInverse, out Matrix worldInverseTranspose);
|
|
|
|
worldInverseTransposeParam.SetValue(worldInverseTranspose);
|
|
|
|
|
|
|
|
dirtyFlags &= ~EffectDirtyFlags.World;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((dirtyFlags & EffectDirtyFlags.WorldViewProj) != 0)
|
|
|
|
{
|
|
|
|
Matrix.Multiply(ref world, ref view, out Matrix worldView);
|
|
|
|
Matrix.Multiply(ref worldView, ref projection, out Matrix worldViewProj);
|
|
|
|
worldViewProjectionParam.SetValue(worldViewProj);
|
|
|
|
|
|
|
|
dirtyFlags &= ~EffectDirtyFlags.WorldViewProj;
|
|
|
|
}
|
2020-12-07 02:52:14 +00:00
|
|
|
|
2020-12-07 09:30:09 +00:00
|
|
|
if ((dirtyFlags & EffectDirtyFlags.PixelShaderIndex) != 0)
|
2020-12-07 02:52:14 +00:00
|
|
|
{
|
|
|
|
int shaderIndex = 0;
|
|
|
|
|
|
|
|
if (normalMapEnabled)
|
|
|
|
{
|
|
|
|
shaderIndex = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
shaderIndexParam.SetValue(shaderIndex);
|
|
|
|
}
|
2020-12-06 03:47:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void CacheEffectParameters()
|
|
|
|
{
|
|
|
|
worldParam = Parameters["World"];
|
|
|
|
worldViewProjectionParam = Parameters["WorldViewProjection"];
|
|
|
|
worldInverseTransposeParam = Parameters["WorldInverseTranspose"];
|
|
|
|
|
|
|
|
ambientColorParam = Parameters["AmbientColor"];
|
|
|
|
|
|
|
|
directionalLightDirectionParam = Parameters["DirectionalLightDirection"];
|
|
|
|
directionalLightColorParam = Parameters["DirectionalLightColor"];
|
2020-12-07 02:52:14 +00:00
|
|
|
|
|
|
|
shaderIndexParam = Parameters["ShaderIndex"];
|
2020-12-06 03:47:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|