gasPump.threejs.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import * as THREE from 'three';
  2. import UseThree from '../../../../utils/threejs/useThree';
  3. import gasPumpBase from './gasPump.threejs.over';
  4. import gasPumpUnder from './gasPump.threejs.under';
  5. import { animateCamera, setModalCenter } from '/@/utils/threejs/util';
  6. import useEvent from '../../../../utils/threejs/useEvent';
  7. // 模型对象、 文字对象
  8. let model: UseThree | undefined,
  9. gasPumpBaseObj: gasPumpBase | undefined,
  10. gasPumpUnderObj: gasPumpUnder | undefined,
  11. group: THREE.Object3D | undefined,
  12. gasPumpType = 'gasPump';
  13. const { mouseDownFn } = useEvent();
  14. // 鼠标点击事件
  15. const mouseEvent = (event) => {
  16. if (event.button == 0) {
  17. mouseDownFn(<UseThree>model, <THREE.Object3D>group, event, (intersects) => {
  18. if (gasPumpType === 'gasPump') {
  19. // gasPumpBaseObj.mousedownModel.call(gasPumpBaseObj, intersects);
  20. }
  21. });
  22. }
  23. };
  24. const addMouseEvent = () => {
  25. // 定义鼠标点击事件
  26. model?.canvasContainer?.addEventListener('mousedown', mouseEvent.bind(null));
  27. };
  28. export const playAnimate = (monitorData: any) => {
  29. if (gasPumpType === 'gasPumpUnder') {
  30. gasPumpUnderObj?.handleAnimation(monitorData);
  31. }
  32. };
  33. export const addgasPumpText = (selectData) => {
  34. if (gasPumpType === 'gasPump') {
  35. // return gasPumpBaseObj.addgasPumpText.call(gasPumpBaseObj, selectData);
  36. }
  37. };
  38. // 切换模型类型
  39. export const setModelType = (type) => {
  40. gasPumpType = type;
  41. return new Promise((resolve) => {
  42. if (gasPumpType === 'gasPump' && gasPumpBaseObj && gasPumpBaseObj.group) {
  43. if (model?.scene?.getObjectByName('gasPumpUnder') && gasPumpUnderObj && gasPumpUnderObj.group) {
  44. model.scene.remove(gasPumpUnderObj.group);
  45. }
  46. group = gasPumpBaseObj.group;
  47. // model.isRender = false;
  48. const oldCameraPosition = { x: 15.9074, y: 5.40264, z: 27.12551 };
  49. setTimeout(async () => {
  50. model?.scene?.add(group);
  51. await animateCamera(
  52. oldCameraPosition,
  53. { x: 0.544, y: 0.3335, z: 0.29222 },
  54. { x: 0.0553, y: 1.2775, z: 3.7402 },
  55. { x: 0.0731, y: -0.162, z: 0.562 },
  56. model,
  57. 0.6
  58. );
  59. }, 300);
  60. resolve(null);
  61. }
  62. if (gasPumpType === 'gasPumpUnder' && gasPumpUnderObj && gasPumpUnderObj.group) {
  63. if (model?.scene?.getObjectByName('gasPump') && gasPumpBaseObj && gasPumpBaseObj.group) {
  64. model.scene.remove(gasPumpBaseObj.group);
  65. }
  66. gasPumpUnderObj.addCssText();
  67. group = gasPumpUnderObj.group;
  68. // model.isRender = true;
  69. const oldCameraPosition = { x: 15.9074, y: 5.40264, z: 27.12551 };
  70. setTimeout(async () => {
  71. model?.scene?.add(group);
  72. await animateCamera(
  73. oldCameraPosition,
  74. { x: 0.544, y: 0.3335, z: 0.29222 },
  75. { x: -11.014079961863667, y: 1.6456786378357198, z: 5.733554317319543 },
  76. { x: -7.043053237796046, y: 0.3354979232986092, z: -0.10561004136342216 },
  77. model,
  78. 0.6
  79. );
  80. }, 300);
  81. resolve(null);
  82. }
  83. });
  84. };
  85. export const mountedThree = () => {
  86. return new Promise(async (resolve) => {
  87. model = new UseThree('#gasPump3D', '#gas3DCSS');
  88. model.setEnvMap('test2');
  89. model.renderer.toneMappingExposure = 1.0;
  90. // model.renderer.outputEncoding = THREE.sRGBEncoding;
  91. gasPumpBaseObj = new gasPumpBase(model);
  92. await gasPumpBaseObj.mountedThree();
  93. gasPumpUnderObj = new gasPumpUnder(model);
  94. await gasPumpUnderObj.mountedThree();
  95. addMouseEvent();
  96. model.animate();
  97. resolve(null);
  98. });
  99. };
  100. export const destroy = () => {
  101. if (model) {
  102. model.isRender = false;
  103. console.log('场景销毁前信息----------->', model.renderer?.info);
  104. gasPumpBaseObj?.destroy();
  105. gasPumpBaseObj = undefined;
  106. group = undefined;
  107. model.destroy();
  108. model = undefined;
  109. }
  110. };