gate.threejs.window.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. import * as THREE from 'three';
  2. import { CSS2DObject } from 'three/examples/jsm/renderers/CSS2DRenderer.js';
  3. import { getTextCanvas, renderVideo } from '/@/utils/threejs/util';
  4. import gsap from 'gsap';
  5. import { drawHot } from '/@/utils/threejs/util';
  6. import { useAppStore } from '/@/store/modules/app';
  7. // import * as dat from 'dat.gui';
  8. // const gui = new dat.GUI();
  9. // gui.domElement.style = 'position:absolute;top:100px;left:10px;z-index:99999999999999';
  10. class FmDc {
  11. modelName = 'fmDc';
  12. model; //
  13. group;
  14. isLRAnimation = true; // 是否开启左右摇摆动画
  15. direction = 1; // 摇摆方向
  16. animationTimer: NodeJS.Timeout | null = null; // 摇摆开启定时器
  17. player1;
  18. player2;
  19. deviceDetailCSS3D;
  20. playerStartClickTime1 = new Date().getTime();
  21. playerStartClickTime2 = new Date().getTime();
  22. fmClock = new THREE.Clock();
  23. mixers: THREE.AnimationMixer | undefined;
  24. appStore = useAppStore();
  25. // backLeftDamperOpenMesh;
  26. // backLeftDamperClosedMesh;
  27. // frontLeftDamperOpenMesh;
  28. // frontLeftDamperClosedMesh;
  29. // backRightDamperOpenMesh;
  30. // backRightDamperClosedMesh;
  31. // frontRightDamperOpenMesh;
  32. // frontRightDamperClosedMesh;
  33. clipActionArr = {
  34. frontDoor: null as unknown as THREE.AnimationAction,
  35. backDoor: null as unknown as THREE.AnimationAction,
  36. };
  37. windowsActionArr = {
  38. frontLeftWindow: <THREE.Mesh[]>[],
  39. backLeftWindow: <THREE.Mesh[]>[],
  40. frontRightWindow: <THREE.Mesh[]>[],
  41. backRightWindow: <THREE.Mesh[]>[],
  42. };
  43. constructor(model) {
  44. this.model = model;
  45. }
  46. addLight() {}
  47. // 重置摄像头
  48. resetCamera() {
  49. this.model.camera.far = 274;
  50. this.model.orbitControls?.update();
  51. this.model.camera.updateProjectionMatrix();
  52. }
  53. // 设置模型位置
  54. setModalPosition() {
  55. this.group?.scale.set(22, 22, 22);
  56. this.group?.position.set(-20, 20, 9);
  57. }
  58. /* 添加监控数据 */
  59. addMonitorText(selectData) {
  60. if (!this.group) {
  61. return;
  62. }
  63. const screenDownText = VENT_PARAM['modalText']
  64. ? VENT_PARAM['modalText']
  65. : History_Type['type'] == 'remote'
  66. ? `国能神东煤炭集团监制`
  67. : '煤炭科学技术研究院有限公司研制';
  68. const screenDownTextX = 80 - (screenDownText.length - 10) * 6;
  69. const textArr = [
  70. {
  71. text: `远程控制自动风门`,
  72. font: 'normal 30px Arial',
  73. color: '#00FF00',
  74. strokeStyle: '#007400',
  75. x: 120,
  76. y: 100,
  77. },
  78. {
  79. text: `净通行高度(m):`,
  80. font: 'normal 30px Arial',
  81. color: '#00FF00',
  82. strokeStyle: '#007400',
  83. x: 0,
  84. y: 155,
  85. },
  86. {
  87. text: `${selectData.fclearheight ? selectData.fclearheight : '-'}`,
  88. font: 'normal 30px Arial',
  89. color: '#00FF00',
  90. strokeStyle: '#007400',
  91. x: 295,
  92. y: 155,
  93. },
  94. {
  95. text: `净通行宽度(m): `,
  96. font: 'normal 30px Arial',
  97. color: '#00FF00',
  98. strokeStyle: '#007400',
  99. x: 0,
  100. y: 215,
  101. },
  102. {
  103. text: ` ${selectData.fclearwidth ? selectData.fclearwidth : '-'}`,
  104. font: 'normal 30px Arial',
  105. color: '#00FF00',
  106. strokeStyle: '#007400',
  107. x: 290,
  108. y: 215,
  109. },
  110. {
  111. text: `故障诊断:`,
  112. font: 'normal 30px Arial',
  113. color: '#00FF00',
  114. strokeStyle: '#007400',
  115. x: 0,
  116. y: 275,
  117. },
  118. {
  119. text: `${selectData.warnLevel_str ? selectData.warnLevel_str : '-'}`,
  120. font: 'normal 30px Arial',
  121. color: '#00FF00',
  122. strokeStyle: '#007400',
  123. x: 290,
  124. y: 275,
  125. },
  126. {
  127. text: screenDownText,
  128. font: 'normal 28px Arial',
  129. color: '#00FF00',
  130. strokeStyle: '#007400',
  131. x: screenDownTextX,
  132. y: 320,
  133. },
  134. ];
  135. //
  136. getTextCanvas(526, 346, textArr, '').then((canvas: HTMLCanvasElement) => {
  137. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  138. textMap.colorSpace = THREE.SRGBColorSpace;
  139. const textMaterial = new THREE.MeshBasicMaterial({
  140. // 关于材质并未讲解 实操即可熟悉 这里是漫反射类似纸张的材质,对应的就有高光类似金属的材质.
  141. map: textMap, // 设置纹理贴图
  142. transparent: true,
  143. side: THREE.FrontSide, // 这里是双面渲染的意思
  144. });
  145. textMaterial.blending = THREE.CustomBlending;
  146. const monitorPlane = this.group.getObjectByName('monitorText');
  147. if (monitorPlane) {
  148. monitorPlane.material = textMaterial;
  149. } else {
  150. const planeGeometry = new THREE.PlaneGeometry(526, 346); // 平面3维几何体PlaneGeometry
  151. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  152. planeMesh.name = 'monitorText';
  153. planeMesh.scale.set(0.002, 0.002, 0.002);
  154. planeMesh.position.set(4.025, 0.67, -0.27);
  155. this.group.add(planeMesh);
  156. }
  157. textMap.dispose();
  158. });
  159. }
  160. /** 添加热点 */
  161. drawHots() {
  162. const hotPositions = [
  163. { x: -0.37, y: 0.26, z: -0.32 },
  164. { x: 0.28, y: -0.2, z: -0.43 },
  165. { x: 0.55, y: -0.22, z: -0.38 },
  166. ];
  167. for (let i = 0; i < 3; i++) {
  168. const hotPoint = drawHot(0.1);
  169. const position = hotPositions[i];
  170. hotPoint.scale.set(0.1, 0.1, 0.1);
  171. hotPoint.position.set(position.x, position.y, position.z);
  172. hotPoint.name = 'hotPoint' + i;
  173. this.group?.add(hotPoint);
  174. }
  175. }
  176. /* 风门动画 */
  177. render() {
  178. if (!this.model) {
  179. return;
  180. }
  181. if (this.mixers && this.fmClock.running) {
  182. this.mixers.update(2);
  183. }
  184. }
  185. /* 点击风窗,风窗全屏 */
  186. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  187. if (this.animationTimer) {
  188. clearTimeout(this.animationTimer);
  189. this.animationTimer = null;
  190. }
  191. }
  192. mouseUpModel() {}
  193. /* 提取风门序列帧,初始化前后门动画 */
  194. initAnimation() {
  195. const fmGroup = this.group?.getObjectByName('fm-window');
  196. if (fmGroup) {
  197. const tracks = fmGroup.animations[0].tracks;
  198. const fontTracks: any[] = [],
  199. backTracks: any[] = [];
  200. for (let i = 0; i < tracks.length; i++) {
  201. const track = tracks[i];
  202. if (track.name.includes('Men_1') || track.name.includes('Men_2')) {
  203. fontTracks.push(track);
  204. } else if (track.name.includes('Men_3') || track.name.includes('Men_4')) {
  205. backTracks.push(track);
  206. }
  207. }
  208. const parentGroup = fmGroup.getObjectByName('QiDongTiaoJieFengMen_1');
  209. // const frontGroup = parentGroup.getObjectByName('FengMen2');
  210. // const backGroup = parentGroup.getObjectByName('FengMen1');
  211. this.mixers = new THREE.AnimationMixer(parentGroup);
  212. const frontDoor = new THREE.AnimationClip('frontDoor', 2.5, fontTracks);
  213. const frontClipAction = this.mixers.clipAction(frontDoor, parentGroup);
  214. frontClipAction.clampWhenFinished = true;
  215. frontClipAction.loop = THREE.LoopOnce;
  216. this.clipActionArr.frontDoor = frontClipAction;
  217. const backDoor = new THREE.AnimationClip('backDoor', 2.5, backTracks);
  218. const backClipAction = this.mixers.clipAction(backDoor, parentGroup);
  219. backClipAction.clampWhenFinished = true;
  220. backClipAction.loop = THREE.LoopOnce;
  221. this.clipActionArr.backDoor = backClipAction;
  222. }
  223. // 编写风窗
  224. }
  225. /* 提取风门序列帧,初始化前后门动画 */
  226. initWindowAnimation() {
  227. const meshArr01: THREE.Object3D[] = []; //front left
  228. const meshArr02: THREE.Object3D[] = []; //front right
  229. const meshArr03: THREE.Object3D[] = []; //back left
  230. const meshArr04: THREE.Object3D[] = []; //back right
  231. const windowGroup = new THREE.Group();
  232. windowGroup.name = 'hiddenGroup';
  233. const fmGroup = this.group?.getObjectByName('fm-window');
  234. const parentGroup = fmGroup.getObjectByName('QiDongTiaoJieFengMen_1');
  235. const frontGroup = parentGroup.getObjectByName('FengMen_1');
  236. const backGroup = parentGroup.getObjectByName('FengMen_2');
  237. const frontLeftObj = frontGroup.getObjectByName('Men_2').getObjectByName('shanye_2');
  238. const frontRightObj = frontGroup.getObjectByName('Men_1').getObjectByName('shanye_1');
  239. const backLeftObj = backGroup.getObjectByName('Men_4').getObjectByName('shanye_4');
  240. const backRightObj = backGroup.getObjectByName('Men_3').getObjectByName('shanye_3');
  241. frontLeftObj.traverse((obj) => {
  242. if (obj.type === 'Mesh' && obj.name && obj.name.startsWith('shanye')) {
  243. obj.rotateOnAxis(new THREE.Vector3(0, 0, 1), 0);
  244. meshArr01.push(obj);
  245. }
  246. });
  247. frontRightObj.traverse((obj) => {
  248. if (obj.type === 'Mesh' && obj.name && obj.name.startsWith('shanye')) {
  249. obj.rotateOnAxis(new THREE.Vector3(0, 0, 1), 0);
  250. meshArr02.push(obj);
  251. }
  252. });
  253. backLeftObj.traverse((obj) => {
  254. if (obj.type === 'Mesh' && obj.name && obj.name.startsWith('shanye')) {
  255. obj.rotateOnAxis(new THREE.Vector3(0, 0, 1), 0);
  256. meshArr03.push(obj);
  257. }
  258. });
  259. backRightObj.traverse((obj) => {
  260. if (obj.type === 'Mesh' && obj.name && obj.name.startsWith('shanye')) {
  261. obj.rotateOnAxis(new THREE.Vector3(0, 0, 1), 0);
  262. meshArr04.push(obj);
  263. }
  264. });
  265. this.windowsActionArr.frontLeftWindow = meshArr01;
  266. this.windowsActionArr.frontRightWindow = meshArr02;
  267. this.windowsActionArr.backLeftWindow = meshArr03;
  268. this.windowsActionArr.backRightWindow = meshArr04;
  269. this.group?.add(windowGroup);
  270. }
  271. deviceDetailCard(position = { x: 0, y: 0, z: 0 }) {
  272. const element = document.getElementById('deviceCard') as HTMLElement;
  273. if (element) {
  274. this.deviceDetailCSS3D = new CSS2DObject(element);
  275. this.deviceDetailCSS3D.name = 'deviceCard';
  276. this.deviceDetailCSS3D.position.set(position.x, position.y, position.z);
  277. this.deviceDetailCSS3D.visible = false;
  278. // this.model.scene.add(this.deviceDetailCSS3D);
  279. this.group.add(this.deviceDetailCSS3D);
  280. }
  281. }
  282. // 播放动画
  283. play(handlerState, timeScale = 0.01) {
  284. let handler = () => {};
  285. if (this.clipActionArr.frontDoor && this.clipActionArr.backDoor) {
  286. switch (handlerState) {
  287. case 1: // 打开前门
  288. handler = () => {
  289. this.clipActionArr.frontDoor.paused = true;
  290. this.clipActionArr.frontDoor.reset();
  291. this.clipActionArr.frontDoor.time = 0;
  292. this.clipActionArr.frontDoor.timeScale = timeScale;
  293. // this.clipActionArr.frontDoor.clampWhenFinished = true;
  294. this.clipActionArr.frontDoor.play();
  295. this.fmClock.start();
  296. // 显示打开前门文字
  297. if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = true;
  298. if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = false;
  299. };
  300. break;
  301. case 2: // 关闭前门
  302. handler = () => {
  303. this.clipActionArr.frontDoor.paused = true;
  304. this.clipActionArr.frontDoor.reset(); //
  305. this.clipActionArr.frontDoor.time = 2.5;
  306. this.clipActionArr.frontDoor.timeScale = -timeScale;
  307. // this.clipActionArr.frontDoor.clampWhenFinished = true;
  308. this.clipActionArr.frontDoor.play();
  309. this.fmClock.start();
  310. if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = false;
  311. if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = true;
  312. };
  313. break;
  314. case 3: // 打开后门
  315. handler = () => {
  316. this.clipActionArr.backDoor.paused = true;
  317. this.clipActionArr.backDoor.reset();
  318. this.clipActionArr.backDoor.time = 0;
  319. this.clipActionArr.backDoor.timeScale = timeScale;
  320. // this.clipActionArr.backDoor.clampWhenFinished = true;
  321. this.clipActionArr.backDoor.play();
  322. this.fmClock.start();
  323. if (this.backDamperOpenMesh) this.backDamperOpenMesh.visible = true;
  324. if (this.backDamperClosedMesh) this.backDamperClosedMesh.visible = false;
  325. };
  326. break;
  327. case 4: // 关闭后门
  328. handler = () => {
  329. this.clipActionArr.backDoor.paused = true;
  330. this.clipActionArr.backDoor.reset();
  331. this.clipActionArr.backDoor.time = 2.5;
  332. this.clipActionArr.backDoor.timeScale = -timeScale;
  333. // this.clipActionArr.backDoor.clampWhenFinished = true;
  334. this.clipActionArr.backDoor.play();
  335. this.fmClock.start();
  336. if (this.backDamperOpenMesh) this.backDamperOpenMesh.visible = false;
  337. if (this.backDamperClosedMesh) this.backDamperClosedMesh.visible = true;
  338. };
  339. break;
  340. default:
  341. }
  342. handler();
  343. }
  344. }
  345. playWindow(rotationParam, flag) {
  346. if (
  347. !this.windowsActionArr.frontLeftWindow ||
  348. !this.windowsActionArr.frontRightWindow ||
  349. !this.windowsActionArr.backLeftWindow ||
  350. !this.windowsActionArr.backRightWindow ||
  351. this.windowsActionArr.frontLeftWindow.length <= 0 ||
  352. this.windowsActionArr.frontRightWindow.length <= 0 ||
  353. this.windowsActionArr.backLeftWindow.length <= 0 ||
  354. this.windowsActionArr.backRightWindow.length <= 0
  355. ) {
  356. return;
  357. }
  358. if (flag === 1) {
  359. // 前门左风窗动画
  360. this.windowsActionArr.frontLeftWindow.forEach((mesh) => {
  361. gsap.to(mesh.rotation, {
  362. z: THREE.MathUtils.degToRad(rotationParam.frontLeftDeg1),
  363. duration: (1 / 9) * Math.abs(rotationParam.frontLeftDeg1 - mesh.rotation.z),
  364. overwrite: true,
  365. });
  366. });
  367. } else if (flag === 2) {
  368. // 后门左风窗动画
  369. this.windowsActionArr.backLeftWindow.forEach((mesh) => {
  370. gsap.to(mesh.rotation, {
  371. z: THREE.MathUtils.degToRad(rotationParam.backLeftDeg1),
  372. duration: (1 / 9) * Math.abs(rotationParam.backLeftDeg1 - mesh.rotation.z),
  373. overwrite: true,
  374. });
  375. });
  376. } else if (flag === 3) {
  377. // 前门右风窗动画
  378. this.windowsActionArr.frontRightWindow.forEach((mesh) => {
  379. gsap.to(mesh.rotation, {
  380. z: THREE.MathUtils.degToRad(rotationParam.frontRightDeg1),
  381. duration: (1 / 9) * Math.abs(rotationParam.frontRightDeg1 - mesh.rotation.z),
  382. overwrite: true,
  383. });
  384. });
  385. } else if (flag === 4) {
  386. // 后门右风窗动画
  387. this.windowsActionArr.backRightWindow.forEach((mesh) => {
  388. gsap.to(mesh.rotation, {
  389. z: THREE.MathUtils.degToRad(rotationParam.backRightDeg1),
  390. duration: (1 / 9) * Math.abs(rotationParam.backRightDeg1 - mesh.rotation.z),
  391. overwrite: true,
  392. });
  393. });
  394. } else if (flag === 0) {
  395. (
  396. [
  397. ...this.windowsActionArr.frontLeftWindow,
  398. ...this.windowsActionArr.frontRightWindow,
  399. ...this.windowsActionArr.backLeftWindow,
  400. ...this.windowsActionArr.backRightWindow,
  401. ] as THREE.Mesh[]
  402. ).forEach((mesh) => {
  403. gsap.to(mesh.rotation, {
  404. z: 0,
  405. overwrite: true,
  406. });
  407. });
  408. }
  409. }
  410. async initCamera(dom1) {
  411. const videoPlayer1 = dom1;
  412. this.player1 = dom1;
  413. let monitorPlane: THREE.Mesh | null = null;
  414. if (!videoPlayer1) {
  415. const textArr = [
  416. {
  417. text: `无信号输入`,
  418. font: 'normal 40px Arial',
  419. color: '#009900',
  420. strokeStyle: '#002200',
  421. x: 170,
  422. y: 40,
  423. },
  424. ];
  425. const canvas = await getTextCanvas(320, 180, '', 'noSinge.png');
  426. let textMaterial: THREE.MeshBasicMaterial | null = null;
  427. if (canvas) {
  428. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  429. textMaterial = new THREE.MeshBasicMaterial({
  430. map: textMap, // 设置纹理贴图
  431. transparent: true,
  432. side: THREE.DoubleSide, // 这里是双面渲染的意思
  433. });
  434. textMaterial.blending = THREE.CustomBlending;
  435. const planeGeometry = new THREE.PlaneGeometry(100, 100); // 平面3维几何体PlaneGeometry
  436. monitorPlane = new THREE.Mesh(planeGeometry, textMaterial);
  437. textMaterial.dispose();
  438. planeGeometry.dispose();
  439. textMap.dispose();
  440. }
  441. }
  442. const player1 = this.group.getObjectByName('player1');
  443. if (player1) {
  444. this.model.clearMesh(player1);
  445. this.group.remove(player1);
  446. }
  447. const noPlayer1 = this.group.getObjectByName('noPlayer1');
  448. if (noPlayer1) {
  449. this.model.clearMesh(noPlayer1);
  450. this.group.remove(noPlayer1);
  451. }
  452. if (!videoPlayer1 && videoPlayer1 === null) {
  453. if (monitorPlane && !this.group.getObjectByName('noPlayer1')) {
  454. const planeMesh = monitorPlane.clone();
  455. planeMesh.name = 'noPlayer1';
  456. planeMesh.scale.set(0.0085, 0.0055, 0.012);
  457. planeMesh.position.set(-3.64, 0.01, -0.41);
  458. this.group?.add(planeMesh.clone());
  459. }
  460. } else if (videoPlayer1) {
  461. try {
  462. const mesh = renderVideo(this.group, videoPlayer1, 'player1');
  463. if (mesh) {
  464. mesh?.scale.set(-0.0275, 0.028, 1);
  465. mesh?.position.set(-3.643, 0.02, -0.4);
  466. mesh.rotation.y = -Math.PI;
  467. this.group.add(mesh);
  468. }
  469. } catch (error) {
  470. console.log('视频信号异常');
  471. }
  472. }
  473. }
  474. mountedThree(playerDom) {
  475. this.group = new THREE.Object3D();
  476. this.group.name = this.modelName;
  477. return new Promise((resolve) => {
  478. this.model.setGLTFModel(['fm-window'], this.group).then(() => {
  479. console.log('带风窗风门模型----->', this.group);
  480. this.setModalPosition();
  481. // 初始化左右摇摆动画;
  482. this.initAnimation();
  483. this.initWindowAnimation();
  484. // this.drawHots();
  485. this.addLight();
  486. // this.deviceDetailCard();
  487. this.model.animate();
  488. resolve(this.model);
  489. });
  490. });
  491. }
  492. destroy() {
  493. if (this.model) {
  494. if (this.mixers) {
  495. this.mixers.uncacheClip(this.clipActionArr.frontDoor.getClip());
  496. this.mixers.uncacheClip(this.clipActionArr.backDoor.getClip());
  497. this.mixers.uncacheAction(this.clipActionArr.frontDoor.getClip(), this.group);
  498. this.mixers.uncacheAction(this.clipActionArr.backDoor.getClip(), this.group);
  499. this.mixers.uncacheRoot(this.group);
  500. if (this.model.animations[0]) this.model.animations[0].tracks = [];
  501. }
  502. this.model.clearGroup(this.group);
  503. this.clipActionArr.backDoor = undefined;
  504. this.clipActionArr.frontDoor = undefined;
  505. this.windowsActionArr.frontWindow = undefined;
  506. this.windowsActionArr.backWindow = undefined;
  507. this.mixers = undefined;
  508. }
  509. }
  510. }
  511. export default FmDc;