gate.threejs.qd.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 { useGlobSetting } from '/@/hooks/setting';
  8. // import * as dat from 'dat.gui';
  9. // const gui = new dat.GUI();
  10. // gui.domElement.style = 'position:absolute;top:100px;left:10px;z-index:99999999999999';
  11. class Fm3 {
  12. modelName = 'fm3';
  13. model; //
  14. group;
  15. isLRAnimation = true; // 是否开启左右摇摆动画
  16. direction = 1; // 摇摆方向
  17. animationTimer: NodeJS.Timeout | null = null; // 摇摆开启定时器
  18. player1;
  19. player2;
  20. deviceDetailCSS3D;
  21. playerStartClickTime1 = new Date().getTime();
  22. playerStartClickTime2 = new Date().getTime();
  23. fmClock = new THREE.Clock();
  24. mixers: THREE.AnimationMixer | undefined;
  25. appStore = useAppStore();
  26. backDamperOpenMesh;
  27. backDamperClosedMesh;
  28. frontDamperOpenMesh;
  29. frontDamperClosedMesh;
  30. clipActionArr = {
  31. frontDoor: null as unknown as THREE.AnimationAction,
  32. backDoor: null as unknown as THREE.AnimationAction,
  33. };
  34. constructor(model) {
  35. this.model = model;
  36. }
  37. addLight() {
  38. const directionalLight = new THREE.DirectionalLight(0xffffff, 1.5);
  39. directionalLight.position.set(344, 690, 344);
  40. this.group?.add(directionalLight);
  41. directionalLight.target = this.group as THREE.Object3D;
  42. const pointLight2 = new THREE.PointLight(0xffeeee, 1, 300);
  43. pointLight2.position.set(-4, 10, 1.8);
  44. pointLight2.shadow.bias = 0.05;
  45. this.group?.add(pointLight2);
  46. const pointLight3 = new THREE.PointLight(0xffeeee, 1, 200);
  47. pointLight3.position.set(-0.5, -0.5, 0.75);
  48. pointLight3.shadow.bias = 0.05;
  49. this.group?.add(pointLight3);
  50. }
  51. // 重置摄像头
  52. resetCamera() {
  53. this.model.camera.far = 274;
  54. this.model.orbitControls?.update();
  55. this.model.camera.updateProjectionMatrix();
  56. }
  57. // 设置模型位置
  58. setModalPosition() {
  59. this.group?.scale.set(22, 22, 22);
  60. this.group?.position.set(-20, 20, 9);
  61. }
  62. /* 添加监控数据 */
  63. addMonitorText(selectData) {
  64. const { sysOrgCode } = useGlobSetting();
  65. if (!this.group) {
  66. return;
  67. }
  68. const screenDownText = VENT_PARAM['modalText']
  69. ? VENT_PARAM['modalText']
  70. : History_Type['type'] == 'remote'
  71. ? `国能神东煤炭集团监制`
  72. : '煤炭科学技术研究院有限公司研制';
  73. const screenDownTextX = 80 - (screenDownText.length - 10) * 6;
  74. const textArr = [
  75. {
  76. text: `${sysOrgCode == 'zlnxnywwek' ? selectData.strinstallpos : '远程控制自动风门'}`, //王洼二矿需要特殊处理
  77. font: 'normal 30px Arial',
  78. color: '#00FF00',
  79. strokeStyle: '#007400',
  80. x: sysOrgCode == 'zlnxnywwek' ? (450 - selectData.strinstallpos.length * 22) / 2 : 120,
  81. y: 100,
  82. },
  83. {
  84. text: `净通行高度(m):`,
  85. font: 'normal 30px Arial',
  86. color: '#00FF00',
  87. strokeStyle: '#007400',
  88. x: 0,
  89. y: 155,
  90. },
  91. {
  92. text: `${selectData.fclearheight ? selectData.fclearheight : '-'}`,
  93. font: 'normal 30px Arial',
  94. color: '#00FF00',
  95. strokeStyle: '#007400',
  96. x: 290,
  97. y: 155,
  98. },
  99. {
  100. text: `净通行宽度(m): `,
  101. font: 'normal 30px Arial',
  102. color: '#00FF00',
  103. strokeStyle: '#007400',
  104. x: 0,
  105. y: 215,
  106. },
  107. {
  108. text: ` ${selectData.fclearwidth ? selectData.fclearwidth : '-'}`,
  109. font: 'normal 30px Arial',
  110. color: '#00FF00',
  111. strokeStyle: '#007400',
  112. x: 280,
  113. y: 215,
  114. },
  115. {
  116. text: `故障诊断:`,
  117. font: 'normal 30px Arial',
  118. color: '#00FF00',
  119. strokeStyle: '#007400',
  120. x: 0,
  121. y: 275,
  122. },
  123. {
  124. text: `${selectData.warnLevel_str ? selectData.warnLevel_str : '-'}`,
  125. font: 'normal 30px Arial',
  126. color: '#00FF00',
  127. strokeStyle: '#007400',
  128. x: 280,
  129. y: 275,
  130. },
  131. {
  132. text: screenDownText,
  133. font: 'normal 28px Arial',
  134. color: '#00FF00',
  135. strokeStyle: '#007400',
  136. x: screenDownTextX,
  137. y: 325,
  138. },
  139. ];
  140. //
  141. getTextCanvas(526, 346, textArr, '').then((canvas: HTMLCanvasElement) => {
  142. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  143. textMap.colorSpace = THREE.SRGBColorSpace;
  144. const textMaterial = new THREE.MeshBasicMaterial({
  145. // 关于材质并未讲解 实操即可熟悉 这里是漫反射类似纸张的材质,对应的就有高光类似金属的材质.
  146. map: textMap, // 设置纹理贴图
  147. transparent: true,
  148. side: THREE.FrontSide, // 这里是双面渲染的意思
  149. });
  150. textMaterial.blending = THREE.CustomBlending;
  151. const monitorPlane = this.group.getObjectByName('monitorText');
  152. if (monitorPlane) {
  153. monitorPlane.material = textMaterial;
  154. } else {
  155. const planeGeometry = new THREE.PlaneGeometry(526, 346); // 平面3维几何体PlaneGeometry
  156. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  157. planeMesh.name = 'monitorText';
  158. planeMesh.scale.set(0.002, 0.002, 0.002);
  159. planeMesh.position.set(3.685, 0.09, -0.41);
  160. this.group.add(planeMesh);
  161. }
  162. textMap.dispose();
  163. });
  164. }
  165. /** 添加热点 */
  166. drawHots() {
  167. const hotPositions = [
  168. { x: -0.37, y: 0.26, z: -0.32 },
  169. { x: 0.28, y: -0.2, z: -0.43 },
  170. { x: 0.55, y: -0.22, z: -0.38 },
  171. ];
  172. for (let i = 0; i < 3; i++) {
  173. const hotPoint = drawHot(0.1);
  174. const position = hotPositions[i];
  175. hotPoint.scale.set(0.1, 0.1, 0.1);
  176. hotPoint.position.set(position.x, position.y, position.z);
  177. hotPoint.name = 'hotPoint' + i;
  178. this.group?.add(hotPoint);
  179. }
  180. }
  181. /* 风门动画 */
  182. render() {
  183. if (!this.model) {
  184. return;
  185. }
  186. if (this.mixers && this.fmClock.running) {
  187. this.mixers.update(2);
  188. }
  189. }
  190. /* 点击风窗,风窗全屏 */
  191. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  192. if (this.animationTimer) {
  193. clearTimeout(this.animationTimer);
  194. this.animationTimer = null;
  195. }
  196. }
  197. mouseUpModel() {}
  198. /* 提取风门序列帧,初始化前后门动画 */
  199. initAnimation() {
  200. const fmGroup = this.group?.getObjectByName('Fm-door');
  201. if (fmGroup) {
  202. const tracks = fmGroup.animations[0].tracks;
  203. const fontTracks: any[] = [],
  204. backTracks: any[] = [];
  205. for (let i = 0; i < tracks.length; i++) {
  206. const track = tracks[i];
  207. if (track.name.startsWith('qianmen')) {
  208. fontTracks.push(track);
  209. } else if (track.name.startsWith('houmen')) {
  210. backTracks.push(track);
  211. }
  212. }
  213. this.mixers = new THREE.AnimationMixer(fmGroup);
  214. const frontDoor = new THREE.AnimationClip('frontDoor', 4, fontTracks);
  215. const frontClipAction = this.mixers.clipAction(frontDoor, fmGroup);
  216. frontClipAction.clampWhenFinished = true;
  217. frontClipAction.loop = THREE.LoopOnce;
  218. this.clipActionArr.frontDoor = frontClipAction;
  219. const backDoor = new THREE.AnimationClip('backDoor', 4, backTracks);
  220. const backClipAction = this.mixers.clipAction(backDoor, fmGroup);
  221. backClipAction.clampWhenFinished = true;
  222. backClipAction.loop = THREE.LoopOnce;
  223. this.clipActionArr.backDoor = backClipAction;
  224. }
  225. }
  226. deviceDetailCard(position = { x: 0, y: 0, z: 0 }) {
  227. const element = document.getElementById('deviceCard') as HTMLElement;
  228. if (element) {
  229. this.deviceDetailCSS3D = new CSS2DObject(element);
  230. this.deviceDetailCSS3D.name = 'deviceCard';
  231. this.deviceDetailCSS3D.position.set(position.x, position.y, position.z);
  232. this.deviceDetailCSS3D.visible = false;
  233. // this.model.scene.add(this.deviceDetailCSS3D);
  234. this.group.add(this.deviceDetailCSS3D);
  235. }
  236. }
  237. // 播放动画
  238. play(handlerState, timeScale = 0.01) {
  239. let handler = () => {};
  240. if (this.clipActionArr.frontDoor && this.clipActionArr.backDoor) {
  241. switch (handlerState) {
  242. case 1: // 打开前门
  243. handler = () => {
  244. this.clipActionArr.frontDoor.paused = true;
  245. this.clipActionArr.frontDoor.reset();
  246. this.clipActionArr.frontDoor.time = 1.2;
  247. this.clipActionArr.frontDoor.timeScale = timeScale;
  248. // this.clipActionArr.frontDoor.clampWhenFinished = true;
  249. this.clipActionArr.frontDoor.play();
  250. this.fmClock.start();
  251. // 显示打开前门文字
  252. if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = true;
  253. if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = false;
  254. };
  255. break;
  256. case 2: // 关闭前门
  257. handler = () => {
  258. this.clipActionArr.frontDoor.paused = true;
  259. this.clipActionArr.frontDoor.reset(); //
  260. this.clipActionArr.frontDoor.time = 4;
  261. this.clipActionArr.frontDoor.timeScale = -timeScale;
  262. // this.clipActionArr.frontDoor.clampWhenFinished = true;
  263. this.clipActionArr.frontDoor.play();
  264. this.fmClock.start();
  265. if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = false;
  266. if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = true;
  267. };
  268. break;
  269. case 3: // 打开后门
  270. handler = () => {
  271. this.clipActionArr.backDoor.paused = true;
  272. this.clipActionArr.backDoor.reset();
  273. this.clipActionArr.backDoor.time = 1.2;
  274. this.clipActionArr.backDoor.timeScale = timeScale;
  275. // this.clipActionArr.backDoor.clampWhenFinished = true;
  276. this.clipActionArr.backDoor.play();
  277. this.fmClock.start();
  278. if (this.backDamperOpenMesh) this.backDamperOpenMesh.visible = true;
  279. if (this.backDamperClosedMesh) this.backDamperClosedMesh.visible = false;
  280. };
  281. break;
  282. case 4: // 关闭后门
  283. handler = () => {
  284. this.clipActionArr.backDoor.paused = true;
  285. this.clipActionArr.backDoor.reset();
  286. this.clipActionArr.backDoor.time = 4;
  287. this.clipActionArr.backDoor.timeScale = -timeScale;
  288. // this.clipActionArr.backDoor.clampWhenFinished = true;
  289. this.clipActionArr.backDoor.play();
  290. this.fmClock.start();
  291. if (this.backDamperOpenMesh) this.backDamperOpenMesh.visible = false;
  292. if (this.backDamperClosedMesh) this.backDamperClosedMesh.visible = true;
  293. };
  294. break;
  295. case 5: // 打开前后门
  296. handler = () => {
  297. this.clipActionArr.frontDoor.paused = true;
  298. this.clipActionArr.frontDoor.reset();
  299. this.clipActionArr.frontDoor.time = 1.2;
  300. this.clipActionArr.frontDoor.timeScale = timeScale;
  301. // this.clipActionArr.frontDoor.clampWhenFinished = true;
  302. this.clipActionArr.frontDoor.play();
  303. this.fmClock.start();
  304. // 显示打开前门文字
  305. if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = true;
  306. if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = false;
  307. this.clipActionArr.backDoor.paused = true;
  308. this.clipActionArr.backDoor.reset();
  309. this.clipActionArr.backDoor.time = 1.2;
  310. this.clipActionArr.backDoor.timeScale = timeScale;
  311. // this.clipActionArr.backDoor.clampWhenFinished = true;
  312. this.clipActionArr.backDoor.play();
  313. this.fmClock.start();
  314. if (this.backDamperOpenMesh) this.backDamperOpenMesh.visible = true;
  315. if (this.backDamperClosedMesh) this.backDamperClosedMesh.visible = false;
  316. };
  317. break;
  318. case 6: // 关闭前后门
  319. handler = () => {
  320. this.clipActionArr.frontDoor.paused = true;
  321. this.clipActionArr.frontDoor.reset(); //
  322. this.clipActionArr.frontDoor.time = 4;
  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. this.clipActionArr.backDoor.paused = true;
  330. this.clipActionArr.backDoor.reset();
  331. this.clipActionArr.backDoor.time = 4;
  332. this.clipActionArr.backDoor.timeScale = -timeScale;
  333. // this.clipActionArr.backDoor.clampWhenFinished = true;
  334. this.clipActionArr.backDoor.play();
  335. this.fmClock.start();
  336. if (this.backDamperOpenMesh) this.backDamperOpenMesh.visible = false;
  337. if (this.backDamperClosedMesh) this.backDamperClosedMesh.visible = true;
  338. };
  339. break;
  340. default:
  341. }
  342. handler();
  343. }
  344. }
  345. mountedThree(playerDom) {
  346. this.group = new THREE.Object3D();
  347. this.group.name = this.modelName;
  348. return new Promise((resolve) => {
  349. this.model.setGLTFModel(['Fm-door', 'Fm-wire-qd', 'Fm-wall-qd'], this.group).then(() => {
  350. this.setModalPosition();
  351. // 初始化左右摇摆动画;
  352. this.initAnimation();
  353. // this.drawHots();
  354. this.addLight();
  355. // this.deviceDetailCard();
  356. this.model.animate();
  357. this.backDamperOpenMesh = this.group.getObjectByName('Dampler_open_1');
  358. if (this.backDamperOpenMesh) this.backDamperOpenMesh.visible = false;
  359. this.backDamperClosedMesh = this.group.getObjectByName('Damper_Closed_1');
  360. if (this.backDamperClosedMesh) this.backDamperClosedMesh.visible = true;
  361. this.frontDamperOpenMesh = this.group.getObjectByName('Damper_Open_2');
  362. if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = false;
  363. this.frontDamperClosedMesh = this.group.getObjectByName('Damper_Closed_2');
  364. if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = true;
  365. resolve(this.model);
  366. });
  367. });
  368. }
  369. destroy() {
  370. if (this.model) {
  371. if (this.mixers) {
  372. this.mixers.uncacheClip(this.clipActionArr.frontDoor.getClip());
  373. this.mixers.uncacheClip(this.clipActionArr.backDoor.getClip());
  374. this.mixers.uncacheAction(this.clipActionArr.frontDoor.getClip(), this.group);
  375. this.mixers.uncacheAction(this.clipActionArr.backDoor.getClip(), this.group);
  376. this.mixers.uncacheRoot(this.group);
  377. if (this.model.animations[0]) this.model.animations[0].tracks = [];
  378. }
  379. this.model.clearGroup(this.group);
  380. this.clipActionArr.backDoor = undefined;
  381. this.clipActionArr.frontDoor = undefined;
  382. this.mixers = undefined;
  383. }
  384. }
  385. }
  386. export default Fm3;