gate.threejs.window.ts 16 KB

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