@charivo/render-live2d 0.3.6 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -38,6 +38,7 @@ The renderer exposes:
38
38
  - `getAvailableExpressions()`
39
39
  - `getAvailableMotionGroups()`
40
40
  - `playExpression(expressionId)`
41
+ - `stopExpression()`
41
42
  - `playMotionByGroup(group, index)`
42
43
  - `lookAt({ x, y })`
43
44
 
package/dist/renderer.js CHANGED
@@ -13638,6 +13638,14 @@ var LAppModel = class extends CubismUserModel {
13638
13638
  host = null;
13639
13639
  motions = new csmMap();
13640
13640
  expressions = new csmMap();
13641
+ // Release state for the applied expression. `activeExpressionFadeOutSeconds`
13642
+ // is the clamped FadeOutTime of the motion the last setExpression() started -
13643
+ // the only thing clearExpression() reads from that motion, so the motion
13644
+ // itself is not kept around; `pendingReleaseFadeSeconds` holds a release that
13645
+ // arrived before that expression had finished fading in, which the
13646
+ // per-frame expression step starts once it saturates - see clearExpression().
13647
+ activeExpressionFadeOutSeconds = null;
13648
+ pendingReleaseFadeSeconds = null;
13641
13649
  eyeBlinkIds = new csmVector();
13642
13650
  lipSyncIds = new csmVector();
13643
13651
  ready = false;
@@ -13748,7 +13756,7 @@ var LAppModel = class extends CubismUserModel {
13748
13756
  this._eyeBlink.updateParameters(this._model, deltaTimeSeconds);
13749
13757
  }
13750
13758
  if (this._expressionManager) {
13751
- this._expressionManager.updateMotion(this._model, deltaTimeSeconds);
13759
+ this.updateExpressionFrame(this._model, deltaTimeSeconds);
13752
13760
  }
13753
13761
  this._model.addParameterValueById(this.idParamAngleX, dragX * 30);
13754
13762
  this._model.addParameterValueById(this.idParamAngleY, dragY * 30);
@@ -13835,8 +13843,53 @@ var LAppModel = class extends CubismUserModel {
13835
13843
  const motion = this.expressions.getValue(expressionId);
13836
13844
  if (motion && this._expressionManager) {
13837
13845
  this._expressionManager.startMotion(motion, false);
13846
+ this.activeExpressionFadeOutSeconds = Math.max(
13847
+ 0,
13848
+ motion.getFadeOutTime()
13849
+ );
13850
+ this.pendingReleaseFadeSeconds = null;
13838
13851
  }
13839
13852
  }
13853
+ /**
13854
+ * Releases the applied expression so the face returns to its base pose.
13855
+ *
13856
+ * The return is the SDK's own expression crossfade: an empty expression (no
13857
+ * Parameters) is queued as a NEWER entry, so calculateExpressionParameters
13858
+ * eases every parameter the new entry does not reference back to its default
13859
+ * at that entry's fade weight, and the manager prunes the older entry once the
13860
+ * new one reaches full fade weight. The neutral entry fades in over the
13861
+ * released expression's own FadeOutTime, so an authored `FadeOutTime: 0` is
13862
+ * honored as the author's request for an instant release.
13863
+ *
13864
+ * Only `Add` and `Multiply` parameters fade, and only when that duration is
13865
+ * positive. `Overwrite` parameters snap to their base value in one frame,
13866
+ * because the SDK rebases the overwrite value from the model at the top of
13867
+ * every entry's parameter pass (cubismexpressionmotion.ts:140-141), leaving
13868
+ * nothing to interpolate from.
13869
+ *
13870
+ * A release requested before the expression has finished fading in is
13871
+ * deferred to the per-frame expression step below: starting the neutral
13872
+ * against an unsaturated expression makes the face MORE expressed first
13873
+ * (measured 0.35 -> 0.91), so the release waits for the fade-in to complete
13874
+ * and then fades. The wait is counted in rendered frames, so a paused
13875
+ * renderer simply defers - at the cost of an expression outliving its
13876
+ * nominal hold.
13877
+ *
13878
+ * Release requests are idempotent: a second call finds no applied expression.
13879
+ * What the residual neutral entry does to later frames is pinned by
13880
+ * __tests__/expression-release.test.ts rather than assumed here.
13881
+ */
13882
+ clearExpression() {
13883
+ if (!this._expressionManager || this.activeExpressionFadeOutSeconds === null)
13884
+ return;
13885
+ const fadeSeconds = this.activeExpressionFadeOutSeconds;
13886
+ if (this.isNewestEntrySaturated()) {
13887
+ this.startNeutralExpression(fadeSeconds);
13888
+ } else {
13889
+ this.pendingReleaseFadeSeconds = fadeSeconds;
13890
+ }
13891
+ this.activeExpressionFadeOutSeconds = null;
13892
+ }
13840
13893
  hasMotion(group, index) {
13841
13894
  const key = `${group}_${index}`;
13842
13895
  return this.motions.getValue(key) !== null;
@@ -14081,8 +14134,45 @@ var LAppModel = class extends CubismUserModel {
14081
14134
  this.motions.clear();
14082
14135
  this._motionManager.stopAllMotions();
14083
14136
  }
14137
+ /**
14138
+ * Expression step of update(), extracted so a deferred release can start on
14139
+ * the frame after the outgoing expression is fully faded in.
14140
+ */
14141
+ updateExpressionFrame(model, deltaTimeSeconds) {
14142
+ if (this.pendingReleaseFadeSeconds !== null && this.isNewestEntrySaturated()) {
14143
+ this.startNeutralExpression(this.pendingReleaseFadeSeconds);
14144
+ this.pendingReleaseFadeSeconds = null;
14145
+ }
14146
+ this._expressionManager.updateMotion(model, deltaTimeSeconds);
14147
+ }
14148
+ /**
14149
+ * Whether the newest queue entry - always the applied expression's, since the
14150
+ * only other enqueue is a neutral that ends the expression - has reached full
14151
+ * fade weight, the same signal the manager's own prune uses.
14152
+ */
14153
+ isNewestEntrySaturated() {
14154
+ const entries = this._expressionManager.getCubismMotionQueueEntries();
14155
+ const lastIndex = entries.getSize() - 1;
14156
+ if (lastIndex < 0) return false;
14157
+ return entries.at(lastIndex).isStarted() && this._expressionManager.getFadeWeight(lastIndex) >= 1;
14158
+ }
14159
+ startNeutralExpression(fadeInSeconds) {
14160
+ const neutralJson = JSON.stringify({
14161
+ Type: "Live2D Expression",
14162
+ FadeInTime: fadeInSeconds,
14163
+ FadeOutTime: 0,
14164
+ Parameters: []
14165
+ });
14166
+ const buffer = new TextEncoder().encode(neutralJson).buffer;
14167
+ this._expressionManager.startMotion(
14168
+ this.loadExpression(buffer, buffer.byteLength, "neutral"),
14169
+ false
14170
+ );
14171
+ }
14084
14172
  releaseExpressions() {
14085
14173
  this.expressions.clear();
14174
+ this.activeExpressionFadeOutSeconds = null;
14175
+ this.pendingReleaseFadeSeconds = null;
14086
14176
  }
14087
14177
  requireGl() {
14088
14178
  if (!this.host) {
@@ -14446,6 +14536,13 @@ var Live2DRendererImpl = class _Live2DRendererImpl {
14446
14536
  this.model.setExpression(expressionId);
14447
14537
  }
14448
14538
  }
14539
+ /**
14540
+ * Drop the active expression and return to the model's base face
14541
+ */
14542
+ stopExpression() {
14543
+ if (!this.model?.isReady()) return;
14544
+ this.model.clearExpression();
14545
+ }
14449
14546
  /**
14450
14547
  * Play a specific motion by group and index
14451
14548
  */