gate.threejs.yy.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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 Fm1 {
  11. modelName = 'fm1';
  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 - 11) * 9;
  72. const textArr = [
  73. {
  74. text: `远程控制自动风门`,
  75. font: 'normal 30px Arial',
  76. color: '#00FF00',
  77. strokeStyle: '#007400',
  78. x: 120,
  79. y: 100,
  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: 325,
  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.002, 0.002, 0.002);
  157. planeMesh.position.set(3.685, 0.09, -0.41);
  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. return false;
  197. });
  198. }
  199. mouseUpModel() {}
  200. /* 提取风门序列帧,初始化前后门动画 */
  201. initAnimation() {
  202. const fmGroup = this.group?.getObjectByName('Fm-door');
  203. if (fmGroup) {
  204. const tracks = fmGroup.animations[0].tracks;
  205. const fontTracks: any[] = [],
  206. backTracks: any[] = [];
  207. for (let i = 0; i < tracks.length; i++) {
  208. const track = tracks[i];
  209. if (track.name.startsWith('qianmen')) {
  210. fontTracks.push(track);
  211. } else if (track.name.startsWith('houmen')) {
  212. backTracks.push(track);
  213. }
  214. }
  215. this.mixers = new THREE.AnimationMixer(fmGroup);
  216. const frontDoor = new THREE.AnimationClip('frontDoor', 4, fontTracks);
  217. const frontClipAction = this.mixers.clipAction(frontDoor, fmGroup);
  218. frontClipAction.clampWhenFinished = true;
  219. frontClipAction.loop = THREE.LoopOnce;
  220. this.clipActionArr.frontDoor = frontClipAction;
  221. const backDoor = new THREE.AnimationClip('backDoor', 4, backTracks);
  222. const backClipAction = this.mixers.clipAction(backDoor, fmGroup);
  223. backClipAction.clampWhenFinished = true;
  224. backClipAction.loop = THREE.LoopOnce;
  225. this.clipActionArr.backDoor = backClipAction;
  226. }
  227. }
  228. deviceDetailCard(position = { x: 0, y: 0, z: 0 }) {
  229. const element = document.getElementById('deviceCard') as HTMLElement;
  230. if (element) {
  231. this.deviceDetailCSS3D = new CSS2DObject(element);
  232. this.deviceDetailCSS3D.name = 'deviceCard';
  233. this.deviceDetailCSS3D.position.set(position.x, position.y, position.z);
  234. this.deviceDetailCSS3D.visible = false;
  235. // this.model.scene.add(this.deviceDetailCSS3D);
  236. this.group.add(this.deviceDetailCSS3D);
  237. }
  238. }
  239. // 播放动画
  240. play(handlerState, timeScale = 0.01) {
  241. if (this.clipActionArr.frontDoor && this.clipActionArr.backDoor) {
  242. let handler = () => {};
  243. switch (handlerState) {
  244. case 1: // 打开前门
  245. handler = () => {
  246. this.clipActionArr.frontDoor.paused = true;
  247. this.clipActionArr.frontDoor.reset();
  248. this.clipActionArr.frontDoor.time = 1.2;
  249. this.clipActionArr.frontDoor.timeScale = timeScale;
  250. // this.clipActionArr.frontDoor.clampWhenFinished = true;
  251. this.clipActionArr.frontDoor.play();
  252. this.fmClock.start();
  253. // 显示打开前门文字
  254. if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = true;
  255. if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = false;
  256. };
  257. break;
  258. case 2: // 关闭前门
  259. handler = () => {
  260. this.clipActionArr.frontDoor.paused = true;
  261. this.clipActionArr.frontDoor.reset(); //
  262. this.clipActionArr.frontDoor.time = 4;
  263. this.clipActionArr.frontDoor.timeScale = -timeScale;
  264. // this.clipActionArr.frontDoor.clampWhenFinished = true;
  265. this.clipActionArr.frontDoor.play();
  266. this.fmClock.start();
  267. if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = false;
  268. if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = true;
  269. };
  270. break;
  271. case 3: // 打开后门
  272. handler = () => {
  273. this.clipActionArr.backDoor.paused = true;
  274. this.clipActionArr.backDoor.reset();
  275. this.clipActionArr.backDoor.time = 1.2;
  276. this.clipActionArr.backDoor.timeScale = timeScale;
  277. // this.clipActionArr.backDoor.clampWhenFinished = true;
  278. this.clipActionArr.backDoor.play();
  279. this.fmClock.start();
  280. if (this.backDamperOpenMesh) this.backDamperOpenMesh.visible = true;
  281. if (this.backDamperClosedMesh) this.backDamperClosedMesh.visible = false;
  282. };
  283. break;
  284. case 4: // 关闭后门
  285. handler = () => {
  286. this.clipActionArr.backDoor.paused = true;
  287. this.clipActionArr.backDoor.reset();
  288. this.clipActionArr.backDoor.time = 4;
  289. this.clipActionArr.backDoor.timeScale = -timeScale;
  290. // this.clipActionArr.backDoor.clampWhenFinished = true;
  291. this.clipActionArr.backDoor.play();
  292. this.fmClock.start();
  293. if (this.backDamperOpenMesh) this.backDamperOpenMesh.visible = false;
  294. if (this.backDamperClosedMesh) this.backDamperClosedMesh.visible = true;
  295. };
  296. break;
  297. // case 5: // 打开前后门
  298. // handler = () => {
  299. // this.clipActionArr.backDoor.paused = true;
  300. // this.clipActionArr.frontDoor.paused = true;
  301. // this.clipActionArr.frontDoor.reset();
  302. // this.clipActionArr.frontDoor.time = 0;
  303. // this.clipActionArr.frontDoor.timeScale = 0.01;
  304. // this.clipActionArr.frontDoor.clampWhenFinished = true;
  305. // this.clipActionArr.frontDoor.play();
  306. // this.clipActionArr.backDoor.reset();
  307. // this.clipActionArr.backDoor.time = 0;
  308. // this.clipActionArr.backDoor.timeScale = 0.01;
  309. // this.clipActionArr.backDoor.clampWhenFinished = true;
  310. // this.clipActionArr.backDoor.play();
  311. // this.frontClock.start();
  312. // this.backClock.start();
  313. // };
  314. // break;
  315. // case 6: // 关闭前后门
  316. // handler = () => {
  317. // debugger;
  318. // this.clipActionArr.backDoor.paused = true;
  319. // this.clipActionArr.frontDoor.paused = true;
  320. // this.clipActionArr.frontDoor.reset();
  321. // this.clipActionArr.frontDoor.time = 4;
  322. // this.clipActionArr.frontDoor.timeScale = -0.01;
  323. // this.clipActionArr.frontDoor.clampWhenFinished = true;
  324. // this.clipActionArr.frontDoor.play();
  325. // this.clipActionArr.backDoor.reset();
  326. // this.clipActionArr.backDoor.time = 4;
  327. // this.clipActionArr.backDoor.timeScale = -0.01;
  328. // this.clipActionArr.backDoor.clampWhenFinished = true;
  329. // this.clipActionArr.backDoor.play();
  330. // this.frontClock.start();
  331. // this.backClock.start();
  332. // };
  333. // break;
  334. default:
  335. }
  336. handler();
  337. // model.clock.start();
  338. // const honglvdeng = group.getObjectByName('honglvdeng');
  339. // const material = honglvdeng.material;
  340. // setTimeout(() => {
  341. // if (handlerState === 2 || handlerState === 4 || handlerState === 6) {
  342. // material.color = new THREE.Color(0x00ff00);
  343. // } else {
  344. // material.color = new THREE.Color(0xff0000);
  345. // }
  346. // }, 1000);
  347. }
  348. }
  349. async initCamera(dom1) {
  350. const videoPlayer1 = dom1;
  351. this.player1 = dom1;
  352. let monitorPlane: THREE.Mesh | null = null;
  353. if (!videoPlayer1) {
  354. const textArr = [
  355. {
  356. text: `无信号输入`,
  357. font: 'normal 40px Arial',
  358. color: '#009900',
  359. strokeStyle: '#002200',
  360. x: 170,
  361. y: 40,
  362. },
  363. ];
  364. const canvas = await getTextCanvas(320, 180, '', 'noSinge.png');
  365. let textMaterial: THREE.MeshBasicMaterial | null = null;
  366. if (canvas) {
  367. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  368. textMaterial = new THREE.MeshBasicMaterial({
  369. map: textMap, // 设置纹理贴图
  370. transparent: true,
  371. side: THREE.DoubleSide, // 这里是双面渲染的意思
  372. });
  373. textMaterial.blending = THREE.CustomBlending;
  374. const planeGeometry = new THREE.PlaneGeometry(100, 100); // 平面3维几何体PlaneGeometry
  375. monitorPlane = new THREE.Mesh(planeGeometry, textMaterial);
  376. textMaterial.dispose();
  377. planeGeometry.dispose();
  378. textMap.dispose();
  379. }
  380. }
  381. const player1 = this.group.getObjectByName('player1');
  382. if (player1) {
  383. this.model.clearMesh(player1);
  384. this.group.remove(player1);
  385. }
  386. const noPlayer1 = this.group.getObjectByName('noPlayer1');
  387. if (noPlayer1) {
  388. this.model.clearMesh(noPlayer1);
  389. this.group.remove(noPlayer1);
  390. }
  391. if (!videoPlayer1 && videoPlayer1 === null) {
  392. if (monitorPlane && !this.group.getObjectByName('noPlayer1')) {
  393. const planeMesh = monitorPlane.clone();
  394. planeMesh.name = 'noPlayer1';
  395. planeMesh.scale.set(0.0085, 0.0055, 0.012);
  396. planeMesh.position.set(-3.64, 0.01, -0.41);
  397. this.group?.add(planeMesh.clone());
  398. }
  399. } else if (videoPlayer1) {
  400. try {
  401. const mesh = renderVideo(this.group, videoPlayer1, 'player1');
  402. if (mesh) {
  403. mesh?.scale.set(-0.0275, 0.028, 1);
  404. mesh?.position.set(-3.643, 0.02, -0.4);
  405. mesh.rotation.y = -Math.PI;
  406. this.group.add(mesh);
  407. }
  408. } catch (error) {
  409. console.log('视频信号异常');
  410. }
  411. }
  412. }
  413. mountedThree(playerDom) {
  414. this.group = new THREE.Object3D();
  415. this.group.name = this.modelName;
  416. return new Promise((resolve) => {
  417. this.model.setGLTFModel(['Fm-door', 'Fm-wire', 'Fm-wall'], this.group).then(() => {
  418. this.setModalPosition();
  419. // 初始化左右摇摆动画;
  420. this.initAnimation();
  421. // this.drawHots();
  422. this.addLight();
  423. // this.deviceDetailCard();
  424. this.model.animate();
  425. this.backDamperOpenMesh = this.group.getObjectByName('Dampler_open_1');
  426. if (this.backDamperOpenMesh) this.backDamperOpenMesh.visible = false;
  427. this.backDamperClosedMesh = this.group.getObjectByName('Damper_Closed_1');
  428. if (this.backDamperClosedMesh) this.backDamperClosedMesh.visible = true;
  429. this.frontDamperOpenMesh = this.group.getObjectByName('Damper_Open_2');
  430. if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = false;
  431. this.frontDamperClosedMesh = this.group.getObjectByName('Damper_Closed_2');
  432. if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = true;
  433. resolve(this.model);
  434. });
  435. });
  436. }
  437. destroy() {
  438. if (this.model) {
  439. if (this.mixers) {
  440. this.mixers.uncacheClip(this.clipActionArr.frontDoor.getClip());
  441. this.mixers.uncacheClip(this.clipActionArr.backDoor.getClip());
  442. this.mixers.uncacheAction(this.clipActionArr.frontDoor.getClip(), this.group);
  443. this.mixers.uncacheAction(this.clipActionArr.backDoor.getClip(), this.group);
  444. this.mixers.uncacheRoot(this.group);
  445. if (this.model.animations[0]) this.model.animations[0].tracks = [];
  446. }
  447. this.model.clearGroup(this.group);
  448. this.clipActionArr.backDoor = undefined;
  449. this.clipActionArr.frontDoor = undefined;
  450. this.mixers = undefined;
  451. // document.getElementById('damper3D').parentElement.remove(document.getElementById('damper3D'))
  452. }
  453. }
  454. }
  455. export default Fm1;