gate.threejs.two.ss.ts 19 KB

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