|
4 | 4 | */
|
5 | 5 |
|
6 | 6 | 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'; |
9 | 10 | import logger, { LogLevel } from './logger.js';
|
10 | 11 |
|
11 | 12 | interface ModelList {
|
@@ -72,8 +73,9 @@ class ModelManager {
|
72 | 73 | private _modelId: number;
|
73 | 74 | private _modelTexturesId: number;
|
74 | 75 | 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; |
77 | 79 | private modelJSONCache: Record<string, any>;
|
78 | 80 |
|
79 | 81 | /**
|
@@ -110,7 +112,7 @@ class ModelManager {
|
110 | 112 | this.cubism5Path = cubism5Path || '';
|
111 | 113 | this._modelId = modelId;
|
112 | 114 | this._modelTexturesId = modelTexturesId;
|
113 |
| - this.modelInitialized = false; |
| 115 | + this.currentModelVersion = 0; |
114 | 116 | this.modelJSONCache = {};
|
115 | 117 | }
|
116 | 118 |
|
@@ -154,28 +156,35 @@ class ModelManager {
|
154 | 156 | async loadLive2D(modelSettingPath: string, modelSetting: object) {
|
155 | 157 | const version = this.checkModelVersion(modelSetting);
|
156 | 158 | if (version === 2) {
|
157 |
| - if (!this.model) { |
| 159 | + if (!this.cubism2model) { |
158 | 160 | if (!this.cubism2Path) {
|
159 | 161 | logger.error('No cubism2Path set, cannot load Cubism 2 Core.')
|
160 | 162 | return;
|
161 | 163 | }
|
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); |
165 | 172 | }
|
166 | 173 | } else {
|
167 | 174 | if (!this.cubism5Path) {
|
168 | 175 | logger.error('No cubism5Path set, cannot load Cubism 5 Core.')
|
169 | 176 | return;
|
170 | 177 | }
|
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 | + } |
179 | 188 | }
|
180 | 189 | logger.info(`Model ${modelSettingPath} loaded`);
|
181 | 190 | }
|
|
0 commit comments