fireDoor.threejs.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 FireDoorSsl from './fireDoor.threejs.ssl';
  6. import { animateCamera } from '/@/utils/threejs/util';
  7. import useEvent from '../../../../utils/threejs/useEvent';
  8. // 模型对象、 文字对象
  9. let model,
  10. fireDoor, //液压风门
  11. fireDoorF, //液压风门
  12. fireDoorSsl, // 思山岭防火门
  13. group: THREE.Object3D,
  14. fhmType = '';
  15. const { mouseDownFn } = useEvent();
  16. // 初始化左右摇摆动画
  17. const startAnimation = () => {
  18. // 定义鼠标点击事件
  19. model.canvasContainer?.addEventListener('mousedown', mouseEvent.bind(null));
  20. model.canvasContainer?.addEventListener('pointerup', (event) => {
  21. event.stopPropagation();
  22. // 单道、 双道
  23. if (fhmType === 'fireDoor') {
  24. fireDoor?.mouseUpModel.call(fireDoor);
  25. }
  26. if (fhmType === 'fireDoorF') {
  27. fireDoorF?.mouseUpModel.call(fireDoorF);
  28. }
  29. if (fhmType === 'fireDoorSsl') {
  30. fireDoorSsl?.mouseUpModel.call(fireDoorSsl);
  31. }
  32. });
  33. };
  34. // 鼠标点击、松开事件
  35. const mouseEvent = (event) => {
  36. if (event.button == 0) {
  37. mouseDownFn(model, group, event, (intersects) => {
  38. if (fhmType === 'fireDoor' && fireDoor) {
  39. fireDoor?.mousedownModel.call(fireDoor, intersects);
  40. }
  41. if (fhmType === 'fireDoorF' && fireDoorF) {
  42. fireDoorF?.mousedownModel.call(fireDoorF, intersects);
  43. }
  44. if (fhmType === 'fireDoorSsl' && fireDoorSsl) {
  45. fireDoorSsl?.mousedownModel.call(fireDoorSsl, intersects);
  46. }
  47. });
  48. // console.log('摄像头控制信息', model.orbitControls, model.camera);
  49. }
  50. };
  51. export const play = (handlerState, flag?) => {
  52. if (fhmType === 'fireDoor' && fireDoor) {
  53. return fireDoor.play.call(fireDoor, handlerState, flag);
  54. }
  55. if (fhmType === 'fireDoorF' && fireDoorF) {
  56. return fireDoorF.play.call(fireDoorF, handlerState, flag);
  57. }
  58. if (fhmType === 'fireDoorSsl' && fireDoorSsl) {
  59. return fireDoorSsl.play.call(fireDoorSsl, handlerState, flag);
  60. }
  61. };
  62. // 切换风门类型
  63. export const setModelType = (type) => {
  64. fhmType = type;
  65. return new Promise((resolve) => {
  66. // 暂停风门1动画
  67. if (fhmType === 'fireDoor' && fireDoor && fireDoor.group) {
  68. if (fireDoor.clipActionArr.door) {
  69. fireDoor.clipActionArr.door.reset();
  70. fireDoor.clipActionArr.door.time = 0.5;
  71. fireDoor.clipActionArr.door.stop();
  72. }
  73. if (fireDoor.damperOpenMesh) fireDoor.damperOpenMesh.visible = false;
  74. if (fireDoor.damperClosedMesh) fireDoor.damperClosedMesh.visible = true;
  75. model.scene.remove(group);
  76. model.startAnimation = fireDoor.render.bind(fireDoor);
  77. group = fireDoor.group;
  78. group.rotation.y = 0;
  79. const oldCameraPosition = { x: -1000, y: 100, z: 500 };
  80. setTimeout(async () => {
  81. resolve(null);
  82. model.scene.add(fireDoor.group);
  83. await animateCamera(
  84. oldCameraPosition,
  85. { x: 0, y: 0, z: 0 },
  86. { x: -654.2006991449887, y: 103.16181473511944, z: -30.348656073478562 },
  87. { x: -7.380506513422206, y: 56.36967052459397, z: -29.230675020846963 },
  88. model,
  89. 0.8
  90. );
  91. }, 300);
  92. } else if (fhmType === 'fireDoorF' && fireDoorF && fireDoorF.group) {
  93. if (fireDoorF.clipActionArr.door) {
  94. fireDoorF.clipActionArr.door.reset();
  95. fireDoorF.clipActionArr.door.time = 0;
  96. fireDoorF.clipActionArr.door.stop();
  97. }
  98. model.scene.remove(group);
  99. model.startAnimation = fireDoorF.render.bind(fireDoorF);
  100. group = fireDoorF.group;
  101. group.rotation.y = 0;
  102. const oldCameraPosition = { x: -1000, y: 100, z: 500 };
  103. setTimeout(async () => {
  104. resolve(null);
  105. model.scene.add(fireDoorF.group);
  106. await animateCamera(
  107. oldCameraPosition,
  108. { x: 0, y: 0, z: 0 },
  109. { x: -655.0169729333649, y: 47.24181078408825, z: -9.781926649842067 },
  110. { x: -7.380506513422206, y: 47.24181078408821, z: -37.9244016972381 },
  111. model,
  112. 0.8
  113. );
  114. }, 300);
  115. } else if (fhmType === 'fireDoorSsl' && fireDoorSsl && fireDoorSsl.group) {
  116. // if (fireDoorSsl.clipActionArr.door) {
  117. // fireDoorSsl.clipActionArr.door.reset();
  118. // fireDoorSsl.clipActionArr.door.time = 0;
  119. // fireDoorSsl.clipActionArr.door.stop();
  120. // }
  121. // model.scene.remove(group);
  122. // model.startAnimation = fireDoorSsl.render.bind(fireDoorSsl);
  123. group = fireDoorSsl.group;
  124. group.rotation.y = 0;
  125. const oldCameraPosition = { x: -1000, y: 100, z: 500 };
  126. setTimeout(async () => {
  127. resolve(null);
  128. model.scene.add(fireDoorSsl.group);
  129. await animateCamera(
  130. oldCameraPosition,
  131. { x: 0, y: 0, z: 0 },
  132. {
  133. x: 342.74781900192056,
  134. y: 183.50210411099545,
  135. z: 451.0806333923029,
  136. },
  137. {
  138. x: 72.33938301176254,
  139. y: -35.03891296652319,
  140. z: -37.91742549963208,
  141. },
  142. model,
  143. 0.8
  144. );
  145. }, 300);
  146. }
  147. });
  148. };
  149. export const initCameraCanvas = async (playerVal1) => {
  150. if (fhmType === 'fireDoor' && fireDoor) {
  151. return await fireDoor.initCamera.call(fireDoor, playerVal1);
  152. } else if (fhmType === 'fireDoorF' && fireDoorF) {
  153. return await fireDoorF.initCamera.call(fireDoorF, playerVal1);
  154. } else if (fhmType === 'fireDoorSsl' && fireDoorSsl) {
  155. return await fireDoorSsl.initCamera.call(fireDoorSsl, playerVal1);
  156. }
  157. };
  158. const setControls = () => {
  159. if (model && model.orbitControls) {
  160. model.orbitControls.maxPolarAngle = Math.PI / 2;
  161. model.orbitControls.minPolarAngle = Math.PI / 3;
  162. model.orbitControls.minDistance = 600;
  163. model.orbitControls.maxDistance = 900;
  164. }
  165. };
  166. export const mountedThree = () => {
  167. return new Promise(async (resolve) => {
  168. model = new UseThree('#damper3D', '', '#deviceDetail');
  169. model.setEnvMap('test1.hdr');
  170. model.renderer.toneMappingExposure = 1.0;
  171. model.camera.position.set(100, 0, 1000);
  172. fireDoor = new FireDoor(model);
  173. fireDoor.mountedThree();
  174. fireDoorF = new FireDoorF(model);
  175. fireDoorF.mountedThree();
  176. fireDoorSsl = new FireDoorSsl(model);
  177. fireDoorSsl.mountedThree();
  178. resolve(null);
  179. setControls();
  180. model.animate();
  181. startAnimation();
  182. });
  183. };
  184. export const destroy = () => {
  185. if (model) {
  186. model.orbitControls.maxPolarAngle = Math.PI;
  187. model.orbitControls.minPolarAngle = 0;
  188. model.orbitControls.enableRotate = true;
  189. model.orbitControls.minDistance = 0;
  190. model.orbitControls.maxDistance = Infinity;
  191. model.orbitControls.update();
  192. model.isRender = false;
  193. if (fireDoor) fireDoor.destroy();
  194. fireDoor = null;
  195. // @ts-ignore-next-line
  196. group = null;
  197. model.mixers = [];
  198. model.destroy();
  199. }
  200. model = null;
  201. };