Remove motion lerper. Reconstruct RMV and motion node. Add motion node pool.
This commit is contained in:
@@ -153,8 +153,8 @@ namespace Cryville.Crtr {
|
||||
bbus.ForwardByTime(dt);
|
||||
UnityEngine.Profiling.Profiler.BeginSample("ChartPlayer.Forward");
|
||||
UnityEngine.Profiling.Profiler.BeginSample("EventBus.Copy");
|
||||
bbus.CopyTo(2, tbus);
|
||||
bbus.CopyTo(3, nbus);
|
||||
bbus.CopyTo(tbus);
|
||||
bbus.CopyTo(nbus);
|
||||
UnityEngine.Profiling.Profiler.EndSample();
|
||||
actualRenderStep = step;
|
||||
|
||||
@@ -250,11 +250,12 @@ namespace Cryville.Crtr {
|
||||
UnityEngine.Profiling.Profiler.GetTotalReservedMemory()
|
||||
#endif
|
||||
);
|
||||
if (ContainerState.RMVPool != null) {
|
||||
if (RMVPool.Shared != null) {
|
||||
statusbuf.AppendFormat(
|
||||
"\nPools: RMV {0}, MC {1}",
|
||||
"\nPools: RMV {0}, MC {1}, MN {2}",
|
||||
RMVPool.Shared.RentedCount,
|
||||
MotionCachePool.Shared.RentedCount
|
||||
MotionCachePool.Shared.RentedCount,
|
||||
MotionNodePool.Shared.RentedCount
|
||||
);
|
||||
}
|
||||
if (loadThread != null) {
|
||||
@@ -673,6 +674,7 @@ namespace Cryville.Crtr {
|
||||
}
|
||||
RMVPool.Shared = new RMVPool();
|
||||
MotionCachePool.Shared = new MotionCachePool();
|
||||
MotionNodePool.Shared = new MotionNodePool();
|
||||
}
|
||||
|
||||
void LoadSkin(FileInfo file) {
|
||||
|
@@ -1,5 +1,3 @@
|
||||
//#define DISABLE_CACHE
|
||||
|
||||
using Cryville.Common;
|
||||
using Cryville.Common.Buffers;
|
||||
using Cryville.Common.Collections.Specialized;
|
||||
@@ -248,32 +246,26 @@ namespace Cryville.Crtr.Event {
|
||||
Children[c].InvalidateMotion(name);
|
||||
}
|
||||
|
||||
public Vector GetRawValue(int key) {
|
||||
public Vector GetComputedValue(int key) {
|
||||
MotionCache tr;
|
||||
if (!CachedValues.TryGetValue(key, out tr))
|
||||
CachedValues.Add(key, tr = _mcpa.Rent(key));
|
||||
Vector r = tr.Value;
|
||||
#if !DISABLE_CACHE
|
||||
if (tr.Valid) return r;
|
||||
#endif
|
||||
float reltime = 0;
|
||||
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
|
||||
GetMotionValue(key).Compute(ref r);
|
||||
if (Parent != null) r.ApplyFrom(Parent.GetComputedValue(key));
|
||||
tr.Valid = true;
|
||||
#endif
|
||||
return r;
|
||||
}
|
||||
|
||||
public T GetRawValue<T>(int key) where T : Vector {
|
||||
return (T)GetRawValue(key);
|
||||
public T GetComputedValue<T>(int key) where T : Vector {
|
||||
return (T)GetComputedValue(key);
|
||||
}
|
||||
|
||||
static readonly int n_pt = IdentifierManager.SharedInstance.Request("pt");
|
||||
public Vector2 ScreenPoint {
|
||||
get {
|
||||
var mv = GetRawValue<Vec2>(n_pt);
|
||||
var mv = GetComputedValue<Vec2>(n_pt);
|
||||
return mv.ToVector2();
|
||||
}
|
||||
}
|
||||
@@ -281,7 +273,7 @@ namespace Cryville.Crtr.Event {
|
||||
static readonly int n_dir = IdentifierManager.SharedInstance.Request("dir");
|
||||
public Vector3 Direction {
|
||||
get {
|
||||
Vec3 r = GetRawValue<Vec3>(n_dir);
|
||||
Vec3 r = GetComputedValue<Vec3>(n_dir);
|
||||
return r.ToVector3();
|
||||
}
|
||||
}
|
||||
@@ -289,7 +281,7 @@ namespace Cryville.Crtr.Event {
|
||||
static readonly int n_normal = IdentifierManager.SharedInstance.Request("normal");
|
||||
public Vector3 Normal {
|
||||
get {
|
||||
Vec3 r = GetRawValue<Vec3>(n_normal);
|
||||
Vec3 r = GetComputedValue<Vec3>(n_normal);
|
||||
return r.ToVector3();
|
||||
}
|
||||
}
|
||||
@@ -304,21 +296,21 @@ namespace Cryville.Crtr.Event {
|
||||
static readonly int n_svm = IdentifierManager.SharedInstance.Request("svm");
|
||||
public float ScrollVelocity {
|
||||
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");
|
||||
public float Distance {
|
||||
get {
|
||||
return GetRawValue<Vec1>(n_dist).Value;
|
||||
return GetComputedValue<Vec1>(n_dist).Value;
|
||||
}
|
||||
}
|
||||
|
||||
static readonly int n_track = IdentifierManager.SharedInstance.Request("track");
|
||||
public float Track {
|
||||
get {
|
||||
return GetRawValue<Vec1>(n_track).Value;
|
||||
return GetComputedValue<Vec1>(n_track).Value;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
@@ -403,7 +395,6 @@ namespace Cryville.Crtr.Event {
|
||||
}
|
||||
else {
|
||||
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) {
|
||||
var start = m.Value.GetRelativeNode(tev.Node.Id);
|
||||
if (start == null) {
|
||||
@@ -412,11 +403,12 @@ namespace Cryville.Crtr.Event {
|
||||
else {
|
||||
var target = value.GetRelativeNode(tev.Node.Id);
|
||||
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 {
|
||||
tev.Node.Value.LerpWith(m.Value.AbsoluteValue, lerpedTime, ref value.AbsoluteValue);
|
||||
tev.Node.Value.LerpWith(m.Value.AbsoluteValue, scaledTime, ref value.AbsoluteValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,3 +1,5 @@
|
||||
using Cryville.Common.Buffers;
|
||||
using Cryville.Common.Collections.Specialized;
|
||||
using Cryville.Common.Pdt;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -6,94 +8,6 @@ using System.Text.RegularExpressions;
|
||||
using UnityEngine;
|
||||
|
||||
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 {
|
||||
readonly Type m_Type;
|
||||
public Type Type {
|
||||
@@ -123,31 +37,32 @@ namespace Cryville.Crtr {
|
||||
}
|
||||
|
||||
public class RealtimeMotionValue {
|
||||
Type _type;
|
||||
public Vector AbsoluteValue;
|
||||
List<MotionNode> RelativeNodes;
|
||||
public Vector RelativeValue;
|
||||
public IntKeyedDictionary<MotionNode> RelativeNodes;
|
||||
internal byte CloneTypeFlag;
|
||||
|
||||
public RealtimeMotionValue Init(Vector init) {
|
||||
RelativeNodes = new List<MotionNode> {
|
||||
new MotionNode() {
|
||||
Id = 0,
|
||||
Time = new Vec1(0),
|
||||
EndTime = new Vec1(0),
|
||||
Value = init
|
||||
}
|
||||
};
|
||||
AbsoluteValue = (Vector)Activator.CreateInstance(init.GetType());
|
||||
_type = init.GetType();
|
||||
RelativeNodes = new IntKeyedDictionary<MotionNode>();
|
||||
AbsoluteValue = init;
|
||||
RelativeValue = (Vector)Activator.CreateInstance(_type);
|
||||
return this;
|
||||
}
|
||||
|
||||
public RealtimeMotionValue Clone() {
|
||||
var r = new RealtimeMotionValue() {
|
||||
AbsoluteValue = AbsoluteValue.Clone()
|
||||
_type = _type,
|
||||
AbsoluteValue = AbsoluteValue.Clone(),
|
||||
RelativeValue = RelativeValue.Clone(),
|
||||
};
|
||||
|
||||
var rel = new List<MotionNode>(RelativeNodes.Count);
|
||||
for (var i = 0; i < RelativeNodes.Count; i++) {
|
||||
rel.Add(RelativeNodes[i].Clone());
|
||||
var rel = new IntKeyedDictionary<MotionNode>(RelativeNodes.Count);
|
||||
foreach (var node in RelativeNodes) {
|
||||
var dnode = MotionNodePool.Shared.Rent(_type);
|
||||
node.Value.CopyTo(dnode);
|
||||
rel.Add(node.Key, dnode);
|
||||
}
|
||||
r.RelativeNodes = rel;
|
||||
|
||||
@@ -156,108 +71,62 @@ namespace Cryville.Crtr {
|
||||
|
||||
public void CopyTo(RealtimeMotionValue dest) {
|
||||
AbsoluteValue.CopyTo(dest.AbsoluteValue);
|
||||
int tcount = RelativeNodes.Count;
|
||||
int dcount = dest.RelativeNodes.Count;
|
||||
int count = Math.Min(tcount, dcount);
|
||||
for (int i = 0; i < count; i++) {
|
||||
RelativeNodes[i].CopyTo(dest.RelativeNodes[i]);
|
||||
RelativeValue.CopyTo(dest.RelativeValue);
|
||||
dest.ReturnAllRelativeNodes();
|
||||
foreach (var node in RelativeNodes) {
|
||||
var dnode = MotionNodePool.Shared.Rent(_type);
|
||||
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());
|
||||
else
|
||||
dest.RelativeNodes.RemoveRange(tcount, dcount - tcount);
|
||||
}
|
||||
|
||||
public void ReturnAllRelativeNodes() {
|
||||
foreach (var node in RelativeNodes) {
|
||||
MotionNodePool.Shared.Return(_type, node.Value);
|
||||
}
|
||||
RelativeNodes.Clear();
|
||||
}
|
||||
|
||||
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) {
|
||||
int i = RelativeNodes.FindIndex(n => n.Id == node.Id);
|
||||
MotionNode cnode;
|
||||
if (i == -1) {
|
||||
cnode = new MotionNode {
|
||||
Id = node.Id,
|
||||
Time = new Vec1(0),
|
||||
EndTime = new Vec1(0),
|
||||
Value = (Vector)Activator.CreateInstance(AbsoluteValue.GetType())
|
||||
};
|
||||
}
|
||||
else {
|
||||
cnode = RelativeNodes[i];
|
||||
RelativeNodes.RemoveAt(i);
|
||||
if (!RelativeNodes.TryGetValue(node.Id, out cnode)) {
|
||||
cnode = MotionNodePool.Shared.Rent(_type);
|
||||
cnode.Id = node.Id;
|
||||
RelativeNodes.Add(node.Id, cnode);
|
||||
}
|
||||
if (node.Time != null) cnode.Time = node.Time;
|
||||
if (node.EndTime != null) cnode.EndTime = node.EndTime;
|
||||
if (node.Value != null) cnode.Value.ReplaceFrom(node.Value);
|
||||
|
||||
int i2 = RelativeNodes.BinarySearch(cnode);
|
||||
if (i2 < 0) i2 = ~i2;
|
||||
RelativeNodes.Insert(i2, cnode);
|
||||
if (node.Value != null) node.Value.CopyTo(cnode.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Computes the motion value at the given relative time.
|
||||
/// Computes the motion value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The vector type of the value.</typeparam>
|
||||
/// <param name="reltime">The relative time.</param>
|
||||
/// <param name="result">The result.</param>
|
||||
/// <returns></returns>
|
||||
[Obsolete]
|
||||
public void GetValue<T>(float reltime, ref T result) where T : Vector {
|
||||
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 void Compute<T>(ref T result) where T : Vector {
|
||||
AbsoluteValue.CopyTo(result);
|
||||
result.ApplyFrom(RelativeValue);
|
||||
}
|
||||
}
|
||||
|
||||
public class MotionNode : IComparable<MotionNode> {
|
||||
public class MotionNode {
|
||||
public short Id = -1;
|
||||
public bool Reset;
|
||||
public Vec1 Time;
|
||||
public Vec1 EndTime;
|
||||
float CmpTime { get { return Time != null ? Time.Value : 0; } }
|
||||
[Obsolete]
|
||||
public TransitionType? Transition;
|
||||
[Obsolete]
|
||||
public Vec1 Rate;
|
||||
public Vector Value;
|
||||
|
||||
public MotionNode Clone() {
|
||||
return new MotionNode() {
|
||||
Id = Id,
|
||||
Time = (Vec1)Time.Clone(),
|
||||
EndTime = (Vec1)EndTime.Clone(),
|
||||
Value = Value.Clone()
|
||||
};
|
||||
public void Init(Type type) {
|
||||
if (Time == null) Time = new Vec1(float.NegativeInfinity);
|
||||
if (EndTime == null) EndTime = new Vec1(float.NegativeInfinity);
|
||||
if (Value == null) Value = (Vector)Activator.CreateInstance(type);
|
||||
}
|
||||
|
||||
public void CopyTo(MotionNode dest) {
|
||||
@@ -267,10 +136,6 @@ namespace Cryville.Crtr {
|
||||
Value.CopyTo(dest.Value);
|
||||
}
|
||||
|
||||
public int CompareTo(MotionNode other) {
|
||||
return CmpTime.CompareTo(other.CmpTime);
|
||||
}
|
||||
|
||||
public void LerpWith(MotionNode start, float lerpedTime, ref MotionNode result) {
|
||||
result.Id = Id;
|
||||
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 Vector() { }
|
||||
|
||||
|
Reference in New Issue
Block a user