main.threejs.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import * as THREE from 'three';
  2. import { animateCamera, getTextCanvas, renderVideo } from '/@/utils/threejs/util';
  3. import UseThree from '../../../../hooks/core/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. // 切换风窗类型
  125. export const setModelType = (type) => {
  126. modalType = type;
  127. return new Promise((resolve) => {
  128. // 显示双道风窗
  129. if (modalType === 'mainWindRect') {
  130. model.startAnimation = mainWindObj.render.bind(mainWindObj);
  131. group = mainWindObj.group;
  132. setTimeout(async () => {
  133. resolve(null);
  134. const position = mainWindObj.group.position;
  135. const oldCameraPosition = { x: -500, y: 100, z: 2000 };
  136. await animateCamera(
  137. oldCameraPosition,
  138. oldCameraPosition,
  139. { x: 0, y: -100.539, z: 104.313 },
  140. { x: position.x, y: position.y, z: position.z },
  141. model,
  142. 0.8
  143. );
  144. model.scene.add(mainWindObj.group);
  145. }, 300);
  146. }
  147. });
  148. };
  149. export const mountedThree = (playerVal1) => {
  150. return new Promise(async (resolve) => {
  151. model = new UseThree('#main3D', '#main3DCSS');
  152. model.setEnvMap('test1');
  153. model.renderer.toneMappingExposure = 1;
  154. addLight();
  155. setControls();
  156. resetCamera();
  157. model.setModel('bg').then(async (gltf) => {
  158. gltf.scene.position.y = 36.6;
  159. bgGroup = gltf.scene;
  160. model.scene.add(bgGroup);
  161. mainWindObj = new mainWindRect(model, playerVal1);
  162. await mainWindObj.mountedThree();
  163. model.animate();
  164. resolve(null);
  165. });
  166. startAnimation();
  167. });
  168. };
  169. export const destroy = () => {
  170. if (model) {
  171. model.deleteModal();
  172. model.clearGroup(bgGroup);
  173. model = null;
  174. group = null;
  175. }
  176. };