Refine dangling transition behaviour (#9466)

This commit is contained in:
Leslie Leigh (李的序)
2021-10-13 13:50:51 +08:00
committed by pandamicro
parent dd43bbb12a
commit 81d41f5446
2 changed files with 166 additions and 32 deletions
+95 -32
View File
@@ -222,6 +222,10 @@ class LayerEval {
public getCurrentTransition (transitionStatus: TransitionStatus): boolean {
const { _currentTransitionPath: currentTransitionPath } = this;
if (currentTransitionPath.length !== 0) {
const lastNode = currentTransitionPath[currentTransitionPath.length - 1];
if (lastNode.to.kind !== NodeKind.animation) {
return false;
}
const {
duration,
normalizedDuration,
@@ -393,6 +397,11 @@ class LayerEval {
assertIsTrue(!this.exited);
graphDebugGroup(`[Layer ${this.name}]: UpdateStart ${deltaTime}s`);
const haltOnNonMotionState = this._continueDanglingTransition();
if (haltOnNonMotionState) {
return 0.0;
}
const MAX_ITERATIONS = 100;
let passConsumed = 0.0;
@@ -458,7 +467,10 @@ class LayerEval {
}
remainTimePiece -= updateRequires;
this._switchTo(transition);
const ranIntoNonMotionState = this._switchTo(transition);
if (ranIntoNonMotionState) {
break;
}
continueNextIterationForce = true;
} else { // If no transition matched, we update current node.
@@ -621,59 +633,110 @@ class LayerEval {
return null;
}
/**
* Try switch current node using specified transition.
* @param transition The transition.
* @returns If the transition finally ran into entry/exit state.
*/
private _switchTo (transition: TransitionEval) {
const { _currentNode: currentNode } = this;
graphDebugGroup(`[SubStateMachine ${this.name}]: STARTED ${currentNode.name} -> ${transition.to.name}.`);
// TODO: what if the first is entry(ie. not animation play)?
// TODO: what if two of the path use same trigger?
let currentTransition = transition;
const { _currentTransitionPath: currentTransitionPath } = this;
for (; ;) {
// Reset triggers
this._resetTriggersOnTransition(currentTransition);
currentTransitionPath.push(currentTransition);
const { to } = currentTransition;
if (to.kind === NodeKind.animation) {
break;
this._consumeTransition(transition);
currentTransitionPath.push(transition);
const motionNode = this._matchTransitionPathUntilMotion();
if (motionNode) {
// Apply transitions
this._doTransitionToMotion(motionNode);
return false;
} else {
return true;
}
}
/**
* Called every frame(not every iteration).
* Returns if we ran into an entry/exit node and still no satisfied transition matched this frame.
*/
private _continueDanglingTransition () {
const {
_currentTransitionPath: currentTransitionPath,
} = this;
const lenCurrentTransitionPath = currentTransitionPath.length;
if (lenCurrentTransitionPath === 0) {
return false;
}
const lastTransition = currentTransitionPath[lenCurrentTransitionPath - 1];
const tailNode = lastTransition.to;
if (tailNode.kind !== NodeKind.animation) {
const motionNode = this._matchTransitionPathUntilMotion();
if (motionNode) {
// Apply transitions
this._doTransitionToMotion(motionNode);
return false;
} else {
return true;
}
}
if (to.kind === NodeKind.entry) {
// We're entering a state machine
this._callEnterMethods(to);
}
return false;
}
private _matchTransitionPathUntilMotion () {
const {
_currentTransitionPath: currentTransitionPath,
} = this;
const lenCurrentTransitionPath = currentTransitionPath.length;
assertIsTrue(lenCurrentTransitionPath !== 0);
const lastTransition = currentTransitionPath[lenCurrentTransitionPath - 1];
let tailNode = lastTransition.to;
for (; tailNode.kind !== NodeKind.animation;) {
const transitionMatch = this._matchTransition(
to,
to,
tailNode,
tailNode,
0.0,
transitionMatchCache,
);
if (!transitionMatch) {
break;
}
currentTransition = transitionMatch.transition;
const transition = transitionMatch.transition;
this._consumeTransition(transition);
currentTransitionPath.push(transition);
tailNode = transition.to;
}
const realTargetNode = currentTransition.to;
if (realTargetNode.kind !== NodeKind.animation) {
// We ran into a exit/entry node.
// Current: ignore the transition, but triggers are consumed
// TODO: what should we done here?
currentTransitionPath.length = 0;
return;
}
return tailNode.kind === NodeKind.animation ? tailNode : null;
}
// Apply transitions
private _consumeTransition (transition: TransitionEval) {
const { to } = transition;
// Reset triggers
this._resetTriggersOnTransition(transition);
if (to.kind === NodeKind.entry) {
// We're entering a state machine
this._callEnterMethods(to);
}
}
private _doTransitionToMotion (targetNode: MotionStateEval) {
this._transitionProgress = 0.0;
this._currentTransitionToNode = realTargetNode;
this._currentTransitionToNode = targetNode;
realTargetNode.resetToPort();
this._callEnterMethods(realTargetNode);
graphDebugGroupEnd();
targetNode.resetToPort();
this._callEnterMethods(targetNode);
}
/**
+71
View File
@@ -924,6 +924,77 @@ describe('NewGen Anim', () => {
},
});
});
test(`Ran into entry/exit node`, () => {
const animationGraph = new AnimationGraph();
const layer = animationGraph.addLayer();
const graph = layer.stateMachine;
const animState = graph.addMotion();
animState.name = 'AnimState';
const animClip = animState.motion = createClipMotionPositionX(1.0, 2.0, 'AnimStateClip');
const subStateMachine = graph.addSubStateMachine();
subStateMachine.name = 'SubStateMachine';
const subStateMachineAnimState = subStateMachine.stateMachine.addMotion();
subStateMachineAnimState.name = 'SubStateMachineAnimState';
const subStateMachineAnimStateClip = subStateMachineAnimState.motion = createClipMotionPositionX(1.0, 3.0, 'SubStateMachineAnimStateClip');
subStateMachine.stateMachine.connect(subStateMachine.stateMachine.entryState, subStateMachineAnimState);
const subStateMachineAnimStateToExit = subStateMachine.stateMachine.connect(
subStateMachineAnimState, subStateMachine.stateMachine.exitState);
subStateMachineAnimStateToExit.exitConditionEnabled = true;
subStateMachineAnimStateToExit.exitCondition = 1.0;
graph.connect(graph.entryState, subStateMachine);
const subStateMachineToAnimState = graph.connect(subStateMachine, animState);
const [ triggerCondition ] = subStateMachineToAnimState.conditions = [new TriggerCondition()];
triggerCondition.trigger = 'trigger';
animationGraph.addVariable('trigger', VariableType.TRIGGER);
const graphEval = createAnimationGraphEval(animationGraph, new Node());
graphEval.update(0.5);
expectAnimationGraphEvalStatusLayer0(graphEval, {
current: {
clip: subStateMachineAnimStateClip.clip!,
weight: 1.0,
},
});
graphEval.update(0.6);
expectAnimationGraphEvalStatusLayer0(graphEval, {
current: {
clip: subStateMachineAnimStateClip.clip!,
weight: 1.0,
},
});
// Still halt
graphEval.update(5.0);
expectAnimationGraphEvalStatusLayer0(graphEval, {
current: {
clip: subStateMachineAnimStateClip.clip!,
weight: 1.0,
},
});
graphEval.setValue('trigger', true);
graphEval.update(0.1);
expectAnimationGraphEvalStatusLayer0(graphEval, {
current: {
clip: subStateMachineAnimStateClip.clip!,
weight: 0.6666667,
},
transition: {
time: 0.1,
next: {
clip: animClip.clip!,
weight: 0.333333,
},
},
});
});
});
describe(`Any state`, () => {