main.threejs.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. import * as THREE from 'three';
  2. import { animateCamera, getTextCanvas, renderVideo } from '/@/utils/threejs/util';
  3. import UseThree from '../../../../utils/threejs/useThree';
  4. import mainWindRect from './mainWind.threejs';
  5. import { useAppStore } from '/@/store/modules/app';
  6. // import * as dat from 'dat.gui';
  7. // const gui = new dat.GUI();
  8. // 模型对象、 文字对象
  9. let model, //
  10. group,
  11. bgGroup,
  12. mainWindObj,
  13. modalType = 'mainWind';
  14. const appStore = useAppStore();
  15. // 打灯光
  16. const addLight = () => {
  17. const pointLight6 = new THREE.PointLight(0xffffff, 1.5, 0);
  18. pointLight6.position.set(-636, 137, 500);
  19. pointLight6.shadow.bias = 0.05;
  20. model.scene.add(pointLight6);
  21. //
  22. const pointLight7 = new THREE.PointLight(0xffffff, 1, 500);
  23. pointLight7.position.set(-209, 72, 83.3);
  24. pointLight7.shadow.bias = -0.05;
  25. model.scene.add(pointLight7);
  26. const spotLight = new THREE.SpotLight();
  27. spotLight.angle = Math.PI / 16;
  28. spotLight.penumbra = 0;
  29. // spotLight.castShadow = true;
  30. spotLight.position.set(500, 500, 687);
  31. model.scene.add(spotLight);
  32. spotLight.shadow.camera.near = 0.5; // default
  33. spotLight.shadow.camera.far = 1000; // default
  34. spotLight.shadow.focus = 1;
  35. spotLight.shadow.bias = -0.000002;
  36. // gui.add(spotLight.position, 'x', -1000, 1000);
  37. // gui.add(spotLight.position, 'y', -1000, 1000);
  38. // gui.add(spotLight.position, 'z', -1000, 1000);
  39. // gui.add(spotLight.shadow, 'bias', -1, 1);
  40. // gui.add(spotLight.shadow, 'focus', -2, 2);
  41. // gui.add(spotLight, 'distance', 0, 1000);
  42. // // gui.add(pointLight5.position, 'x', -1000, 1000);
  43. // // gui.add(pointLight5.position, 'y', -1000, 1000);
  44. // // gui.add(pointLight5.position, 'z', -1000, 1000);
  45. // // gui.add(spotLight.shadow, 'bias', -1, 1);
  46. // // gui.add(spotLight.shadow, 'focus', -2, 2);
  47. // // gui.add(pointLight5, 'distance', 0, 1000);
  48. // gui.add(pointLight6.position, 'x', -1000, 1000);
  49. // gui.add(pointLight6.position, 'y', -1000, 1000);
  50. // gui.add(pointLight6.position, 'z', -1000, 1000);
  51. // gui.add(spotLight.shadow, 'bias', -1, 1);
  52. // gui.add(spotLight.shadow, 'focus', -2, 2);
  53. // gui.add(pointLight6, 'distance', 0, 1000);
  54. // gui.add(pointLight7.position, 'x', -1000, 1000);
  55. // gui.add(pointLight7.position, 'y', -1000, 1000);
  56. // gui.add(pointLight7.position, 'z', -1000, 1000);
  57. // gui.add(spotLight.shadow, 'bias', -1, 1);
  58. // gui.add(spotLight.shadow, 'focus', -2, 2);
  59. // gui.add(pointLight7, 'distance', 0, 1000);
  60. };
  61. // 重置摄像头
  62. const resetCamera = () => {
  63. model.camera.position.set(-500, 0, 2000);
  64. // model.camera.rotation.set(-27.88, 14.35, 7.47);
  65. model.orbitControls?.update();
  66. model.camera.updateProjectionMatrix();
  67. };
  68. const setControls = () => {
  69. model.orbitControls.panSpeed = 0.5;
  70. model.orbitControls.rotateSpeed = 0.5;
  71. model.orbitControls.maxPolarAngle = Math.PI / 3;
  72. model.orbitControls.minPolarAngle = Math.PI / 4;
  73. };
  74. // 初始化事件
  75. const startAnimation = () => {
  76. // 定义鼠标点击事件
  77. model.canvasContainer?.addEventListener('mousedown', mouseEvent.bind(null));
  78. model.canvasContainer?.addEventListener('pointerup', (event) => {
  79. event.stopPropagation();
  80. });
  81. };
  82. // 鼠标点击、松开事件
  83. const mouseEvent = (event) => {
  84. event.stopPropagation();
  85. const widthScale = appStore.getWidthScale;
  86. const heightScale = appStore.getHeightScale;
  87. // 将鼠标位置归一化为设备坐标。x 和 y 方向的取值范围是 (-1 to +1)
  88. model.mouse.x =
  89. ((-model.canvasContainer.getBoundingClientRect().left * widthScale + event.clientX) / (model.canvasContainer.clientWidth * widthScale)) * 2 - 1;
  90. model.mouse.y =
  91. -((-model.canvasContainer.getBoundingClientRect().top + event.clientY) / (model.canvasContainer.clientHeight * heightScale)) * 2 + 1;
  92. (model.rayCaster as THREE.Raycaster).setFromCamera(model.mouse, model.camera as THREE.Camera);
  93. if (group) {
  94. const intersects = model.rayCaster?.intersectObjects(group.children, false) as THREE.Intersection[];
  95. if (intersects.length > 0) {
  96. // 主通风
  97. if (modalType === 'mainWindRect') {
  98. mainWindObj.mousedownModel.call(mainWindObj, intersects);
  99. }
  100. }
  101. }
  102. };
  103. /* 添加监控数据 */
  104. export const addText = (selectData) => {
  105. if (modalType === 'mainWindRect') {
  106. return mainWindObj.addCssText.call(mainWindObj, selectData);
  107. }
  108. };
  109. /* 刷新echarts曲线图 */
  110. export const resetEcharts = (selectData) => {
  111. if (modalType === 'mainWindRect') {
  112. return mainWindObj.addEcharts.call(mainWindObj);
  113. }
  114. };
  115. /**
  116. *
  117. * @param controlType //操作类型
  118. * @param deviceType // 设备类型,前后风机
  119. * @param frequencyVal // 频率
  120. * @param state // 启停状态
  121. * @param smokeDirection // 气流路径
  122. * @returns
  123. */
  124. export const play = (controlType, deviceType, frequencyVal?, state?, smokeDirection?) => {
  125. if (modalType === 'mainWindRect') {
  126. return mainWindObj.playSmoke.call(mainWindObj, controlType, deviceType, frequencyVal, state, smokeDirection);
  127. }
  128. };
  129. export const playAnimate = async (selectData, duration?) => {
  130. if (modalType === 'mainWindRect') {
  131. if (selectData) {
  132. debugger;
  133. if (selectData.Fan1WindowOpen !== undefined) {
  134. // 主风机水平窗开启
  135. if (selectData.Fan1WindowOpen == 1) mainWindObj.openOrCloseWindow('front', 'openWindow');
  136. if (selectData.Fan1WindowOpen == 0) mainWindObj.openOrCloseWindow('front', 'closeWindow');
  137. }
  138. if (selectData.Fan2WindowOpen !== undefined) {
  139. // 备风机水平窗开启
  140. if (selectData.Fan2WindowOpen == 1) mainWindObj.openOrCloseWindow('back', 'openWindow');
  141. if (selectData.Fan2WindowOpen == 0) mainWindObj.openOrCloseWindow('back', 'closeWindow');
  142. }
  143. if (selectData.Fan1ButterflyOpen !== undefined) {
  144. if (selectData.Fan1ButterflyOpen == 1) {
  145. // 主风机蝶阀打开
  146. mainWindObj.openOrCloseValve('front', 'open', duration);
  147. } else {
  148. // 主风机蝶阀关闭
  149. mainWindObj.openOrCloseValve('front', 'close', duration);
  150. }
  151. }
  152. if (selectData.Fan2ButterflyOpen !== undefined) {
  153. if (selectData.Fan2ButterflyOpen == 1) {
  154. // 主风机蝶阀打开
  155. mainWindObj.openOrCloseValve('back', 'open', duration);
  156. } else {
  157. // 主风机蝶阀关闭
  158. mainWindObj.openOrCloseValve('back', 'close', duration);
  159. }
  160. }
  161. if (selectData.Fan1FreqHz !== undefined) {
  162. // 主风机频率设置
  163. mainWindObj.resetSmokeParam('front', selectData.Fan1FreqHz, duration);
  164. }
  165. if (selectData.Fan2FreqHz !== undefined) {
  166. // 主风机频率设置
  167. mainWindObj.resetSmokeParam('back', selectData.Fan2FreqHz, duration);
  168. }
  169. if (selectData.Fan1StartStatus) {
  170. if (selectData.Fan1StartStatus == 1) {
  171. // 主风机开启
  172. mainWindObj.lookMotor('front', 'open', duration);
  173. // if (selectData.Fan1FreqForwardRun && selectData.Fan1FreqForwardRun == 1) {
  174. // // 主风机正转
  175. // await mainWindObj.setSmokeDirection('front', 'tubPositivePath');
  176. // } else if (selectData.Fan1FreqReverseRun && selectData.Fan1FreqReverseRun == 1) {
  177. // // 主风机反转
  178. // await mainWindObj.setSmokeDirection('front', 'tubInversePath');
  179. // }
  180. // 齿轮转动
  181. mainWindObj.startGearAnimation('front', 'open', 'tubPositivePath', selectData.Fan1FreqHz, duration);
  182. await mainWindObj.setSmokeDirection('front', 'tubPositivePath');
  183. if (!mainWindObj.frontSmoke.frameId) mainWindObj.frontSmoke.startSmoke(duration);
  184. } else {
  185. mainWindObj.lookMotor('front', 'close', duration);
  186. }
  187. }
  188. if (selectData.Fan2StartStatus) {
  189. if (selectData.Fan2StartStatus == 1) {
  190. // 备风机开启
  191. mainWindObj.lookMotor('back', 'open', duration);
  192. // if (selectData.Fan2FreqForwardRun && selectData.Fan2FreqForwardRun == 1) {
  193. // // 主风机正转
  194. // } else if (selectData.Fan2FreqReverseRun && selectData.Fan2FreqReverseRun == 1) {
  195. // // 主风机反转
  196. // }
  197. await mainWindObj.setSmokeDirection('back', 'tubPositivePath');
  198. if (!mainWindObj.backSmoke.frameId) mainWindObj.backSmoke.startSmoke(duration);
  199. } else {
  200. mainWindObj.lookMotor('back', 'close', duration);
  201. }
  202. }
  203. }
  204. }
  205. };
  206. // 切换风窗类型
  207. export const setModelType = (type) => {
  208. modalType = type;
  209. return new Promise((resolve) => {
  210. // 显示双道风窗
  211. if (modalType === 'mainWindRect') {
  212. model.startAnimation = mainWindObj.render.bind(mainWindObj);
  213. group = mainWindObj.group;
  214. setTimeout(async () => {
  215. resolve(null);
  216. const position = mainWindObj.group.position;
  217. const oldCameraPosition = { x: -500, y: 100, z: 2000 };
  218. await animateCamera(
  219. oldCameraPosition,
  220. oldCameraPosition,
  221. { x: 0, y: -100.539, z: 104.313 },
  222. { x: position.x, y: position.y, z: position.z },
  223. model,
  224. 0.8
  225. );
  226. model.scene.add(mainWindObj.group);
  227. }, 300);
  228. }
  229. });
  230. };
  231. export const mountedThree = (playerVal1) => {
  232. return new Promise(async (resolve) => {
  233. model = new UseThree('#main3D', '#main3DCSS');
  234. model.setEnvMap('test1');
  235. model.renderer.toneMappingExposure = 1;
  236. addLight();
  237. setControls();
  238. resetCamera();
  239. model.setModel('bg').then(async (gltf) => {
  240. gltf.scene.position.y = 36.6;
  241. bgGroup = gltf.scene;
  242. model.scene.add(bgGroup);
  243. mainWindObj = new mainWindRect(model, playerVal1);
  244. await mainWindObj.mountedThree();
  245. model.animate();
  246. resolve(null);
  247. });
  248. startAnimation();
  249. });
  250. };
  251. export const destroy = () => {
  252. if (model) {
  253. model.deleteModal();
  254. model.clearGroup(bgGroup);
  255. model = null;
  256. group = null;
  257. }
  258. };