balancePress.threejs.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import UseThree from '../../../../utils/threejs/useThree';
  2. import balancePressBase from './balancePress.threejs.base';
  3. import balancePressTun from './balancePress.threejs.tun';
  4. import balancePressSp from './balancePress.threejs.sp';
  5. import { animateCamera } from '/@/utils/threejs/util';
  6. import useEvent from '../../../../utils/threejs/useEvent';
  7. import * as THREE from 'three';
  8. // 模型对象、 文字对象
  9. let model: UseThree,
  10. balancePressBaseObj: balancePressBase | undefined,
  11. balancePressTunObj: balancePressTun | undefined,
  12. balancePressSpObj: balancePressSp | undefined,
  13. group: THREE.Object3D | undefined,
  14. balancePressType = 'balancePressBase'; // workerFaceFiber
  15. const { mouseDownFn, mousemoveFn, mouseUpFn } = useEvent();
  16. // 鼠标点击事件
  17. const mouseEvent = (event) => {
  18. if (event.button == 0) {
  19. model.canvasContainer?.addEventListener('mousemove', mousemove);
  20. mouseDownFn(model, group as THREE.Object3D, event, (intersects) => {
  21. if (balancePressType === 'balancePressType') {
  22. // balancePressBaseObj.mousedownModel.call(balancePressBaseObj, model.rayCaster);
  23. }
  24. });
  25. }
  26. };
  27. const mouseUp = () => {
  28. if (!model) return;
  29. mouseUpFn(model, 1);
  30. model.canvasContainer?.removeEventListener('mousemove', mousemove);
  31. };
  32. const mousemove = () => {
  33. mousemoveFn();
  34. };
  35. const addMouseEvent = () => {
  36. // 定义鼠标点击事件
  37. model.canvasContainer?.addEventListener('mousedown', mouseEvent.bind(null));
  38. model.canvasContainer?.addEventListener('pointerup', mouseUp);
  39. };
  40. export const play = (controlType, deviceType, frequency, state, duration?) => {
  41. if (balancePressType === 'balancePressBase') {
  42. return balancePressBaseObj?.playSmoke.call(balancePressBaseObj, controlType, deviceType, frequency, state, duration);
  43. }
  44. };
  45. export const updateText = (selectData) => {
  46. if (balancePressType === 'balancePressBase') {
  47. return balancePressBaseObj?.addText.call(balancePressBaseObj, selectData);
  48. }
  49. };
  50. // 切换模型类型
  51. export const setModelType = (type) => {
  52. debugger;
  53. balancePressType = type;
  54. return new Promise((resolve) => {
  55. if (balancePressType === 'balancePressBase' && balancePressBaseObj && balancePressBaseObj.group) {
  56. group = balancePressBaseObj.group;
  57. if (group) {
  58. const oldCameraPosition = { x: 27.9165, y: 17.3763, z: 51.3388 };
  59. setTimeout(async () => {
  60. model.scene.add(balancePressBaseObj?.group);
  61. await animateCamera(
  62. oldCameraPosition,
  63. { x: 3.9025, y: 0.7782, z: 6.6307 },
  64. { x: 0.1218, y: 3.8213, z: 17.27671 },
  65. { x: 0.2348, y: 0.7182, z: 6.8413 },
  66. model,
  67. 0.8
  68. );
  69. }, 300);
  70. }
  71. resolve(null);
  72. } else if (balancePressType === 'balancePressTun' && balancePressTunObj && balancePressTunObj.group) {
  73. group = balancePressTunObj.group;
  74. if (group) {
  75. const oldCameraPosition = { x: 27.9165, y: 17.3763, z: 51.3388 };
  76. setTimeout(async () => {
  77. model.scene.add(balancePressTunObj?.group);
  78. await animateCamera(
  79. oldCameraPosition,
  80. { x: 3.9025, y: 0.7782, z: 6.6307 },
  81. { x: 0.2185637607064918, y: 2.476256183591406, z: 2.7017622865334987 },
  82. { x: 0.21856376070649214, y: 0.09479035032624854, z: 0.7165903260342672 },
  83. model,
  84. 1
  85. );
  86. }, 300);
  87. }
  88. resolve(null);
  89. } else if (balancePressType === 'balancePressSp' && balancePressSpObj && balancePressSpObj.group) {
  90. group = balancePressSpObj.group;
  91. if (group) {
  92. setTimeout(async () => {
  93. model.scene.add(balancePressSpObj?.group);
  94. await animateCamera(
  95. { x: 27.9165, y: 17.3763, z: 51.3388 },
  96. { x: 3.9025, y: 0.7782, z: 6.6307 },
  97. { x: 0.1275, y: 2.4409, z: 1.627 },
  98. { x: 0.1275, y: 0.3118, z: 0.3478 },
  99. model,
  100. 1
  101. );
  102. }, 300);
  103. }
  104. resolve(null);
  105. }
  106. });
  107. };
  108. export const mountedThree = () => {
  109. return new Promise(async (resolve) => {
  110. model = new UseThree('#balancePress3D');
  111. model.setEnvMap('test1.hdr');
  112. model.renderer.toneMappingExposure = 1.0;
  113. balancePressBaseObj = new balancePressBase(model);
  114. await balancePressBaseObj.mountedThree();
  115. balancePressTunObj = new balancePressTun(model);
  116. await balancePressTunObj.mountedThree();
  117. balancePressSpObj = new balancePressSp(model);
  118. await balancePressSpObj.mountedThree();
  119. addMouseEvent();
  120. model.animate();
  121. resolve(null);
  122. });
  123. };
  124. export const destroy = () => {
  125. if (model) {
  126. model.isRender = false;
  127. console.log('场景销毁前信息----------->', model.renderer?.info);
  128. balancePressBaseObj?.destroy();
  129. balancePressBaseObj = undefined;
  130. balancePressTunObj?.destroy();
  131. balancePressTunObj = undefined;
  132. balancePressSpObj?.destroy();
  133. balancePressSpObj = undefined;
  134. group = undefined;
  135. model.destroy();
  136. model = undefined;
  137. }
  138. };