This commit is contained in:
2026-05-17 13:59:16 +08:00
parent f76b19697c
commit 870477f864
7 changed files with 176 additions and 93 deletions

164
index.js
View File

@ -1,3 +1,4 @@
import { EXRCubeTexture } from '@babylonjs/core';
import { kernel } from './src/main.ts';
//初始化
@ -47,7 +48,7 @@ export const getAutoLoadModelList = async () => {
}
kernel.model.add({
modelId: model.id,
modelId: model.category,
modelUrl: model.file_url,
modelControlType: model.model_control_type,
@ -80,10 +81,7 @@ export const getPlacementZone = async (sku) => {
}
//执行事件
export const executeEvent = async (dropzone_data, sku) => {
const { wallName, index, transform } = dropzone_data;
const { position, rotation } = transform;
export const getEvent = async (dropzone_data, sku) => {
// 将模型放置到该区域
try {
const response = await fetch(`http://localhost:3001/api/product-configs/by-sku/${sku}`);
@ -94,55 +92,7 @@ export const executeEvent = async (dropzone_data, sku) => {
console.log('关联事件:', result.data.events);
// 使用 for...of 循环以支持 await
for (const event of result.data.events) {
if (event.event_type === 'change_model') {
console.log(event.target_data);
const { id, name, file_url, model_control_type, category } = event.target_data;
console.log('替换百叶模型:', event);
console.log('替换百叶模型类型:', category);
// 生成唯一的模型ID
const modelId = id + '_' + Date.now();
// 先记录模型放置(会自动处理替换逻辑)
kernel.dropZone.recordModelPlacement(wallName, index, modelId);
// 加载并放置模型
await kernel.model.add({
modelId: modelId,
modelUrl: file_url,
modelControlType: model_control_type,
drag: {
enable: true,
axis: Math.abs((rotation.y - 0)) < 90 ? 'x' : 'z',
step: 0.1,
},
transform: {
position: position,
rotation: rotation,
}
});
console.log(`百叶模型已放置为 ${name}`);
}
if (event.event_type === 'change_color') {
const materialName = event.material_name;
const { color, color_map_url, normal_map_url } = event.target_data;
console.log('替换百叶模型颜色:', event.target_data);
kernel.material.apply({
target: materialName,
modelId: modelId, // 指定模型ID只修改该模型的材质
albedoColor: color,
albedoTexture: color_map_url,
normalMap: normal_map_url,
});
console.log(`百叶模型颜色已替换为 ${color}`);
}
}
await executeEvent(dropzone_data, result)
} else {
console.log(`未查询到数据`);
}
@ -150,6 +100,112 @@ export const executeEvent = async (dropzone_data, sku) => {
console.error(`查询SKU配置或替换模型失败:`, error);
}
}
export const executeEvent = async (dropzone_data, result) => {
const { wallName, index, transform } = dropzone_data;
const { position, rotation } = transform;
for (const event of result.data.events) {
if (event.event_type === 'change_model') {
console.log(event.target_data);
const { id, name, file_url, model_control_type, category } = event.target_data;
console.log('替换百叶模型:', event);
console.log('替换百叶模型类型:', category);
// 生成唯一的模型ID
const modelId = id + '_' + Date.now();
// 先记录模型放置(会自动处理替换逻辑)
kernel.dropZone.recordModelPlacement(wallName, index, modelId);
console.log(Math.abs(rotation.y - 90) ,Math.abs(rotation.y - 90) > 5 ? 'x' : 'z' );
// 加载并放置模型
await kernel.model.add({
modelId: modelId,
modelUrl: file_url,
modelControlType: model_control_type,
drag: {
enable: true,
axis:rotation.y === 0 || rotation.y === 180 ? 'x' : 'z',
step: 0.1,
},
transform: {
position: position,
rotation: rotation,
}
});
console.log(`百叶模型已放置为 ${name}`);
}
if (event.event_type === 'change_color') {
const materialName = event.material_name;
const { color, color_map_url, normal_map_url } = event.target_data;
console.log('替换百叶模型颜色:', event.target_data);
kernel.material.apply({
target: materialName,
albedoColor: color,
albedoTexture: color_map_url,
normalMap: normal_map_url,
});
console.log(`百叶模型颜色已替换为 ${color}`);
}
}
}
export const executeEvent2 = async (result) => {
kernel.dropZone.clearZones();
kernel.model.removeAll();
for (const event of result.data.events) {
const { target_data } = event;
if (event.event_type === 'change_model') {
console.log(event.target_data);
const { id, name, file_url, model_control_type, category, placement_zone } = event.target_data;
console.log('替换百叶模型:', event);
console.log('替换百叶模型类型:', category);
if (placement_zone) {
const { alpha, border_color, color, show_border, thickness, walls } = placement_zone
kernel.dropZone.setData({
color: color,
alpha: +alpha,
thickness: thickness,
showBorder: !show_border,
borderColor: border_color,
walls: walls
});
}
// 加载并放置模型
await kernel.model.replace({
modelId: target_data.category,
modelUrl: target_data.file_url,
modelControlType: target_data.model_control_type,
})
console.log(`百叶模型已放置为 ${name}`);
}
if (event.event_type === 'change_color') {
const materialName = event.material_name;
const { color, color_map_url, normal_map_url } = event.target_data;
console.log('替换百叶模型颜色:', event.target_data);
kernel.material.apply({
target: materialName,
albedoColor: color,
albedoTexture: color_map_url,
normalMap: normal_map_url,
});
console.log(`百叶模型颜色已替换为 ${color}`);
}
}
}
export const getHotspot = async () => {