Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix outline shader issue by pre-calculate some params. #76

Open
andy840119 opened this issue Oct 30, 2021 · 2 comments
Open

Fix outline shader issue by pre-calculate some params. #76

andy840119 opened this issue Oct 30, 2021 · 2 comments
Labels
performance Peformance issue shader New or fix shader effect.

Comments

@andy840119
Copy link
Member

andy840119 commented Oct 30, 2021

lowp vec4 outline(sampler2D tex, int radius, mediump vec2 texCoord, mediump vec2 texSize, mediump vec4 colour)
{
	float angle = 0.0;
	float outlineAlpha = 0.0;
	
	for (int i = 0; i < SAMPLES; i++)
	{
		angle += 1.0 / (float(SAMPLES) / 2.0) * PI;

		// todo: might need to adjust step samples amount to fill the inner side.
		// but it will cause lots of performance issue if make step samples larger.
		// so should find a better algorithm to fill inner colour.
		for (int j = 1; j <= STEP_SAMPLES; j++)
		{
			vec2 testPoint = texCoord - vec2(sin(angle), cos(angle)) * (float(radius) * (1.0 / j)) / texSize;
			float sampledAlpha = texture2D(tex, testPoint).a;
			outlineAlpha = max(outlineAlpha, sampledAlpha);
		}
	}

	mediump vec4 ogCol = texture2D(tex, texCoord);
	vec4 outlineCol = mix(vec4(0.0), colour, outlineAlpha);

	return mix(outlineCol, ogCol, ogCol.a);
}

Here's the shader and it can see that sin(angle), cos(angle)) runs lots of time on every frame (maybe run draw_width * draw_height * SAMPLES(2) * STEP_SAMPLES(128) times in each frame)
Maybe it can be calculated at the beginning.
Not really sure has someting like void init(void) before void main(void) and only run one time.

@andy840119 andy840119 added the shader New or fix shader effect. label Oct 30, 2021
@andy840119
Copy link
Member Author

also should think another issue:
should outline be transparent if source is transparent image also?
Because it might be faster if not doing the remaining work if already get the colour from texture2D.

@andy840119
Copy link
Member Author

Also there's another things i cannot understand
How did osu-framework can make outline-link effect with sh_Blur.ts

Here's the demo:

// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using NUnit.Framework;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Effects;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Testing;
using osuTK;
using osuTK.Graphics;

namespace osu.Framework.Tests.Visual
{
    public class OutlineTestScene : TestScene
    {
        [Test]
        public void Test()
        {
            AddStep("Apply shader", () =>
            {
                Add(new Box
                {
                    RelativeSizeAxes = Axes.Both,
                    Colour = Color4.Red
                });

                Add(new SpriteText
                {
                    Text = "Outlined Text",
                    Font = new FontUsage(size: 72),
                }.WithEffect(new OutlineEffect
                {
                    BlurSigma = new Vector2(5f),
                    Strength = 100f,
                    Colour = Color4.Blue,
                    PadExtent = true,
                }));
            });
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Peformance issue shader New or fix shader effect.
Projects
None yet
Development

No branches or pull requests

1 participant