2011-11-16 00:00:17 +00:00
|
|
|
using System;
|
2011-11-23 10:13:38 +00:00
|
|
|
using ANX.Framework.NonXNA;
|
2012-09-05 19:50:10 +00:00
|
|
|
using ANX.Framework.NonXNA.Development;
|
2011-11-16 00:00:17 +00:00
|
|
|
|
2012-08-09 09:45:04 +00:00
|
|
|
// This file is part of the ANX.Framework created by the
|
|
|
|
// "ANX.Framework developer group" and released under the Ms-PL license.
|
|
|
|
// For details see: http://anxframework.codeplex.com/license
|
2011-11-16 00:00:17 +00:00
|
|
|
|
|
|
|
namespace ANX.Framework.Graphics
|
|
|
|
{
|
2012-09-05 19:50:10 +00:00
|
|
|
[PercentageComplete(100)]
|
2012-09-09 07:55:45 +00:00
|
|
|
[TestState(TestStateAttribute.TestState.Tested)]
|
2012-09-05 19:50:10 +00:00
|
|
|
[Developer("AstrorEnales")]
|
2011-11-16 00:00:17 +00:00
|
|
|
public class DualTextureEffect : Effect, IEffectMatrices, IEffectFog
|
2012-09-05 19:50:10 +00:00
|
|
|
{
|
|
|
|
#region Private
|
|
|
|
private Matrix world;
|
|
|
|
private Matrix view;
|
|
|
|
private Matrix projection;
|
|
|
|
private Vector3 fogColor;
|
|
|
|
private Vector3 diffuseColor;
|
|
|
|
private bool isVertexColorEnabled;
|
|
|
|
private bool isFogEnabled;
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Public
|
|
|
|
public Matrix Projection
|
|
|
|
{
|
|
|
|
get { return projection; }
|
|
|
|
set { projection = value; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public Matrix View
|
|
|
|
{
|
|
|
|
get { return view; }
|
|
|
|
set { view = value; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public Matrix World
|
|
|
|
{
|
|
|
|
get { return world; }
|
|
|
|
set { world = value; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public Vector3 FogColor
|
|
|
|
{
|
|
|
|
get { return fogColor; }
|
|
|
|
set { fogColor = value; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public float Alpha { get; set; }
|
|
|
|
public float FogEnd { get; set; }
|
|
|
|
public float FogStart { get; set; }
|
|
|
|
public Texture2D Texture { get; set; }
|
|
|
|
public Texture2D Texture2 { get; set; }
|
|
|
|
|
|
|
|
public Vector3 DiffuseColor
|
|
|
|
{
|
|
|
|
get { return diffuseColor; }
|
|
|
|
set { diffuseColor = value; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool FogEnabled
|
|
|
|
{
|
|
|
|
get { return isFogEnabled; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
isFogEnabled = value;
|
|
|
|
SelectTechnique();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool VertexColorEnabled
|
|
|
|
{
|
|
|
|
get { return isVertexColorEnabled; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
isVertexColorEnabled = value;
|
|
|
|
SelectTechnique();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Constructor
|
|
|
|
public DualTextureEffect(GraphicsDevice graphics)
|
2012-09-06 09:58:13 +00:00
|
|
|
: base(graphics, GetByteCode(), GetSourceLanguage())
|
2011-11-16 00:00:17 +00:00
|
|
|
{
|
2012-09-05 19:50:10 +00:00
|
|
|
diffuseColor = Vector3.One;
|
|
|
|
Alpha = 1f;
|
|
|
|
FogStart = 0f;
|
|
|
|
FogEnd = 1f;
|
|
|
|
FogEnabled = false;
|
|
|
|
VertexColorEnabled = false;
|
|
|
|
world = Matrix.Identity;
|
|
|
|
view = Matrix.Identity;
|
|
|
|
projection = Matrix.Identity;
|
2011-11-16 00:00:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected DualTextureEffect(DualTextureEffect cloneSource)
|
|
|
|
: base(cloneSource)
|
2012-09-05 19:50:10 +00:00
|
|
|
{
|
|
|
|
diffuseColor = cloneSource.diffuseColor;
|
|
|
|
Alpha = cloneSource.Alpha;
|
|
|
|
FogStart = cloneSource.FogStart;
|
|
|
|
FogEnd = cloneSource.FogEnd;
|
|
|
|
FogEnabled = cloneSource.FogEnabled;
|
|
|
|
VertexColorEnabled = cloneSource.VertexColorEnabled;
|
|
|
|
world = cloneSource.world;
|
|
|
|
view = cloneSource.view;
|
|
|
|
projection = cloneSource.projection;
|
|
|
|
Texture = cloneSource.Texture;
|
|
|
|
Texture2 = cloneSource.Texture2;
|
2011-11-16 00:00:17 +00:00
|
|
|
}
|
2012-09-05 19:50:10 +00:00
|
|
|
#endregion
|
2011-11-16 00:00:17 +00:00
|
|
|
|
2012-09-06 09:58:13 +00:00
|
|
|
#region GetByteCode
|
|
|
|
private static byte[] GetByteCode()
|
|
|
|
{
|
|
|
|
var creator = AddInSystemFactory.Instance.GetDefaultCreator<IRenderSystemCreator>();
|
|
|
|
return creator.GetShaderByteCode(PreDefinedShader.DualTextureEffect);
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region GetSourceLanguage
|
|
|
|
private static EffectSourceLanguage GetSourceLanguage()
|
|
|
|
{
|
|
|
|
var creator = AddInSystemFactory.Instance.GetDefaultCreator<IRenderSystemCreator>();
|
|
|
|
return creator.GetStockShaderSourceLanguage;
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
2012-09-05 19:50:10 +00:00
|
|
|
#region Clone
|
|
|
|
public override Effect Clone()
|
2011-11-16 00:00:17 +00:00
|
|
|
{
|
|
|
|
return new DualTextureEffect(this);
|
2012-09-05 19:50:10 +00:00
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region PreBindSetParameters
|
|
|
|
internal override void PreBindSetParameters()
|
|
|
|
{
|
|
|
|
Matrix worldView;
|
|
|
|
Matrix.Multiply(ref world, ref view, out worldView);
|
|
|
|
Matrix wvp;
|
|
|
|
Matrix.Multiply(ref worldView, ref projection, out wvp);
|
|
|
|
Parameters["WorldViewProj"].SetValue(wvp);
|
|
|
|
|
|
|
|
Parameters["DiffuseColor"].SetValue(new Vector4(diffuseColor * Alpha, Alpha));
|
|
|
|
Parameters["FogColor"].SetValue(fogColor);
|
|
|
|
|
|
|
|
if (Texture != null)
|
|
|
|
Parameters["Texture"].SetValue(Texture);
|
|
|
|
if (Texture2 != null)
|
|
|
|
Parameters["Texture2"].SetValue(Texture2);
|
|
|
|
|
2012-09-09 07:55:45 +00:00
|
|
|
if (isFogEnabled)
|
|
|
|
SetFogVector(ref worldView);
|
|
|
|
else
|
|
|
|
Parameters["FogVector"].SetValue(Vector4.Zero);
|
2012-09-05 19:50:10 +00:00
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region SelectTechnique
|
|
|
|
private void SelectTechnique()
|
|
|
|
{
|
|
|
|
if (isVertexColorEnabled)
|
|
|
|
{
|
|
|
|
if (isFogEnabled)
|
|
|
|
CurrentTechnique = Techniques["DualTextureEffectVertexColor"];
|
|
|
|
if (isFogEnabled == false)
|
|
|
|
CurrentTechnique = Techniques["DualTextureEffectNoFogVertexColor"];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (isFogEnabled)
|
|
|
|
CurrentTechnique = Techniques["DualTextureEffect"];
|
|
|
|
if (isFogEnabled == false)
|
|
|
|
CurrentTechnique = Techniques["DualTextureEffectNoFog"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
2012-09-09 07:55:45 +00:00
|
|
|
|
|
|
|
#region SetFogVector
|
|
|
|
private void SetFogVector(ref Matrix worldView)
|
|
|
|
{
|
|
|
|
if (FogStart == FogEnd)
|
|
|
|
{
|
|
|
|
Parameters["FogVector"].SetValue(new Vector4(0f, 0f, 0f, 1f));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
float fogFactor = 1f / (FogStart - FogEnd);
|
|
|
|
Vector4 value;
|
|
|
|
value.X = worldView.M13 * fogFactor;
|
|
|
|
value.Y = worldView.M23 * fogFactor;
|
|
|
|
value.Z = worldView.M33 * fogFactor;
|
|
|
|
value.W = (worldView.M43 + FogStart) * fogFactor;
|
|
|
|
Parameters["FogVector"].SetValue(value);
|
|
|
|
}
|
|
|
|
#endregion
|
2012-09-05 19:50:10 +00:00
|
|
|
}
|
2011-11-16 00:00:17 +00:00
|
|
|
}
|