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. font: 'normal 30px Arial',
  74. color: '#00FF00',
  75. strokeStyle: '#007400',
  76. x: 120,
  77. y: 100,
  78. },
  79. {
  80. text: `净通行高度(m):`,
  81. font: 'normal 30px Arial',
  82. color: '#00FF00',
  83. strokeStyle: '#007400',
  84. x: 0,
  85. y: 155,
  86. },
  87. {
  88. text: `${selectData.fclearheight ? selectData.fclearheight : '-'}`,
  89. font: 'normal 30px Arial',
  90. color: '#00FF00',
  91. strokeStyle: '#007400',
  92. x: 330,
  93. y: 155,
  94. },
  95. {
  96. text: `净通行宽度(m): `,
  97. font: 'normal 30px Arial',
  98. color: '#00FF00',
  99. strokeStyle: '#007400',
  100. x: 0,
  101. y: 215,
  102. },
  103. {
  104. text: ` ${selectData.fclearwidth ? selectData.fclearwidth : '-'}`,
  105. font: 'normal 30px Arial',
  106. color: '#00FF00',
  107. strokeStyle: '#007400',
  108. x: 320,
  109. y: 215,
  110. },
  111. {
  112. text: `故障诊断:`,
  113. font: 'normal 30px Arial',
  114. color: '#00FF00',
  115. strokeStyle: '#007400',
  116. x: 0,
  117. y: 275,
  118. },
  119. {
  120. text: `${selectData.warnLevel_str ? selectData.warnLevel_str : '-'}`,
  121. font: 'normal 30px Arial',
  122. color: '#00FF00',
  123. strokeStyle: '#007400',
  124. x: 320,
  125. y: 275,
  126. },
  127. {
  128. text: screenDownText,
  129. font: 'normal 28px Arial',
  130. color: '#00FF00',
  131. strokeStyle: '#007400',
  132. x: screenDownTextX,
  133. y: 325,
  134. },
  135. ];
  136. //
  137. getTextCanvas(526, 346, textArr, '').then((canvas: HTMLCanvasElement) => {
  138. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  139. textMap.colorSpace = THREE.SRGBColorSpace;
  140. const textMaterial = new THREE.MeshBasicMaterial({
  141. // 关于材质并未讲解 实操即可熟悉 这里是漫反射类似纸张的材质,对应的就有高光类似金属的材质.
  142. map: textMap, // 设置纹理贴图
  143. transparent: true,
  144. side: THREE.FrontSide, // 这里是双面渲染的意思
  145. });
  146. textMaterial.blending = THREE.CustomBlending;
  147. const monitorPlane = this.group.getObjectByName('monitorText');
  148. if (monitorPlane) {
  149. monitorPlane.material = textMaterial;
  150. } else {
  151. const planeGeometry = new THREE.PlaneGeometry(526, 346); // 平面3维几何体PlaneGeometry
  152. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  153. planeMesh.name = 'monitorText';
  154. planeMesh.scale.set(0.002, 0.002, 0.002);
  155. planeMesh.position.set(3.665, 0.09, -0.4);
  156. this.group.add(planeMesh);
  157. }
  158. textMap.dispose();
  159. });
  160. }
  161. /* 风门动画 */
  162. render() {
  163. if (!this.model) {
  164. return;
  165. }
  166. if (this.mixers && this.fmClock.running) {
  167. this.mixers.update(2);
  168. }
  169. }
  170. /* 点击风门 */
  171. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  172. if (this.animationTimer) {
  173. clearTimeout(this.animationTimer);
  174. this.animationTimer = null;
  175. }
  176. }
  177. mouseUpModel() {}
  178. /* 提取风门序列帧,初始化前后门动画 */
  179. initAnimation() {
  180. const fmGroup = this.group?.getObjectByName('Fm-noStation');
  181. if (fmGroup) {
  182. const tracks = fmGroup.animations[0].tracks;
  183. const fontTracks: any[] = [],
  184. backTracks: any[] = [];
  185. for (let i = 0; i < tracks.length; i++) {
  186. const track = tracks[i];
  187. if (track.name.startsWith('qianmen')) {
  188. fontTracks.push(track);
  189. } else if (track.name.startsWith('houmen')) {
  190. backTracks.push(track);
  191. }
  192. }
  193. this.mixers = new THREE.AnimationMixer(fmGroup);
  194. const frontDoor = new THREE.AnimationClip('frontDoor', 22, fontTracks);
  195. const frontClipAction = this.mixers.clipAction(frontDoor, fmGroup);
  196. frontClipAction.clampWhenFinished = true;
  197. frontClipAction.loop = THREE.LoopOnce;
  198. this.clipActionArr.frontDoor = frontClipAction;
  199. const backDoor = new THREE.AnimationClip('backDoor', 22, backTracks);
  200. const backClipAction = this.mixers.clipAction(backDoor, fmGroup);
  201. backClipAction.clampWhenFinished = true;
  202. backClipAction.loop = THREE.LoopOnce;
  203. this.clipActionArr.backDoor = backClipAction;
  204. }
  205. }
  206. // 播放动画
  207. play(handlerState, timeScale = 0.01) {
  208. let handler = () => {};
  209. if (this.clipActionArr.frontDoor && this.clipActionArr.backDoor) {
  210. switch (handlerState) {
  211. case 1: // 打开前门
  212. handler = () => {
  213. this.clipActionArr.frontDoor.paused = true;
  214. this.clipActionArr.frontDoor.reset();
  215. this.clipActionArr.frontDoor.time = 1.2;
  216. this.clipActionArr.frontDoor.timeScale = timeScale;
  217. // this.clipActionArr.frontDoor.clampWhenFinished = true;
  218. this.clipActionArr.frontDoor.play();
  219. this.fmClock.start();
  220. // 显示打开前门文字
  221. if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = true;
  222. if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = false;
  223. };
  224. break;
  225. case 2: // 关闭前门
  226. handler = () => {
  227. this.clipActionArr.frontDoor.paused = true;
  228. this.clipActionArr.frontDoor.reset(); //
  229. this.clipActionArr.frontDoor.time = 4;
  230. this.clipActionArr.frontDoor.timeScale = -timeScale;
  231. // this.clipActionArr.frontDoor.clampWhenFinished = true;
  232. this.clipActionArr.frontDoor.play();
  233. this.fmClock.start();
  234. if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = false;
  235. if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = true;
  236. };
  237. break;
  238. case 3: // 打开后门
  239. handler = () => {
  240. this.clipActionArr.backDoor.paused = true;
  241. this.clipActionArr.backDoor.reset();
  242. this.clipActionArr.backDoor.time = 1.2;
  243. this.clipActionArr.backDoor.timeScale = timeScale;
  244. // this.clipActionArr.backDoor.clampWhenFinished = true;
  245. this.clipActionArr.backDoor.play();
  246. this.fmClock.start();
  247. if (this.backDamperOpenMesh) this.backDamperOpenMesh.visible = true;
  248. if (this.backDamperClosedMesh) this.backDamperClosedMesh.visible = false;
  249. };
  250. break;
  251. case 4: // 关闭后门
  252. handler = () => {
  253. this.clipActionArr.backDoor.paused = true;
  254. this.clipActionArr.backDoor.reset();
  255. this.clipActionArr.backDoor.time = 4;
  256. this.clipActionArr.backDoor.timeScale = -timeScale;
  257. // this.clipActionArr.backDoor.clampWhenFinished = true;
  258. this.clipActionArr.backDoor.play();
  259. this.fmClock.start();
  260. if (this.backDamperOpenMesh) this.backDamperOpenMesh.visible = false;
  261. if (this.backDamperClosedMesh) this.backDamperClosedMesh.visible = true;
  262. };
  263. break;
  264. // case 5: // 打开前后门
  265. // handler = () => {
  266. // this.clipActionArr.backDoor.paused = true;
  267. // this.clipActionArr.frontDoor.paused = true;
  268. // this.clipActionArr.frontDoor.reset();
  269. // this.clipActionArr.frontDoor.time = 0;
  270. // this.clipActionArr.frontDoor.timeScale = 0.01;
  271. // this.clipActionArr.frontDoor.clampWhenFinished = true;
  272. // this.clipActionArr.frontDoor.play();
  273. // this.clipActionArr.backDoor.reset();
  274. // this.clipActionArr.backDoor.time = 0;
  275. // this.clipActionArr.backDoor.timeScale = 0.01;
  276. // this.clipActionArr.backDoor.clampWhenFinished = true;
  277. // this.clipActionArr.backDoor.play();
  278. // this.frontClock.start();
  279. // this.backClock.start();
  280. // };
  281. // break;
  282. // case 6: // 关闭前后门
  283. // handler = () => {
  284. // debugger;
  285. // this.clipActionArr.backDoor.paused = true;
  286. // this.clipActionArr.frontDoor.paused = true;
  287. // this.clipActionArr.frontDoor.reset();
  288. // this.clipActionArr.frontDoor.time = 4;
  289. // this.clipActionArr.frontDoor.timeScale = -0.01;
  290. // this.clipActionArr.frontDoor.clampWhenFinished = true;
  291. // this.clipActionArr.frontDoor.play();
  292. // this.clipActionArr.backDoor.reset();
  293. // this.clipActionArr.backDoor.time = 4;
  294. // this.clipActionArr.backDoor.timeScale = -0.01;
  295. // this.clipActionArr.backDoor.clampWhenFinished = true;
  296. // this.clipActionArr.backDoor.play();
  297. // this.frontClock.start();
  298. // this.backClock.start();
  299. // };
  300. // break;
  301. default:
  302. }
  303. handler();
  304. }
  305. // model.clock.start();
  306. // const honglvdeng = group.getObjectByName('honglvdeng');
  307. // const material = honglvdeng.material;
  308. // setTimeout(() => {
  309. // if (handlerState === 2 || handlerState === 4 || handlerState === 6) {
  310. // material.color = new THREE.Color(0x00ff00);
  311. // } else {
  312. // material.color = new THREE.Color(0xff0000);
  313. // }
  314. // }, 1000);
  315. }
  316. mountedThree(playerDom) {
  317. this.group = new THREE.Object3D();
  318. this.group.name = this.modelName;
  319. return new Promise((resolve) => {
  320. if (!this.model) {
  321. resolve(null);
  322. }
  323. this.model.setGLTFModel('Fm-noStation').then((gltf) => {
  324. debugger;
  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;