gate.threejs.two.ss.ts 19 KB

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