1
0

gate.threejs.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import * as THREE from 'three';
  2. import UseThree from '../../../../utils/threejs/useThree';
  3. import Fm1 from './gate.threejs.yy';
  4. import Fm3 from './gate.threejs.qd';
  5. import Fm2 from './gate.threejs.three';
  6. import { animateCamera } from '/@/utils/threejs/util';
  7. import useEvent from '../../../../utils/threejs/useEvent';
  8. // 模型对象、 文字对象
  9. let model,
  10. fm1: Fm1,
  11. fm2: Fm2,
  12. fm3: Fm3,
  13. group: THREE.Object3D,
  14. fmType = '';
  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 (fmType === 'fm1') {
  24. fm1.mouseUpModel.call(fm1);
  25. } else if (fmType === 'fm2') {
  26. fm2.mouseUpModel.call(fm2);
  27. } else if (fmType === 'fm3') {
  28. fm3.mouseUpModel.call(fm3);
  29. }
  30. });
  31. };
  32. // 鼠标点击、松开事件
  33. const mouseEvent = (event) => {
  34. if (event.button == 0) {
  35. mouseDownFn(model, group, event, (intersects) => {
  36. if (fmType === 'fm1' && fm1) {
  37. fm1.mousedownModel.call(fm1, intersects);
  38. } else if (fmType === 'fm2' && fm2) {
  39. fm2.mousedownModel.call(fm2, intersects);
  40. } else if (fmType === 'fm3' && fm3) {
  41. fm3.mousedownModel.call(fm3, intersects);
  42. }
  43. });
  44. // console.log('摄像头控制信息', model.orbitControls, model.camera);
  45. }
  46. };
  47. export const addMonitorText = (selectData) => {
  48. if (fmType === 'fm1' && fm1) {
  49. return fm1.addMonitorText.call(fm1, selectData);
  50. } else if (fmType === 'fm2' && fm2) {
  51. return fm2.addMonitorText.call(fm2, selectData);
  52. } else if (fmType === 'fm3' && fm3) {
  53. return fm3.addMonitorText.call(fm3, selectData);
  54. }
  55. };
  56. export const deviceDetailCard = () => {
  57. if (fmType === 'fm1') {
  58. return fm1.deviceDetailCard.call(fm1);
  59. } else if (fmType === 'fm3') {
  60. return fm3.deviceDetailCard.call(fm3);
  61. } else {
  62. // return fm2.addMonitorText.call(fm2);
  63. }
  64. };
  65. export const play = (handlerState, flag?) => {
  66. if (fmType === 'fm1' && fm1) {
  67. return fm1.play.call(fm1, handlerState, flag);
  68. } else if (fmType === 'fm2' && fm2) {
  69. return fm2.play.call(fm2, handlerState, flag);
  70. } else if (fmType === 'fm3' && fm3) {
  71. return fm3.play.call(fm3, handlerState, flag);
  72. }
  73. };
  74. // 切换风门类型
  75. export const setModelType = (type) => {
  76. fmType = type;
  77. return new Promise((resolve) => {
  78. // 暂停风门1动画
  79. if (fmType === 'fm1' && fm1 && fm1.group) {
  80. fm1.clipActionArr.frontDoor.reset();
  81. fm1.clipActionArr.frontDoor.time = 0.5;
  82. fm1.clipActionArr.backDoor.reset();
  83. fm1.clipActionArr.backDoor.time = 0.5;
  84. fm1.clipActionArr.frontDoor.stop();
  85. fm1.clipActionArr.backDoor.stop();
  86. if (fm1.frontDamperOpenMesh) fm1.frontDamperOpenMesh.visible = false;
  87. if (fm1.frontDamperClosedMesh) fm1.frontDamperClosedMesh.visible = true;
  88. if (fm1.backDamperOpenMesh) fm1.backDamperOpenMesh.visible = false;
  89. if (fm1.backDamperClosedMesh) fm1.backDamperClosedMesh.visible = true;
  90. model.startAnimation = fm1.render.bind(fm1);
  91. group = fm1.group;
  92. group.rotation.y = 0;
  93. if (model.scene.getObjectByName('fm2')) {
  94. model.scene.remove(fm2.group);
  95. }
  96. if (model.scene.getObjectByName('fm3')) {
  97. model.scene.remove(fm3.group);
  98. }
  99. const oldCameraPosition = { x: -1000, y: 100, z: 500 };
  100. setTimeout(async () => {
  101. resolve(null);
  102. model.scene.add(fm1.group);
  103. await animateCamera(
  104. oldCameraPosition,
  105. { x: 0, y: 0, z: 0 },
  106. { x: 50.99, y: 69.32, z: 93.61 },
  107. { x: -10.04, y: -14.38, z: -31.4 },
  108. model,
  109. 0.8
  110. );
  111. }, 300);
  112. } else if (fmType === 'fm2' && fm2 && fm2.group) {
  113. fm2.clipActionArr.frontDoor.reset();
  114. fm2.clipActionArr.frontDoor.time = 0.5;
  115. fm2.clipActionArr.backDoor.reset();
  116. fm2.clipActionArr.backDoor.time = 0.5;
  117. fm2.clipActionArr.centerDoor.reset();
  118. fm2.clipActionArr.centerDoor.time = 0.5;
  119. fm2.clipActionArr.frontDoor.stop();
  120. fm2.clipActionArr.backDoor.stop();
  121. fm2.clipActionArr.centerDoor.stop();
  122. // 显示单道风窗
  123. model.startAnimation = fm2.render.bind(fm2);
  124. group = fm2.group;
  125. if (model.scene.getObjectByName('fm1')) {
  126. model.scene.remove(fm1.group);
  127. }
  128. if (model.scene.getObjectByName('fm3')) {
  129. model.scene.remove(fm3.group);
  130. }
  131. const oldCameraPosition = { x: -761, y: 569, z: 871 };
  132. setTimeout(async () => {
  133. resolve(null);
  134. model.scene.add(fm2.group);
  135. const position = { x: -2.28, y: -0.91, z: -5.68 };
  136. await animateCamera(
  137. oldCameraPosition,
  138. { x: -2.27, y: -0.91, z: -5.67 },
  139. { x: 66.257, y: 57.539, z: 94.313 },
  140. { x: position.x, y: position.y, z: position.z },
  141. model,
  142. 0.6
  143. );
  144. }, 300);
  145. } else if (fmType === 'fm3' && fm3 && fm3.group) {
  146. fm3.clipActionArr.frontDoor.reset();
  147. fm3.clipActionArr.frontDoor.time = 0.5;
  148. fm3.clipActionArr.backDoor.reset();
  149. fm3.clipActionArr.backDoor.time = 0.5;
  150. fm3.clipActionArr.frontDoor.stop();
  151. fm3.clipActionArr.backDoor.stop();
  152. if (fm3.frontDamperOpenMesh) fm3.frontDamperOpenMesh.visible = false;
  153. if (fm3.frontDamperClosedMesh) fm3.frontDamperClosedMesh.visible = true;
  154. if (fm3.backDamperOpenMesh) fm3.backDamperOpenMesh.visible = false;
  155. if (fm3.backDamperClosedMesh) fm3.backDamperClosedMesh.visible = true;
  156. model.startAnimation = fm3.render.bind(fm3);
  157. group = fm3.group;
  158. group.rotation.y = 0;
  159. if (model.scene.getObjectByName('fm2')) {
  160. model.scene.remove(fm2.group);
  161. }
  162. if (model.scene.getObjectByName('fm1')) {
  163. model.scene.remove(fm1.group);
  164. }
  165. const oldCameraPosition = { x: -1000, y: 100, z: 500 };
  166. setTimeout(async () => {
  167. resolve(null);
  168. model.scene.add(fm3.group);
  169. await animateCamera(
  170. oldCameraPosition,
  171. { x: 0, y: 0, z: 0 },
  172. { x: 50.99, y: 69.32, z: 93.61 },
  173. { x: -10.04, y: -14.38, z: -31.4 },
  174. model,
  175. 0.8
  176. );
  177. }, 300);
  178. }
  179. });
  180. };
  181. export const initCameraCanvas = async (playerVal1) => {
  182. if (fmType === 'fm1' && fm1) {
  183. return await fm1.initCamera.call(fm1, playerVal1);
  184. } else if (fmType === 'fm2' && fm2) {
  185. return fm2.initCamera.call(fm2, playerVal1);
  186. } else if (fmType === 'fm3' && fm3) {
  187. return fm3.initCamera.call(fm3, playerVal1);
  188. }
  189. };
  190. export const mountedThree = (playerDom) => {
  191. return new Promise(async (resolve) => {
  192. model = new UseThree('#damper3D', '', '#deviceDetail');
  193. model.setEnvMap('test1');
  194. model.renderer.toneMappingExposure = 1.0;
  195. model.camera.position.set(100, 0, 1000);
  196. // 单道、 双道
  197. fm1 = new Fm1(model);
  198. await fm1.mountedThree(playerDom);
  199. fm2 = new Fm2(model);
  200. await fm2.mountedThree(playerDom);
  201. fm3 = new Fm3(model);
  202. await fm3.mountedThree(playerDom);
  203. model.animate();
  204. resolve(null);
  205. startAnimation();
  206. });
  207. };
  208. export const destroy = () => {
  209. if (model) {
  210. model.isRender = false;
  211. if (fm1) fm1.destroy();
  212. if (fm2) fm2.destroy();
  213. if (fm3) fm3.destroy();
  214. fm1 = null;
  215. fm2 = null;
  216. fm3 = null;
  217. group = null;
  218. model.mixers = [];
  219. model.destroy();
  220. }
  221. model = null;
  222. };