From bf6892a99ec50d8435afbd60cc623ff19a925c86 Mon Sep 17 00:00:00 2001 From: Yun Hsiao Wu Date: Sun, 12 Jan 2020 14:54:29 +0800 Subject: [PATCH] fix anim curve generation (#6077) --- cocos/core/animation/animation-state.ts | 24 +++------ .../animation/skeletal-animation-component.ts | 26 +-------- .../animation/skeletal-animation-data-hub.ts | 24 ++------- .../animation/skeletal-animation-state.ts | 54 +++++++------------ cocos/core/game.ts | 4 +- cocos/core/renderer/models/skinning-model.ts | 4 +- 6 files changed, 37 insertions(+), 99 deletions(-) diff --git a/cocos/core/animation/animation-state.ts b/cocos/core/animation/animation-state.ts index 17a587e298..50731f1991 100644 --- a/cocos/core/animation/animation-state.ts +++ b/cocos/core/animation/animation-state.ts @@ -33,17 +33,16 @@ import { AnimationBlendState, PropertyBlendState } from './animation-blend-state import { AnimationClip, IRuntimeCurve } from './animation-clip'; import { AnimCurve, RatioSampler } from './animation-curve'; import { additive3D, additiveQuat, BlendFunction } from './blending'; -import { Playable } from './playable'; import { BoundTarget, BufferedTarget } from './bound-target'; +import { Playable } from './playable'; import { WrapMode, WrapModeMask, WrappedInfo } from './types'; -import { warn } from '../platform'; enum PropertySpecialization { NodePosition, NodeScale, NodeRotation, None, -}; +} export class ICurveInstance { public commonTargetIndex?: number; @@ -114,7 +113,7 @@ export class ICurveInstance { return; } let value: any; - if (!lerpRequired) { + if (!this._curve.hasLerp() || !lerpRequired) { value = this._curve.valueAt(index); } else { value = this._curve.valueBetween( @@ -646,18 +645,9 @@ export class AnimationState extends Playable { } public simpleProcess () { - let time = this.time; const duration = this.duration; - - if (time > duration) { - time = time % duration; - if (time === 0) { time = duration; } - } - else if (time < 0) { - time = time % duration; - if (time !== 0) { time += duration; } - } - + let time = this.time % duration; + if (time < 0) { time += duration; } const ratio = time / duration; this._sampleCurves(ratio); @@ -727,14 +717,14 @@ export class AnimationState extends Playable { this.emit('pause', this); } - private _sampleCurves (ratio: number) { + protected _sampleCurves (ratio: number) { // Before we sample, we pull values of common targets. for (let iCommonTarget = 0; iCommonTarget < this._commonTargetStatuses.length; ++iCommonTarget) { const commonTarget = this._commonTargetStatuses[iCommonTarget]; commonTarget.target.pull(); commonTarget.changed = false; } - + for (let iSamplerSharedGroup = 0, szSamplerSharedGroup = this._samplerSharedGroups.length; iSamplerSharedGroup < szSamplerSharedGroup; ++iSamplerSharedGroup) { const samplerSharedGroup = this._samplerSharedGroups[iSamplerSharedGroup]; diff --git a/cocos/core/animation/skeletal-animation-component.ts b/cocos/core/animation/skeletal-animation-component.ts index 3874f30087..7059e8476b 100644 --- a/cocos/core/animation/skeletal-animation-component.ts +++ b/cocos/core/animation/skeletal-animation-component.ts @@ -79,8 +79,6 @@ export class SkeletalAnimationComponent extends AnimationComponent { protected _enablePreSample = true; - protected _animMgr: JointsAnimationInfo = null!; - @property({ type: [Socket], tooltip: 'Joint Sockets', @@ -101,31 +99,9 @@ export class SkeletalAnimationComponent extends AnimationComponent { return this._enablePreSample; } - protected _animInfo: IAnimInfo | null = null; - - set frameID (fid: number) { - const info = this._animInfo; - if (!info) { return; } - info.data[1] = fid; - info.dirty = true; - } - get frameID () { - const info = this._animInfo; - return info && info.data[1] || 0; - } - - public onLoad () { - super.onLoad(); - this._animMgr = cc.director.root.dataPoolManager.jointsAnimationInfo; - this._animInfo = this._animMgr.create(this.node.uuid); - } - public onDestroy () { - if (this._animInfo) { - this._animMgr.destroy(this.node.uuid); - this._animInfo = null; - } super.onDestroy(); + cc.director.root.dataPoolManager.jointsAnimationInfo.destroy(this.node.uuid); } public start () { diff --git a/cocos/core/animation/skeletal-animation-data-hub.ts b/cocos/core/animation/skeletal-animation-data-hub.ts index 3288b310fe..95907869ee 100644 --- a/cocos/core/animation/skeletal-animation-data-hub.ts +++ b/cocos/core/animation/skeletal-animation-data-hub.ts @@ -27,12 +27,10 @@ * @category animation */ -import { Mat4, Quat, Vec3 } from '../math'; +import { clamp01, Mat4, Quat, Vec3 } from '../math'; import { DataPoolManager } from '../renderer/data-pool-manager'; -import { getClassName } from '../utils/js'; -import { AnimationClip, IObjectCurveData, IRuntimeCurve } from './animation-clip'; -import { AnimCurve, RatioSampler } from './animation-curve'; -import { ComponentPath, HierarchyPath, isCustomPath, isPropertyPath } from './target-path'; +import { AnimationClip, IObjectCurveData } from './animation-clip'; +import { HierarchyPath, isCustomPath, isPropertyPath } from './target-path'; type CurveData = Vec3 | Quat | Mat4; type ConvertedProps = Record; @@ -42,7 +40,6 @@ interface IPropertyCurve { values: CurveData[]; } interface ISkeletalCurveInfo { - curves: IRuntimeCurve[]; frames: number; sample: number; } @@ -110,26 +107,15 @@ function convertToSkeletalCurves (clip: AnimationClip): IConvertedData { }, }); } - const values: number[] = new Array(frames); const ratios: number[] = new Array(frames); - for (let i = 0; i < frames; i++) { values[i] = i; ratios[i] = i / (frames - 1); } const info: ISkeletalCurveInfo = { frames, sample: clip.sample, - curves: [{ - curve: new AnimCurve({ values, interpolate: false }, (frames - 1) / clip.sample), - modifiers: [ - new HierarchyPath(''), - new ComponentPath(getClassName(cc.SkeletalAnimationComponent)), - 'frameID', - ], - sampler: new RatioSampler(ratios), - }], }; return { info, data }; } function convertToUniformSample (clip: AnimationClip, curve: IPropertyCurve, frames: number) { - const keys = clip.keys[curve.keys]; curve.keys = 0; + const keys = clip.keys[curve.keys]; const values: CurveData[] = []; if (!keys || keys.length === 1) { for (let i = 0; i < frames; i++) { @@ -142,7 +128,7 @@ function convertToUniformSample (clip: AnimationClip, curve: IPropertyCurve, fra if (idx > keys.length - 1) { idx = keys.length - 1; time = keys[idx]; } else if (idx === 0) { idx = 1; } const from = curve.values[idx - 1].clone() as any; - from.lerp(curve.values[idx], (time - keys[idx - 1]) / (keys[idx] - keys[idx - 1])); + from.lerp(curve.values[idx], clamp01((time - keys[idx - 1]) / (keys[idx] - keys[idx - 1]))); values[i] = from; } } diff --git a/cocos/core/animation/skeletal-animation-state.ts b/cocos/core/animation/skeletal-animation-state.ts index 3487fba0ae..0c2edf5dce 100644 --- a/cocos/core/animation/skeletal-animation-state.ts +++ b/cocos/core/animation/skeletal-animation-state.ts @@ -29,27 +29,24 @@ import { SkinningModelComponent } from '../3d/framework/skinning-model-component'; import { Mat4 } from '../math'; +import { IAnimInfo, JointsAnimationInfo } from '../renderer/models/skeletal-animation-utils'; +import { Node } from '../scene-graph'; import { AnimationClip, IObjectCurveData } from './animation-clip'; import { AnimCurve } from './animation-curve'; import { AnimationState, ICurveInstance } from './animation-state'; import { Socket } from './skeletal-animation-component'; import { SkelAnimDataHub } from './skeletal-animation-data-hub'; -import { ComponentPath, TargetPath, HierarchyPath } from './target-path'; -import { getPathFromRoot, getWorldTransformUntilRoot } from './transform-utils'; -import { Node } from '../scene-graph'; +import { HierarchyPath } from './target-path'; +import { getWorldTransformUntilRoot } from './transform-utils'; const m4_1 = new Mat4(); - -function isFrameIDCurve (modifiers: TargetPath[]) { - return modifiers.length === 3 && - modifiers[0] instanceof HierarchyPath && - modifiers[1] instanceof ComponentPath && - modifiers[2] === 'frameID'; -} +const _defaultCurves = []; // no curves export class SkeletalAnimationState extends AnimationState { protected _preSample = true; + protected _frames = 1; + protected _animInfo: IAnimInfo | null = null; constructor (clip: AnimationClip, name = '', preSample = true) { super(clip, name); @@ -57,10 +54,13 @@ export class SkeletalAnimationState extends AnimationState { } public initialize (root: Node) { + if (this._curveLoaded) { return; } if (this._preSample) { const info = SkelAnimDataHub.getOrExtract(this.clip).info; - super.initialize(root, info.curves); - this.duration = (info.frames - 1) / info.sample; // last key + super.initialize(root, _defaultCurves); + this._frames = info.frames - 1; + this._animInfo = (cc.director.root.dataPoolManager.jointsAnimationInfo as JointsAnimationInfo).create(root.uuid); + this.duration = this._frames / info.sample; // last key } else { super.initialize(root); } @@ -78,25 +78,22 @@ export class SkeletalAnimationState extends AnimationState { } public rebuildSocketCurves (sockets: Socket[]) { - // TODO: curve reuse perhaps? - // delete previous sockets if (!this._samplerSharedGroups.length) { return; } const curves = this._samplerSharedGroups[0].curves; - for (let iCurve = 0; iCurve < curves.length; iCurve++) { - const curveDetail = curves[iCurve].curveDetail; - if (isFrameIDCurve(curveDetail.modifiers)) { - continue; - } else { - curves.splice(iCurve--, 1); - } - } - // build new ones + curves.length = 0; // remove previous curves for (let iSocket = 0; iSocket < sockets.length; ++iSocket) { const curve = this._buildSocketData(sockets[iSocket]); if (curve) { curves.push(curve); } } } + protected _sampleCurves (ratio: number) { + super._sampleCurves(ratio); + const info = this._animInfo!; + info.data[1] = (ratio * this._frames + 0.5) | 0; + info.dirty = true; + } + private _buildSocketData (socket: Socket) { if (!this._targetNode) { return null; } const root = this._targetNode; @@ -126,17 +123,6 @@ export class SkeletalAnimationState extends AnimationState { const m = matrix[i]; Mat4.multiply(m, m, m4_1); } - if (CC_EDITOR) { // assign back to clip to sync with animation editor - const path = getPathFromRoot(socket.target, root); - const curves = this.clip.curves; - const dstcurve = curves.find((curve) => { - const modifier = curve.modifiers[0]; - return modifier instanceof HierarchyPath && modifier.path === path && curve.modifiers[1] === 'matrix'; - }); - if (dstcurve) { dstcurve.data = data.matrix; } - else { curves.push({ modifiers: [ new HierarchyPath(path), 'matrix' ], data: data.matrix }); } - this.clip.curves = curves; - } // wrap up const duration = this.clip.duration; const hierarchyPath = new HierarchyPath(); diff --git a/cocos/core/game.ts b/cocos/core/game.ts index be63e51924..7d9c3e496b 100644 --- a/cocos/core/game.ts +++ b/cocos/core/game.ts @@ -624,8 +624,8 @@ export class Game extends EventTarget { if (!CC_EDITOR && !CC_PREVIEW && cc.internal.SplashScreenWebgl) { cc.internal.SplashScreenWebgl.instance.setOnFinish(cb); cc.internal.SplashScreenWebgl.instance.loadFinish = true; - } else { - cb && cb(); + } else if (cb) { + cb(); } }); } diff --git a/cocos/core/renderer/models/skinning-model.ts b/cocos/core/renderer/models/skinning-model.ts index d4ad54df9d..7682462488 100644 --- a/cocos/core/renderer/models/skinning-model.ts +++ b/cocos/core/renderer/models/skinning-model.ts @@ -52,7 +52,7 @@ interface IJointsInfo { export class SkinningModel extends Model { - public uploadedAnim: AnimationClip | null = null; + public uploadedAnim: AnimationClip | null | undefined = undefined; // uninitialized private _jointsMedium: IJointsInfo; private _skeleton: Skeleton | null = null; @@ -120,7 +120,7 @@ export class SkinningModel extends Model { } public uploadAnimation (anim: AnimationClip | null) { - if (!this._skeleton || !this._mesh) { return; } + if (!this._skeleton || !this._mesh || this.uploadedAnim === anim) { return; } this.uploadedAnim = anim; const resMgr = this._dataPoolManager; const texture = anim ? resMgr.jointsTexturePool.getJointsTextureWithAnimation(this._skeleton, anim) :