gate.threejs.noStation.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 FmNoStation {
  10. modelName = 'FmNoStation';
  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. }
  49. resetCamera() {
  50. this.model.camera.far = 274;
  51. this.model.orbitControls?.update();
  52. this.model.camera.updateProjectionMatrix();
  53. }
  54. // 设置模型位置
  55. setModalPosition() {
  56. this.group?.scale.set(22, 22, 22);
  57. this.group?.position.set(-20, 20, 9);
  58. }
  59. /* 添加监控数据 */
  60. addMonitorText(selectData) {
  61. if (!this.group) {
  62. return;
  63. }
  64. const screenDownText = VENT_PARAM['modalText']
  65. ? VENT_PARAM['modalText']
  66. : History_Type['type'] == 'remote'
  67. ? `国能神东煤炭集团监制`
  68. : '煤炭科学技术研究院有限公司研制';
  69. const screenDownTextX = 90 - (screenDownText.length - 10) * 6;
  70. const textArr = [
  71. {
  72. text: `远程控制自动风门`,
  73. // text: `${selectData.strinstallpos}`,
  74. font: 'normal 30px Arial',
  75. color: '#00FF00',
  76. strokeStyle: '#007400',
  77. x: 120,
  78. y: 100,
  79. },
  80. {
  81. text: `净通行高度(m):`,
  82. font: 'normal 30px Arial',
  83. color: '#00FF00',
  84. strokeStyle: '#007400',
  85. x: 0,
  86. y: 155,
  87. },
  88. {
  89. text: `${selectData.fclearheight ? selectData.fclearheight : '-'}`,
  90. font: 'normal 30px Arial',
  91. color: '#00FF00',
  92. strokeStyle: '#007400',
  93. x: 330,
  94. y: 155,
  95. },
  96. {
  97. text: `净通行宽度(m): `,
  98. font: 'normal 30px Arial',
  99. color: '#00FF00',
  100. strokeStyle: '#007400',
  101. x: 0,
  102. y: 215,
  103. },
  104. {
  105. text: ` ${selectData.fclearwidth ? selectData.fclearwidth : '-'}`,
  106. font: 'normal 30px Arial',
  107. color: '#00FF00',
  108. strokeStyle: '#007400',
  109. x: 320,
  110. y: 215,
  111. },
  112. {
  113. text: `故障诊断:`,
  114. font: 'normal 30px Arial',
  115. color: '#00FF00',
  116. strokeStyle: '#007400',
  117. x: 0,
  118. y: 275,
  119. },
  120. {
  121. text: `${selectData.warnLevel_str ? selectData.warnLevel_str : '-'}`,
  122. font: 'normal 30px Arial',
  123. color: '#00FF00',
  124. strokeStyle: '#007400',
  125. x: 320,
  126. y: 275,
  127. },
  128. {
  129. text: screenDownText,
  130. font: 'normal 28px Arial',
  131. color: '#00FF00',
  132. strokeStyle: '#007400',
  133. x: screenDownTextX,
  134. y: 325,
  135. },
  136. ];
  137. //
  138. getTextCanvas(526, 346, textArr, '').then((canvas: HTMLCanvasElement) => {
  139. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  140. textMap.colorSpace = THREE.SRGBColorSpace;
  141. const textMaterial = new THREE.MeshBasicMaterial({
  142. // 关于材质并未讲解 实操即可熟悉 这里是漫反射类似纸张的材质,对应的就有高光类似金属的材质.
  143. map: textMap, // 设置纹理贴图
  144. transparent: true,
  145. side: THREE.FrontSide, // 这里是双面渲染的意思
  146. });
  147. textMaterial.blending = THREE.CustomBlending;
  148. const monitorPlane = this.group.getObjectByName('monitorText');
  149. if (monitorPlane) {
  150. monitorPlane.material = textMaterial;
  151. } else {
  152. const planeGeometry = new THREE.PlaneGeometry(526, 346); // 平面3维几何体PlaneGeometry
  153. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  154. planeMesh.name = 'monitorText';
  155. planeMesh.scale.set(0.002, 0.002, 0.002);
  156. planeMesh.position.set(3.665, 0.09, -0.4);
  157. this.group.add(planeMesh);
  158. }
  159. textMap.dispose();
  160. });
  161. }
  162. /* 风门动画 */
  163. render() {
  164. if (!this.model) {
  165. return;
  166. }
  167. if (this.mixers && this.fmClock.running) {
  168. this.mixers.update(2);
  169. }
  170. }
  171. /* 点击风门 */
  172. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  173. if (this.animationTimer) {
  174. clearTimeout(this.animationTimer);
  175. this.animationTimer = null;
  176. }
  177. }
  178. mouseUpModel() {}
  179. /* 提取风门序列帧,初始化前后门动画 */
  180. initAnimation() {
  181. const fmGroup = this.group?.getObjectByName('Fm-noStation');
  182. if (fmGroup) {
  183. const tracks = fmGroup.animations[0].tracks;
  184. const fontTracks: any[] = [],
  185. backTracks: any[] = [];
  186. for (let i = 0; i < tracks.length; i++) {
  187. const track = tracks[i];
  188. if (track.name.startsWith('qianmen')) {
  189. fontTracks.push(track);
  190. } else if (track.name.startsWith('houmen')) {
  191. backTracks.push(track);
  192. }
  193. }
  194. this.mixers = new THREE.AnimationMixer(fmGroup);
  195. const frontDoor = new THREE.AnimationClip('frontDoor', 22, fontTracks);
  196. const frontClipAction = this.mixers.clipAction(frontDoor, fmGroup);
  197. frontClipAction.clampWhenFinished = true;
  198. frontClipAction.loop = THREE.LoopOnce;
  199. this.clipActionArr.frontDoor = frontClipAction;
  200. const backDoor = new THREE.AnimationClip('backDoor', 22, backTracks);
  201. const backClipAction = this.mixers.clipAction(backDoor, fmGroup);
  202. backClipAction.clampWhenFinished = true;
  203. backClipAction.loop = THREE.LoopOnce;
  204. this.clipActionArr.backDoor = backClipAction;
  205. }
  206. }
  207. // 播放动画
  208. play(handlerState, timeScale = 0.01) {
  209. let handler = () => {};
  210. if (this.clipActionArr.frontDoor && this.clipActionArr.backDoor) {
  211. switch (handlerState) {
  212. case 1: // 打开前门
  213. handler = () => {
  214. this.clipActionArr.frontDoor.paused = true;
  215. this.clipActionArr.frontDoor.reset();
  216. this.clipActionArr.frontDoor.time = 1.2;
  217. this.clipActionArr.frontDoor.timeScale = timeScale;
  218. // this.clipActionArr.frontDoor.clampWhenFinished = true;
  219. this.clipActionArr.frontDoor.play();
  220. this.fmClock.start();
  221. // 显示打开前门文字
  222. if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = true;
  223. if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = false;
  224. };
  225. break;
  226. case 2: // 关闭前门
  227. handler = () => {
  228. this.clipActionArr.frontDoor.paused = true;
  229. this.clipActionArr.frontDoor.reset(); //
  230. this.clipActionArr.frontDoor.time = 4;
  231. this.clipActionArr.frontDoor.timeScale = -timeScale;
  232. // this.clipActionArr.frontDoor.clampWhenFinished = true;
  233. this.clipActionArr.frontDoor.play();
  234. this.fmClock.start();
  235. if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = false;
  236. if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = true;
  237. };
  238. break;
  239. case 3: // 打开后门
  240. handler = () => {
  241. this.clipActionArr.backDoor.paused = true;
  242. this.clipActionArr.backDoor.reset();
  243. this.clipActionArr.backDoor.time = 1.2;
  244. this.clipActionArr.backDoor.timeScale = timeScale;
  245. // this.clipActionArr.backDoor.clampWhenFinished = true;
  246. this.clipActionArr.backDoor.play();
  247. this.fmClock.start();
  248. if (this.backDamperOpenMesh) this.backDamperOpenMesh.visible = true;
  249. if (this.backDamperClosedMesh) this.backDamperClosedMesh.visible = false;
  250. };
  251. break;
  252. case 4: // 关闭后门
  253. handler = () => {
  254. this.clipActionArr.backDoor.paused = true;
  255. this.clipActionArr.backDoor.reset();
  256. this.clipActionArr.backDoor.time = 4;
  257. this.clipActionArr.backDoor.timeScale = -timeScale;
  258. // this.clipActionArr.backDoor.clampWhenFinished = true;
  259. this.clipActionArr.backDoor.play();
  260. this.fmClock.start();
  261. if (this.backDamperOpenMesh) this.backDamperOpenMesh.visible = false;
  262. if (this.backDamperClosedMesh) this.backDamperClosedMesh.visible = true;
  263. };
  264. break;
  265. // case 5: // 打开前后门
  266. // handler = () => {
  267. // this.clipActionArr.backDoor.paused = true;
  268. // this.clipActionArr.frontDoor.paused = true;
  269. // this.clipActionArr.frontDoor.reset();
  270. // this.clipActionArr.frontDoor.time = 0;
  271. // this.clipActionArr.frontDoor.timeScale = 0.01;
  272. // this.clipActionArr.frontDoor.clampWhenFinished = true;
  273. // this.clipActionArr.frontDoor.play();
  274. // this.clipActionArr.backDoor.reset();
  275. // this.clipActionArr.backDoor.time = 0;
  276. // this.clipActionArr.backDoor.timeScale = 0.01;
  277. // this.clipActionArr.backDoor.clampWhenFinished = true;
  278. // this.clipActionArr.backDoor.play();
  279. // this.frontClock.start();
  280. // this.backClock.start();
  281. // };
  282. // break;
  283. // case 6: // 关闭前后门
  284. // handler = () => {
  285. // debugger;
  286. // this.clipActionArr.backDoor.paused = true;
  287. // this.clipActionArr.frontDoor.paused = true;
  288. // this.clipActionArr.frontDoor.reset();
  289. // this.clipActionArr.frontDoor.time = 4;
  290. // this.clipActionArr.frontDoor.timeScale = -0.01;
  291. // this.clipActionArr.frontDoor.clampWhenFinished = true;
  292. // this.clipActionArr.frontDoor.play();
  293. // this.clipActionArr.backDoor.reset();
  294. // this.clipActionArr.backDoor.time = 4;
  295. // this.clipActionArr.backDoor.timeScale = -0.01;
  296. // this.clipActionArr.backDoor.clampWhenFinished = true;
  297. // this.clipActionArr.backDoor.play();
  298. // this.frontClock.start();
  299. // this.backClock.start();
  300. // };
  301. // break;
  302. default:
  303. }
  304. handler();
  305. }
  306. // model.clock.start();
  307. // const honglvdeng = group.getObjectByName('honglvdeng');
  308. // const material = honglvdeng.material;
  309. // setTimeout(() => {
  310. // if (handlerState === 2 || handlerState === 4 || handlerState === 6) {
  311. // material.color = new THREE.Color(0x00ff00);
  312. // } else {
  313. // material.color = new THREE.Color(0xff0000);
  314. // }
  315. // }, 1000);
  316. }
  317. mountedThree(playerDom) {
  318. this.group = new THREE.Object3D();
  319. this.group.name = this.modelName;
  320. return new Promise((resolve) => {
  321. if (!this.model) {
  322. resolve(null);
  323. }
  324. this.model.setGLTFModel('Fm-noStation').then((gltf) => {
  325. const fmModal = gltf[0];
  326. fmModal.name = 'Fm-noStation';
  327. this.group?.add(fmModal);
  328. this.setModalPosition();
  329. // 初始化左右摇摆动画;
  330. this.initAnimation();
  331. this.addLight();
  332. this.model.animate();
  333. resolve(this.model);
  334. this.backDamperOpenMesh = this.group.getObjectByName('Dampler_open_1');
  335. if (this.backDamperOpenMesh) this.backDamperOpenMesh.visible = false;
  336. this.backDamperClosedMesh = this.group.getObjectByName('Damper_Closed_1');
  337. if (this.backDamperClosedMesh) this.backDamperClosedMesh.visible = true;
  338. this.frontDamperOpenMesh = this.group.getObjectByName('Damper_Open_2');
  339. if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = false;
  340. this.frontDamperClosedMesh = this.group.getObjectByName('Damper_Closed_2');
  341. if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = true;
  342. });
  343. });
  344. }
  345. destroy() {
  346. if (this.model) {
  347. if (this.mixers) {
  348. this.mixers.uncacheClip(this.clipActionArr.frontDoor.getClip());
  349. this.mixers.uncacheClip(this.clipActionArr.backDoor.getClip());
  350. this.mixers.uncacheAction(this.clipActionArr.frontDoor.getClip(), this.group);
  351. this.mixers.uncacheAction(this.clipActionArr.backDoor.getClip(), this.group);
  352. this.mixers.uncacheRoot(this.group);
  353. if (this.model.animations[0]) this.model.animations[0].tracks = [];
  354. }
  355. this.model.clearGroup(this.group);
  356. this.clipActionArr.backDoor = undefined;
  357. this.clipActionArr.frontDoor = undefined;
  358. this.mixers = undefined;
  359. // document.getElementById('damper3D').parentElement.remove(document.getElementById('damper3D'))
  360. }
  361. }
  362. }
  363. export default FmNoStation;