gate.threejs.noStation.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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.isLRAnimation && this.group) {
  167. // 左右摇摆动画
  168. if (Math.abs(this.group.rotation.y) >= 0.2) {
  169. this.direction = -this.direction;
  170. this.group.rotation.y += 0.00002 * 30 * this.direction;
  171. } else {
  172. this.group.rotation.y += 0.00002 * 30 * this.direction;
  173. }
  174. }
  175. if (this.mixers && this.fmClock.running) {
  176. this.mixers.update(2);
  177. }
  178. }
  179. /* 点击风门 */
  180. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  181. this.isLRAnimation = false;
  182. if (this.animationTimer) {
  183. clearTimeout(this.animationTimer);
  184. this.animationTimer = null;
  185. }
  186. }
  187. mouseUpModel() {
  188. // 10s后开始摆动
  189. if (!this.animationTimer && !this.isLRAnimation) {
  190. this.animationTimer = setTimeout(() => {
  191. this.isLRAnimation = true;
  192. }, 10000);
  193. }
  194. }
  195. /* 提取风门序列帧,初始化前后门动画 */
  196. initAnimation() {
  197. const fmGroup = this.group?.getObjectByName('Fm-noStation');
  198. if (fmGroup) {
  199. const tracks = fmGroup.animations[0].tracks;
  200. const fontTracks: any[] = [],
  201. backTracks: any[] = [];
  202. for (let i = 0; i < tracks.length; i++) {
  203. const track = tracks[i];
  204. if (track.name.startsWith('qianmen')) {
  205. fontTracks.push(track);
  206. } else if (track.name.startsWith('houmen')) {
  207. backTracks.push(track);
  208. }
  209. }
  210. this.mixers = new THREE.AnimationMixer(fmGroup);
  211. const frontDoor = new THREE.AnimationClip('frontDoor', 22, fontTracks);
  212. const frontClipAction = this.mixers.clipAction(frontDoor, fmGroup);
  213. frontClipAction.clampWhenFinished = true;
  214. frontClipAction.loop = THREE.LoopOnce;
  215. this.clipActionArr.frontDoor = frontClipAction;
  216. const backDoor = new THREE.AnimationClip('backDoor', 22, backTracks);
  217. const backClipAction = this.mixers.clipAction(backDoor, fmGroup);
  218. backClipAction.clampWhenFinished = true;
  219. backClipAction.loop = THREE.LoopOnce;
  220. this.clipActionArr.backDoor = backClipAction;
  221. }
  222. }
  223. // 播放动画
  224. play(handlerState, timeScale = 0.01) {
  225. let handler = () => {};
  226. if (this.clipActionArr.frontDoor && this.clipActionArr.backDoor) {
  227. switch (handlerState) {
  228. case 1: // 打开前门
  229. handler = () => {
  230. this.clipActionArr.frontDoor.paused = true;
  231. this.clipActionArr.frontDoor.reset();
  232. this.clipActionArr.frontDoor.time = 1.2;
  233. this.clipActionArr.frontDoor.timeScale = timeScale;
  234. // this.clipActionArr.frontDoor.clampWhenFinished = true;
  235. this.clipActionArr.frontDoor.play();
  236. this.fmClock.start();
  237. // 显示打开前门文字
  238. if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = true;
  239. if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = false;
  240. };
  241. break;
  242. case 2: // 关闭前门
  243. handler = () => {
  244. this.clipActionArr.frontDoor.paused = true;
  245. this.clipActionArr.frontDoor.reset(); //
  246. this.clipActionArr.frontDoor.time = 4;
  247. this.clipActionArr.frontDoor.timeScale = -timeScale;
  248. // this.clipActionArr.frontDoor.clampWhenFinished = true;
  249. this.clipActionArr.frontDoor.play();
  250. this.fmClock.start();
  251. if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = false;
  252. if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = true;
  253. };
  254. break;
  255. case 3: // 打开后门
  256. handler = () => {
  257. this.clipActionArr.backDoor.paused = true;
  258. this.clipActionArr.backDoor.reset();
  259. this.clipActionArr.backDoor.time = 1.2;
  260. this.clipActionArr.backDoor.timeScale = timeScale;
  261. // this.clipActionArr.backDoor.clampWhenFinished = true;
  262. this.clipActionArr.backDoor.play();
  263. this.fmClock.start();
  264. if (this.backDamperOpenMesh) this.backDamperOpenMesh.visible = true;
  265. if (this.backDamperClosedMesh) this.backDamperClosedMesh.visible = false;
  266. };
  267. break;
  268. case 4: // 关闭后门
  269. handler = () => {
  270. this.clipActionArr.backDoor.paused = true;
  271. this.clipActionArr.backDoor.reset();
  272. this.clipActionArr.backDoor.time = 4;
  273. this.clipActionArr.backDoor.timeScale = -timeScale;
  274. // this.clipActionArr.backDoor.clampWhenFinished = true;
  275. this.clipActionArr.backDoor.play();
  276. this.fmClock.start();
  277. if (this.backDamperOpenMesh) this.backDamperOpenMesh.visible = false;
  278. if (this.backDamperClosedMesh) this.backDamperClosedMesh.visible = true;
  279. };
  280. break;
  281. // case 5: // 打开前后门
  282. // handler = () => {
  283. // this.clipActionArr.backDoor.paused = true;
  284. // this.clipActionArr.frontDoor.paused = true;
  285. // this.clipActionArr.frontDoor.reset();
  286. // this.clipActionArr.frontDoor.time = 0;
  287. // this.clipActionArr.frontDoor.timeScale = 0.01;
  288. // this.clipActionArr.frontDoor.clampWhenFinished = true;
  289. // this.clipActionArr.frontDoor.play();
  290. // this.clipActionArr.backDoor.reset();
  291. // this.clipActionArr.backDoor.time = 0;
  292. // this.clipActionArr.backDoor.timeScale = 0.01;
  293. // this.clipActionArr.backDoor.clampWhenFinished = true;
  294. // this.clipActionArr.backDoor.play();
  295. // this.frontClock.start();
  296. // this.backClock.start();
  297. // };
  298. // break;
  299. // case 6: // 关闭前后门
  300. // handler = () => {
  301. // debugger;
  302. // this.clipActionArr.backDoor.paused = true;
  303. // this.clipActionArr.frontDoor.paused = true;
  304. // this.clipActionArr.frontDoor.reset();
  305. // this.clipActionArr.frontDoor.time = 4;
  306. // this.clipActionArr.frontDoor.timeScale = -0.01;
  307. // this.clipActionArr.frontDoor.clampWhenFinished = true;
  308. // this.clipActionArr.frontDoor.play();
  309. // this.clipActionArr.backDoor.reset();
  310. // this.clipActionArr.backDoor.time = 4;
  311. // this.clipActionArr.backDoor.timeScale = -0.01;
  312. // this.clipActionArr.backDoor.clampWhenFinished = true;
  313. // this.clipActionArr.backDoor.play();
  314. // this.frontClock.start();
  315. // this.backClock.start();
  316. // };
  317. // break;
  318. default:
  319. }
  320. handler();
  321. }
  322. // model.clock.start();
  323. // const honglvdeng = group.getObjectByName('honglvdeng');
  324. // const material = honglvdeng.material;
  325. // setTimeout(() => {
  326. // if (handlerState === 2 || handlerState === 4 || handlerState === 6) {
  327. // material.color = new THREE.Color(0x00ff00);
  328. // } else {
  329. // material.color = new THREE.Color(0xff0000);
  330. // }
  331. // }, 1000);
  332. }
  333. mountedThree(playerDom) {
  334. this.group = new THREE.Object3D();
  335. this.group.name = this.modelName;
  336. return new Promise((resolve) => {
  337. if (!this.model) {
  338. resolve(null);
  339. }
  340. this.model.setGLTFModel('Fm-noStation').then((gltf) => {
  341. debugger;
  342. const fmModal = gltf[0];
  343. fmModal.name = 'Fm-noStation';
  344. this.group?.add(fmModal);
  345. this.setModalPosition();
  346. // 初始化左右摇摆动画;
  347. this.initAnimation();
  348. this.addLight();
  349. this.model.animate();
  350. resolve(this.model);
  351. this.backDamperOpenMesh = this.group.getObjectByName('Dampler_open_1');
  352. if (this.backDamperOpenMesh) this.backDamperOpenMesh.visible = false;
  353. this.backDamperClosedMesh = this.group.getObjectByName('Damper_Closed_1');
  354. if (this.backDamperClosedMesh) this.backDamperClosedMesh.visible = true;
  355. this.frontDamperOpenMesh = this.group.getObjectByName('Damper_Open_2');
  356. if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = false;
  357. this.frontDamperClosedMesh = this.group.getObjectByName('Damper_Closed_2');
  358. if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = true;
  359. });
  360. });
  361. }
  362. destroy() {
  363. if (this.model) {
  364. if (this.mixers) {
  365. this.mixers.uncacheClip(this.clipActionArr.frontDoor.getClip());
  366. this.mixers.uncacheClip(this.clipActionArr.backDoor.getClip());
  367. this.mixers.uncacheAction(this.clipActionArr.frontDoor.getClip(), this.group);
  368. this.mixers.uncacheAction(this.clipActionArr.backDoor.getClip(), this.group);
  369. this.mixers.uncacheRoot(this.group);
  370. if (this.model.animations[0]) this.model.animations[0].tracks = [];
  371. }
  372. this.model.clearGroup(this.group);
  373. this.clipActionArr.backDoor = undefined;
  374. this.clipActionArr.frontDoor = undefined;
  375. this.mixers = undefined;
  376. // document.getElementById('damper3D').parentElement.remove(document.getElementById('damper3D'))
  377. }
  378. }
  379. }
  380. export default FmNoStation;