From ea58020d3f481ee48eae92427c453c0cbbc4d34e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leslie=20Leigh=20=28=E6=9D=8E=E7=9A=84=E5=BA=8F=29?= Date: Mon, 12 Jun 2023 18:31:21 +0800 Subject: [PATCH] Fix clip frame events are not fired in animation graph (#15405) * Support clip frame events in animation graph * Opt * Opt --- cocos/animation/animation-clip.ts | 12 ++++++++++++ .../animation/marionette/motion/animation-blend.ts | 8 ++++++++ cocos/animation/marionette/motion/clip-motion.ts | 14 ++++++++++++++ cocos/animation/marionette/motion/motion.ts | 2 ++ .../marionette/state-machine/state-machine-eval.ts | 1 + 5 files changed, 37 insertions(+) diff --git a/cocos/animation/animation-clip.ts b/cocos/animation/animation-clip.ts index a40293ef47..518ce7396e 100644 --- a/cocos/animation/animation-clip.ts +++ b/cocos/animation/animation-clip.ts @@ -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) { diff --git a/cocos/animation/marionette/motion/animation-blend.ts b/cocos/animation/marionette/motion/animation-blend.ts index c7d99d41bf..64ddfcc39e 100644 --- a/cocos/animation/marionette/motion/animation-blend.ts +++ b/cocos/animation/marionette/motion/animation-blend.ts @@ -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; } diff --git a/cocos/animation/marionette/motion/clip-motion.ts b/cocos/animation/marionette/motion/clip-motion.ts index 7b0eb746ba..33a749ab36 100644 --- a/cocos/animation/marionette/motion/clip-motion.ts +++ b/cocos/animation/marionette/motion/clip-motion.ts @@ -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 | null = null; + private _frameEventEval: ReturnType | 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; } diff --git a/cocos/animation/marionette/motion/motion.ts b/cocos/animation/marionette/motion/motion.ts index f8d7871868..d4481dd0e2 100644 --- a/cocos/animation/marionette/motion/motion.ts +++ b/cocos/animation/marionette/motion/motion.ts @@ -84,4 +84,6 @@ export abstract class Motion extends EditorExtendable { export interface MotionPort { evaluate(progress: number, context: AnimationGraphEvaluationContext): Pose; + + reenter(): void; } diff --git a/cocos/animation/marionette/state-machine/state-machine-eval.ts b/cocos/animation/marionette/state-machine/state-machine-eval.ts index f4eae03bff..39d3e891ce 100644 --- a/cocos/animation/marionette/state-machine/state-machine-eval.ts +++ b/cocos/animation/marionette/state-machine/state-machine-eval.ts @@ -1345,6 +1345,7 @@ class VMSMInternalState extends EventifiedStateEval { public reenter (initialTimeNormalized: number) { this._progress = initialTimeNormalized; + this._port?.reenter(); } public getStatus () {