28.10.2020

SM.Core:
+ Particle System
+ scriptable system for scripts

~ Moved Texts- and Particles-namespace to SM.Base.Drawing
~ Changed how you tell the stopwatch to pause. (From method to property)
~ Fixed Randomize.GetFloat(min, max)
~ Now automaticly adds the DrawingBase.Transformation to DrawContext.ModelMatrix. No need to change DrawContext.Instances[0], anymore.

SM.OGL:
+ "one-file-shader"-support

SM2D:
+ DrawParticles (Control for Texture and Color not there yet)

~ Changed coordnate system to upper-right as (1,1)
~ Changed default shader to "one-file-shader"
This commit is contained in:
Michel Fedde 2020-10-28 18:19:15 +01:00
parent 03b3942732
commit beb9c19081
45 changed files with 580 additions and 190 deletions

View file

@ -2,11 +2,12 @@
using System.Collections.Generic;
using SM.Base.Contexts;
using SM.Base.Scene;
using SM.OGL.Mesh;
#endregion
namespace SM.Base.Scene
namespace SM.Base.Drawing
{
/// <summary>
/// Contains general basis systems for drawing objects.
@ -31,12 +32,7 @@ namespace SM.Base.Scene
/// <inheritdoc />
public ICollection<string> Flags { get; set; }
/// <inheritdoc />
public virtual void Update(UpdateContext context)
{
}
/// <inheritdoc />
public void Draw(DrawContext context)
{
@ -76,5 +72,11 @@ namespace SM.Base.Scene
/// The current transformation.
/// </summary>
public TTransformation Transform = new TTransformation();
/// <inheritdoc />
protected override void DrawContext(ref DrawContext context)
{
context.ModelMaster = Transform.GetMatrix();
}
}
}

View file

