main.threejs.ts 13 KB

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