gate.threejs.two.ts 18 KB

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