@ -4,7 +4,7 @@ using OpenTK;
#endregion
namespace SM.Base.Scene
namespace SM.Base.Drawing
{
/// <summary>
/// Contains methods for using transformations right.

View file

@ -4,26 +4,26 @@ using OpenTK;
#endregion
namespace SM.Base.Scene
namespace SM.Base.Drawing
{
/// <summary>
/// This represens a drawing instance.
/// </summary>
public struct Instance
public class Instance
{
/// <summary>
/// The model matrix.
/// </summary>
public Matrix4 ModelMatrix;
public Matrix4 ModelMatrix = Matrix4.Identity;
/// <summary>
/// The texture offset.
/// </summary>
public Vector2 TexturePosition;
public Vector2 TexturePosition = Vector2.Zero;
/// <summary>
/// The texture scale.
/// </summary>
public Vector2 TextureScale;
public Vector2 TextureScale = Vector2.One;
}
}

View file

@ -5,7 +5,7 @@ using SM.OGL.Texture;
#endregion
namespace SM.Base.Scene
namespace SM.Base.Drawing
{
/// <summary>
/// Represents a material.

View file

@ -6,24 +6,29 @@ using SM.OGL.Shaders;
#endregion
namespace SM.Base.Scene
namespace SM.Base.Drawing
{
/// <summary>
/// A general class to work with material shaders properly.
/// </summary>
public abstract class MaterialShader : GenericShader
{
/// <inheritdoc />
protected MaterialShader(string combinedData) : base(combinedData)
{}
/// <inheritdoc />
protected MaterialShader(string vertex, string fragment) : base(vertex, fragment)
{
}
/// <inheritdoc />
protected MaterialShader(ShaderFileCollection shaderFileFiles) : base(shaderFileFiles)
{
}
/// <summary>
/// Draws the context.
/// Prepares the context for the drawing.
/// </summary>
/// <param name="context">The context</param>
public virtual void Draw(DrawContext context)
@ -39,6 +44,10 @@ namespace SM.Base.Scene
GL.UseProgram(0);
}
/// <summary>
/// Draws the context.
/// </summary>
/// <param name="context"></param>
protected virtual void DrawProcess(DrawContext context)
{

View file

@ -0,0 +1,19 @@
using SM.Base.Time;
namespace SM.Base.Drawing.Particles
{
/// <summary>
/// A context, with that the particle system sends the information for the movement function.
/// </summary>
public struct ParticleContext
{
/// <summary>
/// The Timer of the particles
/// </summary>
public Timer Timer;
/// <summary>
/// The current speed of the particles.
/// </summary>
public float Speed;
}
}

View file

@ -0,0 +1,126 @@
using System;
using System.Collections.Generic;
using OpenTK;
using SM.Base.Contexts;
using SM.Base.Scene;
using SM.Base.Time;
using SM.Base.Types;
using SM.OGL.Shaders;
namespace SM.Base.Drawing.Particles
{
/// <summary>
/// The (drawing) basis for particles
/// </summary>
public abstract class ParticleDrawingBasis<TTransform, TDirection> : DrawingBasis<TTransform>, IScriptable
where TTransform : GenericTransformation, new()
where TDirection : struct
{
/// <summary>
/// This contains all important information for each particle.
/// </summary>
protected ParticleStruct<TDirection>[] particleStructs;
/// <summary>
/// This contains the different instances for the particles.
/// </summary>
protected List<Instance> instances;
/// <summary>
/// The stopwatch of the particles.
/// </summary>
protected Timer timer;
/// <summary>
/// The amount of particles
/// </summary>
public int Amount = 32;
/// <summary>
/// The maximum speed of the particles
/// </summary>
public float MaxSpeed = 1;
/// <summary>
/// Get/Sets the state of pausing.
/// </summary>
public bool Paused
{
get => timer.Paused;
set => timer.Paused = value;
}
/// <summary>
/// Controls the movement of each particles.
/// </summary>
public abstract Func<TDirection, ParticleContext, TDirection> MovementCalculation { get; set; }
protected ParticleDrawingBasis(TimeSpan duration)
{
timer = new Timer(duration);
}
/// <summary>
/// Triggers the particles.
/// </summary>
public void Trigger()
{
timer.Start();
CreateParticles();
}
/// <inheritdoc />
public void Update(UpdateContext context)
{
if (!timer.Running) return;
ParticleContext particleContext = new ParticleContext()
{
Timer = timer,
};
for (int i = 0; i < Amount; i++)
{
particleContext.Speed = particleStructs[i].Speed;
instances[i].ModelMatrix = CreateMatrix(particleStructs[i], MovementCalculation(particleStructs[i].Direction, particleContext));
}
}
/// <inheritdoc />
protected override void DrawContext(ref DrawContext context)
{
if (!timer.Active) return;
base.DrawContext(ref context);
context.Instances = instances;
context.Shader.Draw(context);
}
/// <summary>
/// Creates the particles.
/// </summary>
protected virtual void CreateParticles()
{
particleStructs = new ParticleStruct<TDirection>[Amount];
instances = new List<Instance>();
for (int i = 0; i < Amount; i++)
{
particleStructs[i] = CreateObject(i);
instances.Add(new Instance());
}
}
/// <summary>
/// Creates a particle.
/// </summary>
protected abstract ParticleStruct<TDirection> CreateObject(int index);
/// <summary>
/// Generates the desired matrix for drawing.
/// </summary>
protected abstract Matrix4 CreateMatrix(ParticleStruct<TDirection> Struct, TDirection relativePosition);
}
}

View file

@ -0,0 +1,19 @@
using OpenTK;
namespace SM.Base.Drawing.Particles
{
/// <summary>
/// Contains methods for particle movements.
/// </summary>
public class ParticleMovement
{
/// <summary>
/// Default movement for 2D.
/// </summary>
public static Vector2 Default2D(Vector2 direction, ParticleContext context) => direction * (context.Timer.Elapsed * context.Speed);
/// <summary>
/// Default movement for 3D.
/// </summary>
public static Vector3 Default3D(Vector3 direction, ParticleContext context) => direction * (context.Timer.Elapsed * context.Speed);
}
}

View file

@ -0,0 +1,25 @@
using OpenTK;
using SM.Base.Types;
namespace SM.Base.Drawing.Particles
{
/// <summary>
/// A particle...
/// </summary>
public struct ParticleStruct<TDirection>
where TDirection : struct
{
/// <summary>
/// A direction, that the particle should travel.
/// </summary>
public TDirection Direction;
/// <summary>
/// A matrix to store rotation and scale.
/// </summary>
public Matrix4 Matrix;
/// <summary>
/// Speeeeeeeeeed
/// </summary>
public float Speed;
}
}

View file

@ -0,0 +1,35 @@
#region usings
using System;
#endregion
namespace SM.Base.Drawing.Text
{
/// <summary>
/// Contains information for a font character.
/// </summary>
[Serializable]
public struct CharParameter
{
/// <summary>
/// The position on the X-axis.
/// </summary>
public int X;
/// <summary>
/// The width of the character.
/// </summary>
public float Width;
/// <summary>
/// The normalized position inside the texture.
/// </summary>
public float NormalizedX;
/// <summary>
/// The normalized width inside the texture.
/// </summary>
public float NormalizedWidth;
}
}

View file

@ -0,0 +1,138 @@
#region usings
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Text;
using OpenTK.Graphics.OpenGL4;
using SM.Base.Textures;
#endregion
namespace SM.Base.Drawing.Text
{
/// <summary>
/// Represents a font.
/// </summary>
public class Font : Texture
{
/// <summary>
/// The char set for the font.
/// <para>Default: <see cref="FontCharStorage.SimpleUTF8" /></para>
/// </summary>
public ICollection<char> CharSet = FontCharStorage.SimpleUTF8;
/// <summary>
/// The font family, that is used to find the right font.
/// </summary>
public FontFamily FontFamily;
/// <summary>
/// The font size.
/// <para>Default: 12</para>
/// </summary>
public float FontSize = 12;
/// <summary>
/// The font style.
/// <para>Default: <see cref="System.Drawing.FontStyle.Regular" /></para>
/// </summary>
public FontStyle FontStyle = FontStyle.Regular;
/// <summary>
/// This contains all information for the different font character.
/// </summary>
public Dictionary<char, CharParameter> Positions = new Dictionary<char, CharParameter>();
/// <summary>
/// Generates a font from a font family from the specified path.
/// </summary>
/// <param name="path">The specified path</param>
public Font(string path)
{
var pfc = new PrivateFontCollection();
pfc.AddFontFile(path);
FontFamily = pfc.Families[0];
}
/// <summary>
/// Generates a font from a specified font family.
/// </summary>
/// <param name="font">Font-Family</param>
public Font(FontFamily font)
{
FontFamily = font;
}
/// <inheritdoc />
public override TextureWrapMode WrapMode { get; set; } = TextureWrapMode.ClampToEdge;
/// <summary>
/// Regenerates the texture.
/// </summary>
public void RegenerateTexture()
{
Width = 0;
Height = 0;
Positions = new Dictionary<char, CharParameter>();
var map = new Bitmap(1000, 20);
var charParams = new Dictionary<char, float[]>();
using (var f = new System.Drawing.Font(FontFamily, FontSize, FontStyle))
{
using (var g = Graphics.FromImage(map))
{
g.Clear(Color.Transparent);
foreach (var c in CharSet)
{
var s = c.ToString();
var size = g.MeasureString(s, f);
try
{
charParams.Add(c, new[] {size.Width, Width});
}
catch
{
// ignored
}
if (Height < size.Height) Height = (int) size.Height;
Width += (int) size.Width;
}
}
map = new Bitmap(Width, Height);
using (var g = Graphics.FromImage(map))
{
foreach (var keyValuePair in charParams)
{
var normalizedX = (keyValuePair.Value[1] + 0.00001f) / Width;
var normalizedWidth = keyValuePair.Value[0] / Width;
CharParameter parameter;
Positions.Add(keyValuePair.Key, parameter = new CharParameter
{
NormalizedWidth = normalizedWidth,
NormalizedX = normalizedX,
Width = keyValuePair.Value[0],
X = (int) keyValuePair.Value[1]
});
g.DrawString(keyValuePair.Key.ToString(), f, Brushes.White, parameter.X, 0);
}
}
}
Map = map;
Recompile();
}
/// <inheritdoc />
public override void Compile()
{
RegenerateTexture();
base.Compile();
}
}
}

View file

@ -0,0 +1,29 @@
namespace SM.Base.Drawing.Text
{
/// <summary>
/// Contains default char sets.
/// </summary>
public class FontCharStorage
{
/// <summary>
/// Contains the english alphabet and the common special character.
/// </summary>
public static readonly char[] SimpleUTF8 =
{
'!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5',
'6',
'7', '8', '9', ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', '`',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~'
};
/// <summary>
/// Contains only numbers.
/// </summary>
public static readonly char[] Numbers =
{
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
};
}
}

View file

@ -0,0 +1,130 @@
#region usings
using System;
using OpenTK;
using OpenTK.Graphics;
using SM.Base.Contexts;
#endregion
namespace SM.Base.Drawing.Text
{
/// <summary>
/// Defines a basis for text drawing.
/// </summary>
/// <typeparam name="TTransform">Transformation type</typeparam>
public abstract class TextDrawingBasis<TTransform> : DrawingBasis<TTransform>
where TTransform : GenericTransformation, new()
{
/// <summary>
/// The different instances for drawing.
/// </summary>
protected Instance[] _instances;
/// <summary>
/// The text, that is drawn.
/// </summary>
protected string _text;
/// <summary>
/// The spacing between numbers.
/// <para>Default: 1</para>
/// </summary>
public float Spacing = 1;
/// <summary>
/// Creates a text object with a font.
/// </summary>
/// <param name="font">The font.</param>
protected TextDrawingBasis(Font font)
{
_material.Texture = font;
}
/// <summary>
/// The font.
/// </summary>
public Font Font
{
get => (Font) _material.Texture;
set
{
_material.Texture = value;
GenerateMatrixes();
}
}
/// <summary>
/// The text, that is drawn.
/// </summary>
public string Text
{
get => _text;
set
{
_text = value;
GenerateMatrixes();
}
}
/// <summary>
/// The font color.
/// </summary>
public Color4 Color
{
get => _material.Tint;
set => _material.Tint = value;
}
/// <inheritdoc />
protected override void DrawContext(ref DrawContext context)
{
if (_instances == null) GenerateMatrixes();
}
/// <summary>
/// This generates the instances.
/// </summary>
/// <exception cref="Exception">The font doesn't contain a character that is needed for the text.</exception>
public void GenerateMatrixes()
{
if (!Font.WasCompiled) Font.RegenerateTexture();
_instances = new Instance[_text.Length];
float x = 0;
var _last = new CharParameter();
for (var i = 0; i < _text.Length; i++)
{
if (_text[i] == 32)
{
x += _last.Width * Spacing;
continue;
}
CharParameter parameter;
try
{
parameter = Font.Positions[_text[i]];
}
catch
{
throw new Exception("Font doesn't contain '" + _text[i] + "'");
}
var matrix = Matrix4.CreateScale(parameter.Width, Font.Height, 1) *
Matrix4.CreateTranslation(x, 0, 0);
_instances[i] = new Instance
{
ModelMatrix = matrix,
TexturePosition = new Vector2(parameter.NormalizedX, 0),
TextureScale = new Vector2(parameter.NormalizedWidth, 1)
};
x += parameter.Width * Spacing;
_last = parameter;
}
}
}
}