Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/animator reset #533

Merged
merged 22 commits into from
Nov 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 42 additions & 9 deletions packages/core/src/animation/Animator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class Animator extends Component {
const { srcPlayData } = animatorLayerData;
const { state: curState } = srcPlayData;
if (curState && curState !== state) {
this._revertDefaultValue(srcPlayData);
this._revertDefaultValue(srcPlayData.state, srcPlayData.stateData);
}

//CM: Not consider same stateName, but different animation
Expand All @@ -119,6 +119,26 @@ export class Animator extends Component {
this._saveDefaultValues(animatorStateData);
}

/**
* @internal
*/
_reset(): void {
const { _animatorController: animatorController } = this;
if (animatorController) {
const layers = animatorController.layers;
for (let i = 0, n = layers.length; i < n; ++i) {
const { states } = layers[i].stateMachine;
const animatorLayerData = this._getAnimatorLayerData(i);
for (let j = 0, m = states.length; j < m; ++j) {
const state = states[j];
const animatorStateData = this._getAnimatorStateData(state.name, state, animatorLayerData);
this._revertDefaultValue(state, animatorStateData);
}
}
}
this._clearPlayData();
}

/**
* Create a cross fade from the current state to another state.
* @param stateName - The state name
Expand Down Expand Up @@ -363,6 +383,7 @@ export class Animator extends Component {
if (owner.crossCurveMark === crossCurveMark) {
crossCurveData[owner.crossCurveIndex].destCurveIndex = i;
} else {
owner.saveDefaultValue();
saveFixed && owner.saveFixedPoseValue();
owner.crossCurveMark = crossCurveMark;
owner.crossCurveIndex = crossCurveData.length;
Expand Down Expand Up @@ -498,7 +519,7 @@ export class Animator extends Component {

let crossWeight = destPlayData.frameTime / (destState._getDuration() * layerData.crossFadeTransition.duration);
crossWeight >= 1.0 && (crossWeight = 1.0);

srcPlayData.update();
destPlayData.update();

Expand Down Expand Up @@ -639,6 +660,12 @@ export class Animator extends Component {
break;
}
}
} else if (owner.type === SkinnedMeshRenderer) {
switch (owner.property) {
case AnimationProperty.BlendShapeWeights:
(<SkinnedMeshRenderer>owner.component).blendShapeWeights = <Float32Array>value;
break;
}
}

if (additive) {
Expand Down Expand Up @@ -717,11 +744,11 @@ export class Animator extends Component {
}
}

private _revertDefaultValue(playData: AnimatorStatePlayData) {
const { clip } = playData.state;
private _revertDefaultValue(state: AnimatorState, stateData: AnimatorStateData) {
const { clip } = state;
if (clip) {
const curves = clip._curveBindings;
const { curveOwners } = playData.stateData;
const { curveOwners } = stateData;
for (let i = curves.length - 1; i >= 0; i--) {
const owner = curveOwners[i];
const { transform } = owner.target;
Expand All @@ -735,6 +762,12 @@ export class Animator extends Component {
case AnimationProperty.Scale:
transform.scale = <Vector3>owner.defaultValue;
break;
case AnimationProperty.BlendShapeWeights:
const { blendShapeWeights } = <SkinnedMeshRenderer>owner.component;
for (let j = 0, length = blendShapeWeights.length; j < length; ++j) {
(<SkinnedMeshRenderer>owner.component).blendShapeWeights[j] = owner.defaultValue[j];
}
break;
}
}
}
Expand All @@ -756,7 +789,7 @@ export class Animator extends Component {
}
}

private _crossFadeByTransition(transition: AnimatorStateTransition, layerIndex: number) {
private _crossFadeByTransition(transition: AnimatorStateTransition, layerIndex: number): void {
const { name } = transition.destinationState;
const animatorStateInfo = this._getAnimatorStateInfo(name, layerIndex, Animator._animatorInfo);
const { state: crossState } = animatorStateInfo;
Expand All @@ -777,8 +810,6 @@ export class Animator extends Component {
const offset = duration * transition.offset;
destPlayData.reset(crossState, animatorStateData, offset);

this._saveDefaultValues(animatorStateData);

switch (layerState) {
// Maybe not play, maybe end.
case LayerState.Standby:
Expand Down Expand Up @@ -870,6 +901,8 @@ export class Animator extends Component {
this._animatorLayersData.length = 0;
this._crossCurveDataCollection.length = 0;
this._animationCurveOwners.length = 0;
this._controllerUpdateFlag.flag = false;
if (this._controllerUpdateFlag) {
this._controllerUpdateFlag.flag = false;
}
}
}
12 changes: 12 additions & 0 deletions packages/core/src/animation/internal/AnimationCurveOwner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ export class AnimationCurveOwner {
case AnimationProperty.Scale:
this.target.transform.scale.cloneTo(<Vector3>this.defaultValue);
break;
case AnimationProperty.BlendShapeWeights:
const { blendShapeWeights } = <SkinnedMeshRenderer>this.component;
for (let i = 0, length = blendShapeWeights.length; i < length; ++i) {
this.defaultValue[i] = (<SkinnedMeshRenderer>this.component).blendShapeWeights[i];
}
break;
}
}

Expand All @@ -72,6 +78,12 @@ export class AnimationCurveOwner {
case AnimationProperty.Scale:
this.target.transform.scale.cloneTo(<Vector3>this.fixedPoseValue);
break;
case AnimationProperty.BlendShapeWeights:
const { blendShapeWeights } = <SkinnedMeshRenderer>this.component;
for (let i = 0, length = blendShapeWeights.length; i < length; ++i) {
this.fixedPoseValue[i] = (<SkinnedMeshRenderer>this.component).blendShapeWeights[i];
}
break;
}
}
}
35 changes: 24 additions & 11 deletions packages/loader/src/scene-loader/GLTFModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,14 @@ export class GLTFModel extends Component {
set clipPreview(value: string) {
if (this._animator) {
if (value) {
this._animator.play(value, 0);
if (value === "_default") {
this._playDefaultState();
} else {
this._animator.play(value, 0);
}
} else {
this._playDefaultState();
// @ts-ignore
this._animator._reset();
}
}
this._clipPreview = value;
Expand All @@ -104,7 +109,7 @@ export class GLTFModel extends Component {
* @param props - Init props
*/
init(props): void {
const { asset = null, speed, animatorController, clipPreview, isClone } = props;
const { asset = null, speed, animatorController, clipPreview, isClone } = props;
if (isClone) {
const rootName = (props as any).gltfRootName;
if (rootName) {
Expand Down Expand Up @@ -153,12 +158,17 @@ export class GLTFModel extends Component {
_playState() {
const playStateName = this._clipPreview;
if (playStateName) {
this._animator.play(playStateName, 0);
if (this._controllerUpdateFlag?.flag) {
this._controllerUpdateFlag.flag = false;
if (playStateName === "_default") {
this._playDefaultState();
} else {
this._animator.play(playStateName, 0);
}
} else {
this._playDefaultState();
// @ts-ignore
this._animator._reset();
}
if (this._controllerUpdateFlag?.flag) {
this._controllerUpdateFlag.flag = false;
}
}

Expand All @@ -169,13 +179,16 @@ export class GLTFModel extends Component {
const { layers } = animatorController;
for (let i = 0, length = layers.length; i < length; ++i) {
//@ts-ignore
const defaultState = layers[i]?.stateMachine?._defaultState ?? layers[i]?.stateMachine?.states[0];
const defaultState = layers[i]?.stateMachine?._defaultState;
const defaultStateName = defaultState?.name;
if (defaultStateName) {
animator.play(defaultStateName, i);
if (this._controllerUpdateFlag?.flag) {
this._controllerUpdateFlag.flag = false;
}
} else {
// @ts-ignore
animator._reset();
}
if (this._controllerUpdateFlag?.flag) {
this._controllerUpdateFlag.flag = false;
}
}
}
Expand Down