Marionette/PoseGraph: disallow empty states in PoseNodeStateMachine (#15282)

This commit is contained in:
Leslie Leigh (李的序)
2023-06-02 10:32:38 +08:00
committed by GitHub
parent 07f3dedad9
commit f596589b8a
2 changed files with 47 additions and 37 deletions
+37 -10
View File
@@ -293,6 +293,11 @@ export class StateMachine extends EditorExtendable {
@serializable
private _anyState: State;
/**
* @internal
*/
public _allowEmptyStates = true;
/**
* // TODO: HACK
* @internal
@@ -310,8 +315,9 @@ export class StateMachine extends EditorExtendable {
}
}
constructor () {
constructor (allowEmptyStates?: boolean) {
super();
this._allowEmptyStates = allowEmptyStates ?? false;
this._entryState = this._addState(new State());
this._entryState.name = 'Entry';
this._exitState = this._addState(new State());
@@ -328,6 +334,10 @@ export class StateMachine extends EditorExtendable {
});
}
public get allowEmptyStates () {
return this._allowEmptyStates;
}
/**
* The entry state.
*/
@@ -413,7 +423,7 @@ export class StateMachine extends EditorExtendable {
* @returns The newly created state machine.
*/
public addSubStateMachine (): SubStateMachine {
return this._addState(new SubStateMachine());
return this._addState(new SubStateMachine(this._allowEmptyStates));
}
/**
@@ -421,6 +431,9 @@ export class StateMachine extends EditorExtendable {
* @returns The newly created empty state.
*/
public addEmpty () {
if (!this._allowEmptyStates) {
throw new Error(`Empty states are now allowed in this state machine.`);
}
return this._addState(new EmptyState());
}
@@ -726,7 +739,7 @@ export class StateMachine extends EditorExtendable {
}
public clone () {
const that = new StateMachine();
const that = new StateMachine(this._allowEmptyStates);
this.copyTo(that);
return that;
}
@@ -740,6 +753,11 @@ export class StateMachine extends EditorExtendable {
@ccclass('cc.animation.SubStateMachine')
export class SubStateMachine extends InteractiveState {
constructor (allowEmptyStates?: boolean) {
super();
this._stateMachine = new StateMachine(allowEmptyStates);
}
get stateMachine () {
return this._stateMachine;
}
@@ -756,7 +774,7 @@ export class SubStateMachine extends InteractiveState {
}
@serializable
private _stateMachine: StateMachine = new StateMachine();
private _stateMachine: StateMachine;
}
@ccclass(`${CLASS_NAME_PREFIX_ANIM}PoseGraphStash`)
@@ -786,6 +804,19 @@ export class Layer implements OwnedBy<AnimationGraph> {
@serializable
public additive = false;
/**
* // TODO: HACK
* @internal
*/
public __callOnAfterDeserializeRecursive () {
this.stateMachine._allowEmptyStates = true;
this.stateMachine.__callOnAfterDeserializeRecursive();
for (const stashId in this._stashes) {
const stash = this._stashes[stashId];
stash.graph.__callOnAfterDeserializeRecursive();
}
}
public stashes (): Iterable<Readonly<[string, PoseGraphStash]>> {
return Object.entries(this._stashes);
}
@@ -810,7 +841,7 @@ export class Layer implements OwnedBy<AnimationGraph> {
* @marked_as_engine_private
*/
constructor () {
this._stateMachine = new StateMachine();
this._stateMachine = new StateMachine(true);
}
get stateMachine () {
@@ -857,11 +888,7 @@ export class AnimationGraph extends AnimationGraphLike implements AnimationGraph
const { _layers: layers } = this;
const nLayers = layers.length;
for (let iLayer = 0; iLayer < nLayers; ++iLayer) {
const layer = layers[iLayer];
layer.stateMachine.__callOnAfterDeserializeRecursive();
for (const [_, stash] of layer.stashes()) {
stash.graph.__callOnAfterDeserializeRecursive();
}
layers[iLayer].__callOnAfterDeserializeRecursive();
}
}
@@ -2,15 +2,16 @@ import { EDITOR } from 'internal:constants';
import { assertIsTrue } from '../../../../core';
import { ccclass, serializable } from '../../../../core/data/decorators';
import { CLASS_NAME_PREFIX_ANIM } from '../../../define';
import { PoseNode, PoseTransformSpaceRequirement } from '../pose-node';
import { blendPoseInto, Pose } from '../../../core/pose';
import { AnimationGraphBindingContext, AnimationGraphEvaluationContext, AnimationGraphSettleContext, AnimationGraphUpdateContext, AnimationGraphUpdateContextGenerator } from '../../animation-graph-context';
import { PoseNode } from '../pose-node';
import { Pose } from '../../../core/pose';
import {
AnimationGraphBindingContext, AnimationGraphEvaluationContext,
AnimationGraphSettleContext, AnimationGraphUpdateContext,
} from '../../animation-graph-context';
import { StateMachine } from '../../animation-graph';
import { TopLevelStateMachineEvaluation } from '../../state-machine/state-machine-eval';
import { poseGraphNodeAppearance, poseGraphNodeCategory } from '../decorator/node';
import { POSE_GRAPH_NODE_MENU_PREFIX_POSE } from './menu-common';
import { input } from '../decorator/input';
import { PoseGraphType } from '../foundation/type-system';
@ccclass(`${CLASS_NAME_PREFIX_ANIM}PoseNodeStateMachine`)
@poseGraphNodeCategory(POSE_GRAPH_NODE_MENU_PREFIX_POSE)
@@ -23,17 +24,14 @@ export class PoseNodeStateMachine extends PoseNode {
public name = '';
@serializable
public stateMachine = new StateMachine();
@serializable
@input({ type: PoseGraphType.POSE })
public emptyStatePose: PoseNode | null = null;
public stateMachine = new StateMachine(false);
/**
* // TODO: HACK
* @internal
*/
public __callOnAfterDeserializeRecursive () {
this.stateMachine._allowEmptyStates = false;
this.stateMachine.__callOnAfterDeserializeRecursive();
}
@@ -45,47 +43,32 @@ export class PoseNodeStateMachine extends PoseNode {
context,
null,
);
this.emptyStatePose?.bind(context);
}
public settle (context: AnimationGraphSettleContext) {
this._stateMachineEval?.settle(context);
this.emptyStatePose?.settle(context);
}
public reenter () {
this._stateMachineEval?.reenter();
this.emptyStatePose?.reenter();
}
protected doUpdate (context: AnimationGraphUpdateContext): void {
const { _stateMachineEval: stateMachineEval } = this;
assertIsTrue(stateMachineEval);
stateMachineEval.update(context);
if (this.emptyStatePose && stateMachineEval.passthroughWeight < (1.0 - 1e-5)) {
const updateContext = this._updateContextGenerator.generate(
context.deltaTime,
context.indicativeWeight * (1.0 - stateMachineEval.passthroughWeight),
);
this.emptyStatePose.update(updateContext);
}
assertIsTrue(stateMachineEval.passthroughWeight > (1.0 - 1e-5));
}
public doEvaluate (context: AnimationGraphEvaluationContext): Pose {
const { _stateMachineEval: stateMachineEval } = this;
assertIsTrue(stateMachineEval);
const stateMachinePose = stateMachineEval.evaluate(context);
if (stateMachineEval.passthroughWeight < (1.0 - 1e-5)) {
const emptyStatePose = this.emptyStatePose?.evaluate(context, PoseTransformSpaceRequirement.LOCAL)
?? PoseNodeStateMachine.evaluateDefaultPose(context, PoseTransformSpaceRequirement.LOCAL);
blendPoseInto(stateMachinePose, emptyStatePose, 1.0 - stateMachineEval.passthroughWeight);
context.popPose();
}
assertIsTrue(stateMachineEval.passthroughWeight > (1.0 - 1e-5));
return stateMachinePose;
}
private _stateMachineEval: TopLevelStateMachineEvaluation | undefined;
private _updateContextGenerator = new AnimationGraphUpdateContextGenerator();
}
if (EDITOR) {