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
|
||||
#define maxInstances //!instanceMax
|
||||
|
||||
struct Instance {
|
||||
mat4 ModelMatrix;
|
||||
vec2 TextureOffset;
|
||||
vec2 TextureScale;
|
||||
};
|
||||
|
||||
layout(location = 0) in vec3 aPos;
|
||||
layout(location = 1) in vec2 aTex;
|
||||
layout(location = 3) in vec4 aColor;
|
||||
|
||||
uniform mat4 MVP;
|
||||
uniform Instance[maxInstances] Instances;
|
||||
uniform bool HasVColor;
|
||||
|
||||
out vec2 vTexture;
|
||||
|
|
@ -14,7 +20,7 @@ out vec4 vColor;
|
|||
out vec3 FragPos;
|
||||
|
||||
void ApplyTexModifier() {
|
||||
vTexture = aTex;
|
||||
vTexture = aTex * Instances[gl_InstanceID].TextureScale + Instances[gl_InstanceID].TextureOffset;
|
||||
}
|
||||
|
||||
void CheckVertexColor() {
|
||||
|
|
@ -23,7 +29,7 @@ void CheckVertexColor() {
|
|||
}
|
||||
|
||||
void ApplyModelTransformation() {
|
||||
gl_Position = MVP * vec4(aPos, 1);
|
||||
gl_Position = MVP * Instances[gl_InstanceID].ModelMatrix * vec4(aPos, 1);
|
||||
|
||||
FragPos = vec3(vec4(aPos, 1));
|
||||
}
|
||||
|
|
@ -15,6 +15,9 @@ namespace SM.Base.Textures
|
|||
/// </summary>
|
||||
public class Texture : TextureBase
|
||||
{
|
||||
private int? _width;
|
||||
private int? _height;
|
||||
|
||||
/// <summary>
|
||||
/// Decides if the bitmap will automatically dispose itself.
|
||||
/// </summary>
|
||||
|
|
@ -28,16 +31,15 @@ namespace SM.Base.Textures
|
|||
/// <inheritdoc />
|
||||
public override int Width
|
||||
{
|
||||
get => Map.Width;
|
||||
protected set
|
||||
{ }
|
||||
get => _width ?? Map.Width;
|
||||
protected set => _width = value;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override int Height
|
||||
{
|
||||
get => Map.Height;
|
||||
protected set {}
|
||||
get => _height ?? Map.Height;
|
||||
protected set => _height = value;
|
||||
}
|
||||
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>
|
||||
/// This create a new uniform manager
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -1,50 +1,33 @@
|
|||
#region usings
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
#endregion
|
||||
using System.Collections.Generic;
|
||||
using OpenTK.Graphics.OpenGL;
|
||||
|
||||
namespace SM.OGL.Shaders
|
||||
{
|
||||
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 GenericShader Parent { 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>();
|
||||
|
||||
for (int i = 0; i < uniformNames.Count; i++)
|
||||
{
|
||||
dic.Add(uniformNames[i], new Uniform(Name + $"[{index}]." + uniformNames[i], ParentShader, Parent));
|
||||
}
|
||||
}
|
||||
|
||||
public void Set(Action<int, Uniform> setAction)
|
||||
{
|
||||
for (var i = 0; i < Size; i++) setAction(i, new Uniform(Location + i));
|
||||
}
|
||||
|
||||
public void Set(Func<int, UniformCollection, bool> setAction)
|
||||
{
|
||||
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;
|
||||
}
|
||||
return storedUniforms[index];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -82,20 +82,16 @@ namespace SM.OGL.Shaders
|
|||
{
|
||||
Location = loc,
|
||||
Name = keySplits[0],
|
||||
Parent = ParentShader,
|
||||
Struct = keySplits.Length > 2
|
||||
Parent = this,
|
||||
ParentShader = ParentShader
|
||||
};
|
||||
|
||||
arrayFilled = true;
|
||||
lastArrayKey = keySplits[0];
|
||||
}
|
||||
|
||||
var curIndex = int.Parse(keySplits[1]);
|
||||
if (array.Size < curIndex) array.Size = curIndex;
|
||||
|
||||
if (array.Struct)
|
||||
if (!array.Offsets.ContainsKey(keySplits[2].Trim('.')))
|
||||
array.Offsets.Add(keySplits[2].Trim('.'), loc - array.Location);
|
||||
if (keySplits[1] == "0")
|
||||
array.uniformNames.Add(keySplits[2].Substring(1));
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
|||
|
|
@ -60,7 +60,7 @@ namespace SM2D.Drawing
|
|||
context.Material = _material;
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -27,18 +27,16 @@ namespace SM2D.Shader
|
|||
Uniforms["MVP"].SetMatrix4(context.ModelMaster * context.View * context.World);
|
||||
Uniforms["HasVColor"]
|
||||
.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];
|
||||
uniforms["ModelMatrix"].SetMatrix4(instance.ModelMatrix);
|
||||
uniforms["TextureOffset"].SetUniform2(instance.TexturePosition);
|
||||
uniforms["TextureScale"].SetUniform2(instance.TextureScale);
|
||||
|
||||
return true;
|
||||
});*/
|
||||
shaderInstance["ModelMatrix"].SetMatrix4(instance.ModelMatrix);
|
||||
shaderInstance["TextureOffset"].SetUniform2(instance.TexturePosition);
|
||||
shaderInstance["TextureScale"].SetUniform2(instance.TextureScale);
|
||||
}
|
||||
|
||||
// Fragment Uniforms
|
||||
Uniforms["Tint"].SetUniform4(context.Material.Tint);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Drawing;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Security.Authentication.ExtendedProtection.Configuration;
|
||||
|
|
@ -52,13 +53,17 @@ namespace SM_TEST
|
|||
|
||||
private static void WindowOnLoad(object sender, EventArgs e)
|
||||
{
|
||||
scene.ShowAxisHelper = true;
|
||||
//scene.ShowAxisHelper = true;
|
||||
|
||||
DrawObject2D kasten = new DrawObject2D();
|
||||
kasten.Texture = new Texture(new Bitmap("herosword.png"));
|
||||
kasten.Transform.ApplyTextureSize(kasten.Texture, 500);
|
||||
scene.Objects.Add(kasten);
|
||||
|
||||
DrawText text = new DrawText(font, "Text");
|
||||
text.Transform.Position.Set(0, 500);
|
||||
scene.Objects.Add(text);
|
||||
|
||||
//particles.Trigger();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue