@charivo/render-live2d 0.6.6 → 0.8.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
|
@@ -24,7 +24,7 @@ const renderManager = createRenderManager(renderer, {
|
|
|
24
24
|
});
|
|
25
25
|
|
|
26
26
|
await renderManager.initialize();
|
|
27
|
-
await renderManager.loadModel
|
|
27
|
+
await renderManager.loadModel("/live2d/hiyori/hiyori.model3.json");
|
|
28
28
|
```
|
|
29
29
|
|
|
30
30
|
## Public Surface
|
|
@@ -32,7 +32,9 @@ await renderManager.loadModel?.("/live2d/hiyori/hiyori.model3.json");
|
|
|
32
32
|
The renderer exposes:
|
|
33
33
|
|
|
34
34
|
- `initialize()`
|
|
35
|
-
- `loadModel(modelPath)`
|
|
35
|
+
- `loadModel(modelPath)` — the latest call wins: a load that a later call
|
|
36
|
+
overtook releases its model and resolves without showing it, however late its
|
|
37
|
+
files arrive
|
|
36
38
|
- `render(message, character?)`
|
|
37
39
|
- `destroy()`
|
|
38
40
|
- `getAvailableExpressions()`
|
|
@@ -44,6 +46,11 @@ The renderer exposes:
|
|
|
44
46
|
playing; an unflagged call keeps the sound, which is what leaves a
|
|
45
47
|
human-triggered motion audible
|
|
46
48
|
- `lookAt({ x, y })`
|
|
49
|
+
- `setGazeOrigin(origin?)` — the point of the model the mouse gaze is measured
|
|
50
|
+
from, as fractions of the model's canvas (`{ x: 0.5, y: 0.14 }` is between
|
|
51
|
+
Haru's eyes); no argument restores the canvas's centre, the Cubism sample's
|
|
52
|
+
convention, under which a pointer on a full body's face reads as above the
|
|
53
|
+
centre. It holds across `loadModel`; taps and `lookAt` are unaffected
|
|
47
54
|
|
|
48
55
|
When used through `@charivo/render`, realtime lip sync, mouse tracking,
|
|
49
56
|
and gaze are handled for you.
|
package/dist/renderer.js
CHANGED
|
@@ -14506,6 +14506,10 @@ var Live2DRendererImpl = class _Live2DRendererImpl {
|
|
|
14506
14506
|
host;
|
|
14507
14507
|
model;
|
|
14508
14508
|
lastModelPath;
|
|
14509
|
+
/** Counts model loads, so one a later load overtook can tell. */
|
|
14510
|
+
loadCount = 0;
|
|
14511
|
+
/** Where the mouse gaze is measured from; unset = the view's centre. */
|
|
14512
|
+
gazeOrigin;
|
|
14509
14513
|
teardownResize;
|
|
14510
14514
|
teardownBrowserLifecycle;
|
|
14511
14515
|
animationFrameId;
|
|
@@ -14537,10 +14541,25 @@ var Live2DRendererImpl = class _Live2DRendererImpl {
|
|
|
14537
14541
|
if (!this.host) throw new Error("Live2D renderer is not initialized");
|
|
14538
14542
|
this.lastModelPath = modelPath;
|
|
14539
14543
|
this.model?.release();
|
|
14544
|
+
const model = await this.loadLatest(modelPath, this.host);
|
|
14545
|
+
if (model) this.model = model;
|
|
14546
|
+
}
|
|
14547
|
+
/**
|
|
14548
|
+
* Load a model for `host`. Resolves undefined, with the model released, when
|
|
14549
|
+
* it is no longer the one wanted by the time it is ready: a later load began
|
|
14550
|
+
* (the latest call wins, however late this one lands), or the host it was
|
|
14551
|
+
* built for is gone (destroy(), or a context restore).
|
|
14552
|
+
*/
|
|
14553
|
+
async loadLatest(modelPath, host) {
|
|
14554
|
+
const load = ++this.loadCount;
|
|
14540
14555
|
const model = new LAppModel();
|
|
14541
|
-
await model.loadAssets(modelPath,
|
|
14556
|
+
await model.loadAssets(modelPath, host);
|
|
14542
14557
|
await model.waitUntilReady();
|
|
14543
|
-
this.
|
|
14558
|
+
if (load !== this.loadCount || this.host !== host) {
|
|
14559
|
+
model.release();
|
|
14560
|
+
return void 0;
|
|
14561
|
+
}
|
|
14562
|
+
return model;
|
|
14544
14563
|
}
|
|
14545
14564
|
async render(_message, _character) {
|
|
14546
14565
|
}
|
|
@@ -14661,7 +14680,7 @@ var Live2DRendererImpl = class _Live2DRendererImpl {
|
|
|
14661
14680
|
const coreModel = this.model.getModel();
|
|
14662
14681
|
const canvas = this.canvas;
|
|
14663
14682
|
if (coreModel) {
|
|
14664
|
-
if (
|
|
14683
|
+
if (this.fitsModelToWidth(coreModel)) {
|
|
14665
14684
|
this.model.getModelMatrix().setWidth(2);
|
|
14666
14685
|
projection.scale(1, canvas.width / canvas.height);
|
|
14667
14686
|
} else {
|
|
@@ -14717,7 +14736,50 @@ var Live2DRendererImpl = class _Live2DRendererImpl {
|
|
|
14717
14736
|
updateViewWithMouse(coords) {
|
|
14718
14737
|
if (!this.model?.isReady()) return;
|
|
14719
14738
|
const { viewX, viewY } = this.toViewCoordinates(coords);
|
|
14720
|
-
this.
|
|
14739
|
+
const origin = this.gazeOriginInView();
|
|
14740
|
+
this.model.setDragging(viewX - origin.x, viewY - origin.y);
|
|
14741
|
+
}
|
|
14742
|
+
/**
|
|
14743
|
+
* `MouseTrackable.setGazeOrigin`: measure the mouse drag from a point of the
|
|
14744
|
+
* model's canvas (fractions, top-left to bottom-right) instead of the view's
|
|
14745
|
+
* centre. Throws on a non-finite `x` or `y`: kept, it would hand the model a
|
|
14746
|
+
* NaN drag on every pointer move.
|
|
14747
|
+
*/
|
|
14748
|
+
setGazeOrigin(origin) {
|
|
14749
|
+
if (origin && !(Number.isFinite(origin.x) && Number.isFinite(origin.y))) {
|
|
14750
|
+
throw new Error(
|
|
14751
|
+
`Live2D: setGazeOrigin() needs a finite x and y, got ${origin.x}, ${origin.y}`
|
|
14752
|
+
);
|
|
14753
|
+
}
|
|
14754
|
+
this.gazeOrigin = origin;
|
|
14755
|
+
}
|
|
14756
|
+
/**
|
|
14757
|
+
* Where the gaze origin sits in view coordinates, the space a drag and a
|
|
14758
|
+
* hit test are given in and the model matrix maps the model into: the
|
|
14759
|
+
* fractions become model units (origin at the canvas's centre, +y up) and
|
|
14760
|
+
* go through that matrix, read at each call because the frame loop refits
|
|
14761
|
+
* it to the canvas. The view's centre when no origin is set.
|
|
14762
|
+
*/
|
|
14763
|
+
gazeOriginInView() {
|
|
14764
|
+
const origin = this.gazeOrigin;
|
|
14765
|
+
const coreModel = this.model?.getModel();
|
|
14766
|
+
if (!origin || !this.model || !this.canvas || !coreModel) {
|
|
14767
|
+
return { x: 0, y: 0 };
|
|
14768
|
+
}
|
|
14769
|
+
const modelMatrix = this.model.getModelMatrix();
|
|
14770
|
+
const scale = this.fitsModelToWidth(coreModel) ? this.canvas.width / this.canvas.height : 1;
|
|
14771
|
+
return {
|
|
14772
|
+
x: scale * modelMatrix.transformX((origin.x - 0.5) * coreModel.getCanvasWidth()),
|
|
14773
|
+
y: scale * modelMatrix.transformY((0.5 - origin.y) * coreModel.getCanvasHeight())
|
|
14774
|
+
};
|
|
14775
|
+
}
|
|
14776
|
+
/**
|
|
14777
|
+
* True when the frame loop fits the model to the canvas's width rather than
|
|
14778
|
+
* its height: a model wider than one unit on a portrait canvas.
|
|
14779
|
+
*/
|
|
14780
|
+
fitsModelToWidth(coreModel) {
|
|
14781
|
+
const canvas = this.canvas;
|
|
14782
|
+
return coreModel.getCanvasWidth() > 1 && canvas.width < canvas.height;
|
|
14721
14783
|
}
|
|
14722
14784
|
handleMouseTap(coords) {
|
|
14723
14785
|
if (!this.model?.isReady()) return;
|
|
@@ -14820,10 +14882,8 @@ var Live2DRendererImpl = class _Live2DRendererImpl {
|
|
|
14820
14882
|
this.host.initialize();
|
|
14821
14883
|
this.resizeCanvas();
|
|
14822
14884
|
if (this.lastModelPath) {
|
|
14823
|
-
const model =
|
|
14824
|
-
|
|
14825
|
-
await model.waitUntilReady();
|
|
14826
|
-
this.model = model;
|
|
14885
|
+
const model = await this.loadLatest(this.lastModelPath, this.host);
|
|
14886
|
+
if (model) this.model = model;
|
|
14827
14887
|
}
|
|
14828
14888
|
this.updateViewMatrices();
|
|
14829
14889
|
this.recoveryPending = false;
|