shuangdaoFcZhq.threejs.ts 17 KB

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