gate.threejs.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. if (fmType === 'fm1' && fm1 && fm1.group) {
  68. fm1.clipActionArr.frontDoor.reset();
  69. fm1.clipActionArr.frontDoor.time = 0.5;
  70. fm1.clipActionArr.backDoor.reset();
  71. fm1.clipActionArr.backDoor.time = 0.5;
  72. fm1.clipActionArr.frontDoor.stop();
  73. fm1.clipActionArr.backDoor.stop();
  74. if (fm1.frontDamperOpenMesh) fm1.frontDamperOpenMesh.visible = false;
  75. if (fm1.frontDamperClosedMesh) fm1.frontDamperClosedMesh.visible = true;
  76. if (fm1.backDamperOpenMesh) fm1.backDamperOpenMesh.visible = false;
  77. if (fm1.backDamperClosedMesh) fm1.backDamperClosedMesh.visible = true;
  78. model.startAnimation = fm1.render.bind(fm1);
  79. group = fm1.group;
  80. group.rotation.y = 0;
  81. if (model.scene.getObjectByName('fm2')) {
  82. model.scene.remove(fm2.group);
  83. }
  84. const oldCameraPosition = { x: -1000, y: 100, z: 500 };
  85. setTimeout(async () => {
  86. resolve(null);
  87. model.scene.add(fm1.group);
  88. await animateCamera(
  89. oldCameraPosition,
  90. { x: 0, y: 0, z: 0 },
  91. { x: 50.99, y: 69.32, z: 93.61 },
  92. { x: -10.04, y: -14.38, z: -31.4 },
  93. model,
  94. 0.8
  95. );
  96. }, 300);
  97. } else if (fmType === 'fm2' && fm2 && fm2.group) {
  98. fm2.clipActionArr.frontDoor.reset();
  99. fm2.clipActionArr.frontDoor.time = 0.5;
  100. fm2.clipActionArr.backDoor.reset();
  101. fm2.clipActionArr.backDoor.time = 0.5;
  102. fm2.clipActionArr.centerDoor.reset();
  103. fm2.clipActionArr.centerDoor.time = 0.5;
  104. fm2.clipActionArr.frontDoor.stop();
  105. fm2.clipActionArr.backDoor.stop();
  106. fm2.clipActionArr.centerDoor.stop();
  107. // 显示单道风窗
  108. model.startAnimation = fm2.render.bind(fm2);
  109. group = fm2.group;
  110. if (model.scene.getObjectByName('fm1')) {
  111. model.scene.remove(fm1.group);
  112. }
  113. const oldCameraPosition = { x: -761, y: 569, z: 871 };
  114. setTimeout(async () => {
  115. resolve(null);
  116. model.scene.add(fm2.group);
  117. const position = { x: -2.28, y: -0.91, z: -5.68 };
  118. await animateCamera(
  119. oldCameraPosition,
  120. { x: -2.27, y: -0.91, z: -5.67 },
  121. { x: 66.257, y: 57.539, z: 94.313 },
  122. { x: position.x, y: position.y, z: position.z },
  123. model,
  124. 0.6
  125. );
  126. }, 300);
  127. }
  128. });
  129. };
  130. export const initCameraCanvas = async (playerVal1?, playerVal2?) => {
  131. if (fmType === 'fm1' && fm1) {
  132. return await fm1.initCamera.call(fm1, playerVal1, playerVal2);
  133. } else if (fmType === 'fm2' && fm2) {
  134. console.log('fm2-------', playerVal1);
  135. return fm2.initCamera.call(fm2, playerVal1);
  136. }
  137. };
  138. export const mountedThree = () => {
  139. return new Promise(async (resolve) => {
  140. model = new UseThree('#damper3D', '', '#deviceDetail');
  141. model.setEnvMap('test1.hdr');
  142. model.renderer.toneMappingExposure = 1.0;
  143. model.camera.position.set(100, 0, 1000);
  144. // 单道、 双道
  145. fm1 = new Fm1(model);
  146. await fm1.mountedThree();
  147. fm2 = new Fm2(model);
  148. await fm2.mountedThree();
  149. model.animate();
  150. resolve(null);
  151. startAnimation();
  152. });
  153. };
  154. export const destroy = () => {
  155. if (model) {
  156. model.isRender = false;
  157. console.log('场景销毁前信息----------->', model.renderer?.info);
  158. if (fm1) fm1.destroy();
  159. if (fm2) fm2.destroy();
  160. fm1 = null;
  161. fm2 = null;
  162. group = null;
  163. model.mixers = [];
  164. model.destroy();
  165. }
  166. model = null;
  167. };