gate.threejs.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import * as THREE from 'three';
  2. import UseThree from '../../../../utils/threejs/useThree';
  3. import Fm1 from './gate.threejs.two';
  4. import Fm2 from './gate.threejs.three';
  5. import { animateCamera } from '/@/utils/threejs/util';
  6. import useEvent from '../../../../utils/threejs/useEvent';
  7. // 模型对象、 文字对象
  8. let model,
  9. fm1: Fm1,
  10. fm2: Fm2,
  11. group: THREE.Object3D,
  12. fmType = '';
  13. const { mouseDownFn } = useEvent();
  14. // 初始化左右摇摆动画
  15. const startAnimation = () => {
  16. // 定义鼠标点击事件
  17. model.canvasContainer?.addEventListener('mousedown', mouseEvent.bind(null));
  18. model.canvasContainer?.addEventListener('pointerup', (event) => {
  19. event.stopPropagation();
  20. // 单道、 双道
  21. if (fmType === 'fm1') {
  22. fm1.mouseUpModel.call(fm1);
  23. } else if (fmType === 'fm2') {
  24. fm2.mouseUpModel.call(fm2);
  25. }
  26. });
  27. };
  28. // 鼠标点击、松开事件
  29. const mouseEvent = (event) => {
  30. if (event.button == 0) {
  31. mouseDownFn(model, group, event, (intersects) => {
  32. if (fmType === 'fm1' && fm1) {
  33. fm1.mousedownModel.call(fm1, intersects);
  34. } else if (fmType === 'fm2' && fm2) {
  35. fm2.mousedownModel.call(fm2, intersects);
  36. }
  37. });
  38. // console.log('摄像头控制信息', model.orbitControls, model.camera);
  39. }
  40. };
  41. export const addMonitorText = (selectData) => {
  42. if (fmType === 'fm1' && fm1) {
  43. return fm1.addMonitorText.call(fm1, selectData);
  44. } else if (fmType === 'fm2' && fm2) {
  45. return fm2.addMonitorText.call(fm2, selectData);
  46. }
  47. };
  48. export const deviceDetailCard = () => {
  49. if (fmType === 'fm1') {
  50. return fm1.deviceDetailCard.call(fm1);
  51. } else {
  52. // return fm2.addMonitorText.call(fm2);
  53. }
  54. };
  55. export const play = (handlerState, flag?) => {
  56. if (fmType === 'fm1' && fm1) {
  57. return fm1.play.call(fm1, handlerState, flag);
  58. } else if (fmType === 'fm2' && fm2) {
  59. return fm2.play.call(fm2, handlerState, flag);
  60. }
  61. };
  62. // 切换风门类型
  63. export const setModelType = (type) => {
  64. fmType = type;
  65. return new Promise((resolve) => {
  66. // 暂停风门1动画
  67. fm1.clipActionArr.frontDoor.reset();
  68. fm1.clipActionArr.frontDoor.time = 0.5;
  69. fm1.clipActionArr.backDoor.reset();
  70. fm1.clipActionArr.backDoor.time = 0.5;
  71. fm1.clipActionArr.frontDoor.stop();
  72. fm1.clipActionArr.backDoor.stop();
  73. if (fmType === 'fm1' && fm1 && fm1.group) {
  74. model.startAnimation = fm1.render.bind(fm1);
  75. group = fm1.group;
  76. group.rotation.y = 0;
  77. if (model.scene.getObjectByName('fm2')) {
  78. model.scene.remove(fm2.group);
  79. }
  80. const oldCameraPosition = { x: -1000, y: 100, z: 500 };
  81. setTimeout(async () => {
  82. resolve(null);
  83. model.scene.add(fm1.group);
  84. await animateCamera(
  85. oldCameraPosition,
  86. { x: 0, y: 0, z: 0 },
  87. { x: 50.99, y: 69.32, z: 93.61 },
  88. { x: -10.04, y: -14.38, z: -31.4 },
  89. model,
  90. 0.8
  91. );
  92. }, 300);
  93. } else if (fmType === 'fm2' && fm2 && fm2.group) {
  94. fm2.clipActionArr.frontDoor.reset();
  95. fm2.clipActionArr.frontDoor.time = 0.5;
  96. fm2.clipActionArr.backDoor.reset();
  97. fm2.clipActionArr.backDoor.time = 0.5;
  98. fm2.clipActionArr.centerDoor.reset();
  99. fm2.clipActionArr.centerDoor.time = 0.5;
  100. fm2.clipActionArr.frontDoor.stop();
  101. fm2.clipActionArr.backDoor.stop();
  102. fm2.clipActionArr.centerDoor.stop();
  103. // 显示单道风窗
  104. model.startAnimation = fm2.render.bind(fm2);
  105. group = fm2.group;
  106. if (model.scene.getObjectByName('fm1')) {
  107. model.scene.remove(fm1.group);
  108. }
  109. const oldCameraPosition = { x: -761, y: 569, z: 871 };
  110. setTimeout(async () => {
  111. resolve(null);
  112. model.scene.add(fm2.group);
  113. const position = { x: -2.28, y: -0.91, z: -5.68 };
  114. await animateCamera(
  115. oldCameraPosition,
  116. { x: -2.27, y: -0.91, z: -5.67 },
  117. { x: 66.257, y: 57.539, z: 94.313 },
  118. { x: position.x, y: position.y, z: position.z },
  119. model,
  120. 0.6
  121. );
  122. }, 300);
  123. }
  124. });
  125. };
  126. export const mountedThree = (playerVal1, playerVal2) => {
  127. return new Promise(async (resolve) => {
  128. model = new UseThree('#damper3D', '', '#deviceDetail');
  129. model.setEnvMap('test1');
  130. model.renderer.toneMappingExposure = 1.0;
  131. model.camera.position.set(100, 0, 1000);
  132. // 单道、 双道
  133. fm1 = new Fm1(model, playerVal1, playerVal2);
  134. await fm1.mountedThree();
  135. fm2 = new Fm2(model, playerVal1);
  136. await fm2.mountedThree();
  137. model.animate();
  138. resolve(null);
  139. startAnimation();
  140. });
  141. };
  142. export const destroy = () => {
  143. if (model) {
  144. model.isRender = false;
  145. console.log('场景销毁前信息----------->', model.renderer?.info);
  146. if (fm1) fm1.destroy();
  147. if (fm2) fm2.destroy();
  148. fm1 = null;
  149. fm2 = null;
  150. group = null;
  151. model.mixers = [];
  152. model.destroy();
  153. }
  154. model = null;
  155. };