main.threejs.ts 17 KB

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