Skip to content

Commit

Permalink
Hide RealKeyframeValue/QuatKeyframeValue Constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
shrinktofit committed Jul 22, 2021
1 parent d0f8145 commit 8fe2330
Show file tree
Hide file tree
Showing 14 changed files with 281 additions and 141 deletions.
12 changes: 6 additions & 6 deletions cocos/core/animation/legacy-clip-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ export class AnimationClipLegacyData {
}
const interpolationMethod = interpolate ? RealInterpolationMode.LINEAR : RealInterpolationMode.CONSTANT;
realCurve.assignSorted(times, (legacyValues as number[]).map(
(value) => new RealKeyframeValue({ value, interpolationMode: interpolationMethod }),
(value) => ({ value, interpolationMode: interpolationMethod }),
));
legacyEasingMethodConverter.convert(realCurve);
return;
Expand All @@ -287,7 +287,7 @@ export class AnimationClipLegacyData {
track.componentsCount = components;
const [{ curve: x }, { curve: y }, { curve: z }, { curve: w }] = track.channels();
const interpolationMode = interpolate ? RealInterpolationMode.LINEAR : RealInterpolationMode.CONSTANT;
const valueToFrame = (value: number): RealKeyframeValue => new RealKeyframeValue({ value, interpolationMode });
const valueToFrame = (value: number): Partial<RealKeyframeValue> => ({ value, interpolationMode });
switch (components) {
case 4:
w.assignSorted(times, (legacyValues as Vec4plus).map((value) => valueToFrame(value.w)));
Expand Down Expand Up @@ -324,7 +324,7 @@ export class AnimationClipLegacyData {
installPathAndSetter(track);
const [{ curve: r }, { curve: g }, { curve: b }, { curve: a }] = track.channels();
const interpolationMode = interpolate ? RealInterpolationMode.LINEAR : RealInterpolationMode.CONSTANT;
const valueToFrame = (value: number): RealKeyframeValue => new RealKeyframeValue({ value, interpolationMode });
const valueToFrame = (value: number): Partial<RealKeyframeValue> => ({ value, interpolationMode });
r.assignSorted(times, (legacyValues as Color[]).map((value) => valueToFrame(value.r)));
legacyEasingMethodConverter.convert(r);
g.assignSorted(times, (legacyValues as Color[]).map((value) => valueToFrame(value.g)));
Expand All @@ -341,7 +341,7 @@ export class AnimationClipLegacyData {
installPathAndSetter(track);
const [{ curve: width }, { curve: height }] = track.channels();
const interpolationMode = interpolate ? RealInterpolationMode.LINEAR : RealInterpolationMode.CONSTANT;
const valueToFrame = (value: number): RealKeyframeValue => new RealKeyframeValue({ value, interpolationMode });
const valueToFrame = (value: number): Partial<RealKeyframeValue> => ({ value, interpolationMode });
width.assignSorted(times, (legacyValues as Size[]).map((value) => valueToFrame(value.width)));
legacyEasingMethodConverter.convert(width);
height.assignSorted(times, (legacyValues as Size[]).map((value) => valueToFrame(value.height)));
Expand All @@ -354,7 +354,7 @@ export class AnimationClipLegacyData {
const track = new RealTrack();
installPathAndSetter(track);
const interpolationMode = interpolate ? RealInterpolationMode.CUBIC : RealInterpolationMode.CONSTANT;
track.channel.curve.assignSorted(times, (legacyValues as CubicSplineNumberValue[]).map((value) => new RealKeyframeValue({
track.channel.curve.assignSorted(times, (legacyValues as CubicSplineNumberValue[]).map((value) => ({
value: value.dataPoint,
leftTangent: value.inTangent,
rightTangent: value.outTangent,
Expand All @@ -376,7 +376,7 @@ export class AnimationClipLegacyData {
track.componentsCount = components;
const [x, y, z, w] = track.channels();
const interpolationMode = interpolate ? RealInterpolationMode.LINEAR : RealInterpolationMode.CONSTANT;
const valueToFrame = (value: number, inTangent: number, outTangent: number): RealKeyframeValue => new RealKeyframeValue({
const valueToFrame = (value: number, inTangent: number, outTangent: number): Partial<RealKeyframeValue> => ({
value,
leftTangent: inTangent,
rightTangent: outTangent,
Expand Down
60 changes: 54 additions & 6 deletions cocos/core/curves/curve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export enum EasingMethod {

@ccclass('cc.RealKeyframeValue')
@uniquelyReferenced
export class RealKeyframeValue {
class RealKeyframeValue {
constructor ({
interpolationMode,
tangentWeightMode,
Expand Down Expand Up @@ -139,6 +139,23 @@ export class RealKeyframeValue {
public easingMethod = EasingMethod.LINEAR;
}

export type { RealKeyframeValue };

/**
* The parameter describing a real keyframe value.
* In the case of partial keyframe value,
* each component of the keyframe value is taken from the parameter.
* For unspecified components, default values are taken:
* - Interpolation mode: linear
* - Tangent weight mode: none
* - Value/Tangents/Tangent weights: 0.0
*/
type RealKeyframeValueParameters = number | Partial<RealKeyframeValue>;

function createRealKeyframeValue (params: RealKeyframeValueParameters) {
return new RealKeyframeValue(typeof params === 'number' ? { value: params } : params);
}

/**
* Curve.
*/
Expand Down Expand Up @@ -245,9 +262,40 @@ export class RealCurve extends EditorExtendableMixin<KeyframeCurve<RealKeyframeV
* @param value Value of the keyframe.
* @returns The index to the new keyframe.
*/
public addKeyFrame (time: number, value: number | RealKeyframeValue): number {
const keyframeValue = typeof value === 'number' ? new RealKeyframeValue({ value }) : value;
return super.addKeyFrame(time, keyframeValue);
public addKeyFrame (time: number, value: RealKeyframeValueParameters): number {
return super.addKeyFrame(time, createRealKeyframeValue(value));
}

/**
* Assigns all keyframes.
* @param keyframes An iterable to keyframes. The keyframes should be sorted by their time.
*/
public assignSorted (keyframes: Iterable<[number, RealKeyframeValueParameters]>): void;

/**
* Assigns all keyframes.
* @param times Times array. Should be sorted.
* @param values Values array. Corresponding to each time in `times`.
*/
public assignSorted (times: readonly number[], values: RealKeyframeValueParameters[]): void;

public assignSorted (
times: Iterable<[number, RealKeyframeValueParameters]> | readonly number[],
values?: readonly RealKeyframeValueParameters[],
) {
if (values !== undefined) {
assertIsTrue(Array.isArray(times));
this.setKeyframes(
times.slice(),
values.map((value) => createRealKeyframeValue(value)),
);
} else {
const keyframes = Array.from(times as Iterable<[number, Partial<RealKeyframeValue>]>);
this.setKeyframes(
keyframes.map(([time]) => time),
keyframes.map(([, value]) => createRealKeyframeValue(value)),
);
}
}

/**
Expand Down Expand Up @@ -331,7 +379,7 @@ export class RealCurve extends EditorExtendableMixin<KeyframeCurve<RealKeyframeV
// Frame values
const keyframeValues = new Array<RealKeyframeValue>(nKeyframes);
for (let iKeyFrame = 0; iKeyFrame < nKeyframes; ++iKeyFrame) {
const keyframeValue = new RealKeyframeValue({});
const keyframeValue = createRealKeyframeValue({});
currentOffset = loadRealKeyFrameValue(dataView, keyframeValue, currentOffset);
keyframeValues[iKeyFrame] = keyframeValue;
}
Expand Down Expand Up @@ -375,7 +423,7 @@ const {
leftTangentWeight: DEFAULT_LEFT_TANGENT_WEIGHT,
rightTangent: DEFAULT_RIGHT_TANGENT,
rightTangentWeight: DEFAULT_RIGHT_TANGENT_WEIGHT,
} = new RealKeyframeValue({});
} = createRealKeyframeValue({});

const REAL_KEY_FRAME_VALUE_MAX_SIZE = KEY_FRAME_VALUE_FLAGS_BYTES
+ VALUE_BYTES
Expand Down
10 changes: 8 additions & 2 deletions cocos/core/curves/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
export {
RealCurve,
RealKeyframeValue,
RealInterpolationMode,
ExtrapolationMode,
TangentWeightMode,
} from './curve';

export type {
RealKeyframeValue,
} from './curve';

export {
QuatCurve,
QuatKeyframeValue,
QuatInterpolationMode,
} from './quat-curve';

export type {
QuatKeyframeValue,
} from './quat-curve';

export {
ObjectCurve,
} from './object-curve';
Expand Down
22 changes: 15 additions & 7 deletions cocos/core/curves/keyframe-curve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,17 @@ export class KeyframeCurve<TKeyframeValue> implements CurveBase, Iterable<KeyFra
public assignSorted (times: Iterable<[number, TKeyframeValue]> | readonly number[], values?: readonly TKeyframeValue[]) {
if (values !== undefined) {
assertIsTrue(Array.isArray(times));
assertIsTrue(times.length === values.length);
this._times = times.slice();
this._values = values.map((value) => value);
this.setKeyframes(
times.slice(),
values.slice(),
);
} else {
const keyframes = Array.from(times as Iterable<[number, TKeyframeValue]>);
this._times = keyframes.map(([time]) => time);
this._values = keyframes.map(([, value]) => value);
this.setKeyframes(
keyframes.map(([time]) => time),
keyframes.map(([, value]) => value),
);
}

assertIsTrue(isSorted(this._times));
}

/**
Expand All @@ -168,6 +169,13 @@ export class KeyframeCurve<TKeyframeValue> implements CurveBase, Iterable<KeyFra
return binarySearchEpsilon(this._times, time);
}

protected setKeyframes (times: number[], values: TKeyframeValue[]) {
assertIsTrue(times.length === values.length);
assertIsTrue(isSorted(times));
this._times = times;
this._values = values;
}

private _insertNewKeyframe (time: number, value: TKeyframeValue) {
const times = this._times;
const values = this._values;
Expand Down
57 changes: 52 additions & 5 deletions cocos/core/curves/quat-curve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { DeserializationContext } from '../data/custom-serializable';

@ccclass('cc.QuaternionKeyframeValue')
@uniquelyReferenced
export class QuatKeyframeValue {
class QuatKeyframeValue {
/**
* Interpolation method used for this keyframe.
*/
Expand All @@ -32,6 +32,22 @@ export class QuatKeyframeValue {
}
}

export type { QuatKeyframeValue };

/**
* The parameter describing a real keyframe value.
* In the case of partial keyframe value,
* each component of the keyframe value is taken from the parameter.
* For unspecified components, default values are taken:
* - Interpolation mode: slerp
* - Value: Identity quaternion
*/
type QuatKeyframeValueParameters = Partial<QuatKeyframeValue>;

function createQuatKeyframeValue (params: QuatKeyframeValueParameters) {
return new QuatKeyframeValue(params);
}

/**
* The method used for interpolation between values of a keyframe and its next keyframe.
*/
Expand Down Expand Up @@ -161,12 +177,43 @@ export class QuatCurve extends KeyframeCurve<QuatKeyframeValue> {
* @param value Value of the keyframe.
* @returns The index to the new keyframe.
*/
public addKeyFrame (time: number, value: IQuatLike | QuatKeyframeValue): number {
const keyframeValue = value instanceof QuatKeyframeValue
? value : new QuatKeyframeValue({ value });
public addKeyFrame (time: number, value: QuatKeyframeValueParameters): number {
const keyframeValue = new QuatKeyframeValue(value);
return super.addKeyFrame(time, keyframeValue);
}

/**
* Assigns all keyframes.
* @param keyframes An iterable to keyframes. The keyframes should be sorted by their time.
*/
public assignSorted (keyframes: Iterable<[number, QuatKeyframeValueParameters]>): void;

/**
* Assigns all keyframes.
* @param times Times array. Should be sorted.
* @param values Values array. Corresponding to each time in `times`.
*/
public assignSorted (times: readonly number[], values: QuatKeyframeValueParameters[]): void;

public assignSorted (
times: Iterable<[number, QuatKeyframeValueParameters]> | readonly number[],
values?: readonly QuatKeyframeValueParameters[],
) {
if (values !== undefined) {
assertIsTrue(Array.isArray(times));
this.setKeyframes(
times.slice(),
values.map((value) => createQuatKeyframeValue(value)),
);
} else {
const keyframes = Array.from(times as Iterable<[number, QuatKeyframeValueParameters]>);
this.setKeyframes(
keyframes.map(([time]) => time),
keyframes.map(([, value]) => createQuatKeyframeValue(value)),
);
}
}

public [serializeTag] (output: SerializationOutput, context: SerializationContext) {
if (!context.toCCON) {
output.writeThis();
Expand Down Expand Up @@ -270,7 +317,7 @@ export class QuatCurve extends KeyframeCurve<QuatKeyframeValue> {
const y = dataView.getFloat32(pQuat + VALUE_BYTES * 1, true);
const z = dataView.getFloat32(pQuat + VALUE_BYTES * 2, true);
const w = dataView.getFloat32(pQuat + VALUE_BYTES * 3, true);
const keyframeValue = new QuatKeyframeValue({
const keyframeValue = createQuatKeyframeValue({
value: { x, y, z, w },
interpolationMode: dataView.getUint8(pInterpolationModes),
});
Expand Down
20 changes: 10 additions & 10 deletions cocos/core/geometry/curve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,12 @@ export class AnimationCurve {
set keyFrames (value) {
this._curve.assignSorted(value.map((legacyCurve) => [
legacyCurve.time,
new RealKeyframeValue({
{
interpolationMode: RealInterpolationMode.CUBIC,
value: legacyCurve.value,
leftTangent: legacyCurve.inTangent,
rightTangent: legacyCurve.outTangent,
}),
},
]));
}

Expand Down Expand Up @@ -201,16 +201,16 @@ export class AnimationCurve {
curve.postExtrapolation = ExtrapolationMode.CLAMP;
if (!keyFrames) {
curve.assignSorted([
[0.0, new RealKeyframeValue({ interpolationMode: RealInterpolationMode.CUBIC, value: 1.0 })],
[1.0, new RealKeyframeValue({ interpolationMode: RealInterpolationMode.CUBIC, value: 1.0 })],
[0.0, { interpolationMode: RealInterpolationMode.CUBIC, value: 1.0 }],
[1.0, { interpolationMode: RealInterpolationMode.CUBIC, value: 1.0 }],
]);
} else {
curve.assignSorted(keyFrames.map((legacyKeyframe) => [legacyKeyframe.time, new RealKeyframeValue({
curve.assignSorted(keyFrames.map((legacyKeyframe) => [legacyKeyframe.time, {
interpolationMode: RealInterpolationMode.CUBIC,
value: legacyKeyframe.value,
leftTangent: legacyKeyframe.inTangent,
rightTangent: legacyKeyframe.outTangent,
})]));
}]));
}
}
this.cachedKey = new OptimizedKey();
Expand All @@ -227,12 +227,12 @@ export class AnimationCurve {
if (!keyFrame) {
this._curve.clear();
} else {
this._curve.addKeyFrame(keyFrame.time, new RealKeyframeValue({
this._curve.addKeyFrame(keyFrame.time, {
interpolationMode: RealInterpolationMode.CUBIC,
value: keyFrame.value,
leftTangent: keyFrame.inTangent,
rightTangent: keyFrame.outTangent,
}));
});
}
}

Expand Down Expand Up @@ -376,8 +376,8 @@ function toLegacyWrapMode (extrapolationMode: ExtrapolationMode): WrapModeMask {
export function constructLegacyCurveAndConvert () {
const curve = new RealCurve();
curve.assignSorted([
[0.0, new RealKeyframeValue({ interpolationMode: RealInterpolationMode.CUBIC, value: 1.0 })],
[1.0, new RealKeyframeValue({ interpolationMode: RealInterpolationMode.CUBIC, value: 1.0 })],
[0.0, { interpolationMode: RealInterpolationMode.CUBIC, value: 1.0 }],
[1.0, { interpolationMode: RealInterpolationMode.CUBIC, value: 1.0 }],
]);
return curve;
}
Loading

0 comments on commit 8fe2330

Please sign in to comment.