fix anim curve generation (#6077)

This commit is contained in:
Yun Hsiao Wu
2020-01-12 14:54:29 +08:00
committed by pandamicro
parent 960e2c203f
commit bf6892a99e
6 changed files with 37 additions and 99 deletions
+7 -17
View File
@@ -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];
@@ -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 () {
@@ -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<string, IPropertyCurve>;
@@ -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;
}
}
@@ -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();
+2 -2
View File
@@ -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();
}
});
}
+2 -2
View File
@@ -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) :