main.threejs.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 (!mainWindObj) return;
  195. let mainObj: mainWindRect | mainXjWindRect | undefined;
  196. if (modalType === 'mainWindRect') {
  197. mainObj = mainWindObj;
  198. // bgGroup.visible = true;
  199. } else if (modalType === 'mainXjWindRect') {
  200. mainObj = mainXjWindObj;
  201. // bgGroup.visible = false;
  202. }
  203. if (selectData && mainObj) {
  204. if (selectData['Fan1FreqHz'] == undefined || selectData['Fan1FreqHz'] == null || selectData['Fan1FreqHz'] == '') selectData['Fan1FreqHz'] = 50;
  205. if (selectData['Fan2FreqHz'] == undefined || selectData['Fan2FreqHz'] == null || selectData['Fan2FreqHz'] == '') selectData['Fan2FreqHz'] = 50;
  206. mainObj.resetSmokeParam('front', selectData.Fan1FreqHz, duration);
  207. mainObj.resetSmokeParam('back', selectData.Fan2FreqHz, duration);
  208. if (selectData.Fan2StartStatus == 1) {
  209. // 主风机开启
  210. mainObj?.lookMotor('front', 'open', duration);
  211. mainObj?.openOrCloseValve('front', 'open', duration);
  212. // 1. 已经运行,首次切入动画
  213. // 2. 在页面上,切换动画
  214. if (selectData.Fan2FreqForwardRun == 1 && selectData.Fan2FreqReverseRun == 0) {
  215. // 主风机正转
  216. mainObj.startGearAnimation('front', 'open', 'tubPositivePath', selectData.Fan2FreqHz, duration);
  217. await mainObj.setSmokeDirection('front', 'tubPositivePath');
  218. } else if (selectData.Fan2FreqReverseRun == 1 && selectData.Fan2FreqForwardRun == 0) {
  219. // 主风机反转
  220. mainObj.startGearAnimation('front', 'open', 'tubInversePath', selectData.Fan2FreqHz, duration);
  221. await mainObj.setSmokeDirection('front', 'tubInversePath');
  222. } else {
  223. // 默认主风机正转
  224. mainObj.startGearAnimation('front', 'open', 'tubPositivePath', selectData.Fan2FreqHz, duration);
  225. await mainObj.setSmokeDirection('front', 'tubPositivePath');
  226. }
  227. if (!mainObj?.frontSmoke?.frameId) mainObj?.frontSmoke?.startSmoke(duration);
  228. } else {
  229. // 主风机停止
  230. mainObj.closeDevice('front');
  231. }
  232. if (selectData.Fan1StartStatus == 1) {
  233. // 主风机开启
  234. mainObj.lookMotor('back', 'open', duration);
  235. mainObj.openOrCloseValve('back', 'open', duration);
  236. // 1. 已经运行,首次切入动画
  237. // 2. 在页面上,切换动画
  238. if (selectData.Fan1FreqForwardRun == 1 && selectData.Fan1FreqReverseRun == 0) {
  239. // 主风机正转
  240. mainObj.startGearAnimation('back', 'open', 'tubPositivePath', selectData.Fan1FreqHz, duration);
  241. await mainObj.setSmokeDirection('back', 'tubPositivePath');
  242. } else if (selectData.Fan1FreqReverseRun == 1 && selectData.Fan1FreqForwardRun == 0) {
  243. // 主风机反转
  244. mainObj.startGearAnimation('back', 'open', 'tubInversePath', selectData.Fan1FreqHz, duration);
  245. await mainObj.setSmokeDirection('back', 'tubInversePath');
  246. } else {
  247. mainObj.startGearAnimation('back', 'open', 'tubPositivePath', selectData.Fan1FreqHz, duration);
  248. await mainObj.setSmokeDirection('back', 'tubPositivePath');
  249. }
  250. if (!mainObj?.backSmoke?.frameId) mainObj?.backSmoke?.startSmoke(duration);
  251. } else {
  252. // 主风机停止
  253. mainObj.closeDevice('back');
  254. }
  255. // 防爆门动画
  256. if (modalType === 'mainWindRect' && selectData['ExplosionVentOpen'] == 1 && explosionVentOpen !== 1) {
  257. if (explosionVentOpen == -1) {
  258. // 直接打开
  259. mainObj.playAnimation('open', 0);
  260. } else {
  261. mainObj.playAnimation('open');
  262. }
  263. explosionVentOpen = 1;
  264. explosionVentClose = 0;
  265. }
  266. if (modalType === 'mainWindRect' && selectData['ExplosionVentClose'] == 1 && explosionVentClose !== 1) {
  267. if (explosionVentOpen == -1) {
  268. // 直接关闭
  269. mainObj.playAnimation('close', 0);
  270. } else {
  271. mainObj.playAnimation('close');
  272. }
  273. explosionVentClose = 1;
  274. explosionVentOpen = 0;
  275. }
  276. }
  277. };
  278. // 切换风机类型
  279. export const setModelType = (type) => {
  280. if (!model) return;
  281. modalType = type;
  282. return new Promise((resolve) => {
  283. // 停止气流动画
  284. mainWindObj?.stopSmoke();
  285. mainXjWindObj?.stopSmoke();
  286. if (group) model?.scene?.remove(group);
  287. if (modalType === 'mainWindRect' && mainWindObj && mainWindObj.group) {
  288. mainXjWindObj?.clearCssText();
  289. (<UseThree>model).startAnimation = mainWindObj.render.bind(mainWindObj);
  290. group = mainWindObj.group;
  291. setTimeout(async () => {
  292. resolve(null);
  293. // const position = mainWindObj.group.position;
  294. const position = new THREE.Vector3(-1.0127, -3.4717, -7.864);
  295. const oldCameraPosition = { x: -332.39, y: 283.47, z: 438.61 };
  296. await animateCamera(
  297. oldCameraPosition,
  298. { x: -3.41, y: -29.01, z: 8.84 },
  299. { x: -1.7927, y: 70.8399, z: 120.8451 },
  300. { x: position.x, y: position.y, z: position.z },
  301. model,
  302. 0.8
  303. );
  304. if (group) model?.scene?.add(group);
  305. }, 300);
  306. }
  307. if (modalType === 'mainXjWindRect' && mainXjWindObj && mainXjWindObj.group) {
  308. mainWindObj?.clearCssText();
  309. (<UseThree>model).startAnimation = mainXjWindObj.render.bind(mainXjWindObj);
  310. group = mainXjWindObj.group;
  311. setTimeout(async () => {
  312. resolve(null);
  313. // const position = mainWindObj.group.position;
  314. const position = new THREE.Vector3(12.96, 23.17, -23.16);
  315. const oldCameraPosition = { x: -332.39, y: 283.47, z: 438.61 };
  316. await animateCamera(
  317. oldCameraPosition,
  318. { x: -3.41, y: -29.01, z: 8.84 },
  319. { x: 12.09, y: 105.51, z: 119.45 },
  320. { x: position.x, y: position.y, z: position.z },
  321. model,
  322. 0.8
  323. );
  324. if (group) model?.scene?.add(group);
  325. }, 300);
  326. }
  327. });
  328. };
  329. export const mountedThree = (playerVal1) => {
  330. return new Promise(async (resolve) => {
  331. model = new UseThree('#main3D', '#main3DCSS');
  332. model.setEnvMap('test1');
  333. model.renderer.toneMappingExposure = 1.0;
  334. if (model.renderer) {
  335. model.renderer.sortObjects = true;
  336. }
  337. addLight();
  338. setControls();
  339. resetCamera();
  340. model.setGLTFModel(['bg']).then(async (gltf) => {
  341. bgGroup = gltf[0] as THREE.Object3D;
  342. bgGroup.position.set(3.43, 27.13, 22.0);
  343. model?.scene?.add(bgGroup);
  344. mainWindObj = new mainWindRect(model, playerVal1);
  345. await mainWindObj.mountedThree();
  346. mainXjWindObj = new mainXjWindRect(model, playerVal1);
  347. await mainXjWindObj.mountedThree();
  348. model?.animate();
  349. resolve(null);
  350. });
  351. startAnimation();
  352. });
  353. };
  354. export const destroy = () => {
  355. if (model) {
  356. model.isRender = false;
  357. console.log('场景销毁前信息----------->', model.renderer?.info);
  358. mainWindObj?.destroy();
  359. mainWindObj = undefined;
  360. mainXjWindObj?.destroy();
  361. mainXjWindObj = undefined;
  362. model.clearGroup(bgGroup);
  363. bgGroup = undefined;
  364. group = undefined;
  365. model.destroy();
  366. model = undefined;
  367. }
  368. };