Skip to content

Commit

Permalink
Expose as quaternion, add interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
smoogipoo committed Nov 15, 2023
1 parent f29492b commit 41e8b81
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 17 deletions.
6 changes: 2 additions & 4 deletions osu.Framework.Tests/Visual/TestScenePerspectiveContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Textures;
using osuTK;
Expand All @@ -31,8 +30,7 @@ public TestScenePerspectiveContainer()
Child = sprite = new Sprite
{
RelativeSizeAxes = Axes.Both
},
ExtraRotation = Matrix3.CreateRotationX(MathF.PI / 2.5f)
}
});

AddSliderStep("X Rotation", 0, MathF.PI, 0, v =>
Expand All @@ -56,7 +54,7 @@ private void load(TextureStore textures)

private void updateRotation()
{
target.ExtraRotation = Matrix3.CreateRotationX(xRotation) * Matrix3.CreateRotationY(yRotation);
target.TransformTo(nameof(ExtraRotation), new Quaternion(xRotation, yRotation, 0), 500, Easing.OutQuint);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ protected class CompositeDrawableDrawNode : DrawNode, ICompositeDrawNode

private int sourceChildrenCount;

private bool hasExtraRotation;

public CompositeDrawableDrawNode(CompositeDrawable source)
: base(source)
{
Expand All @@ -90,8 +88,6 @@ public override void ApplyState()
RectangleF shrunkDrawRectangle = Source.DrawRectangle.Normalize();
shrunkDrawRectangle = shrunkDrawRectangle.Shrink(new Vector2(Math.Min(shrunkDrawRectangle.Width / 2, shrinkage), Math.Min(shrunkDrawRectangle.Height / 2, shrinkage)));

hasExtraRotation = Source.ExtraRotation != Matrix3.Identity;

maskingInfo = !Source.Masking
? null
: new MaskingInfo
Expand Down
8 changes: 4 additions & 4 deletions osu.Framework/Graphics/DrawInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public DrawInfo(Matrix4? matrix = null, Matrix4? matrixInverse = null)
/// <param name="shear">The shear amounts for both directions.</param>
/// <param name="origin">The center of rotation and scale.</param>
/// <param name="extraRotation"></param>
public void ApplyTransform(Vector2 translation, Vector2 scale, float rotation, Vector2 shear, Vector2 origin, Matrix3 extraRotation)
public void ApplyTransform(Vector2 translation, Vector2 scale, float rotation, Vector2 shear, Vector2 origin, Matrix4 extraRotation)
{
if (translation != Vector2.Zero)
{
Expand All @@ -44,10 +44,10 @@ public void ApplyTransform(Vector2 translation, Vector2 scale, float rotation, V
MatrixExtensions.RotateFromRight(ref MatrixInverse, -radians);
}

if (extraRotation != Matrix3.Identity)
if (extraRotation != Matrix4.Identity)
{
Matrix = new Matrix4(extraRotation) * Matrix;
MatrixInverse *= new Matrix4(Matrix3.Transpose(extraRotation));
Matrix = extraRotation * Matrix;
MatrixInverse *= Matrix4.Transpose(extraRotation);
}

if (shear != Vector2.Zero)
Expand Down
10 changes: 5 additions & 5 deletions osu.Framework/Graphics/Drawable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1568,14 +1568,14 @@ public Drawable CreateProxy()

private readonly LayoutValue<DrawInfo> drawInfoBacking = new LayoutValue<DrawInfo>(Invalidation.DrawInfo | Invalidation.RequiredParentSizeToFit | Invalidation.Presence);

private Matrix3 extraRotation = Matrix3.Identity;
private Matrix4 extraRotationMatrix = Matrix4.Identity;

public Matrix3 ExtraRotation
public Quaternion ExtraRotation
{
get => extraRotation;
get => extraRotationMatrix.ExtractRotation();
set
{
extraRotation = value;
extraRotationMatrix = Matrix4.CreateFromQuaternion(value);
Invalidate();
}
}
Expand All @@ -1590,7 +1590,7 @@ private DrawInfo computeDrawInfo()
if (Parent != null)
pos += Parent.ChildOffset;

di.ApplyTransform(pos, drawScale, Rotation, Shear, OriginPosition, ExtraRotation);
di.ApplyTransform(pos, drawScale, Rotation, Shear, OriginPosition, extraRotationMatrix);

return di;
}
Expand Down
41 changes: 41 additions & 0 deletions osu.Framework/Utils/Interpolation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,45 @@ public static Colour4 ValueAt(double time, Colour4 startColour, Colour4 endColou
startLinear.A + t * (endLinear.A - startLinear.A)).ToSRGB();
}

public static Quaternion ValueAt(double time, Quaternion start, Quaternion end, double startTime, double endTime, in TEasing easing)
{
if (start == end)
return start;

double current = time - startTime;
double duration = endTime - startTime;

if (duration == 0 || current == 0)
return start;

float t = Math.Max(0, Math.Min(1, (float)easing.ApplyEasing(current / duration)));

return Quaternion.Slerp(start, end, t);
}

// public static Matrix4 ValueAt(double time, Matrix4 startMatrix, Matrix4 endMatrix, double startTime, double endTime, in TEasing easing)
// {
// if (startMatrix == endMatrix)
// return startMatrix;
//
// double current = time - startTime;
// double duration = endTime - startTime;
//
// if (duration == 0 || current == 0)
// return startMatrix;
//
// var startLinear = startColour.ToLinear();
// var endLinear = endColour.ToLinear();
//
// float t = Math.Max(0, Math.Min(1, (float)easing.ApplyEasing(current / duration)));
//
// return new Colour4(
// startLinear.R + t * (endLinear.R - startLinear.R),
// startLinear.G + t * (endLinear.G - startLinear.G),
// startLinear.B + t * (endLinear.B - startLinear.B),
// startLinear.A + t * (endLinear.A - startLinear.A)).ToSRGB();
// }

public static byte ValueAt(double time, byte val1, byte val2, double startTime, double endTime, in TEasing easing)
=> (byte)Math.Round(ValueAt(time, (double)val1, val2, startTime, endTime, easing));

Expand Down Expand Up @@ -418,6 +457,8 @@ static GenericInterpolation()
FUNCTION = (InterpolationFunc<TValue, TEasing>)(object)(InterpolationFunc<Vector2, TEasing>)GenericInterpolation<TEasing>.ValueAt;
else if (typeof(TValue) == typeof(RectangleF))
FUNCTION = (InterpolationFunc<TValue, TEasing>)(object)(InterpolationFunc<RectangleF, TEasing>)GenericInterpolation<TEasing>.ValueAt;
else if (typeof(TValue) == typeof(Quaternion))
FUNCTION = (InterpolationFunc<TValue, TEasing>)(object)(InterpolationFunc<Quaternion, TEasing>)GenericInterpolation<TEasing>.ValueAt;
else
throw new NotSupportedException($"Type {typeof(TValue)} has no interpolation function. Implement the interface {typeof(IInterpolable<TValue>)} interface on the object.");
}
Expand Down

0 comments on commit 41e8b81

Please sign in to comment.