Skip to content

Commit

Permalink
Fix sibling scheduling
Browse files Browse the repository at this point in the history
  • Loading branch information
acidbubbles committed Sep 2, 2021
1 parent 90de078 commit 1356197
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 25 deletions.
77 changes: 52 additions & 25 deletions src/AtomAnimations/Animations/AtomAnimation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,11 @@ public void PlayClips(string animationName, bool sequencing)
public void PlayClip(AtomAnimationClip clip, bool sequencing, bool allowPreserveLoops = true)
{
paused = false;
var playingChanged = false;
var isPlayingChanged = false;
if (clip.playbackEnabled && clip.playbackMainInLayer) return;
if (!isPlaying)
{
playingChanged = true;
isPlayingChanged = true;
isPlaying = true;
this.sequencing = this.sequencing || sequencing;
fadeManager?.SyncFadeTime();
Expand Down Expand Up @@ -315,17 +315,17 @@ public void PlayClip(AtomAnimationClip clip, bool sequencing, bool allowPreserve
clip.animationPattern.ResetAndPlay();
}

if (sequencing && clip.nextAnimationName != null)
AssignNextAnimation(clip);

if (playingChanged)
if (isPlayingChanged)
onIsPlayingChanged.Invoke(clip);

if (clip.playbackEnabled)
PlaySiblings(clip);

if (sequencing && clip.nextAnimationName != null)
AssignNextAnimation(clip);
}

private void PlaySiblings(AtomAnimationClip clip, bool allowPreserveLoops = true)
private void PlaySiblings(AtomAnimationClip clip)
{
var siblings = clip.animationSet != null ? index.BySet(clip.animationSet) : index.ByName(clip.animationName);
if (siblings.Count < 2) return;
Expand All @@ -346,7 +346,13 @@ private void PlaySiblings(AtomAnimationClip clip, bool allowPreserveLoops = true
}
if (main == null)
{
PlayClip(sibling, sequencing, allowPreserveLoops);
BlendIn(sibling, sibling.blendInDuration);
sibling.playbackMainInLayer = true;
if (!ReferenceEquals(sibling.animationPattern, null))
{
sibling.animationPattern.SetBoolParamValue("loopOnce", false);
sibling.animationPattern.ResetAndPlay();
}
continue;
}
if (clip.animationSet != null && main.animationSet == clip.animationSet) return;
Expand Down Expand Up @@ -665,18 +671,26 @@ private void ScheduleNextAnimation(AtomAnimationClip source, AtomAnimationClip n

nextTime = nextTime.RoundToNearest(source.animationLength);
}
nextTime = nextTime - next.blendInDuration / 2f + (source.animationLength - source.clipTime);
//SuperController.LogMessage($"T{Time.time:0.000}s: clip: {source.clipTime}, next: {nextTime:0.000}s");

if (source.playbackBlendWeight > 0f)
nextTime += source.animationLength - source.clipTime;
}
else if (source.nextAnimationTimeRandomize > 0f)
{
nextTime = Random.Range(nextTime, nextTime + source.nextAnimationTimeRandomize);
}
nextTime -= next.halfBlendInDuration;
}
else
{
nextTime = Mathf.Min(nextTime, source.animationLength - next.blendInDuration);
if (nextTime < float.Epsilon) nextTime = float.Epsilon;
}

if (nextTime < float.Epsilon)
{
// SuperController.LogError($"Timeline: Blending from animation {source.animationNameQualified} to {next.animationNameQualified} with blend time {next.blendInDuration} results in negative value: {nextTime}. Transition will be skipped.");
// return;
nextTime = 0f;
}

ScheduleNextAnimation(source, next, nextTime);
Expand All @@ -685,7 +699,8 @@ private void ScheduleNextAnimation(AtomAnimationClip source, AtomAnimationClip n
private void ScheduleNextAnimation(AtomAnimationClip source, AtomAnimationClip next, float nextTime)
{
source.playbackScheduledNextAnimationName = next.animationName;
source.playbackScheduledNextTimeLeft = nextTime - source.clipTime;
source.playbackScheduledNextTimeLeft = nextTime;

if (next.fadeOnTransition && next.animationLayer == index.mainLayer && fadeManager != null)
{
source.playbackScheduledFadeOutAtRemaining = (fadeManager.fadeOutTime + fadeManager.halfBlackTime) * source.speed * speed;
Expand All @@ -695,33 +710,45 @@ private void ScheduleNextAnimation(AtomAnimationClip source, AtomAnimationClip n
source.playbackScheduledFadeOutAtRemaining = float.NaN;
}
}
SyncSiblingClips(source, next);

ScheduleSiblingsNextAnimation(source, next);
}

private void SyncSiblingClips(AtomAnimationClip source, AtomAnimationClip next)
private void ScheduleSiblingsNextAnimation(AtomAnimationClip source, AtomAnimationClip next)
{
AtomAnimationClip siblingSourceClip = null;
AtomAnimationClip siblingNextClip = null;

var layers = index.ByLayer();
for (var i = 0; i < layers.Count; i++)
{
var layer = layers[i];
if (layer[0].animationLayer == source.animationLayer) continue;

AtomAnimationClip siblingSourceClip = null;
for (var j = 0; j < layer.Count; j++)
{
var clip = layer[j];
if (clip.animationName == source.animationName) siblingSourceClip = clip;
else if (clip.animationName == next.animationName) siblingNextClip = clip;
if (siblingSourceClip != null && siblingNextClip != null) break;
if (clip.animationSet == source.animationSet || clip.animationName == source.animationName)
{
siblingSourceClip = clip;
break;
}
}
if (siblingSourceClip != null && siblingNextClip != null) break;
}
if (siblingSourceClip == null) continue;

if (siblingSourceClip == null || siblingNextClip == null) return;
AtomAnimationClip siblingNextClip = null;
for (var j = 0; j < layer.Count; j++)
{
var clip = layer[j];
if (clip.animationSet == next.animationSet || clip.animationName == next.animationName)
{
siblingNextClip = clip;
break;
}
}
if (siblingNextClip == null) continue;

siblingSourceClip.playbackScheduledNextAnimationName = siblingNextClip.animationName;
siblingSourceClip.playbackScheduledNextTimeLeft = source.playbackScheduledNextTimeLeft;
siblingSourceClip.playbackScheduledNextAnimationName = siblingNextClip.animationName;
siblingSourceClip.playbackScheduledNextTimeLeft = source.playbackScheduledNextTimeLeft;
}
}

#endregion
Expand Down
2 changes: 2 additions & 0 deletions src/AtomAnimations/Animations/AtomAnimationClip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ public bool preserveLoops
}
}

public float halfBlendInDuration { get; private set; } = DefaultBlendDuration / 2f;
public float blendInDuration
{
get
Expand All @@ -265,6 +266,7 @@ public float blendInDuration
{
if (_blendDuration == value) return;
_blendDuration = value;
halfBlendInDuration = value / 2f;
UpdateForcedNextAnimationTime();
onAnimationSettingsChanged.Invoke(nameof(blendInDuration));
}
Expand Down

0 comments on commit 1356197

Please sign in to comment.