gate.threejs.xc.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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) * 6;
  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.isLRAnimation && this.group) {
  205. // 左右摇摆动画
  206. if (Math.abs(this.group.rotation.y) >= 0.2) {
  207. this.direction = -this.direction;
  208. this.group.rotation.y += 0.00002 * 30 * this.direction;
  209. } else {
  210. this.group.rotation.y += 0.00002 * 30 * this.direction;
  211. }
  212. }
  213. if (this.mixers && this.fmClock.running) {
  214. this.mixers.update(2);
  215. }
  216. }
  217. /* 点击风窗,风窗全屏 */
  218. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  219. this.isLRAnimation = false;
  220. if (this.animationTimer) {
  221. clearTimeout(this.animationTimer);
  222. this.animationTimer = null;
  223. }
  224. // 判断是否点击到视频
  225. intersects.find((intersect) => {
  226. const mesh = intersect.object;
  227. // if (mesh.name === 'player1') {
  228. // if (new Date().getTime() - this.playerStartClickTime1 < 400) {
  229. // // 双击,视频放大
  230. // if (this.player1) {
  231. // this.player1.requestPictureInPicture();
  232. // }
  233. // }
  234. // this.playerStartClickTime1 = new Date().getTime();
  235. // return true;
  236. // } else if (mesh.name === 'player2') {
  237. // if (new Date().getTime() - this.playerStartClickTime2 < 400) {
  238. // // 双击,视频放大
  239. // if (this.player2) {
  240. // this.player2.requestPictureInPicture();
  241. // }
  242. // }
  243. // this.playerStartClickTime2 = new Date().getTime();
  244. // return true;
  245. // } else if (mesh.name.startsWith('hotPoint')) {
  246. // if (this.deviceDetailCSS3D) {
  247. // this.deviceDetailCSS3D.position.set(mesh.position.x + 0.035, mesh.position.y + 0.68, mesh.position.z + 0.02);
  248. // console.log('[ deviceDetailCSS3D.position ] >', this.deviceDetailCSS3D.position);
  249. // this.deviceDetailCSS3D.visible = true;
  250. // return true;
  251. // }
  252. // } else {
  253. // if (this.deviceDetailCSS3D) this.deviceDetailCSS3D.visible = false;
  254. // console.log('[ 点击事件 ] >');
  255. // }
  256. return false;
  257. });
  258. }
  259. mouseUpModel() {
  260. // 10s后开始摆动
  261. if (!this.animationTimer && !this.isLRAnimation) {
  262. this.animationTimer = setTimeout(() => {
  263. this.isLRAnimation = true;
  264. }, 10000);
  265. }
  266. }
  267. /* 提取风门序列帧,初始化前后门动画 */
  268. initAnimation() {
  269. const fmGroup = this.group?.getObjectByName('fmXr-door');
  270. if (fmGroup) {
  271. const tracks = fmGroup.animations[0].tracks;
  272. const fontTracks: any[] = [],
  273. backTracks: any[] = [];
  274. for (let i = 0; i < tracks.length; i++) {
  275. const track = tracks[i];
  276. if (track.name.startsWith('qianmen')) {
  277. fontTracks.push(track);
  278. } else if (track.name.startsWith('houmen')) {
  279. backTracks.push(track);
  280. }
  281. }
  282. this.mixers = new THREE.AnimationMixer(fmGroup);
  283. const frontDoor = new THREE.AnimationClip('frontDoor', 4, fontTracks);
  284. const frontClipAction = this.mixers.clipAction(frontDoor, fmGroup);
  285. frontClipAction.clampWhenFinished = true;
  286. frontClipAction.loop = THREE.LoopOnce;
  287. this.clipActionArr.frontDoor = frontClipAction;
  288. const backDoor = new THREE.AnimationClip('backDoor', 4, backTracks);
  289. const backClipAction = this.mixers.clipAction(backDoor, fmGroup);
  290. backClipAction.clampWhenFinished = true;
  291. backClipAction.loop = THREE.LoopOnce;
  292. this.clipActionArr.backDoor = backClipAction;
  293. }
  294. }
  295. deviceDetailCard(position = { x: 0, y: 0, z: 0 }) {
  296. const element = document.getElementById('deviceCard') as HTMLElement;
  297. if (element) {
  298. this.deviceDetailCSS3D = new CSS2DObject(element);
  299. this.deviceDetailCSS3D.name = 'deviceCard';
  300. this.deviceDetailCSS3D.position.set(position.x, position.y, position.z);
  301. this.deviceDetailCSS3D.visible = false;
  302. // this.model.scene.add(this.deviceDetailCSS3D);
  303. this.group.add(this.deviceDetailCSS3D);
  304. }
  305. }
  306. // 播放动画
  307. play(handlerState, timeScale = 0.01) {
  308. if (this.clipActionArr.frontDoor && this.clipActionArr.backDoor) {
  309. let handler = () => {};
  310. switch (handlerState) {
  311. case 1: // 打开前门
  312. handler = () => {
  313. this.clipActionArr.frontDoor.paused = true;
  314. this.clipActionArr.frontDoor.reset();
  315. this.clipActionArr.frontDoor.time = 1.2;
  316. this.clipActionArr.frontDoor.timeScale = timeScale;
  317. // this.clipActionArr.frontDoor.clampWhenFinished = true;
  318. this.clipActionArr.frontDoor.play();
  319. this.fmClock.start();
  320. // 显示打开前门文字
  321. if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = true;
  322. if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = false;
  323. };
  324. break;
  325. case 2: // 关闭前门
  326. handler = () => {
  327. this.clipActionArr.frontDoor.paused = true;
  328. this.clipActionArr.frontDoor.reset(); //
  329. this.clipActionArr.frontDoor.time = 2;
  330. this.clipActionArr.frontDoor.timeScale = -timeScale;
  331. // this.clipActionArr.frontDoor.clampWhenFinished = true;
  332. this.clipActionArr.frontDoor.play();
  333. this.fmClock.start();
  334. if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = false;
  335. if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = true;
  336. };
  337. break;
  338. case 3: // 打开后门
  339. handler = () => {
  340. this.clipActionArr.backDoor.paused = true;
  341. this.clipActionArr.backDoor.reset();
  342. this.clipActionArr.backDoor.time = 1.2;
  343. this.clipActionArr.backDoor.timeScale = timeScale;
  344. // this.clipActionArr.backDoor.clampWhenFinished = true;
  345. this.clipActionArr.backDoor.play();
  346. this.fmClock.start();
  347. if (this.backDamperOpenMesh) this.backDamperOpenMesh.visible = true;
  348. if (this.backDamperClosedMesh) this.backDamperClosedMesh.visible = false;
  349. };
  350. break;
  351. case 4: // 关闭后门
  352. handler = () => {
  353. this.clipActionArr.backDoor.paused = true;
  354. this.clipActionArr.backDoor.reset();
  355. this.clipActionArr.backDoor.time = 2;
  356. this.clipActionArr.backDoor.timeScale = -timeScale;
  357. // this.clipActionArr.backDoor.clampWhenFinished = true;
  358. this.clipActionArr.backDoor.play();
  359. this.fmClock.start();
  360. if (this.backDamperOpenMesh) this.backDamperOpenMesh.visible = false;
  361. if (this.backDamperClosedMesh) this.backDamperClosedMesh.visible = true;
  362. };
  363. break;
  364. // case 5: // 打开前后门
  365. // handler = () => {
  366. // this.clipActionArr.backDoor.paused = true;
  367. // this.clipActionArr.frontDoor.paused = true;
  368. // this.clipActionArr.frontDoor.reset();
  369. // this.clipActionArr.frontDoor.time = 0;
  370. // this.clipActionArr.frontDoor.timeScale = 0.01;
  371. // this.clipActionArr.frontDoor.clampWhenFinished = true;
  372. // this.clipActionArr.frontDoor.play();
  373. // this.clipActionArr.backDoor.reset();
  374. // this.clipActionArr.backDoor.time = 0;
  375. // this.clipActionArr.backDoor.timeScale = 0.01;
  376. // this.clipActionArr.backDoor.clampWhenFinished = true;
  377. // this.clipActionArr.backDoor.play();
  378. // this.frontClock.start();
  379. // this.backClock.start();
  380. // };
  381. // break;
  382. // case 6: // 关闭前后门
  383. // handler = () => {
  384. // debugger;
  385. // this.clipActionArr.backDoor.paused = true;
  386. // this.clipActionArr.frontDoor.paused = true;
  387. // this.clipActionArr.frontDoor.reset();
  388. // this.clipActionArr.frontDoor.time = 4;
  389. // this.clipActionArr.frontDoor.timeScale = -0.01;
  390. // this.clipActionArr.frontDoor.clampWhenFinished = true;
  391. // this.clipActionArr.frontDoor.play();
  392. // this.clipActionArr.backDoor.reset();
  393. // this.clipActionArr.backDoor.time = 4;
  394. // this.clipActionArr.backDoor.timeScale = -0.01;
  395. // this.clipActionArr.backDoor.clampWhenFinished = true;
  396. // this.clipActionArr.backDoor.play();
  397. // this.frontClock.start();
  398. // this.backClock.start();
  399. // };
  400. // break;
  401. default:
  402. }
  403. handler();
  404. // model.clock.start();
  405. // const honglvdeng = group.getObjectByName('honglvdeng');
  406. // const material = honglvdeng.material;
  407. // setTimeout(() => {
  408. // if (handlerState === 2 || handlerState === 4 || handlerState === 6) {
  409. // material.color = new THREE.Color(0x00ff00);
  410. // } else {
  411. // material.color = new THREE.Color(0xff0000);
  412. // }
  413. // }, 1000);
  414. }
  415. }
  416. async initCamera(dom1) {
  417. const videoPlayer1 = dom1;
  418. this.player1 = dom1;
  419. let monitorPlane: THREE.Mesh | null = null;
  420. if (!videoPlayer1) {
  421. const textArr = [
  422. {
  423. text: `无信号输入`,
  424. font: 'normal 40px Arial',
  425. color: '#009900',
  426. strokeStyle: '#002200',
  427. x: 170,
  428. y: 40,
  429. },
  430. ];
  431. const canvas = await getTextCanvas(320, 180, '', 'noSinge.png');
  432. let textMaterial: THREE.MeshBasicMaterial | null = null;
  433. if (canvas) {
  434. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  435. textMaterial = new THREE.MeshBasicMaterial({
  436. map: textMap, // 设置纹理贴图
  437. transparent: true,
  438. side: THREE.DoubleSide, // 这里是双面渲染的意思
  439. });
  440. textMaterial.blending = THREE.CustomBlending;
  441. const planeGeometry = new THREE.PlaneGeometry(100, 100); // 平面3维几何体PlaneGeometry
  442. monitorPlane = new THREE.Mesh(planeGeometry, textMaterial);
  443. textMaterial.dispose();
  444. planeGeometry.dispose();
  445. textMap.dispose();
  446. }
  447. }
  448. const player1 = this.group.getObjectByName('player1');
  449. if (player1) {
  450. this.model.clearMesh(player1);
  451. this.group.remove(player1);
  452. }
  453. const noPlayer1 = this.group.getObjectByName('noPlayer1');
  454. if (noPlayer1) {
  455. this.model.clearMesh(noPlayer1);
  456. this.group.remove(noPlayer1);
  457. }
  458. if (!videoPlayer1 && videoPlayer1 === null) {
  459. if (monitorPlane && !this.group.getObjectByName('noPlayer1')) {
  460. const planeMesh = monitorPlane.clone();
  461. planeMesh.name = 'noPlayer1';
  462. planeMesh.scale.set(0.0085, 0.0055, 0.012);
  463. planeMesh.position.set(-3.64, 0.01, -0.41);
  464. this.group?.add(planeMesh.clone());
  465. }
  466. } else if (videoPlayer1) {
  467. try {
  468. const mesh = renderVideo(this.group, videoPlayer1, 'player1');
  469. if (mesh) {
  470. mesh?.scale.set(-0.0275, 0.028, 1);
  471. mesh?.position.set(-3.643, 0.02, -0.4);
  472. mesh.rotation.y = -Math.PI;
  473. this.group.add(mesh);
  474. }
  475. } catch (error) {
  476. console.log('视频信号异常');
  477. }
  478. }
  479. }
  480. setMeshHide() {
  481. const wire = this.group.getObjectByName('fmXr-wire');
  482. if (wire) {
  483. if (wire.getObjectByName('对象192')) wire.getObjectByName('对象192').visible = false;
  484. if (wire.getObjectByName('Box595')) wire.getObjectByName('Box595').visible = false;
  485. }
  486. }
  487. mountedThree(playerDom) {
  488. this.group = new THREE.Object3D();
  489. this.group.name = this.modelName;
  490. return new Promise((resolve) => {
  491. this.model.setGLTFModel(['fmXr-door', 'fmXr-wire', 'fmXr-wall'], this.group).then(() => {
  492. this.setModalPosition();
  493. // 初始化左右摇摆动画;
  494. this.initAnimation();
  495. // this.drawHots();
  496. this.addLight();
  497. // this.deviceDetailCard();
  498. this.model.animate();
  499. this.setMeshHide();
  500. resolve(this.model);
  501. });
  502. });
  503. }
  504. destroy() {
  505. if (this.model) {
  506. if (this.mixers) {
  507. this.mixers.uncacheClip(this.clipActionArr.frontDoor.getClip());
  508. this.mixers.uncacheClip(this.clipActionArr.backDoor.getClip());
  509. this.mixers.uncacheAction(this.clipActionArr.frontDoor.getClip(), this.group);
  510. this.mixers.uncacheAction(this.clipActionArr.backDoor.getClip(), this.group);
  511. this.mixers.uncacheRoot(this.group);
  512. if (this.model.animations[0]) this.model.animations[0].tracks = [];
  513. }
  514. this.model.clearGroup(this.group);
  515. this.clipActionArr.backDoor = undefined;
  516. this.clipActionArr.frontDoor = undefined;
  517. this.mixers = undefined;
  518. // document.getElementById('damper3D').parentElement.remove(document.getElementById('damper3D'))
  519. }
  520. }
  521. }
  522. export default FmXR;