Skip to content

Commit

Permalink
Merge pull request #176 from andy840119/fix-shader-sizing-issue
Browse files Browse the repository at this point in the history
Fix sizing calculation issue in the shader.
  • Loading branch information
andy840119 authored May 4, 2022
2 parents daf1e6e + 2eb6e9f commit f408c38
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 41 deletions.
3 changes: 1 addition & 2 deletions osu.Framework.Font.Tests/Shaders/OutlineShaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ public void TestComputeScreenSpaceDrawQuad(int radius, float expectedX, float ex
Radius = radius
};

var quad = shader.ComputeScreenSpaceDrawQuad(new Quad(5, 5, 10, 10));
var rectangle = quad.AABBFloat;
var rectangle = shader.ComputeDrawRectangle(new RectangleF(5, 5, 10, 10));
Assert.AreEqual(expectedX, rectangle.X);
Assert.AreEqual(expectedY, rectangle.Y);
Assert.AreEqual(expectedWidth, rectangle.Width);
Expand Down
3 changes: 1 addition & 2 deletions osu.Framework.Font.Tests/Shaders/ShadowShaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ public void TestComputeScreenSpaceDrawQuad(float offsetX, float offsetY, float e
ShadowOffset = new Vector2(offsetX, offsetY)
};

var quad = shader.ComputeScreenSpaceDrawQuad(new Quad(5, 5, 10, 10));
var rectangle = quad.AABBFloat;
var rectangle = shader.ComputeDrawRectangle(new RectangleF(5, 5, 10, 10));
Assert.AreEqual(expectedX, rectangle.X);
Assert.AreEqual(expectedY, rectangle.Y);
Assert.AreEqual(expectedWidth, rectangle.Width);
Expand Down
9 changes: 3 additions & 6 deletions osu.Framework.Font.Tests/Shaders/StepShaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ public void TestComputeScreenSpaceDrawQuad()
}
};

var quad = shader.ComputeScreenSpaceDrawQuad(new Quad(5, 5, 10, 10));
var rectangle = quad.AABBFloat;
var rectangle = shader.ComputeDrawRectangle(new RectangleF(5, 5, 10, 10));
Assert.AreEqual(5 - outline_radius, rectangle.X);
Assert.AreEqual(5 - outline_radius, rectangle.Y);
Assert.AreEqual(10 + outline_radius * 2 + shadow_offset_x, rectangle.Width);
Expand All @@ -86,8 +85,7 @@ public void TestEmptyComputeScreenSpaceDrawQuadWithEmptyShader()
{
var shader = new StepShader();

var quad = shader.ComputeScreenSpaceDrawQuad(new Quad(5, 5, 10, 10));
var rectangle = quad.AABBFloat;
var rectangle = shader.ComputeDrawRectangle(new RectangleF(5, 5, 10, 10));
Assert.AreEqual(5, rectangle.X);
Assert.AreEqual(5, rectangle.Y);
Assert.AreEqual(10, rectangle.Width);
Expand Down Expand Up @@ -123,8 +121,7 @@ public void TestEmptyComputeScreenSpaceDrawQuadNoneMatchedShader()
}
};

var quad = shader.ComputeScreenSpaceDrawQuad(new Quad(5, 5, 10, 10));
var rectangle = quad.AABBFloat;
var rectangle = shader.ComputeDrawRectangle(new RectangleF(5, 5, 10, 10));
Assert.AreEqual(5, rectangle.X);
Assert.AreEqual(5, rectangle.Y);
Assert.AreEqual(10, rectangle.Width);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

