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. console.log('摄像头控制信息', model?.orbitControls, model?.camera);
  23. }
  24. };
  25. const addMouseEvent = () => {
  26. // 定义鼠标点击事件
  27. model?.canvasContainer?.addEventListener('mousedown', mouseEvent.bind(null));
  28. };
  29. const render = () => {
  30. if (model && model.isRender) {
  31. model.animationId = requestAnimationFrame(render);
  32. model.css3dRender?.render(model.scene as THREE.Scene, model.camera as THREE.PerspectiveCamera);
  33. model.stats?.update();
  34. }
  35. };
  36. export const addgasPumpText = (selectData) => {
  37. if (gasPumpType === 'gasPump') {
  38. // return gasPumpBaseObj.addgasPumpText.call(gasPumpBaseObj, selectData);
  39. }
  40. };
  41. // 切换模型类型
  42. export const setModelType = (type) => {
  43. gasPumpType = type;
  44. return new Promise((resolve) => {
  45. if (gasPumpType === 'gasPump' && gasPumpBaseObj && gasPumpBaseObj.group) {
  46. if (model?.scene?.getObjectByName('gasPumpUnder') && gasPumpUnderObj && gasPumpUnderObj.group) {
  47. model.scene.remove(gasPumpUnderObj.group);
  48. }
  49. group = gasPumpBaseObj.group;
  50. const oldCameraPosition = { x: 15.9074, y: 5.40264, z: 27.12551 };
  51. setTimeout(async () => {
  52. model?.scene?.add(group);
  53. await animateCamera(
  54. oldCameraPosition,
  55. { x: 0.544, y: 0.3335, z: 0.29222 },
  56. { x: 0.0553, y: 1.2775, z: 3.7402 },
  57. { x: 0.0731, y: -0.162, z: 0.562 },
  58. model,
  59. 0.6
  60. );
  61. }, 300);
  62. resolve(null);
  63. }
  64. if (gasPumpType === 'gasPumpUnder' && gasPumpUnderObj && gasPumpUnderObj.group) {
  65. if (model?.scene?.getObjectByName('gasPump') && gasPumpBaseObj && gasPumpBaseObj.group) {
  66. model.scene.remove(gasPumpBaseObj.group);
  67. }
  68. group = gasPumpUnderObj.group;
  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: 3.3224253129798633, y: 2.3189559678790963, z: 5.1822770949018695 },
  76. { x: 0.7866051731597254, y: -0.21180783848684975, z: 0.20318654152228077 },
  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');
  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. // render();
  97. model.animate();
  98. resolve(null);
  99. });
  100. };
  101. export const destroy = () => {
  102. if (model) {
  103. model.isRender = false;
  104. console.log('场景销毁前信息----------->', model.renderer?.info);
  105. gasPumpBaseObj?.destroy();
  106. gasPumpBaseObj = undefined;
  107. group = undefined;
  108. model.destroy();
  109. model = undefined;
  110. }
  111. };