opt anim curve (#4636)

This commit is contained in:
Leslie Leigh
2019-06-18 09:30:03 +08:00
committed by Weiliang Wang
parent 81e19a0377
commit 862d2a31ae
3 changed files with 154 additions and 122 deletions
+14 -13
View File
@@ -53,6 +53,11 @@ export interface IPropertyCurve {
* 属性曲线。
*/
curve: AnimCurve;
/**
* 曲线采样器。
*/
sampler: RatioSampler | null;
}
export interface IAnimationEvent {
@@ -278,10 +283,12 @@ export class AnimationClip extends Asset {
const nodeData = this.curveDatas[curveTargetPath];
if (nodeData.props) {
for (const nodePropertyName of Object.keys(nodeData.props)) {
const propertyCurveData = nodeData.props[nodePropertyName];
this._propertyCurves.push({
path: curveTargetPath,
propertyName: nodePropertyName,
curve: this._createCurve(nodeData.props[nodePropertyName], nodePropertyName, true),
curve: new AnimCurve(propertyCurveData, nodePropertyName, this._duration, true),
sampler: propertyCurveData.keys >= 0 ? this._ratioSamplers[propertyCurveData.keys] : null,
});
}
}
@@ -289,11 +296,13 @@ export class AnimationClip extends Asset {
for (const componentName of Object.keys(nodeData.comps)) {
const componentData = nodeData.comps[componentName];
for (const componentPropertyName of Object.keys(componentData)) {
const propertyCurveData = componentData[componentPropertyName];
this._propertyCurves.push({
path: curveTargetPath,
component: componentName,
propertyName: componentPropertyName,
curve: this._createCurve(componentData[componentPropertyName], componentPropertyName, false),
curve: new AnimCurve(propertyCurveData, componentPropertyName, this._duration, false),
sampler: propertyCurveData.keys >= 0 ? this._ratioSamplers[propertyCurveData.keys] : null,
});
}
}
@@ -302,14 +311,6 @@ export class AnimationClip extends Asset {
this._applyStepness();
}
private _createCurve (propertyCurveData: IPropertyCurveData, propertyName: string, isNode: boolean) {
let ratioSampler: null | RatioSampler = null;
if (propertyCurveData.keys >= 0) {
ratioSampler = this._ratioSamplers[propertyCurveData.keys];
}
return new AnimCurve(propertyCurveData, propertyName, this._duration, isNode, ratioSampler);
}
private _createRuntimeEvents () {
if (CC_EDITOR) {
return;
@@ -344,9 +345,9 @@ export class AnimationClip extends Asset {
if (!this._propertyCurves) {
return;
}
for (const propertyCurve of this._propertyCurves) {
propertyCurve.curve.stepfy(this._stepness);
}
// for (const propertyCurve of this._propertyCurves) {
// propertyCurve.curve.stepfy(this._stepness);
// }
}
}
+46 -87
View File
@@ -79,10 +79,6 @@ export interface IPropertyCurveData {
export class RatioSampler {
public ratios: number[];
private _lastSampleRatio: number = -1;
private _lastSampleResult: number = 0;
private _findRatio: (ratios: number[], ratio: number) => number;
constructor (ratios: number[]) {
@@ -106,11 +102,7 @@ export class RatioSampler {
}
public sample (ratio: number) {
if (this._lastSampleRatio !== ratio) {
this._lastSampleRatio = ratio;
this._lastSampleResult = this._findRatio(this.ratios, ratio);
}
return this._lastSampleResult;
return this._findRatio(this.ratios, ratio);
}
}
@@ -125,12 +117,6 @@ export class AnimCurve {
return controlPoints as BezierControlPoints;
}
/**
* The keyframe ratio of the keyframe specified as a number between 0.0 and 1.0 inclusive. (x)
* A null ratio indicates a zero or single frame curve.
*/
public _ratioSampler: RatioSampler | null = null;
@property
public types?: Array<(EasingMethod | null)> = undefined;
@@ -154,9 +140,8 @@ export class AnimCurve {
private _duration: number;
constructor (propertyCurveData: IPropertyCurveData, propertyName: string, duration: number, isNode: boolean, ratioSampler: RatioSampler | null) {
constructor (propertyCurveData: IPropertyCurveData, propertyName: string, duration: number, isNode: boolean) {
this._duration = duration;
this._ratioSampler = ratioSampler;
// Install values.
this._values = propertyCurveData.values;
@@ -209,87 +194,61 @@ export class AnimCurve {
}
}
/**
* @param ratio The normalized time specified as a number between 0.0 and 1.0 inclusive.
*/
public sample (ratio: number): any {
if (!this._stepfiedValues) {
return this._sampleFromOriginal(ratio);
public hasLerp () {
return !!this._lerp;
}
public valueAt (index: number) {
const value = this._values[index];
if (value && value.getNoLerp) {
return value.getNoLerp();
} else {
const ratioStep = 1 / this._stepfiedValues.length;
const i = Math.floor(ratio / ratioStep);
return this._stepfiedValues[i];
return value;
}
}
public stepfy (stepCount: number) {
this._stepfiedValues = undefined;
if (stepCount === 0) {
return;
public valueBetween (ratio: number, from: number, fromRatio: number, to: number, toRatio: number) {
// if (!this._stepfiedValues) {
// return this._sampleFromOriginal(ratio);
// } else {
// const ratioStep = 1 / this._stepfiedValues.length;
// const i = Math.floor(ratio / ratioStep);
// return this._stepfiedValues[i];
// }
if (!this._lerp) {
return this.valueAt(from);
}
this._stepfiedValues = new Array(stepCount);
const ratioStep = 1 / stepCount;
let curRatio = 0;
for (let i = 0; i < stepCount; ++i, curRatio += ratioStep) {
const value = this._sampleFromOriginal(curRatio);
this._stepfiedValues[i] = value instanceof ValueType ? value.clone() : value;
const type = this.types ? this.types[from] : this.type;
const dRatio = (toRatio - fromRatio);
let ratioBetweenFrames = (ratio - fromRatio) / dRatio;
if (type) {
ratioBetweenFrames = computeRatioByType(ratioBetweenFrames, type);
}
const fromVal = this._values[from];
const toVal = this._values[to];
const value = this._lerp(fromVal, toVal, ratioBetweenFrames, dRatio * this._duration);
return value;
}
// public stepfy (stepCount: number) {
// this._stepfiedValues = undefined;
// if (stepCount === 0) {
// return;
// }
// this._stepfiedValues = new Array(stepCount);
// const ratioStep = 1 / stepCount;
// let curRatio = 0;
// for (let i = 0; i < stepCount; ++i, curRatio += ratioStep) {
// const value = this._sampleFromOriginal(curRatio);
// this._stepfiedValues[i] = value instanceof ValueType ? value.clone() : value;
// }
// }
public empty () {
return this._values.length === 0;
}
private _sampleFromOriginal (ratio: number) {
const values = this._values;
const frameCount = this._ratioSampler ?
this._ratioSampler.ratios.length :
this._values.length;
if (frameCount === 0) {
return;
}
// evaluate value
let isLerped = false;
let value: CurveValue;
if (this._ratioSampler === null) {
value = this._values[0];
} else {
let index = this._ratioSampler.sample(ratio);
if (index >= 0) {
value = values[index];
} else {
index = ~index;
if (index <= 0) {
value = values[0];
} else if (index >= frameCount) {
value = values[frameCount - 1];
} else {
const fromVal = values[index - 1];
if (!this._lerp) {
value = fromVal;
} else {
const fromRatio = this._ratioSampler.ratios[index - 1];
const toRatio = this._ratioSampler.ratios[index];
const type = this.types ? this.types[index - 1] : this.type;
const dRatio = (toRatio - fromRatio);
let ratioBetweenFrames = (ratio - fromRatio) / dRatio;
if (type) {
ratioBetweenFrames = computeRatioByType(ratioBetweenFrames, type);
}
// calculate value
const toVal = values[index];
value = this._lerp(fromVal, toVal, ratioBetweenFrames, dRatio * this._duration);
isLerped = true;
}
}
}
}
if (!isLerped && value && value.getNoLerp) {
return value.getNoLerp();
}
return value;
}
}
export class EventInfo {
+94 -22
View File
@@ -4,7 +4,7 @@ import { EventArgumentsOf, EventCallbackOf } from '../core/event/defines';
import { Node } from '../scene-graph';
import { AnimationBlendState, PropertyBlendState } from './animation-blend-state';
import { AnimationClip } from './animation-clip';
import { AnimCurve, CurveTarget, EventInfo } from './animation-curve';
import { AnimCurve, CurveTarget, EventInfo, RatioSampler } from './animation-curve';
import { Playable } from './playable';
import { WrapMode, WrapModeMask, WrappedInfo } from './types';
@@ -16,6 +16,33 @@ interface ICurveInstance {
cached?: any[];
}
/**
* The curves in ISamplerSharedGroup share a same keys.
*/
interface ISamplerSharedGroup {
sampler: RatioSampler | null;
curves: ICurveInstance[];
samplerResultCache: {
from: number,
fromRatio: number,
to: number,
toRatio: number,
};
}
function makeSamplerSharedGroup (sampler: RatioSampler | null): ISamplerSharedGroup {
return {
sampler,
curves: [],
samplerResultCache: {
from: 0,
fromRatio: 0,
to: 0,
toRatio: 0,
},
};
}
const InvalidIndex = -1;
export interface IAnimationEventDefinitionMap {
@@ -191,7 +218,7 @@ export class AnimationState extends Playable {
private _clip: AnimationClip;
private _name: string;
private _lastIterations?: number;
private _curveInstances: ICurveInstance[] = [];
private _samplerSharedGroups: ISamplerSharedGroup[] = [];
private _curveLoaded = false;
private _ignoreIndex = InvalidIndex;
@@ -207,7 +234,7 @@ export class AnimationState extends Playable {
public initialize (root: Node) {
this._curveLoaded = true;
this._curveInstances.length = 0;
this._samplerSharedGroups.length = 0;
this._targetNode = root;
const clip = this._clip;
@@ -237,7 +264,12 @@ export class AnimationState extends Playable {
}
target = targetComponent;
}
this._curveInstances.push({
let samplerSharedGroup = this._samplerSharedGroups.find((value) => value.sampler === propertyCurve.sampler);
if (!samplerSharedGroup) {
samplerSharedGroup = makeSamplerSharedGroup(propertyCurve.sampler);
this._samplerSharedGroups.push(samplerSharedGroup);
}
samplerSharedGroup.curves.push({
target,
propertyName: propertyCurve.propertyName,
curve: propertyCurve.curve,
@@ -500,17 +532,21 @@ export class AnimationState extends Playable {
}
public attachToBlendState (blendState: AnimationBlendState) {
for (const curveInstance of this._curveInstances) {
curveInstance.blendTarget = blendState.refPropertyBlendTarget(
curveInstance.target, curveInstance.propertyName);
for (const samplerSharedGroup of this._samplerSharedGroups) {
for (const curveInstance of samplerSharedGroup.curves) {
curveInstance.blendTarget = blendState.refPropertyBlendTarget(
curveInstance.target, curveInstance.propertyName);
}
}
}
public detachFromBlendState (blendState: AnimationBlendState) {
for (const curveInstance of this._curveInstances) {
curveInstance.blendTarget = null;
blendState.derefPropertyBlendTarget(
curveInstance.target, curveInstance.propertyName);
for (const samplerSharedGroup of this._samplerSharedGroups) {
for (const curveInstance of samplerSharedGroup.curves) {
curveInstance.blendTarget = null;
blendState.derefPropertyBlendTarget(
curveInstance.target, curveInstance.propertyName);
}
}
}
@@ -546,18 +582,54 @@ export class AnimationState extends Playable {
}
private _sampleCurves (ratio: number) {
for (const curveInstace of this._curveInstances) {
const { curve, blendTarget, target, propertyName } = curveInstace;
if (curve.empty()) {
continue;
}
const value = curve.sample(ratio);
if (!curve._blendFunction || !blendTarget || blendTarget.refCount <= 1) {
target[propertyName] = value;
for (const samplerSharedGroup of this._samplerSharedGroups) {
const sampler = samplerSharedGroup.sampler;
const { samplerResultCache } = samplerSharedGroup;
let index: number = 0;
let lerpRequired = false;
if (!sampler) {
index = 0;
} else {
const { weight } = this;
blendTarget.value = curve._blendFunction(value, weight, blendTarget);
blendTarget.weight += weight;
index = sampler.sample(ratio);
if (index < 0) {
index = ~index;
if (index <= 0) {
index = 0;
} else if (index >= sampler.ratios.length) {
index = sampler.ratios.length - 1;
} else {
lerpRequired = true;
samplerResultCache.from = index - 1;
samplerResultCache.fromRatio = sampler.ratios[samplerResultCache.from];
samplerResultCache.to = index;
samplerResultCache.toRatio = sampler.ratios[samplerResultCache.to];
}
}
}
for (const curveInstace of samplerSharedGroup.curves) {
const { curve, blendTarget, target, propertyName } = curveInstace;
if (curve.empty()) {
continue;
}
let value: any;
if (!lerpRequired) {
value = curve.valueAt(index);
} else {
value = curve.valueBetween(
ratio,
samplerResultCache.from,
samplerResultCache.fromRatio,
samplerResultCache.to,
samplerResultCache.toRatio);
}
if (!curve._blendFunction || !blendTarget || blendTarget.refCount <= 1) {
target[propertyName] = value;
} else {
const { weight } = this;
blendTarget.value = curve._blendFunction(value, weight, blendTarget);
blendTarget.weight += weight;
}
}
}
}