Skip to content

Commit

Permalink
Fix the rainbow shader.
Browse files Browse the repository at this point in the history
Co-authored-by: WebFreak001 <[email protected]>
  • Loading branch information
andy840119 and WebFreak001 committed Mar 19, 2023
1 parent f20da1b commit 173718d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 33 deletions.
44 changes: 28 additions & 16 deletions osu.Framework.Font/Graphics/Shaders/RainbowShader.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright (c) karaoke.dev <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Runtime.InteropServices;
using osu.Framework.Graphics.Rendering;
using osu.Framework.Graphics.Shaders.Types;
using osuTK;

namespace osu.Framework.Graphics.Shaders;
Expand All @@ -23,24 +25,34 @@ public class RainbowShader : InternalShader, IHasCurrentTime

public float Mix { get; set; } = 0.5f;

private IUniformBuffer<RainbowParameters>? rainbowParametersBuffer;

public override void ApplyValue(IRenderer renderer)
{
var uv = Uv;
GetUniform<Vector2>(@"g_Uv").UpdateValue(ref uv);

var speed = Speed;
GetUniform<float>(@"g_Speed").UpdateValue(ref speed);

var saturation = Saturation;
GetUniform<float>(@"g_Saturation").UpdateValue(ref saturation);

var brightness = Brightness;
GetUniform<float>(@"g_Brightness").UpdateValue(ref brightness);

var section = Section;
GetUniform<float>(@"g_Section").UpdateValue(ref section);
rainbowParametersBuffer ??= renderer.CreateUniformBuffer<RainbowParameters>();

rainbowParametersBuffer.Data = new RainbowParameters
{
Uv = Uv,
Speed = Speed,
Saturation = Saturation,
Brightness = Brightness,
Section = Section,
Mix = Mix,
};

BindUniformBlock("m_RainbowParameters", rainbowParametersBuffer);
}

var mix = Mix;
GetUniform<float>(@"g_Mix").UpdateValue(ref mix);
[StructLayout(LayoutKind.Sequential, Pack = 1)]
private record struct RainbowParameters
{
public UniformVector2 Uv;
public UniformFloat Speed;
public UniformFloat Saturation;
public UniformFloat Brightness;
public UniformFloat Section;
public UniformFloat Mix;
private readonly UniformPadding4 pad1;
}
}
39 changes: 22 additions & 17 deletions osu.Framework.Font/Resources/Shaders/sh_Rainbow.fs
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
// see the demo: https://developer.amazon.com/es/blogs/appstore/post/acefafad-29ba-4f31-8dae-00805fda3f58/intro-to-shaders-and-surfaces-with-gamemaker-studio-2
#include "sh_CustomizedShaderGlobalUniforms.h"
#include "sh_Utils.h"

varying vec2 v_TexCoord;
varying vec4 v_Colour;
layout(location = 2) in highp vec2 v_TexCoord;

uniform lowp sampler2D m_Sampler;
layout(std140, set = 2, binding = 0) uniform m_RainbowParameters
{
highp vec2 g_Uv;
mediump float g_Speed;
mediump float g_Saturation;
mediump float g_Brightness;
mediump float g_Section;
mediump float g_Mix;
};

layout(set = 1, binding = 0) uniform lowp texture2D m_Texture;
layout(set = 1, binding = 1) uniform lowp sampler m_Sampler;

uniform vec2 g_Uv;
uniform float g_Speed;
uniform float g_Time;
uniform float g_Saturation;
uniform float g_Brightness;
uniform float g_Section;
uniform float g_Mix;
layout(location = 0) out vec4 o_Colour;

void main(void)
{
float pos = (v_TexCoord.y - g_Uv[0]) / (g_Uv[1] - g_Uv[0]);
vec4 texColor = toSRGB(texture2D(m_Sampler, v_TexCoord));
vec4 col = vec4(g_Section * ((g_Time * g_Speed) + pos), g_Saturation, g_Brightness, 1);
vec4 finalCol = mix(texColor, vec4(hsv2rgb(col).xyz, texColor.a), g_Mix);
gl_FragColor = finalCol;
float pos = (v_TexCoord.y - g_Uv[0]) / (g_Uv[1] - g_Uv[0]);
vec4 texColor = toSRGB(texture(sampler2D(m_Texture, m_Sampler), v_TexCoord));

vec4 col = vec4(g_Section * ((g_Time * g_Speed) + pos), g_Saturation, g_Brightness, 1);
vec4 finalCol = mix(texColor, vec4(hsv2rgb(col).xyz, texColor.a), g_Mix);

o_Colour = finalCol;
}

0 comments on commit 173718d

Please sign in to comment.