gate.threejs.two.ssl.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 fmSsl {
  10. modelName = 'fmssl';
  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. // 重置摄像头
  50. resetCamera() {
  51. this.model.camera.far = 274;
  52. this.model.orbitControls?.update();
  53. this.model.camera.updateProjectionMatrix();
  54. }
  55. // 设置模型位置
  56. setModalPosition() {
  57. this.group?.scale.set(22, 22, 22);
  58. this.group?.position.set(-20, 20, 9);
  59. }
  60. /* 添加监控数据 */
  61. addMonitorText(selectData) {
  62. if (!this.group) {
  63. return;
  64. }
  65. const screenDownText = VENT_PARAM['modalText']
  66. ? VENT_PARAM['modalText']
  67. : History_Type['type'] == 'remote'
  68. ? `国能神东煤炭集团监制`
  69. : '煤科通安(北京)智控科技有限公司研制';
  70. const screenDownTextX = 90 - (screenDownText.length - 11) * 6;
  71. const textArr = [
  72. {
  73. text: `远程控制自动风门`,
  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.995, 0.67, -0.27);
  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. mouseUpModel() {}
  172. /* 提取风门序列帧,初始化前后门动画 */
  173. initAnimation() {
  174. debugger;
  175. console.log('this.group', this.group);
  176. const men1 = this.group.getObjectByName('FengMen_SiShanLing').getObjectByName('men1');
  177. const men2 = this.group.getObjectByName('FengMen_SiShanLing').getObjectByName('men2');
  178. const tracks = this.group.animations[0].tracks;
  179. const fontTracks: any[] = [],
  180. backTracks: any[] = [];
  181. for (let i = 0; i < tracks.length; i++) {
  182. const track = tracks[i];
  183. if (track.name.startsWith('men1')) {
  184. fontTracks.push(track);
  185. } else if (track.name.startsWith('men2')) {
  186. backTracks.push(track);
  187. }
  188. }
  189. this.mixers = new THREE.AnimationMixer(this.group.getObjectByName('FengMen_SiShanLing'));
  190. const frontDoor = new THREE.AnimationClip('frontDoor', 22, fontTracks);
  191. const backDoor = new THREE.AnimationClip('backDoor', 22, backTracks);
  192. const frontClipAction = this.mixers.clipAction(frontDoor, men1);
  193. const backClipAction = this.mixers.clipAction(backDoor, men2);
  194. frontClipAction.clampWhenFinished = true;
  195. frontClipAction.loop = THREE.LoopOnce;
  196. this.clipActionArr.frontDoor = frontClipAction;
  197. backClipAction.clampWhenFinished = true;
  198. backClipAction.loop = THREE.LoopOnce;
  199. this.clipActionArr.backDoor = backClipAction;
  200. }
  201. // 播放动画
  202. play(handlerState, timeScale = 0.05) {
  203. if (this.clipActionArr.frontDoor) {
  204. let handler = () => {};
  205. switch (handlerState) {
  206. case 1: // 打开前门
  207. handler = () => {
  208. this.clipActionArr.frontDoor.paused = true;
  209. this.clipActionArr.frontDoor.reset();
  210. this.clipActionArr.frontDoor.time = 0;
  211. this.clipActionArr.frontDoor.timeScale = timeScale;
  212. this.clipActionArr.frontDoor.play();
  213. this.fmClock.start();
  214. };
  215. break;
  216. case 2: // 关闭前门
  217. handler = () => {
  218. this.clipActionArr.frontDoor.paused = true;
  219. this.clipActionArr.frontDoor.reset(); //
  220. this.clipActionArr.frontDoor.time = 1.5;
  221. this.clipActionArr.frontDoor.timeScale = -timeScale;
  222. this.clipActionArr.frontDoor.play();
  223. this.fmClock.start();
  224. };
  225. break;
  226. case 3: // 打开后门
  227. handler = () => {
  228. this.clipActionArr.backDoor.paused = true;
  229. this.clipActionArr.backDoor.reset();
  230. this.clipActionArr.backDoor.time = 0;
  231. this.clipActionArr.backDoor.timeScale = timeScale;
  232. this.clipActionArr.backDoor.play();
  233. this.fmClock.start();
  234. };
  235. break;
  236. case 4: // 关闭后门
  237. handler = () => {
  238. this.clipActionArr.backDoor.paused = true;
  239. this.clipActionArr.backDoor.reset(); //
  240. this.clipActionArr.backDoor.time = 1.5;
  241. this.clipActionArr.backDoor.timeScale = -timeScale;
  242. this.clipActionArr.backDoor.play();
  243. this.fmClock.start();
  244. };
  245. case 5: // 打开前后门
  246. handler = () => {
  247. this.clipActionArr.frontDoor.paused = true;
  248. this.clipActionArr.frontDoor.reset();
  249. this.clipActionArr.frontDoor.time = 0;
  250. this.clipActionArr.frontDoor.timeScale = timeScale;
  251. this.clipActionArr.frontDoor.play();
  252. this.fmClock.start();
  253. // 显示打开前门文字
  254. if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = true;
  255. if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = false;
  256. this.clipActionArr.backDoor.paused = true;
  257. this.clipActionArr.backDoor.reset();
  258. this.clipActionArr.backDoor.time = 0;
  259. this.clipActionArr.backDoor.timeScale = timeScale;
  260. this.clipActionArr.backDoor.play();
  261. this.fmClock.start();
  262. };
  263. break;
  264. case 6: // 关闭前后门
  265. handler = () => {
  266. this.clipActionArr.frontDoor.paused = true;
  267. this.clipActionArr.frontDoor.reset(); //
  268. this.clipActionArr.frontDoor.time = 1.5;
  269. this.clipActionArr.frontDoor.timeScale = -timeScale;
  270. this.clipActionArr.frontDoor.play();
  271. this.fmClock.start();
  272. if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = false;
  273. if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = true;
  274. this.clipActionArr.backDoor.paused = true;
  275. this.clipActionArr.backDoor.reset();
  276. this.clipActionArr.backDoor.time = 1.5;
  277. this.clipActionArr.backDoor.timeScale = -timeScale;
  278. this.clipActionArr.backDoor.play();
  279. this.fmClock.start();
  280. };
  281. break;
  282. default:
  283. }
  284. handler();
  285. }
  286. }
  287. mountedThree(playerDom) {
  288. this.group = new THREE.Object3D();
  289. this.group.name = this.modelName;
  290. return new Promise((resolve) => {
  291. if (!this.model) {
  292. resolve(null);
  293. }
  294. this.model.setGLTFModel('fmssl').then((gltf) => {
  295. this.group = gltf[0];
  296. this.group.name = 'fmssl';
  297. console.log('fmssl', this.group);
  298. this.setModalPosition();
  299. this.initAnimation();
  300. this.addLight();
  301. this.model.animate();
  302. resolve(this.model);
  303. });
  304. });
  305. }
  306. destroy() {
  307. if (this.model) {
  308. if (this.mixers) {
  309. this.mixers.uncacheClip(this.clipActionArr.frontDoor.getClip());
  310. this.mixers.uncacheClip(this.clipActionArr.backDoor.getClip());
  311. this.mixers.uncacheAction(this.clipActionArr.frontDoor.getClip(), this.group);
  312. this.mixers.uncacheAction(this.clipActionArr.backDoor.getClip(), this.group);
  313. this.mixers.uncacheRoot(this.group);
  314. if (this.model.animations[0]) this.model.animations[0].tracks = [];
  315. }
  316. this.model.clearGroup(this.group);
  317. this.clipActionArr.backDoor = undefined;
  318. this.clipActionArr.frontDoor = undefined;
  319. this.mixers = undefined;
  320. // document.getElementById('damper3D').parentElement.remove(document.getElementById('damper3D'))
  321. }
  322. }
  323. }
  324. export default fmSsl;