Fix clip frame events are not fired in animation graph (#15405)

* Support clip frame events in animation graph

* Opt

* Opt
This commit is contained in:
Leslie Leigh (李的序)
2023-06-12 18:31:21 +08:00
committed by GitHub
parent 997491be8f
commit ea58020d3f
5 changed files with 37 additions and 0 deletions
+12
View File
@@ -1529,7 +1529,19 @@ class EventEvaluator {
}
}
public reset () {
this._lastFrameIndex = -1;
this._lastIterations = 0.0;
this._lastDirection = 0;
this._ignoreIndex = InvalidIndex;
this._sampled = false;
}
public sample (ratio: number, direction: number, iterations: number) {
if (this._eventGroups.length === 0) {
return;
}
const length = this._eventGroups.length;
let eventIndex = getEventGroupIndexAtRatio(ratio, this._ratios);
if (eventIndex < 0) {
@@ -212,6 +212,14 @@ class AnimationBlendPort implements MotionPort {
return this._host.__evaluatePort(this, progress, context);
}
public reenter () {
const { childPorts } = this;
const nChildPorts = childPorts.length;
for (let iChild = 0; iChild < nChildPorts; ++iChild) {
childPorts[iChild]?.reenter();
}
}
private _host: AnimationBlendEval;
}
@@ -149,6 +149,9 @@ class ClipMotionEval implements MotionEval {
const pose = clipEval.evaluate(clipTime, context);
// Sample frame events.
this._frameEventEval?.sample(wrapInfo.ratio, wrapInfo.direction, wrapInfo.iterations);
// Evaluate embedded players.
this._clipEmbeddedPlayerEval?.evaluate(clipTime, Math.trunc(wrapInfo.iterations));
@@ -163,6 +166,10 @@ class ClipMotionEval implements MotionEval {
}
}
public reenter () {
this._frameEventEval?.reset();
}
/**
* Preserved here for clip overriding.
*/
@@ -173,12 +180,14 @@ class ClipMotionEval implements MotionEval {
private declare _clip: AnimationClip;
private declare _clipEval: AnimationClipAGEvaluation;
private _clipEmbeddedPlayerEval: ReturnType<AnimationClip['createEmbeddedPlayerEvaluator']> | null = null;
private _frameEventEval: ReturnType<AnimationClip['createEventEvaluator']> | null = null;
private _wrapInfo = new WrappedInfo();
private _duration = 0.0;
private _ignoreEmbeddedPlayers: boolean;
private _setClip (clip: AnimationClip, context: AnimationGraphBindingContext) {
this._clipEval?.destroy();
this._frameEventEval = null;
if (this._clipEmbeddedPlayerEval) {
this._clipEmbeddedPlayerEval.destroy();
this._clipEmbeddedPlayerEval = null;
@@ -189,6 +198,7 @@ class ClipMotionEval implements MotionEval {
? 0.0
: clip.duration / clip.speed; // TODO, a test for `clip.speed === 0` is required!
this._clipEval = createAnimationAGEvaluation(clip, context);
this._frameEventEval = clip.createEventEvaluator(context.origin);
if (!this._ignoreEmbeddedPlayers && clip.containsAnyEmbeddedPlayer()) {
this._clipEmbeddedPlayerEval = clip.createEmbeddedPlayerEvaluator(context.origin);
}
@@ -204,5 +214,9 @@ class ClipMotionPort implements MotionPort {
return this._eval[evaluatePortTag](progress, context);
}
public reenter () {
this._eval.reenter();
}
private _eval: ClipMotionEval;
}
@@ -84,4 +84,6 @@ export abstract class Motion extends EditorExtendable {
export interface MotionPort {
evaluate(progress: number, context: AnimationGraphEvaluationContext): Pose;
reenter(): void;
}
@@ -1345,6 +1345,7 @@ class VMSMInternalState extends EventifiedStateEval {
public reenter (initialTimeNormalized: number) {
this._progress = initialTimeNormalized;
this._port?.reenter();
}
public getStatus () {