import * as THREE from 'three'; import UseThree from '../../../../utils/threejs/useThree'; import FanLocal from './fanLocal.threejs.base'; import FanLocalDual from './fanLocalDual.threejs.base'; import { animateCamera } from '/@/utils/threejs/util'; import useEvent from '../../../../utils/threejs/useEvent'; /** 模型总控制器 */ let model: UseThree; /** 当前展示的具体模型的 Object3D 对象 */ let group: THREE.Object3D; /** 当前展示的具体模型及状态组成的判断唯一性的key */ let cacheKey: string = ''; /** 具体模型内容列表,包含此模型总控制器下的所有可用的具体模型内容 */ const modelContextList: { /** 当前模型类型,在控制器下有多个具体模型时分辨它们 */ type: string; /** 模型的具体内容,即负责模型导入、绘制的上下文对象,一个控制器可以新建多个 */ context?: FanLocal | FanLocalDual; }[] = []; const { mouseDownFn } = useEvent(); /** 分发鼠标事件到具体实现方法 */ function dispatchMouseEvent(event) { if (event.button == 0 && model && group) { mouseDownFn(model, group, event, () => {}); } } /** 为模型控制器设置非默认的摄像头位置 */ function setCamera() { // if (!model || !model.camera) return; // model.camera.position.set(0, -2000, 1000); // model.camera.far = 10000; // model.orbitControls?.update(); // model.camera.updateProjectionMatrix(); } /** 初始化模型CSS展示框的鼠标事件,应该在模型总控制器初始化后调用 */ function initEventListender() { if (!model) return; model.canvasContainer?.addEventListener('mousedown', (e) => dispatchMouseEvent(e)); // model.orbitControls?.addEventListener('change', () => render()); } /** 渲染并更新总模型 */ // function render() { // if (model && model.isRender && model.renderer) { // // model.animationId = requestAnimationFrame(render); // model.css3dRender?.render(model.scene as THREE.Scene, model.camera as THREE.PerspectiveCamera); // model.renderer.render(model.scene as THREE.Scene, model.camera as THREE.PerspectiveCamera); // model.stats?.update(); // } // } /** 刷新(再渲染)总模型 */ // export function refreshModal() { // render(); // // modelContextList.forEach((item) => { // // if (item.context) { // // item.context.render(); // // } // // }); // } /** 设置模型类型并切换,不同的类型通常对应不同的具体模型,在模型总控制器下的具体模型会根据传入的参数彼此交互、切换 */ export function setModelType(modelType: 'fanLocal' | 'fanLocalDual' | string, subModelType: string) { return new Promise((resolve, reject) => { if (!model) return reject('模型控制器未初始化'); // 判断是否是同一个/类模型 if (cacheKey === `${modelType}-${subModelType}`) return resolve(null); cacheKey = `${modelType}-${subModelType}`; modelContextList.forEach(({ type, context }) => { if (!context || !context.group) return; // 先把模型相关的内容隐藏,另起的隐藏子元素的代码是为了隐藏 CSS 元素 context.group.visible = false; context.group.children.forEach((e) => { e.visible = false; }); if (modelType === type) { group = context?.group as THREE.Object3D; context.setModelType(subModelType); setTimeout(async () => { // 还没添加到控制器的添加进去 if (!model.scene?.getObjectByName(group.name) && group) { model.scene?.add(group); } group.visible = true; group.children.forEach((e) => { e.visible = true; }); const oldCameraPosition = { x: -693, y: 474, z: 398 }; const position = { x: 14.826074594663222, y: 16.901762713393836, z: 36.459944037951004 }; await animateCamera( oldCameraPosition, { x: 0, y: 0, z: 0 }, { x: position.x, y: position.y, z: position.z }, { x: 0, y: 0, z: 0 }, model, 0.8 ); resolve(null); }, 400); } }); }); } /** 挂载模型控制器,sceneSelctor表示放置模型的元素选择器,cssSelectors表示放置类似详情框的元素选择器,其中第一项需要是根元素选择器 */ export function mountedThree(sceneSelctor: string, cssSelectors: string[]) { return new Promise(async (resolve) => { const [rootSelector] = cssSelectors; model = new UseThree(sceneSelctor, rootSelector); model.setEnvMap('test1.hdr'); /** @ts-ignore-next-line */ model.renderer.toneMappingExposure = 1.0; if (model.renderer) { model.renderer.sortObjects = true; } const model1 = new FanLocal(model); await model1.mountedThree(); modelContextList.push({ type: 'fanLocal', context: model1, }); const model2 = new FanLocalDual(model); await model2.mountedThree(); modelContextList.push({ type: 'fanLocalDual', context: model2, }); initEventListender(); setCamera(); model.animate(); resolve(null); }); } export const destroy = () => { if (!model) return; model.isRender = false; modelContextList.forEach((item) => { if (item.context) item.context.destroy(); }); model.destroy(); }; // 为了兼容性而添加的方法导出 export function addText(d) { if (modelContextList[0]) { // @ts-ignore modelContextList[0].context?.addText(d); } } export function addCssText() { if (modelContextList[0]) { // @ts-ignore modelContextList[0].context?.addCssText(); } } export function playSmoke(d) { if (modelContextList[0]) { // @ts-ignore modelContextList[0].context?.playSmoke(d); } }