main.threejs.ts 9.5 KB

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