gate.threejs.xc.ts 19 KB

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