gate.threejs.xr.ts 19 KB

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