main.threejs.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. import * as THREE from 'three';
  2. import { animateCamera } from '/@/utils/threejs/util';
  3. import UseThree from '../../../../utils/threejs/useThree';
  4. import mainWindRect from './mainWind.threejs';
  5. import mainXjWindRect from './mainWind.xj.threejs';
  6. import mainLjWindRect from './mainWind.lj.threejs';
  7. import useEvent from '../../../../utils/threejs/useEvent';
  8. // import * as dat from 'dat.gui';
  9. // const gui = new dat.GUI();
  10. // gui.domElement.style = 'position:absolute;top:100px;left:10px;z-index:99999999999999';
  11. // 模型对象、 文字对象
  12. let model: UseThree | undefined, //
  13. group: THREE.Object3D | undefined,
  14. bgGroup: THREE.Object3D | undefined,
  15. mainWindObj: mainWindRect | undefined,
  16. mainXjWindObj: mainXjWindRect | undefined,
  17. mainLjWindObj: mainLjWindRect | undefined,
  18. modalType = 'mainWindRect',
  19. explosionVentClose = -1,
  20. explosionVentOpen = -1;
  21. const { mouseDownFn, mousemoveFn, mouseUpFn } = useEvent();
  22. // 打灯光
  23. const addLight = () => {
  24. const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
  25. directionalLight.position.set(63, 258, -261);
  26. model?.scene?.add(directionalLight);
  27. const pointLight6 = new THREE.PointLight(0xffffff, 1.5, 300);
  28. pointLight6.position.set(64, -12, 129);
  29. pointLight6.shadow.bias = 0.05;
  30. model?.scene?.add(pointLight6);
  31. //
  32. const pointLight7 = new THREE.PointLight(0xffffff, 1, 500);
  33. pointLight7.position.set(21, 64, 75);
  34. pointLight7.shadow.bias = -0.05;
  35. model?.scene?.add(pointLight7);
  36. };
  37. // 重置摄像头
  38. const resetCamera = () => {
  39. if (!model) return;
  40. model.camera?.position.set(-500, 0, 2000);
  41. model.orbitControls?.update();
  42. model.camera?.updateProjectionMatrix();
  43. };
  44. const setControls = () => {
  45. if (model && model.orbitControls) {
  46. model.orbitControls.panSpeed = 0.5;
  47. model.orbitControls.rotateSpeed = 0.5;
  48. model.orbitControls.maxPolarAngle = Math.PI / 3;
  49. model.orbitControls.minPolarAngle = Math.PI / 4;
  50. }
  51. };
  52. // 初始化事件
  53. const startAnimation = () => {
  54. // 定义鼠标点击事件
  55. model?.canvasContainer?.addEventListener('mousedown', mouseEvent.bind(null));
  56. model?.canvasContainer?.addEventListener('pointerup', mouseUp);
  57. };
  58. // 鼠标点击、松开事件
  59. const mouseEvent = (event) => {
  60. if (event.button == 0) {
  61. model?.canvasContainer?.addEventListener('mousemove', mousemove);
  62. mouseDownFn(<UseThree>model, <THREE.Object3D>group, event, (intersects) => {
  63. if (modalType === 'mainWindRect' && mainWindObj) {
  64. mainWindObj?.mousedownModel.call(mainWindObj, intersects);
  65. } else if (modalType === 'mainXjWindRect' && mainXjWindObj) {
  66. mainXjWindObj?.mousedownModel.call(mainXjWindObj, intersects);
  67. }
  68. });
  69. }
  70. };
  71. const mouseUp = () => {
  72. if (!model) return;
  73. mouseUpFn(model);
  74. model.canvasContainer?.removeEventListener('mousemove', mousemove);
  75. };
  76. const mousemove = () => {
  77. mousemoveFn();
  78. };
  79. /* 添加监控数据 */
  80. export const addText = () => {
  81. if (!mainWindObj) return;
  82. if (modalType === 'mainWindRect' && mainWindObj) {
  83. return mainWindObj.addCssText.call(mainWindObj);
  84. } else if (modalType === 'mainXjWindRect' && mainXjWindObj) {
  85. return mainXjWindObj.addCssText.call(mainXjWindObj);
  86. } else if (modalType === 'mainLjWindRect' && mainLjWindObj) {
  87. return mainLjWindObj.addCssText.call(mainLjWindObj);
  88. }
  89. };
  90. /* 刷新echarts曲线图 */
  91. export const resetEcharts = (selectData) => {
  92. if (!mainWindObj) return;
  93. if (modalType === 'mainWindRect' && mainWindObj) {
  94. return mainWindObj.addEcharts.call(mainWindObj);
  95. } else if (modalType === 'mainXjWindRect' && mainXjWindObj) {
  96. return mainXjWindObj.addEcharts.call(mainXjWindObj);
  97. } else if (modalType === 'mainLjWindRect' && mainLjWindObj) {
  98. return mainLjWindObj.addEcharts.call(mainLjWindObj);
  99. }
  100. };
  101. /**
  102. *
  103. * @param controlType //操作类型
  104. * @param deviceType // 设备类型,前后风机
  105. * @param frequencyVal // 频率
  106. * @param state // 启停状态
  107. * @param smokeDirection // 气流路径
  108. * @returns
  109. */
  110. export const play = (controlType, deviceType, frequencyVal?, state?, smokeDirection?) => {
  111. if (!mainWindObj) return;
  112. if (modalType === 'mainWindRect' && mainWindObj) {
  113. return mainWindObj.playSmoke.call(mainWindObj, controlType, deviceType, frequencyVal, state, smokeDirection);
  114. } else if (modalType === 'mainXjWindRect' && mainXjWindObj) {
  115. return mainXjWindObj.playSmoke.call(mainXjWindObj, controlType, deviceType, frequencyVal, state, smokeDirection);
  116. } else if (modalType === 'mainLjWindRect' && mainLjWindObj) {
  117. return mainLjWindObj.playSmoke.call(mainLjWindObj, controlType, deviceType, frequencyVal, state, smokeDirection);
  118. }
  119. };
  120. export const playAnimate1 = async (selectData, duration?) => {
  121. if (!mainWindObj) return;
  122. let mainObj: mainWindRect | mainXjWindRect | undefined;
  123. if (modalType === 'mainWindRect') {
  124. mainObj = mainWindObj;
  125. } else if (modalType === 'mainXjWindRect') {
  126. mainObj = mainXjWindObj;
  127. } else if (modalType === 'mainLjWindRect') {
  128. mainObj = mainLjWindObj;
  129. }
  130. if (selectData && mainObj) {
  131. if (selectData.Fan1WindowOpen !== undefined) {
  132. // 主风机水平窗开启
  133. if (selectData.Fan1WindowOpen == 1) mainObj?.openOrCloseWindow('front', 'openWindow');
  134. if (selectData.Fan1WindowOpen == 0) mainObj?.openOrCloseWindow('front', 'closeWindow');
  135. }
  136. if (selectData.Fan2WindowOpen !== undefined) {
  137. // 备风机水平窗开启
  138. if (selectData.Fan2WindowOpen == 1) mainObj?.openOrCloseWindow('back', 'openWindow');
  139. if (selectData.Fan2WindowOpen == 0) mainObj?.openOrCloseWindow('back', 'closeWindow');
  140. }
  141. if (selectData.Fan1ButterflyOpen !== undefined) {
  142. if (selectData.Fan1ButterflyOpen == 1) {
  143. // 主风机蝶阀打开
  144. mainObj.openOrCloseValve('front', 'open', duration);
  145. } else {
  146. // 主风机蝶阀关闭
  147. mainObj.openOrCloseValve('front', 'close', duration);
  148. }
  149. }
  150. if (selectData.Fan2ButterflyOpen !== undefined) {
  151. if (selectData.Fan2ButterflyOpen == 1) {
  152. // 主风机蝶阀打开
  153. mainObj.openOrCloseValve('back', 'open', duration);
  154. } else {
  155. // 主风机蝶阀关闭
  156. mainObj.openOrCloseValve('back', 'close', duration);
  157. }
  158. }
  159. if (selectData.Fan1FreqHz !== undefined) {
  160. // 主风机频率设置
  161. mainObj.resetSmokeParam('front', selectData.Fan1FreqHz, duration);
  162. }
  163. if (selectData.Fan2FreqHz !== undefined) {
  164. // 主风机频率设置
  165. mainObj.resetSmokeParam('back', selectData.Fan2FreqHz, duration);
  166. }
  167. if (selectData.Fan1StartStatus) {
  168. if (selectData.Fan1StartStatus == 1) {
  169. // 主风机开启
  170. mainObj.lookMotor('front', 'open', duration);
  171. // if (selectData.Fan1FreqForwardRun && selectData.Fan1FreqForwardRun == 1) {
  172. // // 主风机正转
  173. // await mainWindObj.setSmokeDirection('front', 'tubPositivePath');
  174. // } else if (selectData.Fan1FreqReverseRun && selectData.Fan1FreqReverseRun == 1) {
  175. // // 主风机反转
  176. // await mainWindObj.setSmokeDirection('front', 'tubInversePath');
  177. // }
  178. // 齿轮转动
  179. mainObj.startGearAnimation('front', 'open', 'tubPositivePath', selectData.Fan1FreqHz, duration);
  180. await mainObj.setSmokeDirection('front', 'tubPositivePath');
  181. if (!mainObj.frontSmoke?.frameId) mainObj?.frontSmoke?.startSmoke(duration);
  182. } else {
  183. mainObj?.lookMotor('front', 'close', duration);
  184. }
  185. }
  186. if (selectData.Fan2StartStatus) {
  187. if (selectData.Fan2StartStatus == 1) {
  188. // 备风机开启
  189. mainObj.lookMotor('back', 'open', duration);
  190. // if (selectData.Fan2FreqForwardRun && selectData.Fan2FreqForwardRun == 1) {
  191. // // 主风机正转
  192. // } else if (selectData.Fan2FreqReverseRun && selectData.Fan2FreqReverseRun == 1) {
  193. // // 主风机反转
  194. // }
  195. await mainObj.setSmokeDirection('back', 'tubPositivePath');
  196. if (!mainObj.backSmoke?.frameId) mainObj?.backSmoke?.startSmoke(duration);
  197. } else {
  198. mainObj?.lookMotor('back', 'close', duration);
  199. }
  200. }
  201. }
  202. };
  203. export const playAnimate = async (selectData, duration?) => {
  204. // if (Number(selectData.Fan1FreqHz) < 0) selectData.Fan1FreqHz = Math.abs(Number(selectData.Fan1FreqHz));
  205. // if (Number(selectData.Fan2FreqHz) < 0) selectData.Fan2FreqHz = Math.abs(Number(selectData.Fan2FreqHz));
  206. if (!mainWindObj) return;
  207. let mainObj: mainWindRect | mainXjWindRect | undefined;
  208. if (modalType === 'mainWindRect') {
  209. mainObj = mainWindObj;
  210. } else if (modalType === 'mainXjWindRect') {
  211. mainObj = mainXjWindObj;
  212. } else if (modalType === 'mainLjWindRect') {
  213. mainObj = mainLjWindObj;
  214. }
  215. if (selectData && mainObj) {
  216. if (selectData['Fan1FreqHz'] == undefined || selectData['Fan1FreqHz'] == null || selectData['Fan1FreqHz'] == '') selectData['Fan1FreqHz'] = 50;
  217. if (selectData['Fan2FreqHz'] == undefined || selectData['Fan2FreqHz'] == null || selectData['Fan2FreqHz'] == '') selectData['Fan2FreqHz'] = 50;
  218. mainObj.resetSmokeParam('front', selectData.Fan2FreqHz, duration);
  219. mainObj.resetSmokeParam('back', selectData.Fan1FreqHz, duration);
  220. if (selectData.Fan2StartStatus == 1) {
  221. // 主风机开启
  222. mainObj?.lookMotor('front', 'open', duration);
  223. mainObj?.openOrCloseValve('front', 'open', duration);
  224. // 1. 已经运行,首次切入动画
  225. // 2. 在页面上,切换动画
  226. if (selectData.Fan2FreqForwardRun == 1 && selectData.Fan2FreqReverseRun == 0) {
  227. // 主风机正转
  228. if (mainObj['airChu2'] && !mainObj['airChu2'].visible) {
  229. mainObj['airJin1'].visible = false;
  230. mainObj['airJin2'].visible = false;
  231. mainObj['airChu1'].visible = false;
  232. mainObj['airChu2'].visible = true;
  233. }
  234. mainObj.startGearAnimation('front', 'open', 'tubPositivePath', selectData.Fan2FreqHz, duration);
  235. await mainObj.setSmokeDirection('front', 'tubPositivePath');
  236. } else if (selectData.Fan2FreqReverseRun == 1 && selectData.Fan2FreqForwardRun == 0) {
  237. // 主风机反转
  238. if (mainObj['airJin2'] && !mainObj['airJin2'].visible) {
  239. mainObj['airJin1'].visible = false;
  240. mainObj['airJin2'].visible = true;
  241. mainObj['airChu1'].visible = false;
  242. mainObj['airChu2'].visible = false;
  243. }
  244. mainObj.startGearAnimation('front', 'open', 'tubInversePath', selectData.Fan2FreqHz, duration);
  245. await mainObj.setSmokeDirection('front', 'tubInversePath');
  246. } else {
  247. // 默认主风机正转
  248. if (mainObj['airChu2'] && !mainObj['airChu2'].visible) {
  249. mainObj['airJin1'].visible = false;
  250. mainObj['airJin2'].visible = false;
  251. mainObj['airChu1'].visible = false;
  252. mainObj['airChu2'].visible = true;
  253. }
  254. mainObj.startGearAnimation('front', 'open', 'tubPositivePath', selectData.Fan2FreqHz, duration);
  255. await mainObj.setSmokeDirection('front', 'tubPositivePath');
  256. }
  257. if (!mainObj?.frontSmoke?.frameId) mainObj?.frontSmoke?.startSmoke(duration);
  258. } else {
  259. // 主风机停止
  260. mainObj.closeDevice('front');
  261. }
  262. if (selectData.Fan1StartStatus == 1) {
  263. // 主风机开启
  264. mainObj.lookMotor('back', 'open', duration);
  265. mainObj.openOrCloseValve('back', 'open', duration);
  266. // 1. 已经运行,首次切入动画
  267. // 2. 在页面上,切换动画
  268. if (selectData.Fan1FreqForwardRun == 1 && selectData.Fan1FreqReverseRun == 0) {
  269. // 主风机正转
  270. // 主风机正转
  271. if (mainObj['airChu1'] && !mainObj['airChu1'].visible) {
  272. mainObj['airJin1'].visible = false;
  273. mainObj['airJin2'].visible = false;
  274. mainObj['airChu1'].visible = true;
  275. mainObj['airChu2'].visible = false;
  276. }
  277. mainObj.startGearAnimation('back', 'open', 'tubPositivePath', selectData.Fan1FreqHz, duration);
  278. await mainObj.setSmokeDirection('back', 'tubPositivePath');
  279. } else if (selectData.Fan1FreqReverseRun == 1 && selectData.Fan1FreqForwardRun == 0) {
  280. // 主风机反转
  281. if (mainObj['airJin1'] && !mainObj['airJin1'].visible) {
  282. mainObj['airJin1'].visible = true;
  283. mainObj['airJin2'].visible = false;
  284. mainObj['airChu1'].visible = false;
  285. mainObj['airChu2'].visible = false;
  286. }
  287. mainObj.startGearAnimation('back', 'open', 'tubInversePath', selectData.Fan1FreqHz, duration);
  288. await mainObj.setSmokeDirection('back', 'tubInversePath');
  289. } else {
  290. if (mainObj['airChu1'] && !mainObj['airChu1'].visible) {
  291. mainObj['airJin1'].visible = false;
  292. mainObj['airJin2'].visible = false;
  293. mainObj['airChu1'].visible = true;
  294. mainObj['airChu2'].visible = false;
  295. }
  296. mainObj.startGearAnimation('back', 'open', 'tubPositivePath', selectData.Fan1FreqHz, duration);
  297. await mainObj.setSmokeDirection('back', 'tubPositivePath');
  298. }
  299. if (!mainObj?.backSmoke?.frameId) mainObj?.backSmoke?.startSmoke(duration);
  300. } else {
  301. // 主风机停止
  302. mainObj.closeDevice('back');
  303. }
  304. // 防爆门动画
  305. if (modalType === 'mainWindRect' && selectData['ExplosionVentOpen'] == 1 && explosionVentOpen !== 1) {
  306. if (explosionVentOpen == -1) {
  307. // 直接打开
  308. mainObj.playAnimation('open', 0);
  309. } else {
  310. mainObj.playAnimation('open');
  311. }
  312. explosionVentOpen = 1;
  313. explosionVentClose = 0;
  314. }
  315. if (modalType === 'mainWindRect' && selectData['ExplosionVentClose'] == 1 && explosionVentClose !== 1) {
  316. if (explosionVentOpen == -1) {
  317. // 直接关闭
  318. mainObj.playAnimation('close', 0);
  319. } else {
  320. mainObj.playAnimation('close');
  321. }
  322. explosionVentClose = 1;
  323. explosionVentOpen = 0;
  324. }
  325. }
  326. };
  327. // 切换风机类型
  328. export const setModelType = (type) => {
  329. if (!model) return;
  330. modalType = type;
  331. return new Promise((resolve) => {
  332. // 停止气流动画
  333. mainWindObj?.stopSmoke();
  334. mainXjWindObj?.stopSmoke();
  335. if (group) model?.scene?.remove(group);
  336. if (modalType === 'mainWindRect' && mainWindObj && mainWindObj.group) {
  337. mainXjWindObj?.clearCssText();
  338. (<UseThree>model).startAnimation = mainWindObj.render.bind(mainWindObj);
  339. group = mainWindObj.group;
  340. setTimeout(async () => {
  341. resolve(null);
  342. // const position = mainWindObj.group.position;
  343. const position = new THREE.Vector3(-1.0127, -3.4717, -7.864);
  344. const oldCameraPosition = { x: -332.39, y: 283.47, z: 438.61 };
  345. await animateCamera(
  346. oldCameraPosition,
  347. { x: -3.41, y: -29.01, z: 8.84 },
  348. { x: -1.7927, y: 70.8399, z: 120.8451 },
  349. { x: position.x, y: position.y, z: position.z },
  350. model,
  351. 0.8
  352. );
  353. if (group) model?.scene?.add(group);
  354. }, 300);
  355. } else if (modalType === 'mainXjWindRect' && mainXjWindObj && mainXjWindObj.group) {
  356. mainWindObj?.clearCssText();
  357. (<UseThree>model).startAnimation = mainXjWindObj.render.bind(mainXjWindObj);
  358. group = mainXjWindObj.group;
  359. setTimeout(async () => {
  360. resolve(null);
  361. // const position = mainWindObj.group.position;
  362. const position = new THREE.Vector3(12.96, 23.17, -23.16);
  363. const oldCameraPosition = { x: -332.39, y: 283.47, z: 438.61 };
  364. await animateCamera(
  365. oldCameraPosition,
  366. { x: -3.41, y: -29.01, z: 8.84 },
  367. { x: 12.09, y: 105.51, z: 119.45 },
  368. { x: position.x, y: position.y, z: position.z },
  369. model,
  370. 0.8
  371. );
  372. if (group) model?.scene?.add(group);
  373. }, 300);
  374. } else if (modalType === 'mainLjWindRect' && mainLjWindObj && mainLjWindObj.group) {
  375. mainWindObj?.clearCssText();
  376. (<UseThree>model).startAnimation = mainLjWindObj.render.bind(mainLjWindObj);
  377. group = mainLjWindObj.group;
  378. setTimeout(async () => {
  379. resolve(null);
  380. // const position = mainWindObj.group.position;
  381. const position = new THREE.Vector3(-32.98, -4.15, -8.14);
  382. const oldCameraPosition = { x: -332.39, y: 283.47, z: 438.61 };
  383. await animateCamera(
  384. oldCameraPosition,
  385. { x: -3.41, y: -29.01, z: 8.84 },
  386. { x: -33.9, y: 82.51, z: 141.97 },
  387. { x: position.x, y: position.y, z: position.z },
  388. model,
  389. 0.8
  390. );
  391. if (group) model?.scene?.add(group);
  392. }, 300);
  393. }
  394. });
  395. };
  396. export const mountedThree = (playerVal1) => {
  397. return new Promise(async (resolve) => {
  398. model = new UseThree('#main3D', '#main3DCSS');
  399. model.setEnvMap('test1.hdr');
  400. model.renderer.toneMappingExposure = 1.0;
  401. if (model.renderer) {
  402. model.renderer.sortObjects = true;
  403. }
  404. addLight();
  405. setControls();
  406. resetCamera();
  407. model.setGLTFModel(['bg']).then(async (gltf) => {
  408. bgGroup = gltf[0] as THREE.Object3D;
  409. bgGroup.position.set(3.43, 27.13, 22.0);
  410. model?.scene?.add(bgGroup);
  411. mainWindObj = new mainWindRect(model, playerVal1);
  412. await mainWindObj.mountedThree();
  413. mainXjWindObj = new mainXjWindRect(model, playerVal1);
  414. await mainXjWindObj.mountedThree();
  415. mainLjWindObj = new mainLjWindRect(model, playerVal1);
  416. await mainLjWindObj.mountedThree();
  417. model?.animate();
  418. resolve(null);
  419. if (mainWindObj.airJin1) mainWindObj.airJin1.visible = false;
  420. if (mainWindObj.airJin2) mainWindObj.airJin2.visible = false;
  421. if (mainWindObj.airChu1) mainWindObj.airChu1.visible = false;
  422. if (mainWindObj.airChu2) mainWindObj.airChu2.visible = false;
  423. if (mainXjWindObj.airJin1) mainXjWindObj.airJin1.visible = false;
  424. if (mainXjWindObj.airJin2) mainXjWindObj.airJin2.visible = false;
  425. if (mainXjWindObj.airChu1) mainXjWindObj.airChu1.visible = false;
  426. if (mainLjWindObj.airChu2) mainLjWindObj.airChu2.visible = false;
  427. if (mainLjWindObj.airJin1) mainLjWindObj.airJin1.visible = false;
  428. if (mainLjWindObj.airJin2) mainLjWindObj.airJin2.visible = false;
  429. if (mainLjWindObj.airChu1) mainLjWindObj.airChu1.visible = false;
  430. if (mainLjWindObj.airChu2) mainLjWindObj.airChu2.visible = false;
  431. });
  432. startAnimation();
  433. });
  434. };
  435. export const destroy = () => {
  436. if (model) {
  437. model.isRender = false;
  438. console.log('场景销毁前信息----------->', model.renderer?.info);
  439. mainWindObj?.destroy();
  440. mainWindObj = undefined;
  441. mainXjWindObj?.destroy();
  442. mainXjWindObj = undefined;
  443. model.clearGroup(bgGroup);
  444. bgGroup = undefined;
  445. group = undefined;
  446. model.destroy();
  447. model = undefined;
  448. }
  449. };