gate.threejs.xr.ts 18 KB

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