CubismParameterStore (Cocos Creator)
最終更新: 2023年3月14日
このページはCubism 4.2以前の古いバージョン向けです。 最新のページはこちら
概要
CubismParameterStoreを使用することで、CubismModelのパラメータとパーツの値を保存/復元することが出来ます。
CubismParameterStoreを使用しない場合、Expressionなどで値を加工した結果が正しいものにならないことがあります。
Expressionなどの値の操作を復元~保存の間で行った場合、値を操作した結果が保存されるため、復元された操作後の値に加算/乗算してしまい、期待通りの結果になりません。
値の操作を復元~保存の外で行う場合、値を操作する前の状態が復元されるため、その後の加算/乗算による値の操作が適正な結果となります。
該当のチュートリアル記事は こちら をご覧ください。
CubismModelのパラメータとパーツの値を保存/復元するには以下の処理を行っています。
- 保存/復元するパラメータとパーツの参照を取得
- パラメータとパーツの値を保存
- パラメータとパーツの値を復元
保存/復元するパラメータとパーツの参照を取得
CubismParameterStore.onEnableでCubismModelのパラメータとパーツへの参照をキャッシュします。
if (this.destinationParameters == null) {
this.destinationParameters =
ComponentExtensionMethods.findCubismModel(this)?.parameters ?? null;
}
if (this.destinationParts == null) {
this.destinationParts = ComponentExtensionMethods.findCubismModel(this)?.parts ?? null;
}
この処理はCubismParameterStore.onEnable()で行っています。
パラメータとパーツの値を保存
モデルの現在のパラメータとパーツの値を保存します。
値を保存するタイミングはアニメーションを適用した後、Cubismの各種コンポーネントで値を操作する前に行います。
// save parameters value
if (this.destinationParameters != null && this._parameterValues == null) {
this._parameterValues = new Array<number>(this.destinationParameters.length);
}
if (this._parameterValues != null && this.destinationParameters != null) {
for (let i = 0; i < this._parameterValues.length; ++i) {
if (this.destinationParameters[i] != null) {
this._parameterValues[i] = this.destinationParameters[i].value;
}
}
}
// save parts opacity
if (this.destinationParts != null && this._partOpacities == null) {
this._partOpacities = new Array(this.destinationParts.length);
}
if (this._partOpacities != null && this.destinationParts != null) {
for (let i = 0; i < this._partOpacities.length; ++i) {
this._partOpacities[i] = this.destinationParts[i].opacity;
}
}
この処理はCubismParameterStore.saveParameters()で行います。
パラメータとパーツの値を復元
保存したパラメータとパーツの値を復元します。
値を復元するタイミングは、アニメーションを適用する前に行います。
// restore parameters value
if (this._parameterValues != null && this.destinationParameters != null) {
for (let i = 0; i < this._parameterValues.length; ++i) {
this.destinationParameters[i].value = this._parameterValues[i];
}
}
// restore parts opacity
if (this._partOpacities != null && this.destinationParts != null) {
for (let i = 0; i < this._partOpacities.length; ++i) {
this.destinationParts[i].opacity = this._partOpacities[i];
}
}
この処理はCubismParameterStore.restoreParameters()で行っています。
この記事はお役に立ちましたか?
はいいいえ