gate.threejs.window.hjg.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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 FmDcHJG {
  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: 290,
  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: 280,
  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: 280,
  124. y: 275,
  125. },
  126. {
  127. text: screenDownText,
  128. font: 'normal 28px Arial',
  129. color: '#00FF00',
  130. strokeStyle: '#007400',
  131. x: screenDownTextX,
  132. y: 325,
  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-hjg');
  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('_1')) {
  203. fontTracks.push(track);
  204. } else {
  205. backTracks.push(track);
  206. }
  207. }
  208. const parentGroup = fmGroup.getObjectByName('MeiCangLianLuoHangFengMen');
  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-hjg');
  234. const parentGroup = fmGroup.getObjectByName('MeiCangLianLuoHangFengMen');
  235. const frontGroup = parentGroup.getObjectByName('FengMen2');
  236. const backGroup = parentGroup.getObjectByName('FengMen');
  237. const frontLeftObj = frontGroup.getObjectByName('FengMen_L_1');
  238. const frontRightObj = frontGroup.getObjectByName('FengMen_R_1');
  239. const backLeftObj = backGroup.getObjectByName('FengMen_L');
  240. const backRightObj = backGroup.getObjectByName('FengMen_R');
  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.length <= 0 ||
  348. this.windowsActionArr.frontRightWindow.length <= 0 ||
  349. this.windowsActionArr.backLeftWindow.length <= 0 ||
  350. this.windowsActionArr.backRightWindow.length <= 0
  351. ) {
  352. return;
  353. }
  354. if (flag === 1) {
  355. // 前门左风窗动画
  356. this.windowsActionArr.frontLeftWindow.forEach((mesh) => {
  357. gsap.to(mesh.rotation, {
  358. z: THREE.MathUtils.degToRad(rotationParam.frontLeftDeg1),
  359. duration: (1 / 9) * Math.abs(rotationParam.frontLeftDeg1 - mesh.rotation.z),
  360. overwrite: true,
  361. });
  362. });
  363. } else if (flag === 2) {
  364. // 后门左风窗动画
  365. this.windowsActionArr.backLeftWindow.forEach((mesh) => {
  366. gsap.to(mesh.rotation, {
  367. z: THREE.MathUtils.degToRad(rotationParam.backLeftDeg1),
  368. duration: (1 / 9) * Math.abs(rotationParam.backLeftDeg1 - mesh.rotation.z),
  369. overwrite: true,
  370. });
  371. });
  372. } else if (flag === 3) {
  373. // 前门右风窗动画
  374. this.windowsActionArr.frontRightWindow.forEach((mesh) => {
  375. gsap.to(mesh.rotation, {
  376. z: THREE.MathUtils.degToRad(rotationParam.frontRightDeg1),
  377. duration: (1 / 9) * Math.abs(rotationParam.frontRightDeg1 - mesh.rotation.z),
  378. overwrite: true,
  379. });
  380. });
  381. } else if (flag === 4) {
  382. // 后门右风窗动画
  383. this.windowsActionArr.backRightWindow.forEach((mesh) => {
  384. gsap.to(mesh.rotation, {
  385. z: THREE.MathUtils.degToRad(rotationParam.backRightDeg1),
  386. duration: (1 / 9) * Math.abs(rotationParam.backRightDeg1 - mesh.rotation.z),
  387. overwrite: true,
  388. });
  389. });
  390. } else if (flag === 0) {
  391. (
  392. [
  393. ...this.windowsActionArr.frontLeftWindow,
  394. ...this.windowsActionArr.frontRightWindow,
  395. ...this.windowsActionArr.backLeftWindow,
  396. ...this.windowsActionArr.backRightWindow,
  397. ] as THREE.Mesh[]
  398. ).forEach((mesh) => {
  399. gsap.to(mesh.rotation, {
  400. z: 0,
  401. overwrite: true,
  402. });
  403. });
  404. }
  405. }
  406. async initCamera(dom1) {
  407. const videoPlayer1 = dom1;
  408. this.player1 = dom1;
  409. let monitorPlane: THREE.Mesh | null = null;
  410. if (!videoPlayer1) {
  411. const textArr = [
  412. {
  413. text: `无信号输入`,
  414. font: 'normal 40px Arial',
  415. color: '#009900',
  416. strokeStyle: '#002200',
  417. x: 170,
  418. y: 40,
  419. },
  420. ];
  421. const canvas = await getTextCanvas(320, 180, '', 'noSinge.png');
  422. let textMaterial: THREE.MeshBasicMaterial | null = null;
  423. if (canvas) {
  424. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  425. textMaterial = new THREE.MeshBasicMaterial({
  426. map: textMap, // 设置纹理贴图
  427. transparent: true,
  428. side: THREE.DoubleSide, // 这里是双面渲染的意思
  429. });
  430. textMaterial.blending = THREE.CustomBlending;
  431. const planeGeometry = new THREE.PlaneGeometry(100, 100); // 平面3维几何体PlaneGeometry
  432. monitorPlane = new THREE.Mesh(planeGeometry, textMaterial);
  433. textMaterial.dispose();
  434. planeGeometry.dispose();
  435. textMap.dispose();
  436. }
  437. }
  438. const player1 = this.group.getObjectByName('player1');
  439. if (player1) {
  440. this.model.clearMesh(player1);
  441. this.group.remove(player1);
  442. }
  443. const noPlayer1 = this.group.getObjectByName('noPlayer1');
  444. if (noPlayer1) {
  445. this.model.clearMesh(noPlayer1);
  446. this.group.remove(noPlayer1);
  447. }
  448. if (!videoPlayer1 && videoPlayer1 === null) {
  449. if (monitorPlane && !this.group.getObjectByName('noPlayer1')) {
  450. const planeMesh = monitorPlane.clone();
  451. planeMesh.name = 'noPlayer1';
  452. planeMesh.scale.set(0.0085, 0.0055, 0.012);
  453. planeMesh.position.set(-3.64, 0.01, -0.41);
  454. this.group?.add(planeMesh.clone());
  455. }
  456. } else if (videoPlayer1) {
  457. try {
  458. const mesh = renderVideo(this.group, videoPlayer1, 'player1');
  459. if (mesh) {
  460. mesh?.scale.set(-0.0275, 0.028, 1);
  461. mesh?.position.set(-3.643, 0.02, -0.4);
  462. mesh.rotation.y = -Math.PI;
  463. this.group.add(mesh);
  464. }
  465. } catch (error) {
  466. console.log('视频信号异常');
  467. }
  468. }
  469. }
  470. mountedThree(playerDom) {
  471. this.group = new THREE.Object3D();
  472. this.group.name = this.modelName;
  473. return new Promise((resolve) => {
  474. this.model.setGLTFModel(['fm-window-hjg'], this.group).then(() => {
  475. console.log('带风窗风门模型----->', this.group);
  476. this.setModalPosition();
  477. // 初始化左右摇摆动画;
  478. this.initAnimation();
  479. this.initWindowAnimation();
  480. // this.drawHots();
  481. this.addLight();
  482. // this.deviceDetailCard();
  483. this.model.animate();
  484. resolve(this.model);
  485. });
  486. });
  487. }
  488. destroy() {
  489. if (this.model) {
  490. if (this.mixers) {
  491. this.mixers.uncacheClip(this.clipActionArr.frontDoor.getClip());
  492. this.mixers.uncacheClip(this.clipActionArr.backDoor.getClip());
  493. this.mixers.uncacheAction(this.clipActionArr.frontDoor.getClip(), this.group);
  494. this.mixers.uncacheAction(this.clipActionArr.backDoor.getClip(), this.group);
  495. this.mixers.uncacheRoot(this.group);
  496. if (this.model.animations[0]) this.model.animations[0].tracks = [];
  497. }
  498. this.model.clearGroup(this.group);
  499. this.clipActionArr.backDoor = undefined;
  500. this.clipActionArr.frontDoor = undefined;
  501. this.windowsActionArr.frontWindow = undefined;
  502. this.windowsActionArr.backWindow = undefined;
  503. this.mixers = undefined;
  504. }
  505. }
  506. }
  507. export default FmDcHJG;