Inspired by GitHub - alsdhkauuhw/halo-plugin-live2D: live2D for halo-plugin, I tried to render Live2D on the frontend through a Halo plugin. Ran into quite a few pitfalls along the way, so I'm writing them down here.
Choosing the Frontend Framework
Right now, the Live2D ecosystem is built on Cubism. Because it's closed source and the SDK is hard to develop with, the only frontend packages that are easy to use are guansss/pixi-live2d-display and LSTM-Kirigaya/Live2dRender. Since the pixi-based framework can call Cubism's internal APIs better, I built on top of it. But that project is pretty much unmaintained now, so I used its fork omniwaifu/pixi-live2d5, which is compatible with Cubism 5.0 and PixiJS 8. Later on I hit quite a few low-level issues, so I started directly from its playground project and modified that. This way I could work with the original code instead of importing it from outside. Of course, this also makes the project hard to update, but since it's just for personal use, I don't care.
Texture issues / MaskBufferCount
When I first deployed the model, the texture layers got messed up and the model rendered incorrectly. At first I thought it was a problem with the model, but everything was fine in the viewer, and I tried for a long time without fixing it. In the end, I turned on logging and found an error caused by MaskBufferCount not being high enough. I needed to set a large enough MaskBufferCount where the initialize function is called (the init method in the Cubism5InternalModel class contains the renderer's initialize).
This code lives in Cubism's own Live2D/CubismWebFramework, so it needs to be modified.
/**
* WebGL用の描画命令を実装したクラス
*/
export class CubismRenderer_WebGL extends CubismRenderer {
/**
* レンダラの初期化処理を実行する
* 引数に渡したモデルからレンダラの初期化処理に必要な情報を取り出すことができる
*
* @param model モデルのインスタンス
* @param maskBufferCount バッファの生成数
*/
public initialize(model: CubismModel, maskBufferCount = 1): void {
if (model.isUsingMasking()) {
this._clippingManager = new CubismClippingManager_WebGL(); // クリッピングマスク・バッファ前処理方式を初期化
this._clippingManager.initialize(model, maskBufferCount);
}
this._sortedDrawableIndexList.resize(model.getDrawableCount(), 0);
super.initialize(model); // 親クラスの処理を呼ぶ
Parameters not taking effect / CubismIdHandle
Most existing articles and code-completion suggestions recommend setting parameter values with the following code:
coreModel.setParameterValueById("ParamBreath", 1);
But this no longer works in 5.X, because strings like these have all been replaced with the CubismIdHandle type. You need to get a CubismIdHandle from the string like this.
coreModel.setParameterValueById(CubismFramework.getIdManager().getId("ParamBreath"), 1);
Resolution / Anti-aliasing
To fix the low rendering quality at high resolutions, you need to set the following options in PixiJS.
await app.init({
canvas,
autoStart: true,
resizeTo: container,
background: 0x000000,
backgroundAlpha: 0,
preference: "webgl",
+ resolution: window.devicePixelRatio || 1,
+ autoDensity: true,
+ antialias: true,
});
Rendering flicker when scrolling on iOS
On iOS, the model may flicker and disappear while scrolling. This comes from PixiJS's WebGL rendering, and the following code fixes it.
if (isMobile) {
let isScrolling = false;
let scrollTimer: number;
const handleScroll = () => {
if (!isScrolling) {
isScrolling = true;
app.render(); // important
app.renderer.gl.flush(); // important
requestAnimationFrame(() => {
isScrolling = false;
});
}
clearTimeout(scrollTimer);
scrollTimer = window.setTimeout(() => {
app.render(); // important
app.renderer.gl.flush(); // important
}, 100);
};
window.addEventListener('scroll', handleScroll, { passive: true });
window.addEventListener('touchmove', handleScroll, { passive: true });
}
Basically, it triggers render and gl.flush on scroll. .renderer.gl may show a type error; just ignore it (or use as any), because PixiJS isn't limited to WebGL.
Optimization: serving model files via OSS and IndexDB caching
If your server doesn't have enough bandwidth, loading a large model can be slow. One option is to cache it with IndexDB, another is to serve it from OSS; both work pretty much without issues (you can also use both together). If you use IndexDB, you can proxy the XHRLoader class in Cubism that's used to fetch models.
