main.threejs.ts 14 KB

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