namespace osu.Framework.Graphics.Shaders
{
public interface IApplicableToDrawQuad
public interface IApplicableToDrawRectangle
{
Quad ComputeScreenSpaceDrawQuad(Quad originDrawQuad);
RectangleF ComputeDrawRectangle(RectangleF originDrawRectangle);
}
}
9 changes: 3 additions & 6 deletions osu.Framework.Font/Graphics/Shaders/OutlineShader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace osu.Framework.Graphics.Shaders
{
public class OutlineShader : InternalShader, IApplicableToCharacterSize, IApplicableToDrawQuad, IHasTextureSize, IHasInflationPercentage
public class OutlineShader : InternalShader, IApplicableToCharacterSize, IApplicableToDrawRectangle, IHasTextureSize, IHasInflationPercentage
{
public override string ShaderName => "Outline";

Expand All @@ -33,10 +33,7 @@ public override void ApplyValue()
public RectangleF ComputeCharacterDrawRectangle(RectangleF originalCharacterDrawRectangle)
=> originalCharacterDrawRectangle.Inflate(Math.Max(Radius, 0));

public Quad ComputeScreenSpaceDrawQuad(Quad originDrawQuad)
{
var rectangle = ComputeCharacterDrawRectangle(originDrawQuad.AABBFloat);
return Quad.FromRectangle(rectangle);
}
public RectangleF ComputeDrawRectangle(RectangleF originDrawRectangle)
=> ComputeCharacterDrawRectangle(originDrawRectangle);
}
}
10 changes: 3 additions & 7 deletions osu.Framework.Font/Graphics/Shaders/ShadowShader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace osu.Framework.Graphics.Shaders
{
public class ShadowShader : InternalShader, IApplicableToDrawQuad, IHasTextureSize, IHasInflationPercentage
public class ShadowShader : InternalShader, IApplicableToDrawRectangle, IHasTextureSize, IHasInflationPercentage
{
public override string ShaderName => "Shadow";

Expand All @@ -25,17 +25,13 @@ public override void ApplyValue()
GetUniform<Vector2>(@"g_Offset").UpdateValue(ref shadowOffset);
}

public Quad ComputeScreenSpaceDrawQuad(Quad originDrawQuad)
{
var rectangle = originDrawQuad.AABBFloat.Inflate(new MarginPadding
public RectangleF ComputeDrawRectangle(RectangleF originDrawRectangle)
=> originDrawRectangle.Inflate(new MarginPadding
{
Left = Math.Max(-ShadowOffset.X, 0),
Right = Math.Max(ShadowOffset.X, 0),
Top = Math.Max(-ShadowOffset.Y, 0),
Bottom = Math.Max(ShadowOffset.Y, 0),
});

return Quad.FromRectangle(rectangle);
}
}
}
8 changes: 4 additions & 4 deletions osu.Framework.Font/Graphics/Shaders/StepShader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace osu.Framework.Graphics.Shaders
{
public class StepShader : IStepShader, IApplicableToCharacterSize, IApplicableToDrawQuad
public class StepShader : IStepShader, IApplicableToCharacterSize, IApplicableToDrawRectangle
{
public string Name { get; set; }

Expand Down Expand Up @@ -50,8 +50,8 @@ public RectangleF ComputeCharacterDrawRectangle(RectangleF originalCharacterDraw
StepShaders.OfType<IApplicableToCharacterSize>()
.Aggregate(originalCharacterDrawRectangle, (rectangle, shader) => shader.ComputeCharacterDrawRectangle(rectangle));

public Quad ComputeScreenSpaceDrawQuad(Quad originDrawQuad) =>
StepShaders.OfType<IApplicableToDrawQuad>()
.Aggregate(originDrawQuad, (quad, shader) => shader.ComputeScreenSpaceDrawQuad(quad));
public RectangleF ComputeDrawRectangle(RectangleF originDrawRectangle) =>
StepShaders.OfType<IApplicableToDrawRectangle>()
.Aggregate(originDrawRectangle, (quad, shader) => shader.ComputeDrawRectangle(quad));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@ public partial class KaraokeSpriteText<T>
{
protected override Quad ComputeScreenSpaceDrawQuad()
{
var quad = ToScreenSpace(DrawRectangle);
var lyricSpriteTextDrawRectangle = LeftLyricTextShaders.Concat(RightLyricTextShaders).OfType<IApplicableToDrawQuad>()
.Select(x => x.ComputeScreenSpaceDrawQuad(quad).AABBFloat)
.Aggregate(quad.AABBFloat, RectangleF.Union);
var lyricSpriteTextDrawRectangle = LeftLyricTextShaders.Concat(RightLyricTextShaders).OfType<IApplicableToDrawRectangle>()
.Select(x => x.ComputeDrawRectangle(DrawRectangle))
.Aggregate(DrawRectangle, RectangleF.Union);

// after union the size of left side and right side of the internal sprite text draw size, need to calculate the extra size in the karaoke sprite text itself.
var drawRectangle = Shaders.OfType<IApplicableToDrawQuad>()
.Select(x => x.ComputeScreenSpaceDrawQuad(quad).AABBFloat)
var drawRectangle = Shaders.OfType<IApplicableToDrawRectangle>()
.Select(x => x.ComputeDrawRectangle(lyricSpriteTextDrawRectangle))
.Aggregate(lyricSpriteTextDrawRectangle, RectangleF.Union);

return Quad.FromRectangle(drawRectangle);
return ToScreenSpace(drawRectangle);
}

protected override DrawNode CreateDrawNode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ public partial class LyricSpriteText
protected override Quad ComputeScreenSpaceDrawQuad()
{
// make draw size become bigger (for not masking the shader).
var quad = ToScreenSpace(DrawRectangle);
var drawRectangle = Shaders.OfType<IApplicableToDrawQuad>()
.Select(x => x.ComputeScreenSpaceDrawQuad(quad).AABBFloat)
.Aggregate(quad.AABBFloat, RectangleF.Union);
var drawRectangle = Shaders.OfType<IApplicableToDrawRectangle>()
.Select(x => x.ComputeDrawRectangle(DrawRectangle))
.Aggregate(DrawRectangle, RectangleF.Union);

return Quad.FromRectangle(drawRectangle);
return ToScreenSpace(drawRectangle);
}

protected override DrawNode CreateDrawNode()
Expand Down

0 comments on commit f408c38

Please sign in to comment.