gate.threejs.two.yj.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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 FmYjXr {
  10. modelName = 'FmTwoYj';
  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 = 90 - (screenDownText.length - 11) * 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: 330,
  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: 320,
  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: 320,
  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(3.825, 0.01, -0.48);
  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.mixers && this.fmClock.running) {
  204. this.mixers.update(2);
  205. }
  206. }
  207. /* 点击风窗,风窗全屏 */
  208. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  209. if (this.animationTimer) {
  210. clearTimeout(this.animationTimer);
  211. this.animationTimer = null;
  212. }
  213. // 判断是否点击到视频
  214. intersects.find((intersect) => {
  215. const mesh = intersect.object;
  216. // if (mesh.name === 'player1') {
  217. // if (new Date().getTime() - this.playerStartClickTime1 < 400) {
  218. // // 双击,视频放大
  219. // if (this.player1) {
  220. // this.player1.requestPictureInPicture();
  221. // }
  222. // }
  223. // this.playerStartClickTime1 = new Date().getTime();
  224. // return true;
  225. // } else if (mesh.name === 'player2') {
  226. // if (new Date().getTime() - this.playerStartClickTime2 < 400) {
  227. // // 双击,视频放大
  228. // if (this.player2) {
  229. // this.player2.requestPictureInPicture();
  230. // }
  231. // }
  232. // this.playerStartClickTime2 = new Date().getTime();
  233. // return true;
  234. // } else if (mesh.name.startsWith('hotPoint')) {
  235. // if (this.deviceDetailCSS3D) {
  236. // this.deviceDetailCSS3D.position.set(mesh.position.x + 0.035, mesh.position.y + 0.68, mesh.position.z + 0.02);
  237. // console.log('[ deviceDetailCSS3D.position ] >', this.deviceDetailCSS3D.position);
  238. // this.deviceDetailCSS3D.visible = true;
  239. // return true;
  240. // }
  241. // } else {
  242. // if (this.deviceDetailCSS3D) this.deviceDetailCSS3D.visible = false;
  243. // console.log('[ 点击事件 ] >');
  244. // }
  245. return false;
  246. });
  247. }
  248. mouseUpModel() {}
  249. /* 提取风门序列帧,初始化前后门动画 */
  250. initAnimation() {
  251. const fmGroup = this.group?.getObjectByName('Fm-two-Yj');
  252. if (fmGroup) {
  253. const tracks = fmGroup.animations[0].tracks;
  254. const fontTracks: any[] = [],
  255. backTracks: any[] = [];
  256. for (let i = 0; i < tracks.length; i++) {
  257. const track = tracks[i];
  258. if (track.name.startsWith('qianmen')) {
  259. fontTracks.push(track);
  260. } else if (track.name.startsWith('houmen')) {
  261. backTracks.push(track);
  262. }
  263. }
  264. this.mixers = new THREE.AnimationMixer(fmGroup);
  265. const frontDoor = new THREE.AnimationClip('frontDoor', 22, fontTracks);
  266. const frontClipAction = this.mixers.clipAction(frontDoor, fmGroup);
  267. frontClipAction.clampWhenFinished = true;
  268. frontClipAction.loop = THREE.LoopOnce;
  269. this.clipActionArr.frontDoor = frontClipAction;
  270. const backDoor = new THREE.AnimationClip('backDoor', 22, backTracks);
  271. const backClipAction = this.mixers.clipAction(backDoor, fmGroup);
  272. backClipAction.clampWhenFinished = true;
  273. backClipAction.loop = THREE.LoopOnce;
  274. this.clipActionArr.backDoor = backClipAction;
  275. }
  276. }
  277. deviceDetailCard(position = { x: 0, y: 0, z: 0 }) {
  278. const element = document.getElementById('deviceCard') as HTMLElement;
  279. if (element) {
  280. this.deviceDetailCSS3D = new CSS2DObject(element);
  281. this.deviceDetailCSS3D.name = 'deviceCard';
  282. this.deviceDetailCSS3D.position.set(position.x, position.y, position.z);
  283. this.deviceDetailCSS3D.visible = false;
  284. // this.model.scene.add(this.deviceDetailCSS3D);
  285. this.group.add(this.deviceDetailCSS3D);
  286. }
  287. }
  288. // 播放动画
  289. play(handlerState, timeScale = 0.05) {
  290. if (this.clipActionArr.frontDoor && this.clipActionArr.backDoor) {
  291. let handler = () => {};
  292. switch (handlerState) {
  293. case 1: // 打开前门
  294. handler = () => {
  295. this.clipActionArr.frontDoor.paused = true;
  296. this.clipActionArr.frontDoor.reset();
  297. this.clipActionArr.frontDoor.time = 1.2;
  298. this.clipActionArr.frontDoor.timeScale = timeScale;
  299. // this.clipActionArr.frontDoor.clampWhenFinished = true;
  300. this.clipActionArr.frontDoor.play();
  301. this.fmClock.start();
  302. // 显示打开前门文字
  303. if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = true;
  304. if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = false;
  305. };
  306. break;
  307. case 2: // 关闭前门
  308. handler = () => {
  309. this.clipActionArr.frontDoor.paused = true;
  310. this.clipActionArr.frontDoor.reset(); //
  311. this.clipActionArr.frontDoor.time = 5.5;
  312. this.clipActionArr.frontDoor.timeScale = -timeScale;
  313. // this.clipActionArr.frontDoor.clampWhenFinished = true;
  314. this.clipActionArr.frontDoor.play();
  315. this.fmClock.start();
  316. if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = false;
  317. if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = true;
  318. };
  319. break;
  320. case 3: // 打开后门
  321. handler = () => {
  322. this.clipActionArr.backDoor.paused = true;
  323. this.clipActionArr.backDoor.reset();
  324. this.clipActionArr.backDoor.time = 1.2;
  325. this.clipActionArr.backDoor.timeScale = timeScale;
  326. // this.clipActionArr.backDoor.clampWhenFinished = true;
  327. this.clipActionArr.backDoor.play();
  328. this.fmClock.start();
  329. if (this.backDamperOpenMesh) this.backDamperOpenMesh.visible = true;
  330. if (this.backDamperClosedMesh) this.backDamperClosedMesh.visible = false;
  331. };
  332. break;
  333. case 4: // 关闭后门
  334. handler = () => {
  335. this.clipActionArr.backDoor.paused = true;
  336. this.clipActionArr.backDoor.reset();
  337. this.clipActionArr.backDoor.time = 5.5;
  338. this.clipActionArr.backDoor.timeScale = -timeScale;
  339. // this.clipActionArr.backDoor.clampWhenFinished = true;
  340. this.clipActionArr.backDoor.play();
  341. this.fmClock.start();
  342. if (this.backDamperOpenMesh) this.backDamperOpenMesh.visible = false;
  343. if (this.backDamperClosedMesh) this.backDamperClosedMesh.visible = true;
  344. };
  345. break;
  346. case 5: // 打开前后门
  347. handler = () => {
  348. this.clipActionArr.frontDoor.paused = true;
  349. this.clipActionArr.frontDoor.reset();
  350. this.clipActionArr.frontDoor.time = 1.2;
  351. this.clipActionArr.frontDoor.timeScale = timeScale;
  352. // this.clipActionArr.frontDoor.clampWhenFinished = true;
  353. this.clipActionArr.frontDoor.play();
  354. this.fmClock.start();
  355. // 显示打开前门文字
  356. if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = true;
  357. if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = false;
  358. this.clipActionArr.backDoor.paused = true;
  359. this.clipActionArr.backDoor.reset();
  360. this.clipActionArr.backDoor.time = 1.2;
  361. this.clipActionArr.backDoor.timeScale = timeScale;
  362. // this.clipActionArr.backDoor.clampWhenFinished = true;
  363. this.clipActionArr.backDoor.play();
  364. this.fmClock.start();
  365. if (this.backDamperOpenMesh) this.backDamperOpenMesh.visible = true;
  366. if (this.backDamperClosedMesh) this.backDamperClosedMesh.visible = false;
  367. };
  368. break;
  369. case 6: // 关闭前后门
  370. handler = () => {
  371. this.clipActionArr.frontDoor.paused = true;
  372. this.clipActionArr.frontDoor.reset(); //
  373. this.clipActionArr.frontDoor.time = 5.5;
  374. this.clipActionArr.frontDoor.timeScale = -timeScale;
  375. // this.clipActionArr.frontDoor.clampWhenFinished = true;
  376. this.clipActionArr.frontDoor.play();
  377. this.fmClock.start();
  378. if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = false;
  379. if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = true;
  380. this.clipActionArr.backDoor.paused = true;
  381. this.clipActionArr.backDoor.reset();
  382. this.clipActionArr.backDoor.time = 5.5;
  383. this.clipActionArr.backDoor.timeScale = -timeScale;
  384. // this.clipActionArr.backDoor.clampWhenFinished = true;
  385. this.clipActionArr.backDoor.play();
  386. this.fmClock.start();
  387. if (this.backDamperOpenMesh) this.backDamperOpenMesh.visible = false;
  388. if (this.backDamperClosedMesh) this.backDamperClosedMesh.visible = true;
  389. };
  390. default:
  391. }
  392. handler();
  393. // model.clock.start();
  394. // const honglvdeng = group.getObjectByName('honglvdeng');
  395. // const material = honglvdeng.material;
  396. // setTimeout(() => {
  397. // if (handlerState === 2 || handlerState === 4 || handlerState === 6) {
  398. // material.color = new THREE.Color(0x00ff00);
  399. // } else {
  400. // material.color = new THREE.Color(0xff0000);
  401. // }
  402. // }, 1000);
  403. }
  404. }
  405. mountedThree(playerDom) {
  406. this.group = new THREE.Object3D();
  407. this.group.name = this.modelName;
  408. return new Promise((resolve) => {
  409. if (!this.model) {
  410. resolve(null);
  411. }
  412. this.model.setGLTFModel('Fm-two-Yj').then((gltf) => {
  413. const fmModal = gltf[0];
  414. fmModal.name = 'Fm-two-Yj';
  415. this.group?.add(fmModal);
  416. this.setModalPosition();
  417. // 初始化左右摇摆动画;
  418. this.initAnimation();
  419. this.addLight();
  420. this.model.animate();
  421. resolve(this.model);
  422. });
  423. });
  424. }
  425. destroy() {
  426. if (this.model) {
  427. if (this.mixers) {
  428. this.mixers.uncacheClip(this.clipActionArr.frontDoor.getClip());
  429. this.mixers.uncacheClip(this.clipActionArr.backDoor.getClip());
  430. this.mixers.uncacheAction(this.clipActionArr.frontDoor.getClip(), this.group);
  431. this.mixers.uncacheAction(this.clipActionArr.backDoor.getClip(), this.group);
  432. this.mixers.uncacheRoot(this.group);
  433. if (this.model.animations[0]) this.model.animations[0].tracks = [];
  434. }
  435. this.model.clearGroup(this.group);
  436. this.clipActionArr.backDoor = undefined;
  437. this.clipActionArr.frontDoor = undefined;
  438. this.mixers = undefined;
  439. // document.getElementById('damper3D').parentElement.remove(document.getElementById('damper3D'))
  440. }
  441. }
  442. }
  443. export default FmYjXr;