Fixed Shader Instancing (with that particles and Text, aswell)
This commit is contained in:
parent
57fb71d01b
commit
fd53c73fa7
8 changed files with 60 additions and 61 deletions
|
|
@ -1,12 +1,18 @@
|
||||||
#version 330
|
#version 330
|
||||||
#define maxInstances //!instanceMax
|
#define maxInstances //!instanceMax
|
||||||
|
|
||||||
|
struct Instance {
|
||||||
|
mat4 ModelMatrix;
|
||||||
|
vec2 TextureOffset;
|
||||||
|
vec2 TextureScale;
|
||||||
|
};
|
||||||
|
|
||||||
layout(location = 0) in vec3 aPos;
|
layout(location = 0) in vec3 aPos;
|
||||||
layout(location = 1) in vec2 aTex;
|
layout(location = 1) in vec2 aTex;
|
||||||
layout(location = 3) in vec4 aColor;
|
layout(location = 3) in vec4 aColor;
|
||||||
|
|
||||||
uniform mat4 MVP;
|
uniform mat4 MVP;
|
||||||
|
uniform Instance[maxInstances] Instances;
|
||||||
uniform bool HasVColor;
|
uniform bool HasVColor;
|
||||||
|
|
||||||
out vec2 vTexture;
|
out vec2 vTexture;
|
||||||
|
|
@ -14,7 +20,7 @@ out vec4 vColor;
|
||||||
out vec3 FragPos;
|
out vec3 FragPos;
|
||||||
|
|
||||||
void ApplyTexModifier() {
|
void ApplyTexModifier() {
|
||||||
vTexture = aTex;
|
vTexture = aTex * Instances[gl_InstanceID].TextureScale + Instances[gl_InstanceID].TextureOffset;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CheckVertexColor() {
|
void CheckVertexColor() {
|
||||||
|
|
@ -23,7 +29,7 @@ void CheckVertexColor() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void ApplyModelTransformation() {
|
void ApplyModelTransformation() {
|
||||||
gl_Position = MVP * vec4(aPos, 1);
|
gl_Position = MVP * Instances[gl_InstanceID].ModelMatrix * vec4(aPos, 1);
|
||||||
|
|
||||||
FragPos = vec3(vec4(aPos, 1));
|
FragPos = vec3(vec4(aPos, 1));
|
||||||
}
|
}
|
||||||
|
|
@ -15,6 +15,9 @@ namespace SM.Base.Textures
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class Texture : TextureBase
|
public class Texture : TextureBase
|
||||||
{
|
{
|
||||||
|
private int? _width;
|
||||||
|
private int? _height;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Decides if the bitmap will automatically dispose itself.
|
/// Decides if the bitmap will automatically dispose itself.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -28,16 +31,15 @@ namespace SM.Base.Textures
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override int Width
|
public override int Width
|
||||||
{
|
{
|
||||||
get => Map.Width;
|
get => _width ?? Map.Width;
|
||||||
protected set
|
protected set => _width = value;
|
||||||
{ }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override int Height
|
public override int Height
|
||||||
{
|
{
|
||||||
get => Map.Height;
|
get => _height ?? Map.Height;
|
||||||
protected set {}
|
protected set => _height = value;
|
||||||
}
|
}
|
||||||
public float Aspect { get; private set; }
|
public float Aspect { get; private set; }
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,15 @@ namespace SM.OGL.Shaders
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Uniform(string name, GenericShader shader) : this(GL.GetUniformLocation(shader, name), null)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
public Uniform(string name, GenericShader shader, UniformCollection parent) : this(GL.GetUniformLocation(shader, name), parent)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// This create a new uniform manager
|
/// This create a new uniform manager
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
|
|
@ -1,50 +1,33 @@
|
||||||
#region usings
|
using System.Collections.Generic;
|
||||||
|
using OpenTK.Graphics.OpenGL;
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
namespace SM.OGL.Shaders
|
namespace SM.OGL.Shaders
|
||||||
{
|
{
|
||||||
public class UniformArray : IUniform
|
public class UniformArray : IUniform
|
||||||
{
|
{
|
||||||
internal UniformCollection collection;
|
private Dictionary<int, Dictionary<string, Uniform>> storedUniforms = new Dictionary<int, Dictionary<string, Uniform>>();
|
||||||
|
internal List<string> uniformNames = new List<string>();
|
||||||
|
|
||||||
internal Dictionary<string, int> Offsets = new Dictionary<string, int>();
|
|
||||||
internal int Size;
|
|
||||||
|
|
||||||
internal bool Struct = false;
|
|
||||||
public int Location { get; internal set; }
|
public int Location { get; internal set; }
|
||||||
public GenericShader Parent { get; internal set; }
|
|
||||||
public string Name { get; internal set; }
|
public string Name { get; internal set; }
|
||||||
|
public UniformCollection Parent { get; internal set; }
|
||||||
|
public GenericShader ParentShader { get; internal set; }
|
||||||
|
|
||||||
public UniformArray()
|
public Dictionary<string, Uniform> this[int index] => Get(index);
|
||||||
|
|
||||||
|
public Dictionary<string, Uniform> Get(int index)
|
||||||
{
|
{
|
||||||
collection = new UniformCollection()
|
if (!storedUniforms.ContainsKey(index))
|
||||||
{
|
{
|
||||||
ParentShader = Parent
|
Dictionary<string, Uniform> dic = storedUniforms[index] = new Dictionary<string, Uniform>();
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Set(Action<int, Uniform> setAction)
|
for (int i = 0; i < uniformNames.Count; i++)
|
||||||
{
|
{
|
||||||
for (var i = 0; i < Size; i++) setAction(i, new Uniform(Location + i));
|
dic.Add(uniformNames[i], new Uniform(Name + $"[{index}]." + uniformNames[i], ParentShader, Parent));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Set(Func<int, UniformCollection, bool> setAction)
|
return storedUniforms[index];
|
||||||
{
|
|
||||||
collection.ParentShader ??= Parent;
|
|
||||||
|
|
||||||
for (var i = 0; i < Size; i++)
|
|
||||||
{
|
|
||||||
collection.KeyString = $"{Name}[{i}]";
|
|
||||||
|
|
||||||
foreach (var pair in Offsets)
|
|
||||||
collection.Set(pair.Key, new Uniform(Location + pair.Value + i));
|
|
||||||
|
|
||||||
if (!setAction(i, collection)) break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -82,20 +82,16 @@ namespace SM.OGL.Shaders
|
||||||
{
|
{
|
||||||
Location = loc,
|
Location = loc,
|
||||||
Name = keySplits[0],
|
Name = keySplits[0],
|
||||||
Parent = ParentShader,
|
Parent = this,
|
||||||
Struct = keySplits.Length > 2
|
ParentShader = ParentShader
|
||||||
};
|
};
|
||||||
|
|
||||||
arrayFilled = true;
|
arrayFilled = true;
|
||||||
lastArrayKey = keySplits[0];
|
lastArrayKey = keySplits[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
var curIndex = int.Parse(keySplits[1]);
|
if (keySplits[1] == "0")
|
||||||
if (array.Size < curIndex) array.Size = curIndex;
|
array.uniformNames.Add(keySplits[2].Substring(1));
|
||||||
|
|
||||||
if (array.Struct)
|
|
||||||
if (!array.Offsets.ContainsKey(keySplits[2].Trim('.')))
|
|
||||||
array.Offsets.Add(keySplits[2].Trim('.'), loc - array.Location);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -60,7 +60,7 @@ namespace SM2D.Drawing
|
||||||
context.Material = _material;
|
context.Material = _material;
|
||||||
context.Mesh = Plate.Object;
|
context.Mesh = Plate.Object;
|
||||||
|
|
||||||
context.Instances[0].ModelMatrix = Matrix4.CreateScale(context.WorldScale.X, context.WorldScale.Y, 1);
|
context.ModelMaster = Matrix4.CreateScale(context.WorldScale.X, context.WorldScale.Y, 1);
|
||||||
context.Shader.Draw(context);
|
context.Shader.Draw(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,18 +27,16 @@ namespace SM2D.Shader
|
||||||
Uniforms["MVP"].SetMatrix4(context.ModelMaster * context.View * context.World);
|
Uniforms["MVP"].SetMatrix4(context.ModelMaster * context.View * context.World);
|
||||||
Uniforms["HasVColor"]
|
Uniforms["HasVColor"]
|
||||||
.SetUniform1(context.Mesh.Attributes["color"] != null);
|
.SetUniform1(context.Mesh.Attributes["color"] != null);
|
||||||
/*
|
|
||||||
Uniforms.GetArray("Instances").Set((i, uniforms) =>
|
UniformArray instances = Uniforms.GetArray("Instances");
|
||||||
|
for (int i = 0; i < context.Instances.Count; i++)
|
||||||
{
|
{
|
||||||
if (i >= context.Instances.Count) return false;
|
var shaderInstance = instances[i];
|
||||||
|
|
||||||
var instance = context.Instances[i];
|
var instance = context.Instances[i];
|
||||||
uniforms["ModelMatrix"].SetMatrix4(instance.ModelMatrix);
|
shaderInstance["ModelMatrix"].SetMatrix4(instance.ModelMatrix);
|
||||||
uniforms["TextureOffset"].SetUniform2(instance.TexturePosition);
|
shaderInstance["TextureOffset"].SetUniform2(instance.TexturePosition);
|
||||||
uniforms["TextureScale"].SetUniform2(instance.TextureScale);
|
shaderInstance["TextureScale"].SetUniform2(instance.TextureScale);
|
||||||
|
}
|
||||||
return true;
|
|
||||||
});*/
|
|
||||||
|
|
||||||
// Fragment Uniforms
|
// Fragment Uniforms
|
||||||
Uniforms["Tint"].SetUniform4(context.Material.Tint);
|
Uniforms["Tint"].SetUniform4(context.Material.Tint);
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Security.Authentication.ExtendedProtection.Configuration;
|
using System.Security.Authentication.ExtendedProtection.Configuration;
|
||||||
|
|
@ -52,13 +53,17 @@ namespace SM_TEST
|
||||||
|
|
||||||
private static void WindowOnLoad(object sender, EventArgs e)
|
private static void WindowOnLoad(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
scene.ShowAxisHelper = true;
|
//scene.ShowAxisHelper = true;
|
||||||
|
|
||||||
DrawObject2D kasten = new DrawObject2D();
|
DrawObject2D kasten = new DrawObject2D();
|
||||||
kasten.Texture = new Texture(new Bitmap("herosword.png"));
|
kasten.Texture = new Texture(new Bitmap("herosword.png"));
|
||||||
kasten.Transform.ApplyTextureSize(kasten.Texture, 500);
|
kasten.Transform.ApplyTextureSize(kasten.Texture, 500);
|
||||||
scene.Objects.Add(kasten);
|
scene.Objects.Add(kasten);
|
||||||
|
|
||||||
|
DrawText text = new DrawText(font, "Text");
|
||||||
|
text.Transform.Position.Set(0, 500);
|
||||||
|
scene.Objects.Add(text);
|
||||||
|
|
||||||
//particles.Trigger();
|
//particles.Trigger();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue