import * as THREE from 'three'; import UseThree from '../../../../utils/threejs/useThree'; import singleWindow from './dandaoFcBetZh.threejs'; import { animateCamera } from '/@/utils/threejs/util'; import useEvent from '../../../../utils/threejs/useEvent'; // import * as dat from 'dat.gui'; // const gui = new dat.GUI(); // gui.domElement.style = 'position:absolute;top:100px;left:10px;z-index:99999999999999'; // 模型对象、 文字对象 let model: UseThree, singleWindowObj, group: THREE.Object3D, windowType = 'singleWindow'; const rotationParam = { frontDeg0: 0, // 前门初始 frontDeg1: 0, // 前门目标 backDeg0: 0, // 后门初始 backDeg1: 0, // 后门目标 }; const { mouseDownFn } = useEvent(); // 打灯光 const addLight = () => { if (!model || !model.scene) return; const directionalLight = new THREE.DirectionalLight(0xffffff, 1); directionalLight.position.set(-110, 150, 647); model.scene?.add(directionalLight); // directionalLight.target = group; const pointLight2 = new THREE.PointLight(0xffffff, 1, 150); pointLight2.position.set(-101, 34, 16); pointLight2.shadow.bias = 0.05; model.scene.add(pointLight2); const pointLight3 = new THREE.PointLight(0xffffff, 1, 150); pointLight3.position.set(19, 25, -7); pointLight3.shadow.bias = 0.05; model.scene.add(pointLight3); const pointLight6 = new THREE.PointLight(0xffffff, 1, 300); pointLight6.position.set(51, 51, 9); pointLight6.shadow.bias = 0.05; model.scene.add(pointLight6); }; // 初始化左右摇摆动画 const startAnimation = () => { // 定义鼠标点击事件 model.canvasContainer?.addEventListener('mousedown', mouseEvent.bind(null)); model.canvasContainer?.addEventListener('pointerup', (event) => { event.stopPropagation(); // 单道、 双道 if (windowType === 'singleWindow' && singleWindowObj) { singleWindowObj.mouseUpModel.call(singleWindowObj); } }); }; // 鼠标点击、松开事件 const mouseEvent = (event) => { if (event.button == 0) { mouseDownFn(model, group, event, (intersects) => { if (windowType === 'singleWindow' && singleWindowObj) { singleWindowObj.mousedownModel.call(singleWindowObj, intersects); } }); } }; export const addMonitorText = (selectData) => { if (windowType === 'singleWindow' && singleWindowObj) { return singleWindowObj.addMonitorText.call(singleWindowObj, selectData); } }; export function computePlay(data, maxarea, isFirst = false) { if (windowType === 'doubleWindow' || windowType === 'singleWindow') { if (!maxarea) maxarea = 90; rotationParam.frontDeg0 = (90 / maxarea) * Number(isFirst ? 0 : data.frontArea); rotationParam.backDeg0 = (90 / maxarea) * Number(isFirst ? 0 : data.rearArea); rotationParam.frontDeg1 = (90 / maxarea) * Number(data.frontArea) || 0; rotationParam.backDeg1 = (90 / maxarea) * Number(data.rearArea) || 0; if (!rotationParam.frontDeg1 && !rotationParam.backDeg1) { // 当返回值有误时默认关闭 play(rotationParam, 0); } else { if (data.nwindownum == 1 || data.nwindownum == 2) { setTimeout(() => { play(rotationParam, 1); }, 0); } if (data.nwindownum == 2) { setTimeout(() => { play(rotationParam, 2); }, 0); } } } else if (windowType === 'singleXkWindow') { const acosToAngle = (cosValue) => { cosValue = Math.max(Math.min(cosValue, 1), -1); // 计算角度 return Math.asin(cosValue) * (180 / Math.PI); }; const sina = Math.sqrt((Math.sin((78 * Math.PI) / 180) ** 2 * parseFloat(data.frontArea)) / parseFloat(maxarea)); const angleInRadians = acosToAngle(sina); rotationParam.frontDeg1 = angleInRadians; if (!rotationParam.frontDeg1 && !rotationParam.backDeg1) { // 当返回值有误时默认关闭 play(rotationParam, 0); } else { setTimeout(() => { play(rotationParam, 1); }, 0); } } } export const play = (rotationParam, flag) => { if (windowType === 'singleWindow' && singleWindowObj) { return singleWindowObj.play.call(singleWindowObj, rotationParam, flag); } }; // 切换风窗类型 export const setModelType = (type) => { // if (!model || !model.scene) return; windowType = type; return new Promise((resolve) => { // 显示双道风窗 if (windowType === 'singleWindow') { // 显示单道风窗 model.startAnimation = singleWindowObj.render.bind(singleWindowObj); model.scene?.remove(group); group = singleWindowObj.group; const oldCameraPosition = { x: 100, y: 0, z: 10 }; model.scene?.add(singleWindowObj.group); setTimeout(async () => { resolve(null); await animateCamera(oldCameraPosition, { x: 0, y: 0, z: 0 }, { x: 66.257, y: 57.539, z: 94.313 }, { x: 0, y: 0, z: 0 }, model); }, 1000); } }); }; export const mountedThree = (playerDom) => { return new Promise(async (resolve) => { model = new UseThree('#window3D'); if (!model || !model.renderer || !model.camera) return; model.setEnvMap('test1.hdr'); model.camera.position.set(100, 0, 1000); singleWindowObj = new singleWindow(model); singleWindowObj.mountedThree(playerDom); model.animate(); addLight(); startAnimation(); resolve(null); }); }; export const destroy = () => { if (model) { model.isRender = false; console.log('场景销毁前信息----------->', model.renderer?.info); model.isRender = false; singleWindowObj.destroy(); model.destroy(); model = null; group = null; singleWindowObj = null; } };