243 lines
8.2 KiB
JavaScript
243 lines
8.2 KiB
JavaScript
import { EXRCubeTexture } from '@babylonjs/core';
|
|
import { kernel } from './src/main.ts';
|
|
|
|
//初始化
|
|
export const init = async () => {
|
|
const config = {
|
|
container: document.querySelector('#renderDom'),
|
|
modelUrlList: [],
|
|
env: { envPath: 'https://sdk.zguiy.com/resurces/hdr/hdr.env', intensity: 1.2, rotationY: 0.3, background: true },
|
|
gizmo: {
|
|
position: true,
|
|
rotation: true,
|
|
scale: true
|
|
},
|
|
outline: {
|
|
enable: true,
|
|
color: "#2196F3",
|
|
thickness: 1,
|
|
occlusionStrength: 0.1,
|
|
occlusionThreshold: 0.0002
|
|
}
|
|
};
|
|
kernel.init(config);
|
|
|
|
|
|
|
|
}
|
|
|
|
//初始化加载模型
|
|
export const getAutoLoadModelList = async () => {
|
|
const response = await fetch('http://localhost:3001/api/models/auto-load/list')
|
|
const data = await response.json()
|
|
const models = data.data // 这就是模型列表
|
|
|
|
models.forEach(model => {
|
|
console.log(model.placement_zone);
|
|
if (model.placement_zone) {
|
|
const { alpha, border_color, color, show_border, thickness, walls } = model.placement_zone
|
|
kernel.dropZone.setData({
|
|
|
|
color: color,
|
|
alpha: +alpha,
|
|
thickness: thickness,
|
|
showBorder: !show_border,
|
|
borderColor: border_color,
|
|
walls: walls
|
|
});
|
|
}
|
|
|
|
kernel.model.add({
|
|
modelId: model.category,
|
|
modelUrl: model.file_url,
|
|
modelControlType: model.model_control_type,
|
|
|
|
});
|
|
})
|
|
}
|
|
|
|
//获取放置区域
|
|
export const getPlacementZone = async (sku) => {
|
|
const response = await fetch(`http://localhost:3001/api/product-configs/by-sku/${sku}`);
|
|
const result = await response.json();
|
|
if (result.code === 200) {
|
|
// await initPlacementZoneConfig();
|
|
const { enable_placement_zone, wall_divisions } = result.data;
|
|
// const {position_x, position_y, position_z} = data;
|
|
if (enable_placement_zone && wall_divisions != undefined) {
|
|
|
|
// 只清除旧的放置区域网格,不清除模型
|
|
kernel.dropZone.clearZones();
|
|
const divisions = wall_divisions.map(wall => ({
|
|
name: wall.name, // 获取最后一个下划线后的部分
|
|
divisions: wall.divisions
|
|
}))
|
|
|
|
kernel.dropZone.updateDivisions(divisions);
|
|
// 显示放置区域
|
|
kernel.dropZone.show();
|
|
}
|
|
}
|
|
}
|
|
|
|
//执行事件
|
|
export const getEvent = async (dropzone_data, sku) => {
|
|
// 将模型放置到该区域
|
|
try {
|
|
const response = await fetch(`http://localhost:3001/api/product-configs/by-sku/${sku}`);
|
|
const result = await response.json();
|
|
|
|
if (result.code === 200 && result.data) {
|
|
console.log('SKU配置数据:', result.data);
|
|
console.log('关联事件:', result.data.events);
|
|
|
|
// 使用 for...of 循环以支持 await
|
|
await executeEvent(dropzone_data, result)
|
|
} else {
|
|
console.log(`未查询到数据`);
|
|
}
|
|
} catch (error) {
|
|
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 () => {
|
|
try {
|
|
// 从后端获取激活状态的热点列表
|
|
const response = await fetch('http://localhost:3001/api/hotspots?status=active&page=1&pageSize=100');
|
|
const result = await response.json();
|
|
|
|
if (result.code === 200 && result.data.list.length > 0) {
|
|
// 将后端数据转换为 SDK 需要的格式
|
|
const hotspots = result.data.list.map(item => ({
|
|
id: item.id,
|
|
type: 'hotspot',
|
|
name: item.name,
|
|
meshName: item.name, // 可以根据实际情况调整
|
|
icon: item.image_url,
|
|
position: [item.position_x, item.position_y, item.position_z],
|
|
radius: item.radius,
|
|
color: "#000000",
|
|
payload: {
|
|
skus: item.skus || [],
|
|
},
|
|
}));
|
|
|
|
// 渲染热点
|
|
kernel.hotspot.render(hotspots);
|
|
console.log('热点渲染成功:', hotspots);
|
|
} else {
|
|
console.log('没有可用的热点数据');
|
|
}
|
|
} catch (error) {
|
|
console.error('获取热点数据失败:', error);
|
|
}
|
|
}
|