Skip to content

Commit d493fb5

Browse files
Support both sdk
1 parent b582ee3 commit d493fb5

File tree

3 files changed

+57
-18
lines changed

3 files changed

+57
-18
lines changed

src/cubism5/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,8 @@ export class AppDelegate extends LAppDelegate {
187187
// 将新模型加入到模型列表
188188
live2dManager._models.pushBack(instance);
189189
}
190+
191+
get subdelegates() {
192+
return this._subdelegates;
193+
}
190194
}

src/model.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
*/
55

66
import { showMessage } from './message.js';
7-
import { randomSelection } from './utils.js';
8-
import type Model from './live2d/index.js';
7+
import { randomSelection, loadExternalResource } from './utils.js';
8+
import type Cubism2Model from './live2d/index.js';
9+
import type { AppDelegate as Cubism5Model } from './cubism5/index.js';
910
import logger, { LogLevel } from './logger.js';
1011

1112
interface ModelList {
@@ -72,8 +73,9 @@ class ModelManager {
7273
private _modelId: number;
7374
private _modelTexturesId: number;
7475
private modelList: ModelList | null = null;
75-
private model: Model | undefined;
76-
private modelInitialized: boolean;
76+
private cubism2model: Cubism2Model | undefined;
77+
private cubism5model: Cubism5Model | undefined;
78+
private currentModelVersion: number;
7779
private modelJSONCache: Record<string, any>;
7880

7981
/**
@@ -110,7 +112,7 @@ class ModelManager {
110112
this.cubism5Path = cubism5Path || '';
111113
this._modelId = modelId;
112114
this._modelTexturesId = modelTexturesId;
113-
this.modelInitialized = false;
115+
this.currentModelVersion = 0;
114116
this.modelJSONCache = {};
115117
}
116118

@@ -154,28 +156,35 @@ class ModelManager {
154156
async loadLive2D(modelSettingPath: string, modelSetting: object) {
155157
const version = this.checkModelVersion(modelSetting);
156158
if (version === 2) {
157-
if (!this.model) {
159+
if (!this.cubism2model) {
158160
if (!this.cubism2Path) {
159161
logger.error('No cubism2Path set, cannot load Cubism 2 Core.')
160162
return;
161163
}
162-
await import(this.cubism2Path);
163-
const Model = await import('./live2d/index.js');
164-
this.model = new Model.default();
164+
await loadExternalResource(this.cubism2Path, 'js');
165+
const { default: Cubism2Model } = await import('./live2d/index.js');
166+
this.cubism2model = new Cubism2Model();
167+
}
168+
if (!this.cubism2model.gl) {
169+
await this.cubism2model?.init('live2d', modelSettingPath, modelSetting);
170+
} else {
171+
await this.cubism2model?.changeModelWithJSON(modelSettingPath, modelSetting);
165172
}
166173
} else {
167174
if (!this.cubism5Path) {
168175
logger.error('No cubism5Path set, cannot load Cubism 5 Core.')
169176
return;
170177
}
171-
logger.error('Models version of Cubism 3 and later are not supported.')
172-
return;
173-
}
174-
if (!this.modelInitialized) {
175-
this.modelInitialized = true;
176-
await this.model?.init('live2d', modelSettingPath, modelSetting);
177-
} else {
178-
await this.model?.changeModelWithJSON(modelSettingPath, modelSetting);
178+
await loadExternalResource(this.cubism5Path, 'js');
179+
const { AppDelegate: Cubism5Model} = await import('./cubism5/index.js');
180+
this.cubism5model = new (Cubism5Model as any)();
181+
if (!this.cubism5model.subdelegates.at(0)) {
182+
this.cubism5model.initialize();
183+
this.cubism5model.changeModel(modelSettingPath);
184+
this.cubism5model.run();
185+
} else {
186+
this.cubism5model.changeModel(modelSettingPath);
187+
}
179188
}
180189
logger.info(`Model ${modelSettingPath} loaded`);
181190
}

src/utils.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,30 @@ function randomSelection(obj: any) {
1212
return Array.isArray(obj) ? obj[Math.floor(Math.random() * obj.length)] : obj;
1313
}
1414

15-
export { randomSelection };
15+
/**
16+
* 异步加载外部资源。
17+
* @param {string} url - 资源路径。
18+
* @param {string} type - 资源类型。
19+
*/
20+
function loadExternalResource(url: string, type: string): Promise<string> {
21+
return new Promise((resolve: any, reject: any) => {
22+
let tag;
23+
24+
if (type === 'css') {
25+
tag = document.createElement('link');
26+
tag.rel = 'stylesheet';
27+
tag.href = url;
28+
}
29+
else if (type === 'js') {
30+
tag = document.createElement('script');
31+
tag.src = url;
32+
}
33+
if (tag) {
34+
tag.onload = () => resolve(url);
35+
tag.onerror = () => reject(url);
36+
document.head.appendChild(tag);
37+
}
38+
});
39+
}
40+
41+
export { randomSelection, loadExternalResource };

0 commit comments

Comments
 (0)