nitrogen.threejs.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import * as THREE from 'three';
  2. import { animateCamera, setModalCenter, updateAxisCenter } from '/@/utils/threejs/util';
  3. import UseThree from '../../../../utils/threejs/useThree';
  4. import useEvent from '../../../../utils/threejs/useEvent';
  5. import Nitrogen from './nitrogen.dishang.threejs';
  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. compressorObj1: Nitrogen | undefined,
  13. compressorObj2: Nitrogen | undefined,
  14. modalType = 'compressor';
  15. const { mouseDownFn, mousemoveFn, mouseUpFn } = useEvent();
  16. // 鼠标点击、松开事件
  17. const mouseEvent = (event) => {
  18. // 单道、 双道
  19. if (event.button == 0 && model && group) {
  20. model.canvasContainer?.addEventListener('mousemove', mousemove);
  21. mouseDownFn(model, group, event, (intersects) => {
  22. if (modalType === 'compressor1') {
  23. compressorObj1?.mousedownModel.call(compressorObj1, intersects);
  24. }
  25. if (modalType === 'compressor2') {
  26. compressorObj2?.mousedownModel.call(compressorObj2, intersects);
  27. }
  28. });
  29. }
  30. };
  31. const mouseUp = () => {
  32. if (!model) return;
  33. mouseUpFn(model);
  34. model.canvasContainer?.removeEventListener('mousemove', mousemove);
  35. };
  36. const mousemove = () => {
  37. mousemoveFn();
  38. };
  39. const render = () => {
  40. if (model && model.isRender) {
  41. model.animationId = requestAnimationFrame(render.bind(this));
  42. model.stats?.update();
  43. if (model.renderEnabled) {
  44. model.render();
  45. }
  46. }
  47. };
  48. /* 添加监控数据 */
  49. export const addText = () => {
  50. if (!compressorObj1) return;
  51. if (modalType === 'compressor1') {
  52. return compressorObj1.addCssText.call(compressorObj1);
  53. } else if (modalType === 'compressor2') {
  54. return compressorObj2.addCssText.call(compressorObj2);
  55. }
  56. };
  57. export const play = () => {
  58. if (modalType === 'compressor1' && compressorObj1) {
  59. return compressorObj1.play.call(compressorObj1);
  60. }
  61. if (modalType === 'compressor2' && compressorObj2) {
  62. return compressorObj2.play.call(compressorObj2);
  63. }
  64. };
  65. // 切换风窗类型
  66. export const setModelType = (type) => {
  67. modalType = type;
  68. if (group) {
  69. // compressorObj1?.clearCssText();
  70. model?.scene?.remove(group);
  71. // model?.initCSS3Renderer('#compressorCss3D');
  72. }
  73. return new Promise((resolve) => {
  74. // 显示双道风窗
  75. if (modalType === 'compressor1' && compressorObj1 && model) {
  76. group = compressorObj1.group;
  77. model.renderAnimationScene = () => {
  78. compressorObj1?.css3dRender?.render(model?.scene as THREE.Scene, model?.camera as THREE.PerspectiveCamera);
  79. };
  80. // if (model?.scene?.getObjectByName('compressor2')) {
  81. // if (compressorObj2) model?.scene?.remove(compressorObj2.group);
  82. // }
  83. }
  84. if (modalType === 'compressor2' && compressorObj2 && model) {
  85. group = compressorObj2.group;
  86. model.renderAnimationScene = () => {
  87. compressorObj2?.css3dRender?.render(model?.scene as THREE.Scene, model?.camera as THREE.PerspectiveCamera);
  88. };
  89. // if (model?.scene?.getObjectByName('compressor1')) {
  90. // if (compressorObj1) model?.scene?.remove(compressorObj1.group);
  91. // }
  92. }
  93. model?.camera?.position.set(-1000, 100, 500);
  94. const oldCameraPosition = { x: -1000, y: 100, z: 500 };
  95. const oldControlsPosition = { x: 0, y: 0, z: 0 };
  96. let newCameraPosition = { x: 0, y: 0, z: 0 },
  97. newControlsPosition = { x: 0, y: 0, z: 0 };
  98. if (group?.children.length == 6) {
  99. newCameraPosition = { x: -1.7434040517387115, y: 46.41089142470955, z: 114.33388865672832 };
  100. newControlsPosition = { x: -1.6994314417499141, y: -12.38981035037152, z: 10.110013648346193 };
  101. } else if (group?.children.length == 5) {
  102. newCameraPosition = { x: -1.2741159124866441, y: 46.04318455197148, z: 95.25814325750986 };
  103. newControlsPosition = { x: -1.699431, y: -12.38981, z: -1.699431 };
  104. } else if (group?.children.length == 4) {
  105. newCameraPosition = { x: -2.0540155219473597, y: 40.869414434737195, z: 97.8637568969557 };
  106. newControlsPosition = { x: -2.01631417107236, y: -9.544836585137801, z: 8.50481206883075 };
  107. } else if (group?.children.length == 3) {
  108. newCameraPosition = { x: -0.8949900855095378, y: 44.03213312648178, z: 78.19288289339097 };
  109. newControlsPosition = { x: -0.4945087067301434, y: -9.810777468925817, z: -3.179982369050871 };
  110. } else if (group?.children.length == 2) {
  111. newCameraPosition = { x: -1.2741159124866441, y: 46.04318455197148, z: 70.25814325750986 };
  112. newControlsPosition = { x: -1.699431, y: -12.38981, z: -1.699431 };
  113. } else if (group?.children.length == 1) {
  114. newCameraPosition = { x: -1.2741159124866441, y: 46.04318455197148, z: 70.25814325750986 };
  115. newControlsPosition = { x: -1.699431, y: -12.38981, z: -1.699431 };
  116. }
  117. setTimeout(async () => {
  118. resolve(null);
  119. if (group) model?.scene?.add(group);
  120. await animateCamera(oldCameraPosition, oldControlsPosition, newCameraPosition, newControlsPosition, model, 0.8);
  121. }, 300);
  122. });
  123. };
  124. export const clearCssText = () => {
  125. if (modalType === 'compressor' && compressorObj1) {
  126. return compressorObj1.clearCssText.call(compressorObj1);
  127. }
  128. };
  129. export const mountedThree = (monitorDataGroupNum1, monitorDataGroupNum2) => {
  130. return new Promise(async (resolve) => {
  131. model = new UseThree('#compressor3D');
  132. model.setEnvMap('test1');
  133. model.renderer.toneMappingExposure = 1.0;
  134. model.canvasContainer?.addEventListener('mousedown', mouseEvent);
  135. model.canvasContainer?.addEventListener('pointerup', mouseUp);
  136. compressorObj1 = new Nitrogen(model);
  137. await compressorObj1.mountedThree(monitorDataGroupNum1, '#compressorCss3D');
  138. compressorObj1.modelName = 'compressor1';
  139. compressorObj2 = new Nitrogen(model);
  140. await compressorObj2.mountedThree(monitorDataGroupNum2, '#compressorCss3D1');
  141. compressorObj2.modelName = 'compressor2';
  142. resolve(null);
  143. // render();
  144. model.animate();
  145. // 添加摄像机辅助
  146. // const helper = new THREE.CameraHelper(model.camera as THREE.Camera);
  147. // model?.scene?.add(helper);
  148. });
  149. };
  150. export const destroy = () => {
  151. if (model) {
  152. model.isRender = false;
  153. console.log('场景销毁前信息----------->', model.renderer?.info);
  154. compressorObj1?.destroy();
  155. group = undefined;
  156. model.destroy();
  157. model = undefined;
  158. }
  159. };