1
This commit is contained in:
@ -192,6 +192,17 @@ const executeEvent = async (dropzone_data, result) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查模型是否已存在
|
||||||
|
* @param {string} modelId - 模型ID
|
||||||
|
* @returns {boolean} 模型是否存在
|
||||||
|
*/
|
||||||
|
const isModelExists = (modelId) => {
|
||||||
|
const kernel = getKernel();
|
||||||
|
// 调用 SDK 的 API 检查模型是否存在
|
||||||
|
return kernel.model.exists(modelId);
|
||||||
|
}
|
||||||
|
|
||||||
//换棚子
|
//换棚子
|
||||||
const executeEvent2 = async (result) => {
|
const executeEvent2 = async (result) => {
|
||||||
const kernel = getKernel();
|
const kernel = getKernel();
|
||||||
@ -199,10 +210,24 @@ const executeEvent2 = async (result) => {
|
|||||||
// 检查是否有模型更换事件
|
// 检查是否有模型更换事件
|
||||||
const hasModelChange = result.data.events.some(e => e.event_type === 'change_model');
|
const hasModelChange = result.data.events.some(e => e.event_type === 'change_model');
|
||||||
|
|
||||||
// 只有在需要更换模型时才清除
|
// 检查新模型是否已经存在
|
||||||
|
let modelAlreadyExists = false;
|
||||||
if (hasModelChange) {
|
if (hasModelChange) {
|
||||||
|
const firstModelEvent = result.data.events.find(e => e.event_type === 'change_model');
|
||||||
|
if (firstModelEvent) {
|
||||||
|
const { category } = firstModelEvent.target_data;
|
||||||
|
modelAlreadyExists = isModelExists(category);
|
||||||
|
console.log(`检查模型 ${category} 是否存在:`, modelAlreadyExists);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 只有在需要更换模型且模型不存在时才清除
|
||||||
|
if (hasModelChange && !modelAlreadyExists) {
|
||||||
|
console.log('模型不存在,执行清除操作');
|
||||||
kernel.dropZone.clearZones();
|
kernel.dropZone.clearZones();
|
||||||
kernel.model.removeAll();
|
kernel.model.removeAll();
|
||||||
|
} else if (modelAlreadyExists) {
|
||||||
|
console.log('模型已存在,跳过清除操作,仅更新材质');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 先处理所有 change_model 事件
|
// 先处理所有 change_model 事件
|
||||||
@ -219,6 +244,12 @@ const executeEvent2 = async (result) => {
|
|||||||
console.log('替换百叶模型:', event);
|
console.log('替换百叶模型:', event);
|
||||||
console.log('替换百叶模型类型:', category);
|
console.log('替换百叶模型类型:', category);
|
||||||
|
|
||||||
|
// 如果模型已存在,跳过加载
|
||||||
|
if (modelAlreadyExists) {
|
||||||
|
console.log(`模型 ${category} 已存在,跳过加载`);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (placement_zone) {
|
if (placement_zone) {
|
||||||
const { alpha, border_color, color, show_border, thickness, walls } = placement_zone
|
const { alpha, border_color, color, show_border, thickness, walls } = placement_zone
|
||||||
kernel.dropZone.setData({
|
kernel.dropZone.setData({
|
||||||
@ -338,5 +369,6 @@ window.AppLogic = {
|
|||||||
executeEvent,
|
executeEvent,
|
||||||
executeEvent2,
|
executeEvent2,
|
||||||
getHotspot,
|
getHotspot,
|
||||||
getProductConfig
|
getProductConfig,
|
||||||
|
isModelExists // 暴露检查模型是否存在的API
|
||||||
};
|
};
|
||||||
@ -129017,7 +129017,7 @@ discard;}}
|
|||||||
* @param modelId 模型ID
|
* @param modelId 模型ID
|
||||||
*/
|
*/
|
||||||
cloneMaterials(meshes, modelId) {
|
cloneMaterials(meshes, modelId) {
|
||||||
const scene = this.mainApp.appScene.object;
|
this.mainApp.appScene.object;
|
||||||
const clonedMaterials = /* @__PURE__ */ new Map();
|
const clonedMaterials = /* @__PURE__ */ new Map();
|
||||||
meshes.forEach((mesh) => {
|
meshes.forEach((mesh) => {
|
||||||
if (mesh.material) {
|
if (mesh.material) {
|
||||||
@ -129027,15 +129027,10 @@ discard;}}
|
|||||||
const newName = `${originalName}_${modelId}`;
|
const newName = `${originalName}_${modelId}`;
|
||||||
const clonedMaterial = originalMaterial.clone(newName);
|
const clonedMaterial = originalMaterial.clone(newName);
|
||||||
clonedMaterials.set(originalName, clonedMaterial);
|
clonedMaterials.set(originalName, clonedMaterial);
|
||||||
console.log(`[cloneMaterials] 克隆材质: ${originalName} -> ${newName}`);
|
|
||||||
console.log(`[cloneMaterials] 克隆后的材质名称: ${clonedMaterial.name}`);
|
|
||||||
console.log(`[cloneMaterials] 材质是否在场景中:`, scene.materials.includes(clonedMaterial));
|
|
||||||
}
|
}
|
||||||
mesh.material = clonedMaterials.get(originalName);
|
mesh.material = clonedMaterials.get(originalName);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
console.log(`已为模型 ${modelId} 克隆 ${clonedMaterials.size} 个材质`);
|
|
||||||
console.log(`[cloneMaterials] 场景中的所有材质:`, scene.materials.map((m) => m.name));
|
|
||||||
}
|
}
|
||||||
/** 为网格设置阴影(投射和接收) */
|
/** 为网格设置阴影(投射和接收) */
|
||||||
createModelRoot(modelId, meshes) {
|
createModelRoot(modelId, meshes) {
|
||||||
@ -129226,7 +129221,6 @@ discard;}}
|
|||||||
* @returns 模型名称,未找到返回 undefined
|
* @returns 模型名称,未找到返回 undefined
|
||||||
*/
|
*/
|
||||||
findModelNameByMesh(mesh) {
|
findModelNameByMesh(mesh) {
|
||||||
console.log(111111111111);
|
|
||||||
const keys = this.modelDic.Keys();
|
const keys = this.modelDic.Keys();
|
||||||
for (const key of keys) {
|
for (const key of keys) {
|
||||||
const meshes = this.modelDic.Get(key);
|
const meshes = this.modelDic.Get(key);
|
||||||
@ -129269,7 +129263,6 @@ discard;}}
|
|||||||
* @param modelConfig 模型配置对象
|
* @param modelConfig 模型配置对象
|
||||||
*/
|
*/
|
||||||
async replaceModel(modelConfig) {
|
async replaceModel(modelConfig) {
|
||||||
console.log(modelConfig.modelId, this.modelDic);
|
|
||||||
this.removeByName(modelConfig.modelId);
|
this.removeByName(modelConfig.modelId);
|
||||||
return await this.addSingle(
|
return await this.addSingle(
|
||||||
modelConfig.modelId,
|
modelConfig.modelId,
|
||||||
@ -129293,7 +129286,6 @@ discard;}}
|
|||||||
this.modelDic.Remove(modelName);
|
this.modelDic.Remove(modelName);
|
||||||
this.modelMetadataDic.Remove(modelName);
|
this.modelMetadataDic.Remove(modelName);
|
||||||
this.mainApp.gameManager?.updateDictionaries();
|
this.mainApp.gameManager?.updateDictionaries();
|
||||||
console.log(`Model removed: ${modelName}`);
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* 清除所有已添加的模型并释放内存
|
* 清除所有已添加的模型并释放内存
|
||||||
@ -129301,7 +129293,6 @@ discard;}}
|
|||||||
*/
|
*/
|
||||||
removeAll() {
|
removeAll() {
|
||||||
const modelNames = this.modelDic.Keys();
|
const modelNames = this.modelDic.Keys();
|
||||||
console.log(`开始清除所有模型,共 ${modelNames.length} 个模型`);
|
|
||||||
modelNames.forEach((modelName) => {
|
modelNames.forEach((modelName) => {
|
||||||
const meshes = this.modelDic.Get(modelName);
|
const meshes = this.modelDic.Get(modelName);
|
||||||
if (meshes?.length) {
|
if (meshes?.length) {
|
||||||
@ -129459,6 +129450,14 @@ discard;}}
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* 检查模型是否存在
|
||||||
|
* @param modelId 模型ID
|
||||||
|
* @returns 模型是否存在
|
||||||
|
*/
|
||||||
|
exists(modelId) {
|
||||||
|
return this.modelDic.Has(modelId);
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* 应用 transform 到模型
|
* 应用 transform 到模型
|
||||||
* @param modelId 模型ID
|
* @param modelId 模型ID
|
||||||
@ -355223,7 +355222,6 @@ clipPos=viewProjection*worldPos;previousClipPos=previousViewProjection*previousW
|
|||||||
}
|
}
|
||||||
this.updateDictionaries();
|
this.updateDictionaries();
|
||||||
this.cacheRollerDoorMeshes();
|
this.cacheRollerDoorMeshes();
|
||||||
console.log("材质字典:", this.materialDic);
|
|
||||||
this.setRollerDoorScale("Box006.001", new Vector3(0.12, 0.02, 0.118));
|
this.setRollerDoorScale("Box006.001", new Vector3(0.12, 0.02, 0.118));
|
||||||
this.setRollerDoorScale("Box005.001", new Vector3(0.13, 0.02, 0.12));
|
this.setRollerDoorScale("Box005.001", new Vector3(0.13, 0.02, 0.12));
|
||||||
}
|
}
|
||||||
@ -355749,8 +355747,6 @@ clipPos=viewProjection*worldPos;previousClipPos=previousViewProjection*previousW
|
|||||||
*/
|
*/
|
||||||
applyMaterial(options) {
|
applyMaterial(options) {
|
||||||
this.updateDictionaries();
|
this.updateDictionaries();
|
||||||
console.log("[applyMaterial] 查找材质:", options.target);
|
|
||||||
console.log("[applyMaterial] 当前所有材质:", this.materialDic.Values().map((m) => m.name));
|
|
||||||
const targetMaterials = [];
|
const targetMaterials = [];
|
||||||
this.materialDic.Values().forEach((material) => {
|
this.materialDic.Values().forEach((material) => {
|
||||||
if (material.name === options.target || material.name.startsWith(`${options.target}_`)) {
|
if (material.name === options.target || material.name.startsWith(`${options.target}_`)) {
|
||||||
@ -355761,7 +355757,6 @@ clipPos=viewProjection*worldPos;previousClipPos=previousViewProjection*previousW
|
|||||||
console.warn(`Material not found: ${options.target}`);
|
console.warn(`Material not found: ${options.target}`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.log("[applyMaterial] 找到材质:", targetMaterials.map((m) => m.name));
|
|
||||||
targetMaterials.forEach((material) => {
|
targetMaterials.forEach((material) => {
|
||||||
if (options.albedoColor) {
|
if (options.albedoColor) {
|
||||||
const color = Color3.FromHexString(options.albedoColor);
|
const color = Color3.FromHexString(options.albedoColor);
|
||||||
@ -355784,7 +355779,6 @@ clipPos=viewProjection*worldPos;previousClipPos=previousViewProjection*previousW
|
|||||||
}
|
}
|
||||||
material.markDirty();
|
material.markDirty();
|
||||||
});
|
});
|
||||||
console.log(`Material applied to ${targetMaterials.length} material(s): ${options.target}`, options);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
5
index.js
5
index.js
@ -202,7 +202,8 @@ export const executeEvent2 = async (result) => {
|
|||||||
|
|
||||||
// 检查是否有模型更换事件
|
// 检查是否有模型更换事件
|
||||||
const hasModelChange = result.data.events.some(e => e.event_type === 'change_model');
|
const hasModelChange = result.data.events.some(e => e.event_type === 'change_model');
|
||||||
|
const modelExists = await kernel.model.exists(modelId);
|
||||||
|
console.log(modelExists);
|
||||||
// 只有在需要更换模型时才清除
|
// 只有在需要更换模型时才清除
|
||||||
if (hasModelChange) {
|
if (hasModelChange) {
|
||||||
kernel.dropZone.clearZones();
|
kernel.dropZone.clearZones();
|
||||||
@ -241,7 +242,7 @@ export const executeEvent2 = async (result) => {
|
|||||||
modelControlType: model_control_type,
|
modelControlType: model_control_type,
|
||||||
})
|
})
|
||||||
|
|
||||||
console.log(`百叶模型已放置为 ${name}`);
|
console.log(`模型已放置为 ${name}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -427,7 +427,6 @@ export class AppModel extends Monobehiver {
|
|||||||
* @returns 模型名称,未找到返回 undefined
|
* @returns 模型名称,未找到返回 undefined
|
||||||
*/
|
*/
|
||||||
findModelNameByMesh(mesh: AbstractMesh): string | undefined {
|
findModelNameByMesh(mesh: AbstractMesh): string | undefined {
|
||||||
console.log(111111111111);
|
|
||||||
|
|
||||||
const keys = this.modelDic.Keys();
|
const keys = this.modelDic.Keys();
|
||||||
for (const key of keys) {
|
for (const key of keys) {
|
||||||
@ -711,6 +710,15 @@ export class AppModel extends Monobehiver {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查模型是否存在
|
||||||
|
* @param modelId 模型ID
|
||||||
|
* @returns 模型是否存在
|
||||||
|
*/
|
||||||
|
exists(modelId: string): boolean {
|
||||||
|
return this.modelDic.Has(modelId);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 应用 transform 到模型
|
* 应用 transform 到模型
|
||||||
* @param modelId 模型ID
|
* @param modelId 模型ID
|
||||||
|
|||||||
Reference in New Issue
Block a user