mirror of
https://github.com/AlexMacocian/MonoGame.Extended.git
synced 2026-07-25 08:22:15 +00:00
committed by
Dylan Wilson
parent
df61d36661
commit
3a164d5385
@@ -1,74 +0,0 @@
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
|
||||
namespace MonoGame.Extended.Graphics.Effects
|
||||
{
|
||||
/// <summary>
|
||||
/// An <see cref="Effect" /> that allows 2D objects, within a 3D context, to be represented on a 2D monitor.
|
||||
/// </summary>
|
||||
/// <seealso cref="MatrixChainEffect" />
|
||||
/// <seealso cref="ITexture2DEffect" />
|
||||
public class DefaultEffect2D : MatrixChainEffect, ITexture2DEffect
|
||||
{
|
||||
private Texture2D _texture;
|
||||
private bool _textureIsDirty;
|
||||
private EffectParameter _textureParameter;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DefaultEffect2D" /> class.
|
||||
/// </summary>
|
||||
/// <param name="graphicsDevice">The graphics device.</param>
|
||||
public DefaultEffect2D(GraphicsDevice graphicsDevice)
|
||||
: base(graphicsDevice, EffectResource.DefaultEffect2D.Bytecode)
|
||||
{
|
||||
CacheEffectParameters();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DefaultEffect2D" /> class.
|
||||
/// </summary>
|
||||
/// <param name="cloneSource">The clone source.</param>
|
||||
public DefaultEffect2D(Effect cloneSource)
|
||||
: base(cloneSource)
|
||||
{
|
||||
CacheEffectParameters();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the <see cref="Texture2D" />.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The <see cref="Texture2D" />.
|
||||
/// </value>
|
||||
public Texture2D Texture
|
||||
{
|
||||
get { return _texture; }
|
||||
set
|
||||
{
|
||||
_texture = value;
|
||||
_textureIsDirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void CacheEffectParameters()
|
||||
{
|
||||
_textureParameter = Parameters["Texture"];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Computes derived parameter values immediately before applying the effect.
|
||||
/// </summary>
|
||||
protected override bool OnApply()
|
||||
{
|
||||
var result = base.OnApply();
|
||||
|
||||
// ReSharper disable once InvertIf
|
||||
if (_textureIsDirty)
|
||||
{
|
||||
_textureParameter.SetValue(_texture);
|
||||
_textureIsDirty = false;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
float4x4 WorldViewProjection;
|
||||
Texture2D Texture : register(t0);
|
||||
sampler TextureSampler : register(s0)
|
||||
{
|
||||
Texture = (Texture);
|
||||
};
|
||||
|
||||
struct VertexShaderInput
|
||||
{
|
||||
float4 Position : POSITION0;
|
||||
float4 Color : COLOR0;
|
||||
float2 TexureCoordinate : TEXCOORD0;
|
||||
};
|
||||
|
||||
struct VertexShaderOutput
|
||||
{
|
||||
float4 Position : SV_Position;
|
||||
float4 Color : COLOR0;
|
||||
float2 TexureCoordinate : TEXCOORD0;
|
||||
};
|
||||
|
||||
struct PixelShaderOutput
|
||||
{
|
||||
float4 Color : COLOR0;
|
||||
};
|
||||
|
||||
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
|
||||
{
|
||||
VertexShaderOutput output;
|
||||
output.Position = mul(input.Position, WorldViewProjection);
|
||||
output.Color = input.Color;
|
||||
output.TexureCoordinate = input.TexureCoordinate;
|
||||
return output;
|
||||
}
|
||||
|
||||
PixelShaderOutput PixelShaderFunction(VertexShaderOutput input)
|
||||
{
|
||||
PixelShaderOutput output;
|
||||
output.Color = tex2D(TextureSampler, input.TexureCoordinate) * input.Color;
|
||||
return output;
|
||||
}
|
||||
|
||||
technique
|
||||
{
|
||||
pass
|
||||
{
|
||||
VertexShader = compile vs_2_0 VertexShaderFunction();
|
||||
PixelShader = compile ps_2_0 PixelShaderFunction();
|
||||
}
|
||||
}
|
||||
Binary file not shown.
@@ -1,6 +0,0 @@
|
||||
:: Can only use 2MGFX.exe on Windows...
|
||||
SET PATH=%PATH%;C:\Program Files (x86)\MSBuild\MonoGame\v3.0\Tools
|
||||
|
||||
2MGFX.exe %cd%\DefaultEffect2D.fx %cd%\DefaultEffect2D.mgfxo
|
||||
|
||||
pause
|
||||
+11
-10
@@ -24,6 +24,16 @@ namespace MonoGame.Extended.Graphics
|
||||
private Matrix? _viewMatrix;
|
||||
private Matrix? _projectionMatrix;
|
||||
|
||||
/// <summary>
|
||||
/// The array of <see cref="TDrawCallInfo" /> structs currently enqueued.
|
||||
/// </summary>
|
||||
protected TDrawCallInfo[] DrawCalls;
|
||||
|
||||
/// <summary>
|
||||
/// The number of <see cref="TDrawCallInfo" /> structs currently enqueued.
|
||||
/// </summary>
|
||||
protected int EnqueuedDrawCallCount;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="GraphicsDevice" /> associated with this <see cref="Batcher{TDrawCallInfo}" />.
|
||||
/// </summary>
|
||||
@@ -41,16 +51,6 @@ namespace MonoGame.Extended.Graphics
|
||||
/// </value>
|
||||
public bool HasBegun { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the number of <see cref="TDrawCallInfo" /> structs currently enqueued.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The number of <see cref="TDrawCallInfo" /> structs currently enqueued.
|
||||
/// </value>
|
||||
protected int EnqueuedDrawCallCount { get; set; }
|
||||
|
||||
protected TDrawCallInfo[] DrawCalls { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Batcher{TDrawCallInfo}" /> class.
|
||||
/// </summary>
|
||||
@@ -285,6 +285,7 @@ namespace MonoGame.Extended.Graphics
|
||||
var oldRasterizerState = GraphicsDevice.RasterizerState;
|
||||
|
||||
GraphicsDevice.BlendState = _blendState;
|
||||
|
||||
GraphicsDevice.SamplerStates[0] = _samplerState;
|
||||
GraphicsDevice.DepthStencilState = _depthStencilState;
|
||||
GraphicsDevice.RasterizerState = _rasterizerState;
|
||||
+14
-8
@@ -22,6 +22,7 @@ namespace MonoGame.Extended.Graphics
|
||||
/// </remarks>
|
||||
public sealed class Batcher2D : Batcher<Batcher2D.DrawCallInfo>
|
||||
{
|
||||
|
||||
internal const int DefaultMaximumVerticesCount = 8192;
|
||||
internal const int DefaultMaximumIndicesCount = 12288;
|
||||
|
||||
@@ -62,7 +63,12 @@ namespace MonoGame.Extended.Graphics
|
||||
int maximumDrawCallsCount = DefaultBatchMaximumDrawCallsCount)
|
||||
: base(
|
||||
graphicsDevice,
|
||||
new DefaultEffect2D(graphicsDevice), maximumDrawCallsCount)
|
||||
new DefaultEffect(graphicsDevice)
|
||||
{
|
||||
TextureEnabled = true,
|
||||
VertexColorEnabled = true
|
||||
}, maximumDrawCallsCount)
|
||||
|
||||
{
|
||||
_vertices = new VertexPositionColorTexture[maximumVerticesCount];
|
||||
_vertexBuffer = new DynamicVertexBuffer(graphicsDevice, VertexPositionColorTexture.VertexDeclaration, maximumVerticesCount, BufferUsage.WriteOnly);
|
||||
@@ -97,7 +103,7 @@ namespace MonoGame.Extended.Graphics
|
||||
var currentDrawCall = DrawCalls[0];
|
||||
DrawCalls[newDrawCallsCount++] = DrawCalls[0];
|
||||
|
||||
var drawCallIndexCount = currentDrawCall.PrimitiveType.GetVerticesCount(currentDrawCall.PrimitiveCount);
|
||||
var drawCallIndexCount = currentDrawCall.PrimitiveCount * 3;
|
||||
Array.Copy(_indices, currentDrawCall.StartIndex, _sortedIndices, 0, drawCallIndexCount);
|
||||
var sortedIndexCount = drawCallIndexCount;
|
||||
|
||||
@@ -106,7 +112,7 @@ namespace MonoGame.Extended.Graphics
|
||||
for (var i = 1; i < EnqueuedDrawCallCount; i++)
|
||||
{
|
||||
currentDrawCall = DrawCalls[i];
|
||||
drawCallIndexCount = currentDrawCall.PrimitiveType.GetVerticesCount(currentDrawCall.PrimitiveCount);
|
||||
drawCallIndexCount = currentDrawCall.PrimitiveCount * 3;
|
||||
Array.Copy(_indices, currentDrawCall.StartIndex, _sortedIndices, sortedIndexCount, drawCallIndexCount);
|
||||
sortedIndexCount += drawCallIndexCount;
|
||||
|
||||
@@ -434,13 +440,13 @@ namespace MonoGame.Extended.Graphics
|
||||
StartIndex = startIndex;
|
||||
PrimitiveCount = primitiveCount;
|
||||
Texture = texture;
|
||||
TextureKey = (uint)RuntimeHelpers.GetHashCode(Texture);
|
||||
TextureKey = (uint)RuntimeHelpers.GetHashCode(texture);
|
||||
DepthKey = *(uint*)&depth;
|
||||
}
|
||||
|
||||
public void SetState(Effect effect)
|
||||
{
|
||||
var textureEffect = effect as ITexture2DEffect;
|
||||
var textureEffect = effect as ITextureEffect;
|
||||
if (textureEffect != null)
|
||||
textureEffect.Texture = Texture;
|
||||
}
|
||||
@@ -457,11 +463,11 @@ namespace MonoGame.Extended.Graphics
|
||||
[SuppressMessage("ReSharper", "ImpureMethodCallOnReadonlyValueField")]
|
||||
public int CompareTo(DrawCallInfo other)
|
||||
{
|
||||
var result = ((byte)PrimitiveType).CompareTo((byte)other.PrimitiveType);
|
||||
var result = TextureKey.CompareTo(other.TextureKey);;
|
||||
if (result != 0)
|
||||
return result;
|
||||
result = TextureKey.CompareTo(other.TextureKey);
|
||||
return result != 0 ? result : DepthKey.CompareTo(other.DepthKey);
|
||||
result = DepthKey.CompareTo(other.DepthKey);
|
||||
return result != 0 ? result : ((byte)PrimitiveType).CompareTo((byte)other.PrimitiveType);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using MonoGame.Extended.Collections;
|
||||
|
||||
namespace MonoGame.Extended.Graphics.Effects
|
||||
{
|
||||
/// <summary>
|
||||
/// An <see cref="Effect" /> that allows objects, within a 3D context, to be represented on a 2D monitor.
|
||||
/// </summary>
|
||||
/// <seealso cref="MatrixChainEffect" />
|
||||
/// <seealso cref="ITextureEffect" />
|
||||
public class DefaultEffect : MatrixChainEffect, ITextureEffect
|
||||
{
|
||||
/// <summary>
|
||||
/// The bitmask for use with <see cref="MatrixChainEffect.Flags" /> indicating wether <see cref="Texture" /> has
|
||||
/// changed in the last frame.
|
||||
/// </summary>
|
||||
protected static uint DirtyTextureBitMask = BitVector32.CreateMask(UseDefaultProjectionBitMask);
|
||||
|
||||
/// <summary>
|
||||
/// The bitmask for use with <see cref="MatrixChainEffect.Flags" /> indicating wether the underlying vertex shader and
|
||||
/// fragment (pixel) shaders have changed to one of the pre-defined shaders in the last frame.
|
||||
/// </summary>
|
||||
protected static uint DirtyShaderIndexBitMask = BitVector32.CreateMask(DirtyTextureBitMask);
|
||||
|
||||
/// <summary>
|
||||
/// The bitmask for use with <see cref="MatrixChainEffect.Flags" /> indicating wether the material color has changed in
|
||||
/// the last frame.
|
||||
/// </summary>
|
||||
public static uint DirtyMaterialColorBitMask = BitVector32.CreateMask(DirtyShaderIndexBitMask);
|
||||
|
||||
private Texture2D _texture;
|
||||
private EffectParameter _textureParameter;
|
||||
|
||||
private float _alpha = 1;
|
||||
private Color _diffuseColor = Color.White;
|
||||
private EffectParameter _diffuseColorParameter;
|
||||
|
||||
private bool _textureEnabled;
|
||||
private bool _vertexColorEnabled;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the material <see cref="Texture2D" />.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The material <see cref="Texture2D" />.
|
||||
/// </value>
|
||||
public Texture2D Texture
|
||||
{
|
||||
get { return _texture; }
|
||||
set
|
||||
{
|
||||
_texture = value;
|
||||
Flags[DirtyTextureBitMask] = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the material color alpha.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// The alpha channel uses the premultiplied (associated) representation. This means that the RGB components of a
|
||||
/// color represent
|
||||
/// the color of the object of pixel, adjusted for its opacity by multiplication of <see cref="Alpha" />.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public float Alpha
|
||||
{
|
||||
get { return _alpha; }
|
||||
|
||||
set
|
||||
{
|
||||
_alpha = value;
|
||||
Flags[DirtyMaterialColorBitMask] = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets whether texturing is enabled.
|
||||
/// </summary>
|
||||
public bool TextureEnabled
|
||||
{
|
||||
get { return _textureEnabled; }
|
||||
|
||||
set
|
||||
{
|
||||
if (_textureEnabled == value)
|
||||
return;
|
||||
_textureEnabled = value;
|
||||
Flags[DirtyShaderIndexBitMask] = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets whether vertex color is enabled.
|
||||
/// </summary>
|
||||
public bool VertexColorEnabled
|
||||
{
|
||||
get { return _vertexColorEnabled; }
|
||||
|
||||
set
|
||||
{
|
||||
if (_vertexColorEnabled == value)
|
||||
return;
|
||||
_vertexColorEnabled = value;
|
||||
Flags[DirtyShaderIndexBitMask] = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DefaultEffect" /> class.
|
||||
/// </summary>
|
||||
/// <param name="graphicsDevice">The graphics device.</param>
|
||||
public DefaultEffect(GraphicsDevice graphicsDevice)
|
||||
: base(graphicsDevice, EffectResource.DefaultEffect.Bytecode)
|
||||
{
|
||||
Initialize();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DefaultEffect" /> class.
|
||||
/// </summary>
|
||||
/// <param name="graphicsDevice">The graphics device.</param>
|
||||
/// <param name="byteCode">The byte code of the shader program.</param>
|
||||
public DefaultEffect(GraphicsDevice graphicsDevice, byte[] byteCode)
|
||||
: base(graphicsDevice, byteCode)
|
||||
{
|
||||
Initialize();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DefaultEffect" /> class.
|
||||
/// </summary>
|
||||
/// <param name="cloneSource">The clone source.</param>
|
||||
public DefaultEffect(Effect cloneSource)
|
||||
: base(cloneSource)
|
||||
{
|
||||
Initialize();
|
||||
}
|
||||
|
||||
private void Initialize()
|
||||
{
|
||||
Flags[DirtyMaterialColorBitMask] = true;
|
||||
_textureParameter = Parameters["Texture"];
|
||||
_diffuseColorParameter = Parameters["DiffuseColor"];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Computes derived parameter values immediately before applying the effect.
|
||||
/// </summary>
|
||||
protected override void OnApply()
|
||||
{
|
||||
base.OnApply();
|
||||
|
||||
if (Flags[DirtyTextureBitMask])
|
||||
{
|
||||
_textureParameter.SetValue(_texture);
|
||||
Flags[DirtyTextureBitMask] = false;
|
||||
}
|
||||
|
||||
// ReSharper disable once InvertIf
|
||||
if (Flags[DirtyMaterialColorBitMask])
|
||||
{
|
||||
UpdateMaterialColor();
|
||||
Flags[DirtyMaterialColorBitMask] = false;
|
||||
}
|
||||
|
||||
// ReSharper disable once InvertIf
|
||||
if (Flags[DirtyShaderIndexBitMask])
|
||||
{
|
||||
var shaderIndex = 0;
|
||||
|
||||
if (_textureEnabled)
|
||||
shaderIndex += 1;
|
||||
|
||||
if (_vertexColorEnabled)
|
||||
shaderIndex += 2;
|
||||
|
||||
Flags[DirtyShaderIndexBitMask] = false;
|
||||
CurrentTechnique = Techniques[shaderIndex];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the material color parameters associated with this <see cref="DefaultEffect" />.
|
||||
/// </summary>
|
||||
protected virtual void UpdateMaterialColor()
|
||||
{
|
||||
var diffuseColorVector3 = _diffuseColor.ToVector3();
|
||||
|
||||
var diffuseColorVector4 = new Vector4()
|
||||
{
|
||||
X = diffuseColorVector3.X * Alpha,
|
||||
Y = diffuseColorVector3.Y * Alpha,
|
||||
Z = diffuseColorVector3.Z * Alpha,
|
||||
W = Alpha,
|
||||
};
|
||||
|
||||
_diffuseColorParameter.SetValue(diffuseColorVector4);
|
||||
}
|
||||
}
|
||||
}
|
||||
+17
-14
@@ -23,23 +23,16 @@ namespace MonoGame.Extended.Graphics.Effects
|
||||
/// </remarks>
|
||||
public class EffectResource
|
||||
{
|
||||
private static EffectResource _defaultEffect;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="Effects.DefaultEffect2D" /> embedded into the MonoGame.Extended library.
|
||||
/// Gets the <see cref="Effects.DefaultEffect" /> embedded into the MonoGame.Extended.Graphics library.
|
||||
/// </summary>
|
||||
public static readonly EffectResource DefaultEffect2D =
|
||||
new EffectResource("MonoGame.Extended.Graphics.Effects.Resources.DefaultEffect2D.mgfxo");
|
||||
public static EffectResource DefaultEffect => _defaultEffect ?? (_defaultEffect = new EffectResource("MonoGame.Extended.Graphics.Effects.Resources.DefaultEffect.ogl.mgfxo"));
|
||||
|
||||
private readonly string _resourceName;
|
||||
private volatile byte[] _bytecode;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="EffectResource" /> class.
|
||||
/// </summary>
|
||||
/// <param name="resourceName">The name of the embedded resource. This must include the namespace(s).</param>
|
||||
public EffectResource(string resourceName)
|
||||
{
|
||||
_resourceName = resourceName;
|
||||
}
|
||||
private readonly Assembly _assembly;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the bytecode of the <see cref="Effect" /> file.
|
||||
@@ -59,8 +52,7 @@ namespace MonoGame.Extended.Graphics.Effects
|
||||
if (_bytecode != null)
|
||||
return _bytecode;
|
||||
|
||||
var assembly = typeof(EffectResource).GetTypeInfo().Assembly;
|
||||
var stream = assembly.GetManifestResourceStream(_resourceName);
|
||||
var stream = _assembly.GetManifestResourceStream(_resourceName);
|
||||
using (var memoryStream = new MemoryStream())
|
||||
{
|
||||
stream.CopyTo(memoryStream);
|
||||
@@ -71,5 +63,16 @@ namespace MonoGame.Extended.Graphics.Effects
|
||||
return _bytecode;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="EffectResource" /> class.
|
||||
/// </summary>
|
||||
/// <param name="resourceName">The name of the embedded resource. This must include the namespace(s).</param>
|
||||
/// <param name="assembly">The assembly which the embedded resource is apart of.</param>
|
||||
public EffectResource(string resourceName, Assembly assembly = null)
|
||||
{
|
||||
_resourceName = resourceName;
|
||||
_assembly = assembly ?? typeof(EffectResource).GetTypeInfo().Assembly;
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -5,7 +5,7 @@ namespace MonoGame.Extended.Graphics.Effects
|
||||
/// <summary>
|
||||
/// Defines an <see cref="Effect" /> that uses a <see cref="Texture2D" />.
|
||||
/// </summary>
|
||||
public interface ITexture2DEffect
|
||||
public interface ITextureEffect
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the <see cref="Texture2D" />.
|
||||
+59
-38
@@ -1,5 +1,6 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using MonoGame.Extended.Collections;
|
||||
|
||||
namespace MonoGame.Extended.Graphics.Effects
|
||||
{
|
||||
@@ -11,32 +12,25 @@ namespace MonoGame.Extended.Graphics.Effects
|
||||
/// <seealso cref="IEffectMatrices" />
|
||||
public abstract class MatrixChainEffect : Effect, IMatrixChainEffect
|
||||
{
|
||||
/// <summary>
|
||||
/// The bitmask for use with <see cref="Flags"/> indicating wether <see cref="World"/>, <see cref="View"/>, or <see cref="Projection"/> has changed in the last frame.
|
||||
/// </summary>
|
||||
protected static uint DirtyWorldViewProjectionBitMask = BitVector32.CreateMask();
|
||||
|
||||
/// <summary>
|
||||
/// The bitmask for use with <see cref="Flags"/> indicating wether to use a default projection matrix or a custom projection matrix.
|
||||
/// </summary>
|
||||
protected static uint UseDefaultProjectionBitMask = BitVector32.CreateMask(DirtyWorldViewProjectionBitMask);
|
||||
|
||||
/// <summary>
|
||||
/// The dirty flags associated with this <see cref="MatrixChainEffect"/>.
|
||||
/// </summary>
|
||||
protected BitVector32 Flags;
|
||||
|
||||
private Matrix _projection = Matrix.Identity;
|
||||
private Matrix _view = Matrix.Identity;
|
||||
private Matrix _world = Matrix.Identity;
|
||||
private bool _worldViewProjectionIsDirty;
|
||||
private EffectParameter _worldViewProjectionParameter;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MatrixChainEffect" /> class.
|
||||
/// </summary>
|
||||
/// <param name="graphicsDevice">The graphics device.</param>
|
||||
/// <param name="effectCode">The effect code.</param>
|
||||
protected MatrixChainEffect(GraphicsDevice graphicsDevice, byte[] effectCode)
|
||||
: base(graphicsDevice, effectCode)
|
||||
{
|
||||
CacheEffectParameters();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MatrixChainEffect" /> class.
|
||||
/// </summary>
|
||||
/// <param name="cloneSource">The clone source.</param>
|
||||
protected MatrixChainEffect(Effect cloneSource)
|
||||
: base(cloneSource)
|
||||
{
|
||||
CacheEffectParameters();
|
||||
}
|
||||
private EffectParameter _matrixParameter;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the model-to-world <see cref="Matrix" />.
|
||||
@@ -74,6 +68,34 @@ namespace MonoGame.Extended.Graphics.Effects
|
||||
set { SetProjection(ref value); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MatrixChainEffect" /> class.
|
||||
/// </summary>
|
||||
/// <param name="graphicsDevice">The graphics device.</param>
|
||||
/// <param name="byteCode">The effect code.</param>
|
||||
protected MatrixChainEffect(GraphicsDevice graphicsDevice, byte[] byteCode)
|
||||
: base(graphicsDevice, byteCode)
|
||||
{
|
||||
Initialize();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MatrixChainEffect" /> class.
|
||||
/// </summary>
|
||||
/// <param name="cloneSource">The clone source.</param>
|
||||
protected MatrixChainEffect(Effect cloneSource)
|
||||
: base(cloneSource)
|
||||
{
|
||||
Initialize();
|
||||
}
|
||||
|
||||
private void Initialize()
|
||||
{
|
||||
Flags[UseDefaultProjectionBitMask] = true;
|
||||
|
||||
_matrixParameter = Parameters["WorldViewProjection"];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the model-to-world <see cref="Matrix" />.
|
||||
/// </summary>
|
||||
@@ -81,7 +103,7 @@ namespace MonoGame.Extended.Graphics.Effects
|
||||
public void SetWorld(ref Matrix world)
|
||||
{
|
||||
_world = world;
|
||||
_worldViewProjectionIsDirty = true;
|
||||
Flags[DirtyWorldViewProjectionBitMask] = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -91,7 +113,7 @@ namespace MonoGame.Extended.Graphics.Effects
|
||||
public void SetView(ref Matrix view)
|
||||
{
|
||||
_view = view;
|
||||
_worldViewProjectionIsDirty = true;
|
||||
Flags[DirtyWorldViewProjectionBitMask] = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -101,34 +123,33 @@ namespace MonoGame.Extended.Graphics.Effects
|
||||
public void SetProjection(ref Matrix projection)
|
||||
{
|
||||
_projection = projection;
|
||||
_worldViewProjectionIsDirty = true;
|
||||
}
|
||||
|
||||
private void CacheEffectParameters()
|
||||
{
|
||||
_worldViewProjectionParameter = Parameters["WorldViewProjection"];
|
||||
Flags[DirtyWorldViewProjectionBitMask] = true;
|
||||
Flags[UseDefaultProjectionBitMask] = false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Computes derived parameter values immediately before applying the effect.
|
||||
/// </summary>
|
||||
protected override bool OnApply()
|
||||
protected override void OnApply()
|
||||
{
|
||||
base.OnApply();
|
||||
|
||||
// ReSharper disable once InvertIf
|
||||
if (_worldViewProjectionIsDirty)
|
||||
if (Flags[DirtyWorldViewProjectionBitMask] || Flags[UseDefaultProjectionBitMask])
|
||||
{
|
||||
_worldViewProjectionIsDirty = false;
|
||||
if (Flags[UseDefaultProjectionBitMask])
|
||||
{
|
||||
var viewport = GraphicsDevice.Viewport;
|
||||
_projection = Matrix.CreateOrthographicOffCenter(0, viewport.Width, viewport.Height, 0, 0, -1);
|
||||
}
|
||||
|
||||
Matrix worldViewProjection;
|
||||
Matrix.Multiply(ref _world, ref _view, out worldViewProjection);
|
||||
Matrix.Multiply(ref worldViewProjection, ref _projection, out worldViewProjection);
|
||||
_worldViewProjectionParameter.SetValue(worldViewProjection);
|
||||
}
|
||||
_matrixParameter.SetValue(worldViewProjection);
|
||||
|
||||
// in MonoGame 3.6 this method won't return anything; see https://github.com/MonoGame/MonoGame/pull/5090
|
||||
return false;
|
||||
Flags[DirtyWorldViewProjectionBitMask] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,71 @@
|
||||
#include "Macros.fxh"
|
||||
#include "Structures.fxh"
|
||||
|
||||
DECLARE_TEXTURE(Texture, 0);
|
||||
|
||||
BEGIN_CONSTANTS
|
||||
MATRIX_CONSTANTS
|
||||
|
||||
float4x4 WorldViewProjection _vs(c0) _cb(c0);
|
||||
|
||||
END_CONSTANTS
|
||||
|
||||
float4 DiffuseColor = float4(1, 1, 1, 1);
|
||||
|
||||
VertexShaderOutputPosition VertexShaderFunctionPosition(VertexShaderInputPosition input)
|
||||
{
|
||||
VertexShaderOutputPosition output;
|
||||
output.Position = mul(input.Position, WorldViewProjection);
|
||||
return output;
|
||||
}
|
||||
|
||||
float4 PixelShaderFunctionPosition(VertexShaderOutputPosition input) : SV_Target0
|
||||
{
|
||||
return DiffuseColor;
|
||||
}
|
||||
|
||||
VertexShaderOutputPositionTexture VertexShaderFunctionPositionTexture(VertexShaderInputPositionTexture input)
|
||||
{
|
||||
VertexShaderOutputPositionTexture output;
|
||||
output.Position = mul(input.Position, WorldViewProjection);
|
||||
output.TextureCoordinate = input.TextureCoordinate;
|
||||
return output;
|
||||
}
|
||||
|
||||
float4 PixelShaderFunctionPositionTexture(VertexShaderOutputPositionTexture input) : SV_Target0
|
||||
{
|
||||
return SAMPLE_TEXTURE(Texture, input.TextureCoordinate) * DiffuseColor;
|
||||
}
|
||||
|
||||
VertexShaderOutputPosition VertexShaderFunctionPositionColor(VertexShaderInputPositionColor input)
|
||||
{
|
||||
VertexShaderOutputPositionColor output;
|
||||
output.Position = mul(input.Position, WorldViewProjection);
|
||||
output.Color = input.Color;
|
||||
return output;
|
||||
}
|
||||
|
||||
float4 PixelShaderFunctionPositionColor(VertexShaderOutputPositionColor input) : SV_Target0
|
||||
{
|
||||
return input.Color * DiffuseColor;
|
||||
}
|
||||
|
||||
VertexShaderOutputPositionColorTexture VertexShaderFunctionPositionColorTexture(VertexShaderInputPositionColorTexture input)
|
||||
{
|
||||
VertexShaderOutputPositionColorTexture output;
|
||||
output.Position = mul(input.Position, WorldViewProjection);
|
||||
output.Color = input.Color;
|
||||
output.TextureCoordinate = input.TextureCoordinate;
|
||||
return output;
|
||||
}
|
||||
|
||||
float4 PixelShaderFunctionPositionColorTexture(VertexShaderOutputPositionColorTexture input) : SV_Target0
|
||||
{
|
||||
float4 textureColor = SAMPLE_TEXTURE(Texture, input.TextureCoordinate);
|
||||
return textureColor * input.Color * DiffuseColor;
|
||||
}
|
||||
|
||||
TECHNIQUE(Position, VertexShaderFunctionPosition, PixelShaderFunctionPosition);
|
||||
TECHNIQUE(PositionTexture, VertexShaderFunctionPositionTexture, PixelShaderFunctionPositionTexture);
|
||||
TECHNIQUE(PositionColor, VertexShaderFunctionPositionColor, PixelShaderFunctionPositionColor);
|
||||
TECHNIQUE(PositionColorTexture, VertexShaderFunctionPositionColorTexture, PixelShaderFunctionPositionColorTexture);
|
||||
Binary file not shown.
@@ -0,0 +1,53 @@
|
||||
#ifdef SM4
|
||||
|
||||
// Macros for targetting shader model 4.0 (DX11)
|
||||
|
||||
#define TECHNIQUE(name, vsname, psname ) \
|
||||
technique name { pass { VertexShader = compile vs_4_0_level_9_1 vsname (); PixelShader = compile ps_4_0_level_9_1 psname(); } }
|
||||
|
||||
#define BEGIN_CONSTANTS cbuffer Parameters : register(b0) {
|
||||
#define MATRIX_CONSTANTS }; cbuffer ProjectionMatrix : register(b1) {
|
||||
#define END_CONSTANTS };
|
||||
|
||||
#define _vs(r)
|
||||
#define _ps(r)
|
||||
#define _cb(r) : packoffset(r)
|
||||
|
||||
#define DECLARE_TEXTURE(Name, index) \
|
||||
Texture2D<float4> Name : register(t##index); \
|
||||
sampler Name##Sampler : register(s##index)
|
||||
|
||||
#define DECLARE_CUBEMAP(Name, index) \
|
||||
TextureCube<float4> Name : register(t##index); \
|
||||
sampler Name##Sampler : register(s##index)
|
||||
|
||||
#define SAMPLE_TEXTURE(Name, texCoord) Name.Sample(Name##Sampler, texCoord)
|
||||
#define SAMPLE_CUBEMAP(Name, texCoord) Name.Sample(Name##Sampler, texCoord)
|
||||
|
||||
#else
|
||||
|
||||
// Macros for targetting shader model 2.0 (DX9)
|
||||
|
||||
#define TECHNIQUE(name, vsname, psname ) \
|
||||
technique name { pass { VertexShader = compile vs_2_0 vsname (); PixelShader = compile ps_2_0 psname(); } }
|
||||
|
||||
#define BEGIN_CONSTANTS
|
||||
#define MATRIX_CONSTANTS
|
||||
#define END_CONSTANTS
|
||||
|
||||
#define _vs(r) : register(vs, r)
|
||||
#define _ps(r) : register(ps, r)
|
||||
#define _cb(r)
|
||||
|
||||
#define DECLARE_TEXTURE(Name, index) \
|
||||
texture2D Name; \
|
||||
sampler Name##Sampler : register(s##index) = sampler_state { Texture = (Name); };
|
||||
|
||||
#define DECLARE_CUBEMAP(Name, index) \
|
||||
textureCUBE Name; \
|
||||
sampler Name##Sampler : register(s##index) = sampler_state { Texture = (Name); };
|
||||
|
||||
#define SAMPLE_TEXTURE(Name, texCoord) tex2D(Name##Sampler, texCoord)
|
||||
#define SAMPLE_CUBEMAP(Name, texCoord) texCUBE(Name##Sampler, texCoord)
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,15 @@
|
||||
setlocal
|
||||
|
||||
SET TWOMGFX="C:\Program Files (x86)\MSBuild\MonoGame\v3.0\Tools\2mgfx.exe"
|
||||
|
||||
@for /f %%f IN ('dir /b *.fx') do (
|
||||
|
||||
call %TWOMGFX% %%~nf.fx %%~nf.ogl.mgfxo /Profile:OpenGL
|
||||
|
||||
call %TWOMGFX% %%~nf.fx %%~nf.dx11.mgfxo /Profile:DirectX_11
|
||||
|
||||
)
|
||||
|
||||
endlocal
|
||||
|
||||
pause
|
||||
@@ -0,0 +1,51 @@
|
||||
// Vertex shader input structures.
|
||||
|
||||
struct VertexShaderInputPosition
|
||||
{
|
||||
float4 Position : SV_Position;
|
||||
};
|
||||
|
||||
struct VertexShaderInputPositionColor
|
||||
{
|
||||
float4 Position : SV_Position;
|
||||
float4 Color : COLOR;
|
||||
};
|
||||
|
||||
struct VertexShaderInputPositionTexture
|
||||
{
|
||||
float4 Position : SV_Position;
|
||||
float2 TextureCoordinate : TEXCOORD0;
|
||||
};
|
||||
|
||||
struct VertexShaderInputPositionColorTexture
|
||||
{
|
||||
float4 Position : SV_Position;
|
||||
float4 Color : COLOR;
|
||||
float2 TextureCoordinate : TEXCOORD0;
|
||||
};
|
||||
|
||||
// Vertex shader output structures.
|
||||
|
||||
struct VertexShaderOutputPosition
|
||||
{
|
||||
float4 Position : SV_Position;
|
||||
};
|
||||
|
||||
struct VertexShaderOutputPositionColor
|
||||
{
|
||||
float4 Position : SV_Position;
|
||||
float4 Color : COLOR0;
|
||||
};
|
||||
|
||||
struct VertexShaderOutputPositionTexture
|
||||
{
|
||||
float4 Position : SV_Position;
|
||||
float2 TextureCoordinate : TEXCOORD0;
|
||||
};
|
||||
|
||||
struct VertexShaderOutputPositionColorTexture
|
||||
{
|
||||
float4 Position : SV_Position;
|
||||
float4 Color : COLOR0;
|
||||
float2 TextureCoordinate : TEXCOORD0;
|
||||
};
|
||||
@@ -8,7 +8,7 @@
|
||||
<ProjectGuid>{9B3AB8A1-78AA-471A-AFD0-B10B932115BC}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>MonoGame.Extended.Graphics</RootNamespace>
|
||||
<RootNamespace>MonoGame.Extended</RootNamespace>
|
||||
<AssemblyName>MonoGame.Extended.Graphics</AssemblyName>
|
||||
<DefaultLanguage>en-US</DefaultLanguage>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
@@ -38,31 +38,30 @@
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<!-- A reference to the entire .NET Framework is automatically included -->
|
||||
<None Include="Effects\Resources\compile.bat" />
|
||||
<EmbeddedResource Include="Effects\Resources\DefaultEffect2D.mgfxo" />
|
||||
<None Include="Graphics\Effects\Resources\RebuildEffects.bat" />
|
||||
<EmbeddedResource Include="Graphics\Effects\Resources\DefaultEffect.dx11.mgfxo" />
|
||||
<EmbeddedResource Include="Graphics\Effects\Resources\DefaultEffect.ogl.mgfxo" />
|
||||
<None Include="Graphics\Effects\Resources\Macros.fxh" />
|
||||
<None Include="Graphics\Effects\Resources\Structures.fxh" />
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Batcher.cs" />
|
||||
<Compile Include="IBatchDrawCallInfo.cs" />
|
||||
<Compile Include="Batcher2D.cs" />
|
||||
<Compile Include="Effects\DefaultEffect2D.cs" />
|
||||
<Compile Include="Effects\EffectResource.cs" />
|
||||
<Compile Include="Effects\ITexture2DEffect.cs" />
|
||||
<Compile Include="Effects\MatrixChainEffect.cs" />
|
||||
<Compile Include="Geometry\GeometryBuilder.cs" />
|
||||
<Compile Include="Geometry\GeometryBuilder2D.cs" />
|
||||
<Compile Include="IMatrixChainEffect.cs" />
|
||||
<Compile Include="PrimitiveTypeExtensions.cs" />
|
||||
<Compile Include="Graphics\Batcher.cs" />
|
||||
<Compile Include="Graphics\IBatchDrawCallInfo.cs" />
|
||||
<Compile Include="Graphics\Batcher2D.cs" />
|
||||
<Compile Include="Graphics\Effects\DefaultEffect.cs" />
|
||||
<Compile Include="Graphics\Effects\EffectResource.cs" />
|
||||
<Compile Include="Graphics\Effects\ITextureEffect.cs" />
|
||||
<Compile Include="Graphics\Effects\MatrixChainEffect.cs" />
|
||||
<Compile Include="Graphics\Geometry\GeometryBuilder.cs" />
|
||||
<Compile Include="Graphics\Geometry\GeometryBuilder2D.cs" />
|
||||
<Compile Include="Graphics\IMatrixChainEffect.cs" />
|
||||
<Compile Include="Graphics\PrimitiveTypeExtensions.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="FlipFlags.cs" />
|
||||
<Compile Include="Graphics\FlipFlags.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="Effects\Resources\DefaultEffect2D.fx" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="MonoGame.Framework">
|
||||
<HintPath>..\..\Dependencies\MonoGame.Framework.dll</HintPath>
|
||||
</Reference>
|
||||
<Content Include="Graphics\Effects\Resources\DefaultEffect.fx" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\MonoGame.Extended\MonoGame.Extended.csproj">
|
||||
@@ -70,6 +69,11 @@
|
||||
<Name>MonoGame.Extended</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="MonoGame.Framework, Version=3.6.0.1625, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MonoGame.Framework.Portable.3.6.0.1625\lib\portable-net45+win8+wpa81\MonoGame.Framework.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="MonoGame.Framework.Portable" version="3.6.0.1625" targetFramework="portable45-net45+win8+wpa81" />
|
||||
</packages>
|
||||
Reference in New Issue
Block a user