Remove motion lerper. Reconstruct RMV and motion node. Add motion node pool.

This commit is contained in:
2023-04-19 13:48:06 +08:00
parent a5439430fb
commit 659f2165ff
3 changed files with 94 additions and 209 deletions

View File

@@ -153,8 +153,8 @@ namespace Cryville.Crtr {
bbus.ForwardByTime(dt); bbus.ForwardByTime(dt);
UnityEngine.Profiling.Profiler.BeginSample("ChartPlayer.Forward"); UnityEngine.Profiling.Profiler.BeginSample("ChartPlayer.Forward");
UnityEngine.Profiling.Profiler.BeginSample("EventBus.Copy"); UnityEngine.Profiling.Profiler.BeginSample("EventBus.Copy");
bbus.CopyTo(2, tbus); bbus.CopyTo(tbus);
bbus.CopyTo(3, nbus); bbus.CopyTo(nbus);
UnityEngine.Profiling.Profiler.EndSample(); UnityEngine.Profiling.Profiler.EndSample();
actualRenderStep = step; actualRenderStep = step;
@@ -250,11 +250,12 @@ namespace Cryville.Crtr {
UnityEngine.Profiling.Profiler.GetTotalReservedMemory() UnityEngine.Profiling.Profiler.GetTotalReservedMemory()
#endif #endif
); );
if (ContainerState.RMVPool != null) { if (RMVPool.Shared != null) {
statusbuf.AppendFormat( statusbuf.AppendFormat(
"\nPools: RMV {0}, MC {1}", "\nPools: RMV {0}, MC {1}, MN {2}",
RMVPool.Shared.RentedCount, RMVPool.Shared.RentedCount,
MotionCachePool.Shared.RentedCount MotionCachePool.Shared.RentedCount,
MotionNodePool.Shared.RentedCount
); );
} }
if (loadThread != null) { if (loadThread != null) {
@@ -673,6 +674,7 @@ namespace Cryville.Crtr {
} }
RMVPool.Shared = new RMVPool(); RMVPool.Shared = new RMVPool();
MotionCachePool.Shared = new MotionCachePool(); MotionCachePool.Shared = new MotionCachePool();
MotionNodePool.Shared = new MotionNodePool();
} }
void LoadSkin(FileInfo file) { void LoadSkin(FileInfo file) {

View File

@@ -1,5 +1,3 @@
//#define DISABLE_CACHE
using Cryville.Common; using Cryville.Common;
using Cryville.Common.Buffers; using Cryville.Common.Buffers;
using Cryville.Common.Collections.Specialized; using Cryville.Common.Collections.Specialized;
@@ -248,32 +246,26 @@ namespace Cryville.Crtr.Event {
Children[c].InvalidateMotion(name); Children[c].InvalidateMotion(name);
} }
public Vector GetRawValue(int key) { public Vector GetComputedValue(int key) {
MotionCache tr; MotionCache tr;
if (!CachedValues.TryGetValue(key, out tr)) if (!CachedValues.TryGetValue(key, out tr))
CachedValues.Add(key, tr = _mcpa.Rent(key)); CachedValues.Add(key, tr = _mcpa.Rent(key));
Vector r = tr.Value; Vector r = tr.Value;
#if !DISABLE_CACHE
if (tr.Valid) return r; if (tr.Valid) return r;
#endif GetMotionValue(key).Compute(ref r);
float reltime = 0; if (Parent != null) r.ApplyFrom(Parent.GetComputedValue(key));
if (rootPrototype != null) reltime = (float)(Time - rootPrototype.Time);
GetMotionValue(key).GetValue(reltime, ref r);
if (Parent != null) r.ApplyFrom(Parent.GetRawValue(key));
#if !DISABLE_CACHE
tr.Valid = true; tr.Valid = true;
#endif
return r; return r;
} }
public T GetRawValue<T>(int key) where T : Vector { public T GetComputedValue<T>(int key) where T : Vector {
return (T)GetRawValue(key); return (T)GetComputedValue(key);
} }
static readonly int n_pt = IdentifierManager.SharedInstance.Request("pt"); static readonly int n_pt = IdentifierManager.SharedInstance.Request("pt");
public Vector2 ScreenPoint { public Vector2 ScreenPoint {
get { get {
var mv = GetRawValue<Vec2>(n_pt); var mv = GetComputedValue<Vec2>(n_pt);
return mv.ToVector2(); return mv.ToVector2();
} }
} }
@@ -281,7 +273,7 @@ namespace Cryville.Crtr.Event {
static readonly int n_dir = IdentifierManager.SharedInstance.Request("dir"); static readonly int n_dir = IdentifierManager.SharedInstance.Request("dir");
public Vector3 Direction { public Vector3 Direction {
get { get {
Vec3 r = GetRawValue<Vec3>(n_dir); Vec3 r = GetComputedValue<Vec3>(n_dir);
return r.ToVector3(); return r.ToVector3();
} }
} }
@@ -289,7 +281,7 @@ namespace Cryville.Crtr.Event {
static readonly int n_normal = IdentifierManager.SharedInstance.Request("normal"); static readonly int n_normal = IdentifierManager.SharedInstance.Request("normal");
public Vector3 Normal { public Vector3 Normal {
get { get {
Vec3 r = GetRawValue<Vec3>(n_normal); Vec3 r = GetComputedValue<Vec3>(n_normal);
return r.ToVector3(); return r.ToVector3();
} }
} }
@@ -304,21 +296,21 @@ namespace Cryville.Crtr.Event {
static readonly int n_svm = IdentifierManager.SharedInstance.Request("svm"); static readonly int n_svm = IdentifierManager.SharedInstance.Request("svm");
public float ScrollVelocity { public float ScrollVelocity {
get { get {
return GetRawValue<Vec1>(n_sv).Value * GetRawValue<Vec1m>(n_svm).Value; return GetComputedValue<Vec1>(n_sv).Value * GetComputedValue<Vec1m>(n_svm).Value;
} }
} }
static readonly int n_dist = IdentifierManager.SharedInstance.Request("dist"); static readonly int n_dist = IdentifierManager.SharedInstance.Request("dist");
public float Distance { public float Distance {
get { get {
return GetRawValue<Vec1>(n_dist).Value; return GetComputedValue<Vec1>(n_dist).Value;
} }
} }
static readonly int n_track = IdentifierManager.SharedInstance.Request("track"); static readonly int n_track = IdentifierManager.SharedInstance.Request("track");
public float Track { public float Track {
get { get {
return GetRawValue<Vec1>(n_track).Value; return GetComputedValue<Vec1>(n_track).Value;
} }
} }
#endregion #endregion
@@ -403,7 +395,6 @@ namespace Cryville.Crtr.Event {
} }
else { else {
var scaledTime = (float)((Time - m.Key.Time - ChartPlayer.actualRenderStep * tev.sumfix) / m.Key.Duration); var scaledTime = (float)((Time - m.Key.Time - ChartPlayer.actualRenderStep * tev.sumfix) / m.Key.Duration);
var lerpedTime = MotionLerper.GetEaseTime(scaledTime, TransitionType.Ease, 1);
if (tev.Node.Id >= 0) { if (tev.Node.Id >= 0) {
var start = m.Value.GetRelativeNode(tev.Node.Id); var start = m.Value.GetRelativeNode(tev.Node.Id);
if (start == null) { if (start == null) {
@@ -412,11 +403,12 @@ namespace Cryville.Crtr.Event {
else { else {
var target = value.GetRelativeNode(tev.Node.Id); var target = value.GetRelativeNode(tev.Node.Id);
if (target == null) value.SetRelativeNode(tev.Node); if (target == null) value.SetRelativeNode(tev.Node);
else tev.Node.LerpWith(m.Value.GetRelativeNode(tev.Node.Id), lerpedTime, ref target); else tev.Node.LerpWith(m.Value.GetRelativeNode(tev.Node.Id), scaledTime, ref target);
} }
} }
else { else {
tev.Node.Value.LerpWith(m.Value.AbsoluteValue, lerpedTime, ref value.AbsoluteValue); tev.Node.Value.LerpWith(m.Value.AbsoluteValue, scaledTime, ref value.AbsoluteValue);
}
} }
} }
} }

View File

@@ -1,3 +1,5 @@
using Cryville.Common.Buffers;
using Cryville.Common.Collections.Specialized;
using Cryville.Common.Pdt; using Cryville.Common.Pdt;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@@ -6,94 +8,6 @@ using System.Text.RegularExpressions;
using UnityEngine; using UnityEngine;
namespace Cryville.Crtr { namespace Cryville.Crtr {
[Obsolete]
public enum TransitionType : byte {
In = 0,
Out = 1,
InOut = 2,
OutIn = 3,
Ease = 0,
Sine = 4,
Expo = 8,
Circ = 12,
Elastic = 16,
Back = 20,
Bounce = 24,
}
[Obsolete]
public static class MotionLerper {
public static void Lerp<T>(double time, double tt, T tv, double ft, T fv, TransitionType type, float rate, ref T result) where T : Vector {
if (fv == null) fv = (T)Activator.CreateInstance(tv.GetType());
Lerp((float)((time - ft) / (tt - ft)), fv, tv, type, rate, ref result);
}
public static void Lerp<T>(float scaledTime, T from, T to, TransitionType type, float rate, ref T result) where T : Vector {
if (scaledTime >= 1) { result = to; return; }
if (scaledTime < 0) { result = from; return; }
var r = (Vector)result;
to.LerpWith(from, GetEaseTime(scaledTime, type, rate), ref r);
}
public static float GetEaseTime(float time, TransitionType type, float rate) {
switch ((byte)type & 3) {
case (byte)TransitionType.In:
return 1 - GetEaseOutTime(1 - time, type, rate);
case (byte)TransitionType.Out:
return GetEaseOutTime(time, type, rate);
case (byte)TransitionType.InOut:
time *= 2;
if (time <= 1) return (1 - GetEaseOutTime(1 - time, type, rate)) / 2;
else return (GetEaseOutTime(time - 1, type, rate) + 1) / 2;
case (byte)TransitionType.OutIn:
time *= 2;
if (time <= 1) return GetEaseOutTime(time, type, rate) / 2;
else return (2 - GetEaseOutTime(2 - time, type, rate)) / 2;
default:
throw new ArgumentException("Unknown transition");
}
}
static float GetEaseOutTime(float p, TransitionType type, float rate) {
switch ((byte)type & 252) {
case (byte)TransitionType.Ease:
return 1 - Mathf.Pow(1 - p, rate);
case (byte)TransitionType.Sine:
return Mathf.Sin(p * (Mathf.PI / 2));
case (byte)TransitionType.Expo:
return 1 - Mathf.Pow(2, -10 * p);
case (byte)TransitionType.Circ:
p -= 1;
return Mathf.Sqrt(1 - p * p);
case (byte)TransitionType.Elastic:
float s = rate / (2 * Mathf.PI) * Mathf.Asin(1);
return Mathf.Pow(2, -10 * p) * Mathf.Sin((p - s) * (2 * Mathf.PI) / rate) + 1;
case (byte)TransitionType.Back:
p -= 1;
return p * p * ((rate + 1) * p + rate) + 1;
case (byte)TransitionType.Bounce:
if (p < 1 / 2.75) {
return 7.5625f * p * p;
}
else if (p < (2 / 2.75)) {
p -= 1.5f / 2.75f;
return 7.5625f * p * p + .75f;
}
else if (p < 2.5 / 2.75) {
p -= 2.25f / 2.75f;
return 7.5625f * p * p + .9375f;
}
else {
p -= 2.625f / 2.75f;
return 7.5625f * p * p + .984375f;
}
default:
throw new ArgumentException("Unknown transition");
}
}
}
public struct MotionRegistry { public struct MotionRegistry {
readonly Type m_Type; readonly Type m_Type;
public Type Type { public Type Type {
@@ -123,31 +37,32 @@ namespace Cryville.Crtr {
} }
public class RealtimeMotionValue { public class RealtimeMotionValue {
Type _type;
public Vector AbsoluteValue; public Vector AbsoluteValue;
List<MotionNode> RelativeNodes; public Vector RelativeValue;
public IntKeyedDictionary<MotionNode> RelativeNodes;
internal byte CloneTypeFlag; internal byte CloneTypeFlag;
public RealtimeMotionValue Init(Vector init) { public RealtimeMotionValue Init(Vector init) {
RelativeNodes = new List<MotionNode> { _type = init.GetType();
new MotionNode() { RelativeNodes = new IntKeyedDictionary<MotionNode>();
Id = 0, AbsoluteValue = init;
Time = new Vec1(0), RelativeValue = (Vector)Activator.CreateInstance(_type);
EndTime = new Vec1(0),
Value = init
}
};
AbsoluteValue = (Vector)Activator.CreateInstance(init.GetType());
return this; return this;
} }
public RealtimeMotionValue Clone() { public RealtimeMotionValue Clone() {
var r = new RealtimeMotionValue() { var r = new RealtimeMotionValue() {
AbsoluteValue = AbsoluteValue.Clone() _type = _type,
AbsoluteValue = AbsoluteValue.Clone(),
RelativeValue = RelativeValue.Clone(),
}; };
var rel = new List<MotionNode>(RelativeNodes.Count); var rel = new IntKeyedDictionary<MotionNode>(RelativeNodes.Count);
for (var i = 0; i < RelativeNodes.Count; i++) { foreach (var node in RelativeNodes) {
rel.Add(RelativeNodes[i].Clone()); var dnode = MotionNodePool.Shared.Rent(_type);
node.Value.CopyTo(dnode);
rel.Add(node.Key, dnode);
} }
r.RelativeNodes = rel; r.RelativeNodes = rel;
@@ -156,108 +71,62 @@ namespace Cryville.Crtr {
public void CopyTo(RealtimeMotionValue dest) { public void CopyTo(RealtimeMotionValue dest) {
AbsoluteValue.CopyTo(dest.AbsoluteValue); AbsoluteValue.CopyTo(dest.AbsoluteValue);
int tcount = RelativeNodes.Count; RelativeValue.CopyTo(dest.RelativeValue);
int dcount = dest.RelativeNodes.Count; dest.ReturnAllRelativeNodes();
int count = Math.Min(tcount, dcount); foreach (var node in RelativeNodes) {
for (int i = 0; i < count; i++) { var dnode = MotionNodePool.Shared.Rent(_type);
RelativeNodes[i].CopyTo(dest.RelativeNodes[i]); node.Value.CopyTo(dnode);
dest.RelativeNodes.Add(node.Key, dnode);
} }
if (tcount > dcount) }
for (int i = dcount; i < tcount; i++)
dest.RelativeNodes.Add(RelativeNodes[i].Clone()); public void ReturnAllRelativeNodes() {
else foreach (var node in RelativeNodes) {
dest.RelativeNodes.RemoveRange(tcount, dcount - tcount); MotionNodePool.Shared.Return(_type, node.Value);
}
RelativeNodes.Clear();
} }
public MotionNode GetRelativeNode(short id) { public MotionNode GetRelativeNode(short id) {
return RelativeNodes.Find(n => n.Id == id); MotionNode result;
RelativeNodes.TryGetValue(id, out result);
return result;
} }
public void SetRelativeNode(MotionNode node) { public void SetRelativeNode(MotionNode node) {
int i = RelativeNodes.FindIndex(n => n.Id == node.Id);
MotionNode cnode; MotionNode cnode;
if (i == -1) { if (!RelativeNodes.TryGetValue(node.Id, out cnode)) {
cnode = new MotionNode { cnode = MotionNodePool.Shared.Rent(_type);
Id = node.Id, cnode.Id = node.Id;
Time = new Vec1(0), RelativeNodes.Add(node.Id, cnode);
EndTime = new Vec1(0),
Value = (Vector)Activator.CreateInstance(AbsoluteValue.GetType())
};
}
else {
cnode = RelativeNodes[i];
RelativeNodes.RemoveAt(i);
} }
if (node.Time != null) cnode.Time = node.Time; if (node.Time != null) cnode.Time = node.Time;
if (node.EndTime != null) cnode.EndTime = node.EndTime; if (node.EndTime != null) cnode.EndTime = node.EndTime;
if (node.Value != null) cnode.Value.ReplaceFrom(node.Value); if (node.Value != null) node.Value.CopyTo(cnode.Value);
int i2 = RelativeNodes.BinarySearch(cnode);
if (i2 < 0) i2 = ~i2;
RelativeNodes.Insert(i2, cnode);
} }
/// <summary> /// <summary>
/// Computes the motion value at the given relative time. /// Computes the motion value.
/// </summary> /// </summary>
/// <typeparam name="T">The vector type of the value.</typeparam> /// <typeparam name="T">The vector type of the value.</typeparam>
/// <param name="reltime">The relative time.</param>
/// <param name="result">The result.</param> /// <param name="result">The result.</param>
/// <returns></returns> public void Compute<T>(ref T result) where T : Vector {
[Obsolete] AbsoluteValue.CopyTo(result);
public void GetValue<T>(float reltime, ref T result) where T : Vector { result.ApplyFrom(RelativeValue);
int i = BinarySearch(reltime);
if (i >= 0) RelativeNodes[i].Value.CopyTo(result);
else {
i = ~i;
if (i >= RelativeNodes.Count) {
RelativeNodes[i - 1].Value.CopyTo(result);
}
else if (i == 0) {
RelativeNodes[0].Value.CopyTo(result);
}
else {
MotionNode t = RelativeNodes[i], f = RelativeNodes[i - 1];
MotionLerper.Lerp(reltime, t.Time.Value, (T)t.Value, f.Time.Value, (T)f.Value, t.Transition.Value, t.Rate.Value, ref result);
}
}
result.ApplyFrom(AbsoluteValue);
}
// Adopted from System.Collections.Generic.ArraySortHelper<T>.InternalBinarySearch(T[] array, int index, int length, T value, IComparer<T> comparer)
int BinarySearch(float value) {
int num = 0;
int num2 = RelativeNodes.Count - 1;
while (num <= num2) {
int num3 = num + (num2 - num >> 1);
int num4 = RelativeNodes[num3].Time.Value.CompareTo(value);
if (num4 == 0) return num3;
if (num4 < 0) num = num3 + 1;
else num2 = num3 - 1;
}
return ~num;
} }
} }
public class MotionNode : IComparable<MotionNode> { public class MotionNode {
public short Id = -1; public short Id = -1;
public bool Reset; public bool Reset;
public Vec1 Time; public Vec1 Time;
public Vec1 EndTime; public Vec1 EndTime;
float CmpTime { get { return Time != null ? Time.Value : 0; } }
[Obsolete]
public TransitionType? Transition;
[Obsolete]
public Vec1 Rate;
public Vector Value; public Vector Value;
public MotionNode Clone() { public void Init(Type type) {
return new MotionNode() { if (Time == null) Time = new Vec1(float.NegativeInfinity);
Id = Id, if (EndTime == null) EndTime = new Vec1(float.NegativeInfinity);
Time = (Vec1)Time.Clone(), if (Value == null) Value = (Vector)Activator.CreateInstance(type);
EndTime = (Vec1)EndTime.Clone(),
Value = Value.Clone()
};
} }
public void CopyTo(MotionNode dest) { public void CopyTo(MotionNode dest) {
@@ -267,10 +136,6 @@ namespace Cryville.Crtr {
Value.CopyTo(dest.Value); Value.CopyTo(dest.Value);
} }
public int CompareTo(MotionNode other) {
return CmpTime.CompareTo(other.CmpTime);
}
public void LerpWith(MotionNode start, float lerpedTime, ref MotionNode result) { public void LerpWith(MotionNode start, float lerpedTime, ref MotionNode result) {
result.Id = Id; result.Id = Id;
if (Time == null) result.Time = start.Time; if (Time == null) result.Time = start.Time;
@@ -288,6 +153,32 @@ namespace Cryville.Crtr {
} }
} }
internal class MotionNodePool : CategorizedPool<Type, MotionNode> {
public static MotionNodePool Shared;
private class Bucket : ObjectPool<MotionNode> {
readonly Type _type;
public Bucket(Type type, int capacity) : base(capacity) {
_type = type;
}
protected override MotionNode Construct() {
var result = new MotionNode();
result.Init(_type);
return result;
}
}
readonly Dictionary<Type, ObjectPool<MotionNode>> m_buckets;
protected override IReadOnlyDictionary<Type, ObjectPool<MotionNode>> Buckets { get { return m_buckets; } }
public MotionNodePool() {
m_buckets = new Dictionary<Type, ObjectPool<MotionNode>>();
foreach (var reg in ChartPlayer.motionRegistry) {
var type = reg.Value.Type;
if (!m_buckets.ContainsKey(type))
m_buckets.Add(type, new Bucket(type, 4096));
}
m_buckets.TrimExcess();
}
}
public abstract class Vector { public abstract class Vector {
public Vector() { } public Vector() { }