fireDoor.threejs.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import * as THREE from 'three';
  2. import UseThree from '../../../../utils/threejs/useThree';
  3. import FireDoor from './fireDoor.threejs.fire';
  4. import FireDoorF from './fireDoor.threejs.fireF';
  5. import { animateCamera } from '/@/utils/threejs/util';
  6. import useEvent from '../../../../utils/threejs/useEvent';
  7. import { useGlobSetting } from '/@/hooks/setting';
  8. // 模型对象、 文字对象
  9. let model,
  10. fireDoor, //液压风门
  11. fireDoorF, //液压风门
  12. group: THREE.Object3D,
  13. fmType = '';
  14. const { mouseDownFn } = useEvent();
  15. // 初始化左右摇摆动画
  16. const startAnimation = () => {
  17. // 定义鼠标点击事件
  18. model.canvasContainer?.addEventListener('mousedown', mouseEvent.bind(null));
  19. model.canvasContainer?.addEventListener('pointerup', (event) => {
  20. event.stopPropagation();
  21. // 单道、 双道
  22. if (fmType === 'fireDoor') {
  23. fireDoor?.mouseUpModel.call(fireDoor);
  24. }
  25. if (fmType === 'fireDoorF') {
  26. fireDoorF?.mouseUpModel.call(fireDoorF);
  27. }
  28. });
  29. };
  30. // 鼠标点击、松开事件
  31. const mouseEvent = (event) => {
  32. if (event.button == 0) {
  33. mouseDownFn(model, group, event, (intersects) => {
  34. if (fmType === 'fireDoor' && fireDoor) {
  35. fireDoor?.mousedownModel.call(fireDoor, intersects);
  36. }
  37. if (fmType === 'fireDoorF' && fireDoorF) {
  38. fireDoorF?.mousedownModel.call(fireDoorF, intersects);
  39. }
  40. });
  41. // console.log('摄像头控制信息', model.orbitControls, model.camera);
  42. }
  43. };
  44. export const play = (handlerState, flag?) => {
  45. debugger;
  46. if (fmType === 'fireDoor' && fireDoor) {
  47. return fireDoor.play.call(fireDoor, handlerState, flag);
  48. }
  49. if (fmType === 'fireDoorF' && fireDoorF) {
  50. return fireDoorF.play.call(fireDoorF, handlerState, flag);
  51. }
  52. };
  53. // 切换风门类型
  54. export const setModelType = (type) => {
  55. fmType = type;
  56. return new Promise((resolve) => {
  57. // 暂停风门1动画
  58. if (fmType === 'fireDoor' && fireDoor && fireDoor.group) {
  59. if (fireDoor.clipActionArr.door) {
  60. fireDoor.clipActionArr.door.reset();
  61. fireDoor.clipActionArr.door.time = 0.5;
  62. fireDoor.clipActionArr.door.stop();
  63. }
  64. if (fireDoor.damperOpenMesh) fireDoor.damperOpenMesh.visible = false;
  65. if (fireDoor.damperClosedMesh) fireDoor.damperClosedMesh.visible = true;
  66. model.scene.remove(group);
  67. model.startAnimation = fireDoor.render.bind(fireDoor);
  68. group = fireDoor.group;
  69. group.rotation.y = 0;
  70. const oldCameraPosition = { x: -1000, y: 100, z: 500 };
  71. setTimeout(async () => {
  72. resolve(null);
  73. model.scene.add(fireDoor.group);
  74. await animateCamera(
  75. oldCameraPosition,
  76. { x: 0, y: 0, z: 0 },
  77. { x: -654.2006991449887, y: 103.16181473511944, z: -30.348656073478562 },
  78. { x: -7.380506513422206, y: 56.36967052459397, z: -29.230675020846963 },
  79. model,
  80. 0.8
  81. );
  82. }, 300);
  83. } else if (fmType === 'fireDoorF' && fireDoorF && fireDoorF.group) {
  84. if (fireDoorF.clipActionArr.door) {
  85. fireDoorF.clipActionArr.door.reset();
  86. fireDoorF.clipActionArr.door.time = 0;
  87. fireDoorF.clipActionArr.door.stop();
  88. }
  89. model.scene.remove(group);
  90. model.startAnimation = fireDoorF.render.bind(fireDoorF);
  91. group = fireDoorF.group;
  92. group.rotation.y = 0;
  93. const oldCameraPosition = { x: -1000, y: 100, z: 500 };
  94. setTimeout(async () => {
  95. resolve(null);
  96. model.scene.add(fireDoorF.group);
  97. await animateCamera(
  98. oldCameraPosition,
  99. { x: 0, y: 0, z: 0 },
  100. { x: -655.0169729333649, y: 47.24181078408825, z: -9.781926649842067 },
  101. { x: -7.380506513422206, y: 47.24181078408821, z: -37.9244016972381 },
  102. model,
  103. 0.8
  104. );
  105. }, 300);
  106. }
  107. });
  108. };
  109. export const initCameraCanvas = async (playerVal1) => {
  110. if (fmType === 'fireDoor' && fireDoor) {
  111. return await fireDoor.initCamera.call(fireDoor, playerVal1);
  112. } else if (fmType === 'fireDoorF' && fireDoorF) {
  113. return await fireDoorF.initCamera.call(fireDoorF, playerVal1);
  114. }
  115. };
  116. const setControls = () => {
  117. if (model && model.orbitControls) {
  118. model.orbitControls.maxPolarAngle = Math.PI / 2;
  119. model.orbitControls.minPolarAngle = Math.PI / 3;
  120. model.orbitControls.minDistance = 600;
  121. model.orbitControls.maxDistance = 900;
  122. }
  123. };
  124. export const mountedThree = () => {
  125. // const { sysOrgCode } = useGlobSetting();
  126. return new Promise(async (resolve) => {
  127. model = new UseThree('#damper3D', '', '#deviceDetail');
  128. model.setEnvMap('test1');
  129. model.renderer.toneMappingExposure = 1.0;
  130. model.camera.position.set(100, 0, 1000);
  131. fireDoor = new FireDoor(model);
  132. fireDoor.mountedThree();
  133. fireDoorF = new FireDoorF(model);
  134. fireDoorF.mountedThree();
  135. resolve(null);
  136. setControls();
  137. model.animate();
  138. startAnimation();
  139. });
  140. };
  141. export const destroy = () => {
  142. if (model) {
  143. model.orbitControls.maxPolarAngle = Math.PI;
  144. model.orbitControls.minPolarAngle = 0;
  145. model.orbitControls.enableRotate = true;
  146. model.orbitControls.minDistance = 0;
  147. model.orbitControls.maxDistance = Infinity;
  148. model.orbitControls.update();
  149. model.isRender = false;
  150. if (fireDoor) fireDoor.destroy();
  151. fireDoor = null;
  152. group = null;
  153. model.mixers = [];
  154. model.destroy();
  155. }
  156. model = null;
  157. };