155 lines
4.4 KiB
C#
155 lines
4.4 KiB
C#
using Microsoft.Xna.Framework;
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
namespace Kav
|
|
{
|
|
public class DiffuseLitSpriteEffect : Effect
|
|
{
|
|
EffectParameter textureParam;
|
|
|
|
EffectParameter ambientColorParam;
|
|
|
|
EffectParameter directionalLightDirectionParam;
|
|
EffectParameter directionalLightColorParam;
|
|
|
|
EffectParameter worldParam;
|
|
EffectParameter worldViewProjectionParam;
|
|
EffectParameter worldInverseTransposeParam;
|
|
|
|
Texture2D texture;
|
|
|
|
Vector3 ambientColor;
|
|
|
|
Vector3 directionalLightDirection;
|
|
Vector3 directionalLightColor;
|
|
|
|
Matrix world = Matrix.Identity;
|
|
Matrix view = Matrix.Identity;
|
|
Matrix projection = Matrix.Identity;
|
|
|
|
EffectDirtyFlags dirtyFlags = EffectDirtyFlags.All;
|
|
|
|
public Texture2D Texture
|
|
{
|
|
get { return texture; }
|
|
set
|
|
{
|
|
texture = value;
|
|
textureParam.SetValue(texture);
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
private void CacheEffectParameters()
|
|
{
|
|
textureParam = Parameters["Texture"];
|
|
|
|
worldParam = Parameters["World"];
|
|
worldViewProjectionParam = Parameters["WorldViewProjection"];
|
|
worldInverseTransposeParam = Parameters["WorldInverseTranspose"];
|
|
|
|
ambientColorParam = Parameters["AmbientColor"];
|
|
|
|
directionalLightDirectionParam = Parameters["DirectionalLightDirection"];
|
|
directionalLightColorParam = Parameters["DirectionalLightColor"];
|
|
}
|
|
}
|
|
}
|