windrect.threejs.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import * as THREE from 'three';
  2. import { animateCamera, getTextCanvas, renderVideo } from '/@/utils/threejs/util';
  3. import UseThree from '../../../../utils/threejs/useThree';
  4. import lmWindRect from './longmen.threejs';
  5. import zdWindRect from './zhedie.threejs';
  6. import dsWindRect from './duishe.threejs';
  7. import lmWindRectSide from './longmenSide.threejs';
  8. import { useAppStore } from '/@/store/modules/app';
  9. import gsap from 'gsap';
  10. // import * as dat from 'dat.gui';
  11. // const gui = new dat.GUI();
  12. // gui.domElement.style = 'position:absolute;top:100px;left:10px;z-index:99999999999999';
  13. // 模型对象、 文字对象
  14. let model, //
  15. group,
  16. lmWindRectObj,
  17. zdWindRectObj,
  18. dsWindRectObj,
  19. lmWindRectSideObj,
  20. windRectType = 'lmWindRect';
  21. const appStore = useAppStore();
  22. // 初始化左右摇摆动画
  23. const startAnimation = () => {
  24. // 定义鼠标点击事件
  25. model.canvasContainer?.addEventListener('mousedown', mouseEvent.bind(null));
  26. model.canvasContainer?.addEventListener('pointerup', (event) => {
  27. event.stopPropagation();
  28. // 单道、 双道
  29. if (windRectType === 'lmWindRect') {
  30. lmWindRectObj.mouseUpModel.call(lmWindRectObj);
  31. } else if (windRectType === 'zdWindRect') {
  32. zdWindRectObj.mouseUpModel.call(zdWindRectObj);
  33. } else if (windRectType === 'dsWindRect') {
  34. dsWindRectObj.mouseUpModel.call(dsWindRectObj);
  35. } else if (windRectType === 'lmWindSide') {
  36. lmWindRectSideObj.mouseUpModel.call(lmWindRectSideObj);
  37. }
  38. });
  39. };
  40. // 鼠标点击、松开事件
  41. const mouseEvent = (event) => {
  42. event.stopPropagation();
  43. const widthScale = appStore.getWidthScale;
  44. const heightScale = appStore.getHeightScale;
  45. // 将鼠标位置归一化为设备坐标。x 和 y 方向的取值范围是 (-1 to +1)
  46. model.mouse.x =
  47. ((-model.canvasContainer.getBoundingClientRect().left * widthScale + event.clientX) / (model.canvasContainer.clientWidth * widthScale)) * 2 - 1;
  48. model.mouse.y =
  49. -((-model.canvasContainer.getBoundingClientRect().top + event.clientY) / (model.canvasContainer.clientHeight * heightScale)) * 2 + 1;
  50. (model.rayCaster as THREE.Raycaster).setFromCamera(model.mouse, model.camera as THREE.Camera);
  51. if (group) {
  52. const intersects = model.rayCaster?.intersectObjects(group.children, false) as THREE.Intersection[];
  53. if (intersects.length > 0) {
  54. // 单道、 双道
  55. if (windRectType === 'lmWindRect') {
  56. lmWindRectObj.mousedownModel.call(lmWindRectObj, intersects);
  57. } else if (windRectType === 'zdWindRect') {
  58. zdWindRectObj.mousedownModel.call(zdWindRectObj, intersects);
  59. } else if (windRectType === 'dsWindRect') {
  60. dsWindRectObj.mousedownModel.call(dsWindRectObj, intersects);
  61. } else if (windRectType === 'lmWindSide') {
  62. lmWindRectSideObj.mousedownModel.call(lmWindRectSideObj, intersects);
  63. }
  64. }
  65. }
  66. };
  67. /* 添加监控数据 */
  68. export const addFmText = (selectData) => {
  69. if (windRectType === 'lmWindRect') {
  70. return lmWindRectObj.addFmText.call(lmWindRectObj, selectData);
  71. } else if (windRectType === 'zdWindRect') {
  72. return zdWindRectObj.addFmText.call(zdWindRectObj, selectData);
  73. } else if (windRectType === 'dsWindRect') {
  74. return dsWindRectObj.addFmText.call(dsWindRectObj, selectData);
  75. } else if (windRectType === 'lmWindSide') {
  76. lmWindRectSideObj.addFmText.call(lmWindRectSideObj, selectData);
  77. }
  78. };
  79. export const play = (flag, isDirect = false) => {
  80. if (windRectType === 'lmWindRect') {
  81. return lmWindRectObj.play.call(lmWindRectObj, flag, isDirect);
  82. } else if (windRectType === 'zdWindRect') {
  83. return zdWindRectObj.play.call(zdWindRectObj, flag, isDirect);
  84. } else if (windRectType === 'dsWindRect') {
  85. return dsWindRectObj.play.call(dsWindRectObj, flag, isDirect);
  86. } else if (windRectType === 'lmWindSide') {
  87. lmWindRectSideObj.play.call(lmWindRectSideObj, flag, isDirect);
  88. }
  89. };
  90. export const playCamera = (flag) => {
  91. if (windRectType === 'lmWindSide') {
  92. lmWindRectSideObj.playCamera.call(lmWindRectSideObj, flag);
  93. }
  94. };
  95. // 切换风窗类型
  96. export const setModelType = (type) => {
  97. windRectType = type;
  98. model.camera.position.set(-1000, 100, 500);
  99. // 暂停龙门动画
  100. const lmGsap = gsap.getById('lm') || null;
  101. if (lmGsap != null) {
  102. lmGsap.pause();
  103. lmGsap.kill();
  104. }
  105. lmWindRectObj.deviceRunState = '';
  106. dsWindRectObj.isRun = false;
  107. lmWindRectSideObj.deviceRunState = '';
  108. lmWindRectSideObj.tanTouRunState = '';
  109. return new Promise((resolve) => {
  110. // 显示双道风窗
  111. if (windRectType === 'lmWindRect') {
  112. model.startAnimation = lmWindRectObj.render.bind(lmWindRectObj);
  113. group = lmWindRectObj.group;
  114. const cfTanTou = lmWindRectObj.group?.getObjectByName('lmTanTou') as THREE.Group;
  115. cfTanTou.position.setY(0);
  116. if (model.scene.getObjectByName('zdcf')) {
  117. model.scene.remove(zdWindRectObj.group);
  118. }
  119. if (model.scene.getObjectByName('dscf')) {
  120. model.scene.remove(dsWindRectObj.group);
  121. }
  122. if (model.scene.getObjectByName('lmcfSide')) {
  123. model.scene.remove(lmWindRectSideObj.group);
  124. }
  125. const oldCameraPosition = { x: -1000, y: 100, z: 500 };
  126. model.scene.add(lmWindRectObj.group);
  127. setTimeout(async () => {
  128. resolve(null);
  129. const position = lmWindRectObj.group.position;
  130. await animateCamera(
  131. oldCameraPosition,
  132. oldCameraPosition,
  133. { x: 46.257, y: 57.539, z: 94.313 },
  134. { x: position.x, y: position.y, z: position.z },
  135. model,
  136. 0.8
  137. );
  138. }, 300);
  139. } else if (windRectType === 'zdWindRect') {
  140. // 停用混合器上所有预定的动作
  141. zdWindRectObj.animationAction.time = 0;
  142. model.startAnimation = zdWindRectObj.render.bind(zdWindRectObj);
  143. group = zdWindRectObj.group;
  144. if (model.scene.getObjectByName('lmcf')) {
  145. model.scene.remove(lmWindRectObj.group);
  146. }
  147. if (model.scene.getObjectByName('dscf')) {
  148. model.scene.remove(dsWindRectObj.group);
  149. }
  150. if (model.scene.getObjectByName('lmcfSide')) {
  151. model.scene.remove(lmWindRectSideObj.group);
  152. }
  153. model.scene.add(zdWindRectObj.group);
  154. const oldCameraPosition = { x: -1000, y: 100, z: 500 };
  155. setTimeout(async () => {
  156. resolve(null);
  157. const position = zdWindRectObj.group.position;
  158. await animateCamera(
  159. oldCameraPosition,
  160. oldCameraPosition,
  161. { x: 66.257, y: 57.539, z: 104.313 },
  162. { x: position.x, y: position.y, z: position.z },
  163. model,
  164. 0.8
  165. );
  166. }, 300);
  167. } else if (windRectType === 'dsWindRect') {
  168. // dsWindRectObj.isRun = false;
  169. model.startAnimation = dsWindRectObj.render.bind(dsWindRectObj);
  170. group = dsWindRectObj.group;
  171. const dsTanTou = dsWindRectObj.group?.getObjectByName('dsTanTou') as THREE.Group;
  172. dsTanTou.position.setY(0);
  173. if (model.scene.getObjectByName('lmcf')) {
  174. model.scene.remove(lmWindRectObj.group);
  175. }
  176. if (model.scene.getObjectByName('zdcf')) {
  177. model.scene.remove(zdWindRectObj.group);
  178. }
  179. if (model.scene.getObjectByName('lmcfSide')) {
  180. model.scene.remove(lmWindRectSideObj.group);
  181. }
  182. model.scene.add(dsWindRectObj.group);
  183. setTimeout(async () => {
  184. resolve(null);
  185. const position = dsWindRectObj.group.position;
  186. const oldCameraPosition = { x: -1020, y: 100, z: 500 };
  187. await animateCamera(
  188. oldCameraPosition,
  189. oldCameraPosition,
  190. { x: 46.257, y: 57.539, z: 104.313 },
  191. { x: position.x, y: position.y, z: position.z },
  192. model,
  193. 0.8
  194. );
  195. }, 300);
  196. } else if (windRectType === 'lmWindSide') {
  197. model.startAnimation = lmWindRectSideObj.render.bind(lmWindRectSideObj);
  198. group = lmWindRectSideObj.group;
  199. const cfTanTou = lmWindRectSideObj.group?.getObjectByName('probe_bar') as THREE.Group;
  200. cfTanTou.position.setY(0.377);
  201. if (model.scene.getObjectByName('lmcf')) {
  202. model.scene.remove(lmWindRectObj.group);
  203. }
  204. if (model.scene.getObjectByName('zdcf')) {
  205. model.scene.remove(zdWindRectObj.group);
  206. }
  207. if (model.scene.getObjectByName('dscf')) {
  208. model.scene.remove(dsWindRectObj.group);
  209. }
  210. const oldCameraPosition = { x: -1000, y: 100, z: 500 };
  211. model.scene.add(lmWindRectSideObj.group);
  212. setTimeout(async () => {
  213. resolve(null);
  214. const position = lmWindRectSideObj.group.position;
  215. // const position = new THREE.Vector3(0, 0, 0)
  216. await animateCamera(
  217. oldCameraPosition,
  218. oldCameraPosition,
  219. { x: 46.257, y: 57.539, z: 94.313 },
  220. // { x: 1, y: 2, z: 4 },
  221. { x: position.x, y: position.y, z: position.z },
  222. model,
  223. 0.8
  224. );
  225. // lmWindRectSideObj.playCamera(true);
  226. }, 300);
  227. }
  228. });
  229. };
  230. export const mountedThree = (playerVal1, playerVal2) => {
  231. return new Promise(async (resolve) => {
  232. model = new UseThree('#window3D');
  233. model.setEnvMap('test1');
  234. model.renderer.toneMappingExposure = 1;
  235. lmWindRectObj = new lmWindRect(model, playerVal1, playerVal2);
  236. await lmWindRectObj.mountedThree();
  237. zdWindRectObj = new zdWindRect(model, playerVal1);
  238. await zdWindRectObj.mountedThree();
  239. dsWindRectObj = new dsWindRect(model, playerVal1);
  240. await dsWindRectObj.mountedThree();
  241. lmWindRectSideObj = new lmWindRectSide(model, playerVal1, playerVal2);
  242. await lmWindRectSideObj.mountedThree();
  243. resolve(null);
  244. model.animate();
  245. startAnimation();
  246. });
  247. };
  248. export const destroy = () => {
  249. if (model) {
  250. lmWindRectObj?.destroy();
  251. zdWindRectObj?.destroy();
  252. zdWindRectObj?.destroy();
  253. lmWindRectSideObj?.destroy();
  254. model.deleteModal();
  255. model = null;
  256. group = null;
  257. }
  258. };