Loads and loads of small improvements I added while developing on my game
This commit is contained in:
parent
41421b1df9
commit
a7c71e7ea1
107 changed files with 2278 additions and 1023 deletions
|
|
@ -1,7 +1,10 @@
|
|||
using System.ComponentModel;
|
||||
using OpenTK;
|
||||
using OpenTK.Graphics.OpenGL4;
|
||||
using SM.Base.Contexts;
|
||||
using SM.Base;
|
||||
using SM.Base.Drawing;
|
||||
using SM.Base.PostProcess;
|
||||
using SM.Base.Windows;
|
||||
using SM.OGL.Framebuffer;
|
||||
using SM.OGL.Texture;
|
||||
using SM.Utility;
|
||||
|
|
@ -10,37 +13,76 @@ namespace SM.Base.PostEffects
|
|||
{
|
||||
public class BloomEffect : PostProcessEffect
|
||||
{
|
||||
private static BezierCurve _defaultCurve = new BezierCurve(Vector2.UnitY, new Vector2(0.32f, 1), new Vector2(0.432f, 0), new Vector2(1,0));
|
||||
|
||||
private const float _textureScale = .75f;
|
||||
|
||||
private Framebuffer _bloomBuffer1;
|
||||
private Framebuffer _bloomBuffer2;
|
||||
|
||||
private ColorAttachment _xBuffer;
|
||||
private ColorAttachment _yBuffer;
|
||||
|
||||
private PostProcessShader _shader = new PostProcessShader(AssemblyUtility.ReadAssemblyFile("SM.Base.PostEffects.Shaders.bloom_blur.glsl"));
|
||||
private PostProcessShader _mergeShader = new PostProcessShader(AssemblyUtility.ReadAssemblyFile("SM.Base.PostEffects.Shaders.bloom_merge.glsl"));
|
||||
private PostProcessShader _shader = new PostProcessShader(AssemblyUtility.ReadAssemblyFile(SMRenderer.PostProcessPath+".bloom_blur.glsl"));
|
||||
private PostProcessShader _mergeShader = new PostProcessShader(AssemblyUtility.ReadAssemblyFile(SMRenderer.PostProcessPath+".bloom_merge_vert.glsl"), AssemblyUtility.ReadAssemblyFile(SMRenderer.PostProcessPath+".bloom_merge.glsl"));
|
||||
|
||||
private int _bloomLocation;
|
||||
private bool _hdr;
|
||||
private Framebuffer _source;
|
||||
|
||||
public int Iterations = 5;
|
||||
public float Threshold = 0.8f;
|
||||
|
||||
public float[] Weights = { 0.227027f, 0.1945946f, 0.1216216f, 0.054054f, 0.016216f };
|
||||
|
||||
public BloomEffect(int bloomLocation, bool hdr = false)
|
||||
private BezierCurve _weightCurve ;
|
||||
private float[] _weights;
|
||||
|
||||
public int Iterations = 1;
|
||||
public float Threshold = .8f;
|
||||
public float Power = 1;
|
||||
|
||||
public bool Enable = true;
|
||||
|
||||
public float MinAmount = 0;
|
||||
public float MaxAmount = 1;
|
||||
public TextureBase AmountMap;
|
||||
public TextureTransformation AmountTransform = new TextureTransformation();
|
||||
|
||||
public BezierCurve WeightCurve
|
||||
{
|
||||
_bloomLocation = bloomLocation;
|
||||
get => _weightCurve;
|
||||
set
|
||||
{
|
||||
_weightCurve = value;
|
||||
UpdateWeights();
|
||||
}
|
||||
}
|
||||
|
||||
public int WeightCurvePickAmount = 4;
|
||||
|
||||
|
||||
public BloomEffect(Framebuffer source = null, bool hdr = false)
|
||||
{
|
||||
_source = source;
|
||||
_hdr = hdr;
|
||||
|
||||
WeightCurve = _defaultCurve;
|
||||
}
|
||||
|
||||
|
||||
private void UpdateWeights()
|
||||
{
|
||||
_weights = new float[WeightCurvePickAmount];
|
||||
|
||||
for (int i = 0; i < WeightCurvePickAmount; i++)
|
||||
{
|
||||
_weights[i] = _weightCurve.CalculatePoint((float)(i + 1) / (WeightCurvePickAmount + 1)).Y;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void InitProcess()
|
||||
{
|
||||
Pipeline.MainFramebuffer.ColorAttachments["color"].PixelInformation = PixelInformation.RGBA_HDR;
|
||||
_source.ColorAttachments["color"].PixelInformation = PixelInformation.RGBA_HDR;
|
||||
|
||||
_bloomBuffer1 = new Framebuffer(SMRenderer.CurrentWindow);
|
||||
_bloomBuffer1 = new Framebuffer(Pipeline.ConnectedWindow, _textureScale);
|
||||
_bloomBuffer1.Append("xBuffer", _xBuffer = new ColorAttachment(0, PixelInformation.RGBA_HDR));
|
||||
_bloomBuffer1.Compile();
|
||||
_bloomBuffer2 = new Framebuffer(SMRenderer.CurrentWindow);
|
||||
_bloomBuffer2 = new Framebuffer(Pipeline.ConnectedWindow, _textureScale);
|
||||
_bloomBuffer2.Append("yBuffer", _yBuffer = new ColorAttachment(0, PixelInformation.RGBA_HDR));
|
||||
_bloomBuffer2.Compile();
|
||||
|
||||
|
|
@ -50,35 +92,51 @@ namespace SM.Base.PostEffects
|
|||
|
||||
public override void Draw(DrawContext context)
|
||||
{
|
||||
Framebuffer target = Framebuffer.GetCurrentlyActive();
|
||||
|
||||
bool first = true, hoz = true;
|
||||
int iter = Iterations * 2;
|
||||
for (int i = 0; i < iter; i++)
|
||||
if (Enable)
|
||||
{
|
||||
(hoz ? _bloomBuffer1 : _bloomBuffer2).Activate();
|
||||
|
||||
_shader.Draw(first ? Pipeline.MainFramebuffer.ColorAttachments["color"] : (hoz ? _yBuffer : _xBuffer), collection =>
|
||||
GL.Viewport(0,0, (int)(Pipeline.ConnectedWindow.Width * _textureScale), (int)(Pipeline.ConnectedWindow.Height * _textureScale));
|
||||
|
||||
Framebuffer target = Framebuffer.GetCurrentlyActive();
|
||||
bool first = true, hoz = true;
|
||||
int iter = Iterations * 2;
|
||||
for (int i = 0; i < iter; i++)
|
||||
{
|
||||
collection["First"].SetUniform1(first);
|
||||
collection["Threshold"].SetUniform1(Threshold);
|
||||
(hoz ? _bloomBuffer1 : _bloomBuffer2).Activate();
|
||||
|
||||
_shader.Draw(collection =>
|
||||
{
|
||||
collection["renderedTexture"].SetTexture(first ? _source.ColorAttachments["color"] : (hoz ? _yBuffer : _xBuffer));
|
||||
collection["RenderScale"].SetUniform1(_textureScale);
|
||||
|
||||
collection["Horizontal"].SetUniform1(hoz);
|
||||
|
||||
collection["Weights"].SetUniform1(Weights);
|
||||
collection["WeightCount"].SetUniform1(Weights.Length);
|
||||
});
|
||||
|
||||
hoz = !hoz;
|
||||
if (first) first = false;
|
||||
collection["First"].SetUniform1(first);
|
||||
collection["Threshold"].SetUniform1(Threshold);
|
||||
|
||||
collection["Horizontal"].SetUniform1(hoz);
|
||||
|
||||
collection["Weights"].SetUniform1(_weights);
|
||||
collection["WeightCount"].SetUniform1(WeightCurvePickAmount);
|
||||
collection["Power"].SetUniform1(Power);
|
||||
});
|
||||
|
||||
hoz = !hoz;
|
||||
if (first) first = false;
|
||||
}
|
||||
|
||||
GL.Viewport(Pipeline.ConnectedWindow.ClientRectangle);
|
||||
target.Activate();
|
||||
}
|
||||
|
||||
target.Activate();
|
||||
_mergeShader.Draw(Pipeline.MainFramebuffer.ColorAttachments["color"], collection =>
|
||||
_mergeShader.Draw(collection =>
|
||||
{
|
||||
collection["Scene"].SetTexture(_source.ColorAttachments["color"]);
|
||||
collection["Bloom"].SetTexture(_yBuffer);
|
||||
|
||||
collection["Exposure"].SetUniform1(context.UsedCamera.Exposure);
|
||||
collection["MinAmount"].SetUniform1(MinAmount);
|
||||
collection["MaxAmount"].SetUniform1(MaxAmount);
|
||||
collection["AmountMap"].SetTexture(AmountMap, collection["HasAmountMap"]);
|
||||
collection["TextureTransform"].SetMatrix3(AmountTransform.GetMatrix());
|
||||
|
||||
collection["Exposure"].SetUniform1(context.UseCamera.Exposure);
|
||||
collection["HDR"].SetUniform1(_hdr);
|
||||
});
|
||||
}
|
||||
|
|
|
|||
46
SMCode/SM.Base/PostEffects/PostProcessFinals.cs
Normal file
46
SMCode/SM.Base/PostEffects/PostProcessFinals.cs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
using System.Windows.Controls;
|
||||
using OpenTK.Graphics.OpenGL4;
|
||||
using SM.Base.PostProcess;
|
||||
using SM.Base.Windows;
|
||||
using SM.OGL.Framebuffer;
|
||||
using SM.Utility;
|
||||
|
||||
namespace SM.Base.PostEffects
|
||||
{
|
||||
public class PostProcessFinals
|
||||
{
|
||||
static PostProcessShader _hdrExposureShader = new PostProcessShader(AssemblyUtility.ReadAssemblyFile(SMRenderer.PostProcessPath+".finalize_hdr.glsl"));
|
||||
static PostProcessShader _gammaShader = new PostProcessShader(AssemblyUtility.ReadAssemblyFile(SMRenderer.PostProcessPath + ".finalize_gamma.glsl"));
|
||||
|
||||
public static float Gamma = 2.2f;
|
||||
|
||||
public static void ResolveMultisampledBuffers(Framebuffer multisampledBuffers, Framebuffer target)
|
||||
{
|
||||
multisampledBuffers.Activate(FramebufferTarget.ReadFramebuffer);
|
||||
target.Activate(FramebufferTarget.DrawFramebuffer);
|
||||
GL.BlitFramebuffer(0, 0, (int)multisampledBuffers.Size.X, (int)multisampledBuffers.Size.Y, 0, 0, (int)target.Size.X, (int)target.Size.Y, ClearBufferMask.ColorBufferBit, BlitFramebufferFilter.Nearest);
|
||||
|
||||
target.Activate();
|
||||
}
|
||||
|
||||
public static void FinalizeHDR(ColorAttachment attachment, float exposure)
|
||||
{
|
||||
|
||||
_hdrExposureShader.Draw(u =>
|
||||
{
|
||||
u["Gamma"].SetUniform1(Gamma);
|
||||
u["Exposure"].SetUniform1(exposure);
|
||||
u["Scene"].SetTexture(attachment);
|
||||
});
|
||||
}
|
||||
|
||||
public static void FinalizeGamma(ColorAttachment attachment)
|
||||
{
|
||||
_gammaShader.Draw(u =>
|
||||
{
|
||||
u["Gamma"].SetUniform1(Gamma);
|
||||
u["Scene"].SetTexture(attachment);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,8 @@
|
|||
#version 330
|
||||
#define PI 3.14159265359
|
||||
|
||||
uniform sampler2D renderedTexture;
|
||||
uniform float RenderScale;
|
||||
|
||||
uniform bool First;
|
||||
uniform float Threshold;
|
||||
|
|
@ -9,27 +11,35 @@ uniform bool Horizontal;
|
|||
|
||||
uniform float[32] Weights;
|
||||
uniform int WeightCount;
|
||||
uniform float Power;
|
||||
|
||||
layout(location = 0) out vec4 color;
|
||||
|
||||
vec4 GetRenderColorOffset(vec2 offset);
|
||||
|
||||
float brightness(vec3 c)
|
||||
{
|
||||
return max(max(c.r, c.g), c.b);
|
||||
}
|
||||
|
||||
float bright;
|
||||
|
||||
float GetWeight(int dif) {
|
||||
return Weights[dif];
|
||||
}
|
||||
|
||||
void main() {
|
||||
vec3 thres = vec3(First ? Threshold : 0);
|
||||
|
||||
vec2 tex_offset = 1.0 / textureSize(renderedTexture, 0);
|
||||
vec3 result = max(GetRenderColorOffset(vec2(0)).rgb - thres, 0) * Weights[0];
|
||||
vec2 tex_offset = 1.0 / textureSize(renderedTexture, 0) * vec2(Horizontal ? 1 : 0, Horizontal ? 0 : 1);
|
||||
|
||||
if (Horizontal) {
|
||||
for(int i = 1; i < WeightCount; i++) {
|
||||
result += max(GetRenderColorOffset(vec2(tex_offset.x * i, 0)).rgb - thres, 0) * Weights[i];
|
||||
result += max(GetRenderColorOffset(vec2(-tex_offset.x * i, 0)).rgb - thres, 0) * Weights[i];
|
||||
}
|
||||
} else {
|
||||
for(int i = 1; i < WeightCount; i++) {
|
||||
result += GetRenderColorOffset(vec2(0, tex_offset.x * i)).rgb * Weights[i];
|
||||
result += GetRenderColorOffset(vec2(0, -tex_offset.x * i)).rgb * Weights[i];
|
||||
}
|
||||
vec3 result = max(GetRenderColorOffset(vec2(0)).rgb - thres, 0) * (First ? Power : 1);
|
||||
result *= GetWeight(0);
|
||||
|
||||
for(int i = 1; i < WeightCount; i++) {
|
||||
result += max(GetRenderColorOffset(tex_offset * i).rgb - thres, 0) * (First ? Power : 1) * GetWeight(i);
|
||||
result += max(GetRenderColorOffset(-tex_offset * i).rgb - thres, 0) * (First ? Power : 1) * GetWeight(i);
|
||||
}
|
||||
|
||||
color = vec4(result, 1);
|
||||
}
|
||||
|
|
@ -1,10 +1,16 @@
|
|||
#version 330
|
||||
|
||||
in vec2 vTexture;
|
||||
in vec2 TransformedTexture;
|
||||
|
||||
uniform sampler2D renderedTexture;
|
||||
uniform sampler2D Scene;
|
||||
uniform sampler2D Bloom;
|
||||
|
||||
uniform float MinAmount;
|
||||
uniform float MaxAmount;
|
||||
uniform sampler2D AmountMap;
|
||||
uniform bool HasAmountMap;
|
||||
|
||||
uniform float Exposure;
|
||||
uniform bool HDR;
|
||||
|
||||
|
|
@ -12,10 +18,13 @@ layout(location = 0) out vec4 color;
|
|||
|
||||
void main() {
|
||||
vec3 result = texture(Bloom, vTexture).rgb;
|
||||
if (HasAmountMap) result *= clamp(length(texture(AmountMap, TransformedTexture).rgb) * (MaxAmount - MinAmount) + MinAmount, 0, 1);
|
||||
if (!HDR) {
|
||||
result = vec3(1.0) - exp(-result * Exposure);
|
||||
}
|
||||
result += texture(renderedTexture, vTexture).rgb;
|
||||
|
||||
|
||||
result = texture(Scene, vTexture).rgb + result;
|
||||
|
||||
color = vec4(result, 1);
|
||||
}
|
||||
11
SMCode/SM.Base/PostEffects/Shaders/bloom_merge_vert.glsl
Normal file
11
SMCode/SM.Base/PostEffects/Shaders/bloom_merge_vert.glsl
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#version 330
|
||||
|
||||
layout(location = 1) in vec2 aTex;
|
||||
|
||||
uniform mat3 TextureTransform;
|
||||
|
||||
out vec2 TransformedTexture;
|
||||
|
||||
void vertex() {
|
||||
TransformedTexture = vec2(TextureTransform * vec3(aTex, 1));
|
||||
}
|
||||
12
SMCode/SM.Base/PostEffects/Shaders/finalize_gamma.glsl
Normal file
12
SMCode/SM.Base/PostEffects/Shaders/finalize_gamma.glsl
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#version 330
|
||||
|
||||
in vec2 vTexture;
|
||||
|
||||
uniform sampler2D Scene;
|
||||
uniform float Gamma;
|
||||
|
||||
layout(location = 0) out vec4 color;
|
||||
|
||||
void main() {
|
||||
color = vec4(pow(texture(Scene, vTexture).rgb, vec3(1 / Gamma)), 1);
|
||||
}
|
||||
15
SMCode/SM.Base/PostEffects/Shaders/finalize_hdr.glsl
Normal file
15
SMCode/SM.Base/PostEffects/Shaders/finalize_hdr.glsl
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
#version 330
|
||||
|
||||
in vec2 vTexture;
|
||||
|
||||
uniform sampler2D Scene;
|
||||
uniform float Exposure;
|
||||
uniform float Gamma;
|
||||
|
||||
layout(location = 0) out vec4 color;
|
||||
|
||||
void main() {
|
||||
vec3 result = vec3(1) - exp(-texture(Scene, vTexture).rgb * Exposure);
|
||||
|
||||
color = vec4(pow(result, vec3(1 / Gamma)), 1);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue