27.09.2020

~ Moved Default-Shader to 2D to provied 2D-specific feature
~ Fixed UVs in Polygon
This commit is contained in:
Michel Fedde 2020-09-27 11:58:14 +02:00
parent 617a7ef044
commit 2aa12f8d25
19 changed files with 166 additions and 83 deletions

View file

@ -0,0 +1,47 @@
using OpenTK.Graphics.OpenGL4;
using SM.Base.Contexts;
using SM.Base.Scene;
using SM.OGL.Shaders;
using SM.Utility;
namespace SM2D.Shader
{
public class Default2DShader : GenericShader, IShader
{
protected override bool AutoCompile { get; } = true;
public Default2DShader() : base(new ShaderFileCollection(
AssemblyUtility.ReadAssemblyFile("Shader.ShaderFiles.default.vert"),
AssemblyUtility.ReadAssemblyFile("Shader.ShaderFiles.default.frag")))
{
}
public void Draw(DrawContext context)
{
GL.UseProgram(this);
GL.BindVertexArray(context.Mesh);
// Vertex Uniforms
Uniforms["MVP"].SetMatrix4(context.View * context.World);
Uniforms["HasVColor"].SetUniform1(context.Mesh.AttribDataIndex.ContainsKey(3) && context.Mesh.AttribDataIndex[3] != null);
for (int i = 0; i < context.Instances.Length; i++)
{
GL.UniformMatrix4(Uniforms["ModelMatrix"] + i, false, ref context.Instances[i].ModelMatrix);
GL.Uniform2(Uniforms["TextureOffset"] + i, context.Instances[i].TexturePosition);
GL.Uniform2(Uniforms["TextureScale"] + i, context.Instances[i].TextureScale);
}
// Fragment Uniforms
Uniforms["Tint"].SetUniform4(context.Material.Tint);
Uniforms["Texture"].SetTexture(context.Material.Texture, Uniforms["UseTexture"]);
DrawObject(context.Mesh, context.Instances.Length);
CleanUp();
GL.UseProgram(0);
}
}
}

View file

@ -0,0 +1,15 @@
#version 330
in vec2 vTexture;
in vec4 vColor;
uniform vec4 Tint;
uniform bool UseTexture;
uniform sampler2D Texture;
layout(location = 0) out vec4 color;
void main() {
color = vColor * Tint;
if (UseTexture) color *= texture(Texture, vTexture);
}

View file

@ -0,0 +1,23 @@
#version 330
#define maxInstances 32
layout(location = 0) in vec3 aPos;
layout(location = 1) in vec2 aTex;
layout(location = 3) in vec4 aColor;
uniform mat4 MVP;
uniform bool HasVColor;
uniform mat4 ModelMatrix[maxInstances];
uniform vec2 TextureOffset[maxInstances];
uniform vec2 TextureScale[maxInstances];
out vec2 vTexture;
out vec4 vColor;
void main() {
vTexture = aTex * TextureScale[gl_InstanceID] + TextureOffset[gl_InstanceID];
if (HasVColor) vColor = aColor;
else vColor = vec4(1);
gl_Position = MVP * ModelMatrix[gl_InstanceID] * vec4(aPos, 1);
}