1
This commit is contained in:
37
index.js
37
index.js
@ -1,9 +1,13 @@
|
||||
import { EXRCubeTexture } from '@babylonjs/core';
|
||||
import apiConfig from './src/config.js';
|
||||
import { setSkuMapping, getSkuByModelId, clearSkuMapping, clearAllSkuMappings } from './src/skuMapping.js';
|
||||
|
||||
// 存储 kernel 实例
|
||||
let kernelInstance = null;
|
||||
|
||||
// 导出 SKU 映射相关函数,方便外部使用
|
||||
export { getSkuByModelId, clearSkuMapping, clearAllSkuMappings };
|
||||
|
||||
/**
|
||||
* 初始化应用逻辑 - 注入 kernel 实例
|
||||
* @param {Object} kernel - SDK kernel 实例
|
||||
@ -131,7 +135,7 @@ export const getEvent = async (dropzone_data, sku) => {
|
||||
console.log('关联事件:', result.data.events);
|
||||
|
||||
// 使用 for...of 循环以支持 await
|
||||
await executeEvent(dropzone_data, result)
|
||||
await executeEvent(dropzone_data, result, sku)
|
||||
} else {
|
||||
console.log(`未查询到数据`);
|
||||
}
|
||||
@ -141,8 +145,7 @@ export const getEvent = async (dropzone_data, sku) => {
|
||||
}
|
||||
|
||||
//点击放置区域执行事件 一般是换配件
|
||||
//点击放置区域执行事件 一般是换配件
|
||||
export const executeEvent = async (dropzone_data, result) => {
|
||||
export const executeEvent = async (dropzone_data, result, sku) => {
|
||||
const kernel = getKernel();
|
||||
|
||||
const { wallName, index, transform } = dropzone_data;
|
||||
@ -160,6 +163,9 @@ export const executeEvent = async (dropzone_data, result) => {
|
||||
modelName = name;
|
||||
kernel.dropZone.recordModelPlacement(wallName, index, name + '_' + modelId);
|
||||
|
||||
// 记录模型ID到SKU的映射
|
||||
setSkuMapping(modelId, sku);
|
||||
|
||||
await kernel.model.add({
|
||||
modelName: name,
|
||||
modelId: modelId,
|
||||
@ -192,8 +198,8 @@ export const executeEvent = async (dropzone_data, result) => {
|
||||
albedoColor: color,
|
||||
albedoTexture: color_map_url,
|
||||
normalMap: normal_map_url,
|
||||
metallic: metallic,
|
||||
roughness: roughness
|
||||
metallic: +metallic,
|
||||
roughness: +roughness
|
||||
});
|
||||
|
||||
console.log(`百叶模型颜色已替换为 ${color}`);
|
||||
@ -203,7 +209,7 @@ export const executeEvent = async (dropzone_data, result) => {
|
||||
|
||||
|
||||
//一般是换棚子/换颜色/设置放置区域
|
||||
export const executeEvent2 = async (result) => {
|
||||
export const executeEvent2 = async (result, sku) => {
|
||||
const kernel = getKernel();
|
||||
|
||||
// 检查是否有模型更换事件
|
||||
@ -219,16 +225,16 @@ export const executeEvent2 = async (result) => {
|
||||
console.log(`检查模型 ${name + '_' + category} 是否存在:`, modelAlreadyExists);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(hasModelChange && !modelAlreadyExists);
|
||||
kernel.dropZone.hide();
|
||||
// 只有在需要更换模型且模型不存在时才清除
|
||||
if (hasModelChange && !modelAlreadyExists) {
|
||||
console.log('模型不存在,执行清除操作');
|
||||
|
||||
kernel.model.removeAll();
|
||||
} else if (modelAlreadyExists) {
|
||||
kernel.dropZone.hide();
|
||||
console.log('模型已存在,跳过清除操作,仅更新材质');
|
||||
}
|
||||
// 清除所有 SKU 映射
|
||||
clearAllSkuMappings();
|
||||
}
|
||||
|
||||
// 先处理所有 change_model 事件
|
||||
for (const event of result.data.events) {
|
||||
@ -259,6 +265,9 @@ export const executeEvent2 = async (result) => {
|
||||
});
|
||||
}
|
||||
|
||||
// 记录模型ID到SKU的映射
|
||||
setSkuMapping(category, sku);
|
||||
|
||||
// 加载并放置模型(使用 category 作为 modelId)
|
||||
await kernel.model.add({
|
||||
modelName: name,
|
||||
@ -276,15 +285,13 @@ export const executeEvent2 = async (result) => {
|
||||
if (event.event_type === 'change_color') {
|
||||
const materialName = event.material_name;
|
||||
const { color, color_map_url, normal_map_url, metallic, roughness } = event.target_data;
|
||||
console.log('替换百叶模型颜色:', event.target_data);
|
||||
console.log('替换模型颜色:', event.target_data);
|
||||
|
||||
kernel.material.apply({
|
||||
target: materialName,
|
||||
albedoColor: color,
|
||||
albedoTexture: color_map_url,
|
||||
normalMap: normal_map_url,
|
||||
metallic: metallic,
|
||||
roughness: roughness
|
||||
});
|
||||
|
||||
console.log(`百叶模型颜色已替换为 ${color}`);
|
||||
@ -341,7 +348,7 @@ export const getProductConfig = async (sku) => {
|
||||
getPlacementZone(sku)
|
||||
}
|
||||
else {
|
||||
executeEvent2(result)
|
||||
executeEvent2(result, sku)
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user