_updateBlendWeights()

in src/core/animpack/state/Blend1dState.js [259:306]


  _updateBlendWeights() {
    if (this._thresholds.length === 0) return;

    if (this._thresholds.length === 1) {
      const state = this._states.get(this._thresholds[0].name);
      state.setWeight(1);
      return;
    }

    // Initially set all sub-state weights to zero
    this._states.forEach(state => {
      state.setWeight(0);
    });

    this._phaseLeadState = null;

    // Find the first threshold that is greater than or equal to the parameter value
    let targetIndex = this._thresholds.findIndex(threshold => {
      return threshold.value >= this._blendValue;
    });

    if (targetIndex === 0 || targetIndex === -1) {
      // Give one state full influence
      targetIndex = targetIndex === -1 ? this._thresholds.length - 1 : 0;
      const state = this._states.get(this._thresholds[targetIndex].name);
      state.setWeight(1);
    } else {
      // Linear interpolate influence between two states
      const thresholdA = this._thresholds[targetIndex - 1];
      const thresholdB = this._thresholds[targetIndex];

      const factorB =
        (this.blendValue - thresholdA.value) /
        (thresholdB.value - thresholdA.value);
      const factorA = 1 - factorB;

      const stateA = this._states.get(thresholdA.name);
      const stateB = this._states.get(thresholdB.name);

      stateA.setWeight(factorA);
      stateB.setWeight(factorB);

      // Set phase-matching if needed
      if (thresholdA.phaseMatch && thresholdB.phaseMatch) {
        this._phaseLeadState = factorA > factorB ? stateA : stateB;
      }
    }
  }