Skip to content

Commit

Permalink
Merge pull request #1 from public-aks2dio/master
Browse files Browse the repository at this point in the history
Improvements and fixes
  • Loading branch information
txtxj authored Sep 18, 2024
2 parents 996f39e + 75b3e71 commit 81b5710
Show file tree
Hide file tree
Showing 74 changed files with 349 additions and 255 deletions.
File renamed without changes.
3 changes: 3 additions & 0 deletions Assets/Plugins/Citrine.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Assets/Plugins/Citrine/KeyframeReducer.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Assets/Plugins/Citrine/KeyframeReducer/AnimationCurve.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using UnityEditor;
using UnityEngine;

namespace Citrine.Utils.AnimationCompression
namespace Citrine.Animation.Editor
{
internal abstract class AnimationCurveBase<T> where T : struct
{
private const float Epsilon = (float)1e-6;

internal AnimationCurve[] curve;

internal EditorCurveBinding[] binding;
Expand All @@ -36,15 +35,15 @@ protected virtual void SetKeys(List<IKeyframeBase<T>> list)
}

protected abstract T Interpolate(IKeyframeBase<T> begin, IKeyframeBase<T> end, float time);

private float GetTimeAt(int index) => curve[0].keys[index].time;

private bool CalculateErrorAtTime(IKeyframeBase<T> key0, IKeyframeBase<T> key1,
float time, Func<T, T, float, bool> errorFunction, float error)
{
return errorFunction(Interpolate(key0, key1, time), Evaluate(time), error);
}

private bool CheckConstantAndReduce(IKeyframeBase<T> begin, IKeyframeBase<T> end, Func<T, T, float, bool> errorFunction, float error)
{
begin.ClearSlope();
Expand All @@ -64,7 +63,7 @@ private bool CheckConstantAndReduce(IKeyframeBase<T> begin, IKeyframeBase<T> end

return true;
}

private bool IsReducible(int beginIndex, int endIndex, Func<T, T, float, bool> errorFunction, float error, float sampleRate)
{
IKeyframeBase<T> begin = GetKey(beginIndex);
Expand Down Expand Up @@ -92,12 +91,12 @@ private bool IsReducible(int beginIndex, int endIndex, Func<T, T, float, bool> e
return false;
}
}

CalculateErrorAtTime(GetKey(0), GetKey(3), 1f, errorFunction, error);

return true;
}

internal void ReduceKeyframes(Func<T, T, float, bool> errorFunction, float error, float sampleRate)
{
if (length <= 2)
Expand All @@ -107,16 +106,16 @@ internal void ReduceKeyframes(Func<T, T, float, bool> errorFunction, float error

IKeyframeBase<T> begin = GetKey(0);
IKeyframeBase<T> end = GetKey(length - 1);

if (CheckConstantAndReduce(begin, end, errorFunction, error))
{
return;
}

List<IKeyframeBase<T>> reducedKeyframes = new List<IKeyframeBase<T>>(length);

reducedKeyframes.Add(GetKey(0));

int comparerFrameIndex = 0;
for (int curIndex = 2; curIndex < length; curIndex++)
{
Expand All @@ -126,7 +125,7 @@ internal void ReduceKeyframes(Func<T, T, float, bool> errorFunction, float error
comparerFrameIndex = curIndex - 1;
}
}

reducedKeyframes.Add(GetKey(length - 1));

if (reducedKeyframes.Count < length)
Expand Down Expand Up @@ -166,14 +165,14 @@ internal bool CheckData()

return true;
}

protected float Hermite(Keyframe begin, Keyframe end, float time)
{
if (end.time - begin.time < Epsilon)
return begin.value;

float normal = end.time - begin.time;

float t = (time - begin.time) / normal;
float t2 = t * t;
float t3 = t2 * t;
Expand All @@ -196,4 +195,4 @@ protected float Bezier(Keyframe begin, Keyframe end, float time)
throw new NotImplementedException("We don't know how to interpolate a bezier curve with weights.");
}
}
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using UnityEditor;
using UnityEngine;

namespace Citrine.Utils.AnimationCompression
namespace Citrine.Animation.Editor
{
internal class QuaternionAnimationCurve : AnimationCurveBase<Quaternion>
{
Expand All @@ -16,7 +16,7 @@ protected override Quaternion Evaluate(float time)
{
return new (curve[0].Evaluate(time), curve[1].Evaluate(time), curve[2].Evaluate(time), curve[3].Evaluate(time));
}

protected override Quaternion Interpolate(IKeyframeBase<Quaternion> begin, IKeyframeBase<Quaternion> end, float time)
{
Quaternion ret = new Quaternion();
Expand All @@ -40,7 +40,7 @@ protected override IKeyframeBase<Quaternion> GetKey(int index)
{
return new QuaternionKeyframe(curve[0].keys[index], curve[1].keys[index], curve[2].keys[index], curve[3].keys[index]);
}

protected override void SetKey(int index, IKeyframeBase<Quaternion> key)
{
(curve[0].keys[index], curve[1].keys[index], curve[2].keys[index], curve[3].keys[index])
Expand All @@ -61,11 +61,11 @@ protected override void SetKeys(List<IKeyframeBase<Quaternion>> list)
keyZ[i] = list[i].keyframe[2];
keyW[i] = list[i].keyframe[3];
}

curve[0].keys = keyX;
curve[1].keys = keyY;
curve[2].keys = keyZ;
curve[3].keys = keyW;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using UnityEditor;
using UnityEngine;

namespace Citrine.Utils.AnimationCompression
namespace Citrine.Animation.Editor
{
internal class Vector3AnimationCurve : AnimationCurveBase<Vector3>
{
Expand All @@ -16,7 +16,7 @@ protected override Vector3 Evaluate(float time)
{
return new (curve[0].Evaluate(time), curve[1].Evaluate(time), curve[2].Evaluate(time));
}

protected override Vector3 Interpolate(IKeyframeBase<Vector3> begin, IKeyframeBase<Vector3> end, float time)
{
Vector3 ret = new Vector3();
Expand All @@ -32,15 +32,15 @@ protected override Vector3 Interpolate(IKeyframeBase<Vector3> begin, IKeyframeBa
ret[i] = Bezier(begin.keyframe[i], end.keyframe[i], time);
}
}

return ret;
}

protected override IKeyframeBase<Vector3> GetKey(int index)
{
return new Vector3Keyframe(curve[0].keys[index], curve[1].keys[index], curve[2].keys[index]);
}

protected override void SetKey(int index, IKeyframeBase<Vector3> key)
{
(curve[0].keys[index], curve[1].keys[index], curve[2].keys[index]) = (key.keyframe[0], key.keyframe[1], key.keyframe[2]);
Expand All @@ -58,10 +58,10 @@ protected override void SetKeys(List<IKeyframeBase<Vector3>> list)
keyY[i] = list[i].keyframe[1];
keyZ[i] = list[i].keyframe[2];
}

curve[0].keys = keyX;
curve[1].keys = keyY;
curve[2].keys = keyZ;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "Citrine.KeyframeReducer.Editor",
"rootNamespace": "",
"references": [],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "Citrine.KeyframeReducer.Example",
"rootNamespace": "",
"references": [
"GUID:32abbc7a1f6c70e4596affd7fa5a2366"
],
"includePlatforms": [],
"excludePlatforms": [
"Android",
"Editor",
"EmbeddedLinux",
"GameCoreScarlett",
"GameCoreXboxOne",
"iOS",
"LinuxStandalone64",
"CloudRendering",
"Lumin",
"macOSStandalone",
"PS4",
"PS5",
"Stadia",
"Switch",
"tvOS",
"WSA",
"WebGL",
"WindowsStandalone32",
"WindowsStandalone64",
"XboxOne"
],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions Assets/Plugins/Citrine/KeyframeReducer/Keyframe.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using UnityEngine;

namespace Citrine.Utils.AnimationCompression
namespace Citrine.Animation.Editor
{
internal interface IKeyframeBase<T> where T : struct
{
public Keyframe[] keyframe { get; set; }

public float time { get; }

public T value { get; set; }

public void ClearSlope();
}
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using UnityEngine;

namespace Citrine.Utils.AnimationCompression
namespace Citrine.Animation.Editor
{
internal struct QuaternionKeyframe : IKeyframeBase<Quaternion>
{
Expand Down Expand Up @@ -36,4 +36,4 @@ public void ClearSlope()
}
}
}
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using UnityEngine;

namespace Citrine.Utils.AnimationCompression
namespace Citrine.Animation.Editor
{
public struct Vector3Keyframe : IKeyframeBase<Vector3>
internal struct Vector3Keyframe : IKeyframeBase<Vector3>
{
public Keyframe[] keyframe { get; set; }

Expand Down Expand Up @@ -38,4 +38,4 @@ public void ClearSlope()
}
}
}
}
}
File renamed without changes.
Loading

0 comments on commit 81b5710

Please sign in to comment.