gate.threejs.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. import * as THREE from 'three';
  2. import UseThree from '../../../../utils/threejs/useThree';
  3. import Fm1 from './gate.threejs.yy';
  4. import Fm3 from './gate.threejs.qd';
  5. import FmXR from './gate.threejs.xr';
  6. import Fm2 from './gate.threejs.three';
  7. import FmTwoSs from './gate.threejs.two.ss';
  8. import { animateCamera } from '/@/utils/threejs/util';
  9. import useEvent from '../../../../utils/threejs/useEvent';
  10. import { useGlobSetting } from '/@/hooks/setting';
  11. // 模型对象、 文字对象
  12. let model,
  13. fm1, //液压风门
  14. fm2, //三道风门
  15. fm3, //气动风门
  16. fmXr: FmXR, //行人风门
  17. fmTwoSs, //
  18. group: THREE.Object3D,
  19. fmType = '';
  20. const { mouseDownFn } = useEvent();
  21. // 初始化左右摇摆动画
  22. const startAnimation = () => {
  23. // 定义鼠标点击事件
  24. model.canvasContainer?.addEventListener('mousedown', mouseEvent.bind(null));
  25. model.canvasContainer?.addEventListener('pointerup', (event) => {
  26. event.stopPropagation();
  27. // 单道、 双道
  28. if (fmType === 'fm1') {
  29. fm1?.mouseUpModel.call(fm1);
  30. } else if (fmType === 'fm2') {
  31. fm2?.mouseUpModel.call(fm2);
  32. } else if (fmType === 'fm3') {
  33. fm3?.mouseUpModel.call(fm3);
  34. } else if (fmType === 'fmXr') {
  35. fmXr?.mouseUpModel.call(fmXr);
  36. } else if (fmType === 'fmTwoSs') {
  37. fmTwoSs?.mouseUpModel.call(fmTwoSs);
  38. }
  39. });
  40. };
  41. // 鼠标点击、松开事件
  42. const mouseEvent = (event) => {
  43. if (event.button == 0) {
  44. mouseDownFn(model, group, event, (intersects) => {
  45. if (fmType === 'fm1' && fm1) {
  46. fm1?.mousedownModel.call(fm1, intersects);
  47. } else if (fmType === 'fm2' && fm2) {
  48. fm2?.mousedownModel.call(fm2, intersects);
  49. } else if (fmType === 'fm3' && fm3) {
  50. fm3?.mousedownModel.call(fm3, intersects);
  51. } else if (fmType === 'fmXr' && fmXr) {
  52. fmXr?.mousedownModel.call(fmXr, intersects);
  53. } else if (fmType === 'fmTwoSs' && fmTwoSs) {
  54. fmTwoSs?.mousedownModel.call(fmTwoSs, intersects);
  55. }
  56. });
  57. // console.log('摄像头控制信息', model.orbitControls, model.camera);
  58. }
  59. };
  60. export const addMonitorText = (selectData) => {
  61. if (fmType === 'fm1' && fm1) {
  62. return fm1?.addMonitorText.call(fm1, selectData);
  63. } else if (fmType === 'fm2' && fm2) {
  64. return fm2?.addMonitorText.call(fm2, selectData);
  65. } else if (fmType === 'fm3' && fm3) {
  66. return fm3?.addMonitorText.call(fm3, selectData);
  67. } else if (fmType === 'fmXr' && fmXr) {
  68. return fmXr?.addMonitorText.call(fmXr, selectData);
  69. } else if (fmType === 'fmTwoSs' && fmTwoSs) {
  70. return fmTwoSs?.addMonitorText.call(fmTwoSs, selectData);
  71. }
  72. };
  73. export const deviceDetailCard = () => {
  74. if (fmType === 'fm1') {
  75. return fm1?.deviceDetailCard.call(fm1);
  76. } else if (fmType === 'fm3') {
  77. return fm3?.deviceDetailCard.call(fm3);
  78. } else if (fmType === 'fmXr') {
  79. return fmXr?.deviceDetailCard.call(fmXr);
  80. } else {
  81. // return fm2.addMonitorText.call(fm2);
  82. }
  83. };
  84. export const play = (handlerState, flag?) => {
  85. if (fmType === 'fm1' && fm1) {
  86. return fm1.play.call(fm1, handlerState, flag);
  87. } else if (fmType === 'fm2' && fm2) {
  88. return fm2.play.call(fm2, handlerState, flag);
  89. } else if (fmType === 'fm3' && fm3) {
  90. return fm3.play.call(fm3, handlerState, flag);
  91. } else if (fmType === 'fmXr' && fmXr) {
  92. return fmXr.play.call(fmXr, handlerState, flag);
  93. } else if (fmType === 'fmTwoSs' && fmTwoSs) {
  94. return fmTwoSs.play.call(fmTwoSs, handlerState, flag);
  95. }
  96. };
  97. // 切换风门类型
  98. export const setModelType = (type) => {
  99. fmType = type;
  100. return new Promise((resolve) => {
  101. // 暂停风门1动画
  102. if (fmType === 'fm1' && fm1 && fm1.group) {
  103. if (fm1.clipActionArr.frontDoor && fm1.clipActionArr.backDoor) {
  104. fm1.clipActionArr.frontDoor.reset();
  105. fm1.clipActionArr.frontDoor.time = 0.5;
  106. fm1.clipActionArr.backDoor.reset();
  107. fm1.clipActionArr.backDoor.time = 0.5;
  108. fm1.clipActionArr.frontDoor.stop();
  109. fm1.clipActionArr.backDoor.stop();
  110. }
  111. if (fm1.frontDamperOpenMesh) fm1.frontDamperOpenMesh.visible = false;
  112. if (fm1.frontDamperClosedMesh) fm1.frontDamperClosedMesh.visible = true;
  113. if (fm1.backDamperOpenMesh) fm1.backDamperOpenMesh.visible = false;
  114. if (fm1.backDamperClosedMesh) fm1.backDamperClosedMesh.visible = true;
  115. model.scene.remove(group);
  116. model.startAnimation = fm1.render.bind(fm1);
  117. group = fm1.group;
  118. group.rotation.y = 0;
  119. const oldCameraPosition = { x: -1000, y: 100, z: 500 };
  120. setTimeout(async () => {
  121. resolve(null);
  122. model.scene.add(fm1.group);
  123. await animateCamera(
  124. oldCameraPosition,
  125. { x: 0, y: 0, z: 0 },
  126. { x: 50.99, y: 69.32, z: 93.61 },
  127. { x: -10.04, y: -14.38, z: -31.4 },
  128. model,
  129. 0.8
  130. );
  131. }, 300);
  132. } else if (fmType === 'fm2' && fm2 && fm2.group) {
  133. if (fm2.clipActionArr.frontDoor && fm2.clipActionArr.backDoor) {
  134. fm2.clipActionArr.frontDoor.reset();
  135. fm2.clipActionArr.frontDoor.time = 0.5;
  136. fm2.clipActionArr.backDoor.reset();
  137. fm2.clipActionArr.backDoor.time = 0.5;
  138. fm2.clipActionArr.centerDoor.reset();
  139. fm2.clipActionArr.centerDoor.time = 0.5;
  140. fm2.clipActionArr.frontDoor.stop();
  141. fm2.clipActionArr.backDoor.stop();
  142. fm2.clipActionArr.centerDoor.stop();
  143. }
  144. // 显示单道风窗
  145. model.startAnimation = fm2.render.bind(fm2);
  146. model.scene.remove(group);
  147. group = fm2.group;
  148. const oldCameraPosition = { x: -761, y: 569, z: 871 };
  149. setTimeout(async () => {
  150. resolve(null);
  151. model.scene.add(fm2.group);
  152. const position = { x: -2.28, y: -0.91, z: -5.68 };
  153. await animateCamera(
  154. oldCameraPosition,
  155. { x: -2.27, y: -0.91, z: -5.67 },
  156. { x: 66.257, y: 57.539, z: 94.313 },
  157. { x: position.x, y: position.y, z: position.z },
  158. model,
  159. 0.6
  160. );
  161. }, 300);
  162. } else if (fmType === 'fm3' && fm3 && fm3.group) {
  163. if (fm3.clipActionArr.frontDoor && fm3.clipActionArr.backDoor) {
  164. fm3.clipActionArr.frontDoor.reset();
  165. fm3.clipActionArr.frontDoor.time = 0.5;
  166. fm3.clipActionArr.backDoor.reset();
  167. fm3.clipActionArr.backDoor.time = 0.5;
  168. fm3.clipActionArr.frontDoor.stop();
  169. fm3.clipActionArr.backDoor.stop();
  170. }
  171. if (fm3.frontDamperOpenMesh) fm3.frontDamperOpenMesh.visible = false;
  172. if (fm3.frontDamperClosedMesh) fm3.frontDamperClosedMesh.visible = true;
  173. if (fm3.backDamperOpenMesh) fm3.backDamperOpenMesh.visible = false;
  174. if (fm3.backDamperClosedMesh) fm3.backDamperClosedMesh.visible = true;
  175. model.scene.remove(group);
  176. model.startAnimation = fm3.render.bind(fm3);
  177. group = fm3.group;
  178. group.rotation.y = 0;
  179. const oldCameraPosition = { x: -1000, y: 100, z: 500 };
  180. setTimeout(async () => {
  181. resolve(null);
  182. model.scene.add(fm3.group);
  183. await animateCamera(
  184. oldCameraPosition,
  185. { x: 0, y: 0, z: 0 },
  186. { x: 50.99, y: 69.32, z: 93.61 },
  187. { x: -10.04, y: -14.38, z: -31.4 },
  188. model,
  189. 0.8
  190. );
  191. }, 300);
  192. } else if (fmType === 'fmXr' && fmXr && fmXr.group) {
  193. if (fmXr.clipActionArr.frontDoor && fmXr.clipActionArr.backDoor) {
  194. fmXr.clipActionArr.frontDoor.reset();
  195. fmXr.clipActionArr.frontDoor.time = 0.5;
  196. fmXr.clipActionArr.backDoor.reset();
  197. fmXr.clipActionArr.backDoor.time = 0.5;
  198. fmXr.clipActionArr.frontDoor.stop();
  199. fmXr.clipActionArr.backDoor.stop();
  200. }
  201. if (fmXr.frontDamperOpenMesh) fmXr.frontDamperOpenMesh.visible = false;
  202. if (fmXr.frontDamperClosedMesh) fmXr.frontDamperClosedMesh.visible = true;
  203. if (fmXr.backDamperOpenMesh) fmXr.backDamperOpenMesh.visible = false;
  204. if (fmXr.backDamperClosedMesh) fmXr.backDamperClosedMesh.visible = true;
  205. model.startAnimation = fmXr.render.bind(fmXr);
  206. model.scene.remove(group);
  207. group = fmXr.group;
  208. group.rotation.y = 0;
  209. const oldCameraPosition = { x: -1000, y: 100, z: 500 };
  210. setTimeout(async () => {
  211. resolve(null);
  212. model.scene.add(fmXr.group);
  213. await animateCamera(
  214. oldCameraPosition,
  215. { x: 0, y: 0, z: 0 },
  216. { x: 50.99, y: 69.32, z: 93.61 },
  217. { x: -10.04, y: -14.38, z: -31.4 },
  218. model,
  219. 0.8
  220. );
  221. }, 300);
  222. } else if (fmType === 'fmTwoSs' && fmTwoSs && fmTwoSs.group) {
  223. if (fmTwoSs.clipActionArr.frontDoor && fmTwoSs.clipActionArr.backDoor) {
  224. fmTwoSs.clipActionArr.frontDoor.reset();
  225. fmTwoSs.clipActionArr.frontDoor.time = 0.5;
  226. fmTwoSs.clipActionArr.backDoor.reset();
  227. fmTwoSs.clipActionArr.backDoor.time = 0.5;
  228. fmTwoSs.clipActionArr.frontDoor.stop();
  229. fmTwoSs.clipActionArr.backDoor.stop();
  230. }
  231. if (fmTwoSs.frontDamperOpenMesh) fmTwoSs.frontDamperOpenMesh.visible = false;
  232. if (fmTwoSs.frontDamperClosedMesh) fmTwoSs.frontDamperClosedMesh.visible = true;
  233. if (fmTwoSs.backDamperOpenMesh) fmTwoSs.backDamperOpenMesh.visible = false;
  234. if (fmTwoSs.backDamperClosedMesh) fmTwoSs.backDamperClosedMesh.visible = true;
  235. model.startAnimation = fmTwoSs.render.bind(fmTwoSs);
  236. model.scene.remove(group);
  237. group = fmTwoSs.group;
  238. group.rotation.y = 0;
  239. const oldCameraPosition = { x: -1000, y: 100, z: 500 };
  240. setTimeout(async () => {
  241. resolve(null);
  242. model.scene.add(fmTwoSs.group);
  243. await animateCamera(
  244. oldCameraPosition,
  245. { x: 0, y: 0, z: 0 },
  246. { x: 50.99, y: 69.32, z: 93.61 },
  247. { x: -10.04, y: -14.38, z: -31.4 },
  248. model,
  249. 0.8
  250. );
  251. }, 300);
  252. }
  253. });
  254. };
  255. export const initCameraCanvas = async (playerVal1) => {
  256. if (fmType === 'fm1' && fm1) {
  257. return await fm1.initCamera.call(fm1, playerVal1);
  258. } else if (fmType === 'fm2' && fm2) {
  259. return fm2.initCamera.call(fm2, playerVal1);
  260. } else if (fmType === 'fm3' && fm3) {
  261. return fm3.initCamera.call(fm3, playerVal1);
  262. } else if (fmType === 'fmXr' && fmXr) {
  263. return fmXr.initCamera.call(fmXr, playerVal1);
  264. }
  265. };
  266. export const mountedThree = (playerDom) => {
  267. const { sysOrgCode } = useGlobSetting();
  268. // sysOrgCode = 'yjmdsankuang';
  269. return new Promise(async (resolve) => {
  270. model = new UseThree('#damper3D', '', '#deviceDetail');
  271. model.setEnvMap('test1');
  272. model.renderer.toneMappingExposure = 1.0;
  273. model.camera.position.set(100, 0, 1000);
  274. switch (sysOrgCode) {
  275. case 'yjmdsankuang': //窑街三矿
  276. case 'yjmdhswmk': //窑街海石湾
  277. case 'yjmdjhmk': //窑街金河矿
  278. const FmYj = await import('./gate.threejs.yj');
  279. if (FmYj) fm3 = new FmYj.default(model);
  280. if (fm3) fm3.mountedThree(playerDom);
  281. const FmTwoYj = await import('./gate.threejs.two.yj');
  282. if (FmTwoYj) fmTwoSs = new FmTwoYj.default(model);
  283. fmTwoSs.mountedThree(playerDom);
  284. // if (FmTwoYj) fm2 = new FmTwoYj.default(model);
  285. // fm2.mountedThree(playerDom);
  286. break;
  287. default:
  288. fm3 = new Fm3(model);
  289. fm3.mountedThree(playerDom);
  290. fmTwoSs = new FmTwoSs(model);
  291. fmTwoSs.mountedThree(playerDom);
  292. fm2 = new Fm2(model);
  293. fm2.mountedThree(playerDom);
  294. break;
  295. }
  296. fmXr = new FmXR(model);
  297. fmXr.mountedThree(playerDom);
  298. fm1 = new Fm1(model);
  299. fm1.mountedThree(playerDom);
  300. resolve(null);
  301. model.animate();
  302. startAnimation();
  303. });
  304. };
  305. export const destroy = () => {
  306. if (model) {
  307. model.isRender = false;
  308. if (fm1) fm1.destroy();
  309. if (fm2) fm2.destroy();
  310. if (fm3) fm3.destroy();
  311. if (fmXr) fmXr.destroy();
  312. if (fmTwoSs) fmTwoSs.destroy();
  313. fm1 = null;
  314. fm2 = null;
  315. fm3 = null;
  316. fmXr = null;
  317. fmTwoSs = null;
  318. group = null;
  319. model.mixers = [];
  320. model.destroy();
  321. }
  322. model = null;
  323. };