This commit is contained in:
2026-04-24 19:39:58 +08:00
parent 01fdc0ee37
commit c992660011
4 changed files with 43 additions and 17 deletions

View File

@ -277,6 +277,7 @@
<button class="option-btn" data-option="louver-2">百叶2</button> <button class="option-btn" data-option="louver-2">百叶2</button>
<button class="option-btn" data-option="louver-3">百叶3</button> <button class="option-btn" data-option="louver-3">百叶3</button>
<button class="option-btn" data-option="louver-4">百叶4</button> <button class="option-btn" data-option="louver-4">百叶4</button>
<button class="option-btn" data-option="louver-4">卷帘小</button>
</div> </div>
</div> </div>
</div> </div>
@ -418,7 +419,7 @@
const modelUrl = `https://sdk.zguiy.com/resurces/model/${currentText}.glb`; const modelUrl = `https://sdk.zguiy.com/resurces/model/${currentText}.glb`;
console.log('替换百叶模型:', modelUrl); console.log('替换百叶模型:', modelUrl);
try { try {
await kernel.model.replace('百叶', modelUrl); await kernel.model.replace('卷帘小', modelUrl);
console.log(`百叶模型已替换为 ${currentText}`); console.log(`百叶模型已替换为 ${currentText}`);
} catch (error) { } catch (error) {
console.error(`百叶模型替换失败:`, error); console.error(`百叶模型替换失败:`, error);
@ -512,7 +513,8 @@
document.getElementById('remove-model-btn').addEventListener('click', () => { document.getElementById('remove-model-btn').addEventListener('click', () => {
const pickedMesh = window.getCurrentPickedMesh(); const pickedMesh = window.getCurrentPickedMesh();
if (pickedMesh) { if (pickedMesh) {
const success = kernel.model.remove(pickedMesh); const meshName = pickedMesh.name;
const success = kernel.model.remove(meshName);
if (success) { if (success) {
console.log('模型已移除'); console.log('模型已移除');
// 关闭信息框 // 关闭信息框

View File

@ -16,10 +16,9 @@ const config = {
kernel.init(config); kernel.init(config);
kernel.model.add("百叶窗小","https://sdk.zguiy.com/resurces/model/百叶窗小.glb") kernel.model.add("卷帘大", "https://sdk.zguiy.com/resurces/model/卷帘大.glb")
kernel.model.add("卷帘","https://sdk.zguiy.com/resurces/model/卷帘.glb") kernel.model.add("卷帘", "https://sdk.zguiy.com/resurces/model/卷帘.glb")
kernel.model.add("卷帘小","https://sdk.zguiy.com/resurces/model/卷帘小.glb") kernel.model.add("小", "https://sdk.zguiy.com/resurces/model/小.glb")
kernel.model.add("小桌","https://sdk.zguiy.com/resurces/model/小桌.glb")
kernel.on('model:load:progress', (data) => { kernel.on('model:load:progress', (data) => {
console.log('模型加载事件', data); console.log('模型加载事件', data);
@ -34,9 +33,6 @@ kernel.on('model:loaded', (data) => {
if (progressContainer) { if (progressContainer) {
progressContainer.style.display = 'none'; progressContainer.style.display = 'none';
} }
}); });
kernel.on('all:ready', (data) => { kernel.on('all:ready', (data) => {
@ -46,12 +42,10 @@ kernel.on('all:ready', (data) => {
attribute: 'alpha', attribute: 'alpha',
value: 0.5, value: 0.5,
}); });
kernel.hotspot.render([ kernel.hotspot.render([
{ {
id: "h1", id: "h1",
type:'hotspot', type: 'hotspot',
name: "卷帘门", name: "卷帘门",
meshName: "Valve_01", meshName: "Valve_01",
icon: "https://bpic.588ku.com/element_pic/20/06/30/d1046b01afc0b9586844350d131f4daf.jpg!/fw/253/quality/90/unsharp/true/compress/true", icon: "https://bpic.588ku.com/element_pic/20/06/30/d1046b01afc0b9586844350d131f4daf.jpg!/fw/253/quality/90/unsharp/true/compress/true",
@ -90,7 +84,7 @@ kernel.on('model:click', (data) => {
document.getElementById('info-position').textContent = `坐标: [${position.x.toFixed(2)}, ${position.y.toFixed(2)}, ${position.z.toFixed(2)}]`; document.getElementById('info-position').textContent = `坐标: [${position.x.toFixed(2)}, ${position.y.toFixed(2)}, ${position.z.toFixed(2)}]`;
// 将DOM附加到点击的3D坐标会自动显示 // 将DOM附加到点击的3D坐标会自动显示
kernel.domTo3D.attach('model-info', infoDiv, [position.x, position.y, position.z], { x: -2, y: -2}); kernel.domTo3D.attach('model-info', infoDiv, [position.x, position.y, position.z], { x: -2, y: -2 });
} }
}); });

View File

@ -291,6 +291,21 @@ export class AppModel extends Monobehiver {
}); });
} }
/**
* 根据 mesh 名称查找 mesh 对象
* @param meshName mesh 名称
* @returns mesh 对象,未找到返回 undefined
*/
private findMeshByName(meshName: string): AbstractMesh | undefined {
const keys = this.modelDic.Keys();
for (const key of keys) {
const meshes = this.modelDic.Get(key);
const found = meshes?.find(m => m.name === meshName);
if (found) return found;
}
return undefined;
}
/** /**
* 根据 mesh 查找所属的模型名称 * 根据 mesh 查找所属的模型名称
* @param mesh 网格对象 * @param mesh 网格对象
@ -308,11 +323,24 @@ export class AppModel extends Monobehiver {
} }
/** /**
* 根据 mesh 移除所属的整个模型 * 根据 mesh 或 mesh 名称移除所属的整个模型
* @param mesh 网格对象 * @param meshOrName 网格对象或网格名称
* @returns 是否成功移除 * @returns 是否成功移除
*/ */
remove(mesh: AbstractMesh): boolean { remove(meshOrName: AbstractMesh | string): boolean {
let mesh: AbstractMesh | undefined;
// 判断传入的是对象还是字符串
if (typeof meshOrName === 'string') {
mesh = this.findMeshByName(meshOrName);
if (!mesh) {
console.warn(`未找到名为 ${meshOrName} 的网格`);
return false;
}
} else {
mesh = meshOrName;
}
const modelName = this.findModelNameByMesh(mesh); const modelName = this.findModelNameByMesh(mesh);
if (modelName) { if (modelName) {
this.removeByName(modelName); this.removeByName(modelName);
@ -328,6 +356,8 @@ export class AppModel extends Monobehiver {
* @param newModelUrl 新模型URL * @param newModelUrl 新模型URL
*/ */
async replaceModel(modelName: string, newModelUrl: string): Promise<LoadResult> { async replaceModel(modelName: string, newModelUrl: string): Promise<LoadResult> {
console.log( modelName,this.modelDic);
this.removeByName(modelName); this.removeByName(modelName);
return await this.addSingle(modelName, newModelUrl); return await this.addSingle(modelName, newModelUrl);
} }

View File

@ -93,6 +93,6 @@ export class MainApp {
async dispose(): Promise<void> { async dispose(): Promise<void> {
this.appModel?.clean(); this.appModel?.clean();
this.appEnv?.clean(); this.appEnv?.clean();
this.appHotspot?.clear(); // this.appHotspot?.clear();
} }
} }