CubismParameterStore (Cocos Creator)

Updated: 03/14/2023

This page is for Cubism version 4.2 or earlier. Click here for the latest version.

Summary

CubismParameterStore can be used to store/restore CubismModel parameters and part values.

If CubismParameterStore is not used, the results of processing values with Expression or other methods may not be correct.

If a value operation such as Expression is performed between restore and save, the result of the value manipulation is saved, so the value is added/multiplied to the restored post-operation value, and the result is not as expected.
When value manipulation is performed outside of restore-to-save, the state before the value was manipulated is restored, so that subsequent manipulation of the value through addition/multiplication will result in the proper result.

Click here for the corresponding tutorial article.

The following process is used to save/restore CubismModel parameters and part values.

  1. Get reference to parameters and parts to be saved/restored
  2. Save parameter and part values
  3. Restore parameter and part values

Get reference to parameters and parts to be saved/restored

CubismParameterStore.onEnable caches references to CubismModel parameters and parts.

    if (this.destinationParameters == null) {
      this.destinationParameters =
        ComponentExtensionMethods.findCubismModel(this)?.parameters ?? null;
    }

    if (this.destinationParts == null) {
      this.destinationParts = ComponentExtensionMethods.findCubismModel(this)?.parts ?? null;
    }

This is done by CubismParameterStore.onEnable().

Save parameter and part values

Stores the current parameter and part values for the model.
The timing for saving the values is after the animation is applied and before manipulating the values in the various Cubism components.

    // 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;
      }
    }

This is done by CubismParameterStore.saveParameters().

Restore parameter and part values

Restores saved parameter and part values.
The timing for restoring the values is before the animation is applied.

    // 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];
      }
    }

This is done by CubismParameterStore.restoreParameters().

Was this article helpful?
YesNo
Please let us know what you think about this article.