gate.threejs.three.tl.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 * as dat from 'dat.gui';
  6. // const gui = new dat.GUI();
  7. // gui.domElement.style = 'position:absolute;top:10px;right:10px;z-index:99999999999999';
  8. class FmThreeTl {
  9. modelName = 'fm2';
  10. model; //
  11. group;
  12. isLRAnimation = true; // 是否开启左右摇摆动画
  13. direction = 1; // 摇摆方向
  14. animationTimer: NodeJS.Timeout | null = null; // 摇摆开启定时器
  15. deviceDetailCSS3D;
  16. playerStartClickTime1 = new Date().getTime();
  17. playerStartClickTime2 = new Date().getTime();
  18. fmClock = new THREE.Clock();
  19. mixers: THREE.AnimationMixer | undefined;
  20. clipActionArr = {
  21. frontDoor: null as unknown as THREE.AnimationAction,
  22. centerDoor: null as unknown as THREE.AnimationAction,
  23. backDoor: null as unknown as THREE.AnimationAction,
  24. };
  25. backDamperOpenMesh;
  26. backDamperClosedMesh;
  27. frontDamperOpenMesh;
  28. frontDamperClosedMesh;
  29. centerDamperOpenMesh;
  30. centerDamperClosedMesh;
  31. constructor(model) {
  32. this.model = model;
  33. }
  34. addLight() {
  35. const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
  36. directionalLight.position.set(-0.8, 23, 3.9);
  37. this.group?.add(directionalLight);
  38. directionalLight.target = this.group as THREE.Object3D;
  39. // const pointLight2 = new THREE.PointLight(0xffeeee, 1, 310);
  40. // pointLight2.position.set(-1.5, 2, -0.9);
  41. // pointLight2.shadow.bias = 0.05;
  42. // this.group?.add(pointLight2);
  43. // const pointLight3 = new THREE.PointLight(0xffeeee, 1, 310);
  44. // pointLight3.position.set(1.3, 2, -0.9);
  45. // pointLight3.shadow.bias = 0.05;
  46. // this.group?.add(pointLight3);
  47. // const pointLight4 = new THREE.PointLight(0xffeeee, 1, 150);
  48. // pointLight4.position.set(4.3, 1, -0.9);
  49. // pointLight4.shadow.bias = 0.05;
  50. // this.group?.add(pointLight4);
  51. // const pointLight5 = new THREE.PointLight(0xffeeee, 1, 150);
  52. // pointLight5.position.set(4.3, 1, -0.9);
  53. // pointLight5.shadow.bias = 0.05;
  54. // this.group?.add(pointLight5);
  55. // const pointLight6 = new THREE.PointLight(0xffeeee, 1, 150);
  56. // pointLight6.position.set(-4.4, 1, -0.9);
  57. // pointLight6.shadow.bias = 0.05;
  58. // this.group?.add(pointLight6);
  59. }
  60. // 重置摄像头
  61. resetCamera() {
  62. this.model.camera.far = 274;
  63. this.model.orbitControls?.update();
  64. this.model.camera.updateProjectionMatrix();
  65. }
  66. // 设置模型位置
  67. setModalPosition() {
  68. this.group?.scale.set(22, 22, 22);
  69. this.group?.position.set(-20, 20, 9);
  70. }
  71. /* 添加监控数据 */
  72. addMonitorText(selectData) {
  73. if (!this.group) {
  74. return;
  75. }
  76. const screenDownText = VENT_PARAM['modalText']
  77. ? VENT_PARAM['modalText']
  78. : History_Type['type'] == 'remote'
  79. ? `国能神东煤炭集团监制`
  80. : '煤炭科学技术研究院有限公司研制';
  81. const screenDownTextX = 80 - (screenDownText.length - 11) * 6;
  82. const textArr = [
  83. {
  84. text: `远程控制自动风门`,
  85. font: 'normal 30px Arial',
  86. color: '#00FF00',
  87. strokeStyle: '#007400',
  88. x: 120,
  89. y: 100,
  90. },
  91. {
  92. text: `净通行高度(m):`,
  93. font: 'normal 30px Arial',
  94. color: '#00FF00',
  95. strokeStyle: '#007400',
  96. x: 0,
  97. y: 155,
  98. },
  99. {
  100. text: `${selectData.fclearheight ? selectData.fclearheight : '-'}`,
  101. font: 'normal 30px Arial',
  102. color: '#00FF00',
  103. strokeStyle: '#007400',
  104. x: 290,
  105. y: 155,
  106. },
  107. {
  108. text: `净通行宽度(m): `,
  109. font: 'normal 30px Arial',
  110. color: '#00FF00',
  111. strokeStyle: '#007400',
  112. x: 0,
  113. y: 215,
  114. },
  115. {
  116. text: ` ${selectData.fclearwidth ? selectData.fclearwidth : '-'}`,
  117. font: 'normal 30px Arial',
  118. color: '#00FF00',
  119. strokeStyle: '#007400',
  120. x: 280,
  121. y: 215,
  122. },
  123. {
  124. text: `故障诊断:`,
  125. font: 'normal 30px Arial',
  126. color: '#00FF00',
  127. strokeStyle: '#007400',
  128. x: 0,
  129. y: 275,
  130. },
  131. {
  132. text: `${selectData.warnLevel_str ? selectData.warnLevel_str : '-'}`,
  133. font: 'normal 30px Arial',
  134. color: '#00FF00',
  135. strokeStyle: '#007400',
  136. x: 280,
  137. y: 275,
  138. },
  139. {
  140. text: screenDownText,
  141. font: 'normal 28px Arial',
  142. color: '#00FF00',
  143. strokeStyle: '#007400',
  144. x: screenDownTextX,
  145. y: 325,
  146. },
  147. ];
  148. //
  149. getTextCanvas(526, 346, textArr, '').then((canvas: HTMLCanvasElement) => {
  150. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  151. textMap.colorSpace = THREE.SRGBColorSpace;
  152. const textMaterial = new THREE.MeshBasicMaterial({
  153. // 关于材质并未讲解 实操即可熟悉 这里是漫反射类似纸张的材质,对应的就有高光类似金属的材质.
  154. map: textMap, // 设置纹理贴图
  155. transparent: true,
  156. side: THREE.FrontSide, // 这里是双面渲染的意思
  157. });
  158. textMaterial.blending = THREE.CustomBlending;
  159. const monitorPlane = this.group.getObjectByName('monitorText');
  160. if (monitorPlane) {
  161. monitorPlane.material = textMaterial;
  162. } else {
  163. const planeGeometry = new THREE.PlaneGeometry(526, 346); // 平面3维几何体PlaneGeometry
  164. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  165. planeMesh.name = 'monitorText';
  166. planeMesh.scale.set(0.002, 0.002, 0.002);
  167. planeMesh.position.set(6.645, 0.09, -0.39);
  168. this.group.add(planeMesh);
  169. }
  170. });
  171. }
  172. /** 添加热点 */
  173. drawHots() {
  174. const hotPositions = [
  175. { x: -0.37, y: 0.26, z: -0.32 },
  176. { x: 0.28, y: -0.2, z: -0.43 },
  177. { x: 0.55, y: -0.22, z: -0.38 },
  178. ];
  179. for (let i = 0; i < 3; i++) {
  180. const hotPoint = drawHot(0.1);
  181. const position = hotPositions[i];
  182. // hotPoint.scale.set(0.3, 0.3, 0.3);
  183. hotPoint.position.set(position.x, position.y, position.z);
  184. hotPoint.name = 'hotPoint' + i;
  185. this.group?.add(hotPoint);
  186. }
  187. }
  188. /* 风门动画 */
  189. render() {
  190. if (!this.model) {
  191. return;
  192. }
  193. if (this.isLRAnimation && this.group) {
  194. // 左右摇摆动画
  195. if (Math.abs(this.group.rotation.y) >= 0.2) {
  196. this.direction = -this.direction;
  197. this.group.rotation.y += 0.00002 * 30 * this.direction;
  198. } else {
  199. this.group.rotation.y += 0.00002 * 30 * this.direction;
  200. }
  201. }
  202. // 风门开关动画
  203. if (this.mixers && this.fmClock.running) this.mixers.update(2);
  204. }
  205. /* 点击风窗,风窗全屏 */
  206. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  207. this.isLRAnimation = false;
  208. if (this.animationTimer) {
  209. clearTimeout(this.animationTimer);
  210. this.animationTimer = null;
  211. }
  212. // 判断是否点击到视频
  213. intersects.find((intersect) => {
  214. const mesh = intersect.object;
  215. console.log('[ 点击事件 ] >');
  216. return false;
  217. });
  218. }
  219. mouseUpModel() {
  220. // 10s后开始摆动
  221. if (!this.animationTimer && !this.isLRAnimation) {
  222. this.animationTimer = setTimeout(() => {
  223. this.isLRAnimation = true;
  224. }, 10000);
  225. }
  226. }
  227. /* 提取风门序列帧,初始化前后门动画 */
  228. initAnimation() {
  229. const fmGroup = this.group?.getObjectByName('fm-three-tl');
  230. if (fmGroup) {
  231. const tracks = fmGroup.animations[0].tracks;
  232. const fontTracks: any[] = [],
  233. centerTracks: any[] = [],
  234. backTracks: any[] = [];
  235. console.log('风门帧', tracks);
  236. for (let i = 0; i < tracks.length; i++) {
  237. const track = tracks[i];
  238. if (track.name.startsWith('qianmen')) {
  239. fontTracks.push(track);
  240. } else if (track.name.startsWith('houmen') && !track.name.includes('_3')) {
  241. centerTracks.push(track);
  242. } else {
  243. backTracks.push(track);
  244. }
  245. }
  246. this.mixers = new THREE.AnimationMixer(fmGroup);
  247. const frontDoor = new THREE.AnimationClip('frontDoor', 4, fontTracks);
  248. const frontClipAction = this.mixers.clipAction(frontDoor, fmGroup);
  249. frontClipAction.clampWhenFinished = true;
  250. frontClipAction.loop = THREE.LoopOnce;
  251. this.clipActionArr.frontDoor = frontClipAction;
  252. const backDoor = new THREE.AnimationClip('backDoor', 4, backTracks);
  253. const backClipAction = this.mixers.clipAction(backDoor, fmGroup);
  254. backClipAction.clampWhenFinished = true;
  255. backClipAction.loop = THREE.LoopOnce;
  256. this.clipActionArr.backDoor = backClipAction;
  257. const centerDoor = new THREE.AnimationClip('centerDoor', 4, centerTracks);
  258. const centerClipAction = this.mixers.clipAction(centerDoor, fmGroup);
  259. centerClipAction.clampWhenFinished = true;
  260. centerClipAction.loop = THREE.LoopOnce;
  261. this.clipActionArr.centerDoor = centerClipAction;
  262. }
  263. }
  264. deviceDetailCard(position = { x: 0, y: 0, z: 0 }) {
  265. const element = document.getElementById('deviceCard') as HTMLElement;
  266. this.deviceDetailCSS3D = new CSS2DObject(element);
  267. this.deviceDetailCSS3D.name = 'deviceCard';
  268. this.deviceDetailCSS3D.position.set(position.x, position.y, position.z);
  269. this.deviceDetailCSS3D.visible = false;
  270. // this.model.scene.add(this.deviceDetailCSS3D);
  271. this.group.add(this.deviceDetailCSS3D);
  272. }
  273. // 播放动画
  274. play(handlerState, timeScale = 0.01) {
  275. if (this.clipActionArr.frontDoor && this.clipActionArr.backDoor) {
  276. let handler = () => {};
  277. switch (handlerState) {
  278. case 1: // 打开前门
  279. handler = () => {
  280. this.clipActionArr.frontDoor.paused = true;
  281. this.clipActionArr.frontDoor.reset();
  282. this.clipActionArr.frontDoor.time = 1.2;
  283. this.clipActionArr.frontDoor.timeScale = timeScale;
  284. this.clipActionArr.frontDoor.clampWhenFinished = true;
  285. this.clipActionArr.frontDoor.play();
  286. this.fmClock.start();
  287. // 显示打开前门文字
  288. if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = true;
  289. if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = false;
  290. };
  291. break;
  292. case 2: // 关闭前门
  293. handler = () => {
  294. this.clipActionArr.frontDoor.paused = true;
  295. this.clipActionArr.frontDoor.reset(); //
  296. this.clipActionArr.frontDoor.time = 4;
  297. this.clipActionArr.frontDoor.timeScale = -timeScale;
  298. this.clipActionArr.frontDoor.clampWhenFinished = true;
  299. this.clipActionArr.frontDoor.play();
  300. this.fmClock.start();
  301. // 显示打开前门文字
  302. if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = false;
  303. if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = true;
  304. };
  305. break;
  306. case 3: // 打开后门
  307. handler = () => {
  308. this.clipActionArr.backDoor.paused = true;
  309. this.clipActionArr.backDoor.reset();
  310. this.clipActionArr.backDoor.time = 1.2;
  311. this.clipActionArr.backDoor.timeScale = timeScale;
  312. this.clipActionArr.backDoor.clampWhenFinished = true;
  313. this.clipActionArr.backDoor.play();
  314. this.fmClock.start();
  315. if (this.backDamperOpenMesh) this.backDamperOpenMesh.visible = true;
  316. if (this.backDamperClosedMesh) this.backDamperClosedMesh.visible = false;
  317. };
  318. break;
  319. case 4: // 关闭后门
  320. handler = () => {
  321. this.clipActionArr.backDoor.paused = true;
  322. this.clipActionArr.backDoor.reset();
  323. this.clipActionArr.backDoor.time = 4;
  324. this.clipActionArr.backDoor.timeScale = -timeScale;
  325. this.clipActionArr.backDoor.clampWhenFinished = true;
  326. this.clipActionArr.backDoor.play();
  327. this.fmClock.start();
  328. if (this.backDamperOpenMesh) this.backDamperOpenMesh.visible = false;
  329. if (this.backDamperClosedMesh) this.backDamperClosedMesh.visible = true;
  330. };
  331. break;
  332. case 8: // 打开后中间门
  333. handler = () => {
  334. this.clipActionArr.centerDoor.paused = true;
  335. this.clipActionArr.centerDoor.reset();
  336. this.clipActionArr.centerDoor.time = 1.2;
  337. this.clipActionArr.centerDoor.timeScale = timeScale;
  338. this.clipActionArr.centerDoor.clampWhenFinished = true;
  339. this.clipActionArr.centerDoor.play();
  340. this.fmClock.start();
  341. if (this.centerDamperOpenMesh) this.centerDamperOpenMesh.visible = true;
  342. if (this.centerDamperClosedMesh) this.centerDamperClosedMesh.visible = false;
  343. };
  344. break;
  345. case 9: // 关闭中间门
  346. handler = () => {
  347. this.clipActionArr.centerDoor.paused = true;
  348. this.clipActionArr.centerDoor.reset();
  349. this.clipActionArr.centerDoor.time = 4;
  350. this.clipActionArr.centerDoor.timeScale = -timeScale;
  351. this.clipActionArr.centerDoor.clampWhenFinished = true;
  352. this.clipActionArr.centerDoor.play();
  353. this.fmClock.start();
  354. if (this.centerDamperOpenMesh) this.centerDamperOpenMesh.visible = false;
  355. if (this.centerDamperClosedMesh) this.centerDamperClosedMesh.visible = true;
  356. };
  357. break;
  358. default:
  359. }
  360. handler();
  361. // model.clock.start();
  362. // const honglvdeng = group.getObjectByName('honglvdeng');
  363. // const material = honglvdeng.material;
  364. // setTimeout(() => {
  365. // if (handlerState === 2 || handlerState === 4 || handlerState === 6) {
  366. // material.color = new THREE.Color(0x00ff00);
  367. // } else {
  368. // material.color = new THREE.Color(0xff0000);
  369. // }
  370. // }, 1000);
  371. }
  372. }
  373. async initCamera(dom1?) {
  374. const videoPlayer1 = dom1;
  375. let monitorPlane: THREE.Mesh | null = null;
  376. const canvas = await getTextCanvas(320, 180, '', 'noSinge.png');
  377. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  378. const textMaterial = new THREE.MeshBasicMaterial({
  379. map: textMap, // 设置纹理贴图
  380. transparent: true,
  381. side: THREE.DoubleSide, // 这里是双面渲染的意思
  382. });
  383. textMaterial.blending = THREE.CustomBlending;
  384. monitorPlane = this.group?.getObjectByName('noPlayer');
  385. if (monitorPlane) {
  386. monitorPlane.material = textMaterial;
  387. } else {
  388. const planeGeometry = new THREE.PlaneGeometry(100, 100); // 平面3维几何体PlaneGeometry
  389. monitorPlane = new THREE.Mesh(planeGeometry, textMaterial);
  390. textMaterial.dispose();
  391. planeGeometry.dispose();
  392. }
  393. const videoPlayer = this.group.getObjectByName('player1');
  394. if (videoPlayer) {
  395. this.model.clearMesh(videoPlayer);
  396. this.group.remove(videoPlayer);
  397. }
  398. const noPlayer1 = this.group.getObjectByName('noPlayer1');
  399. if (noPlayer1) {
  400. this.model.clearMesh(noPlayer1);
  401. this.group.remove(noPlayer1);
  402. }
  403. if (!videoPlayer1 && videoPlayer1 === null) {
  404. monitorPlane.name = 'noPlayer1';
  405. monitorPlane.scale.set(0.0085, 0.0056, 0.012);
  406. monitorPlane.position.set(-4.23, 0.02, -0.39);
  407. this.group?.add(monitorPlane);
  408. } else if (videoPlayer1) {
  409. const mesh = renderVideo(this.group, videoPlayer1, 'player1');
  410. if (mesh) {
  411. mesh?.scale.set(-0.028, 0.0285, 1);
  412. mesh?.position.set(-4.238, 0.02, -0.4);
  413. mesh.rotation.y = -Math.PI;
  414. this.group.add(mesh);
  415. }
  416. }
  417. }
  418. resetModal() {
  419. const fmThreeBase = this.group.getObjectByName('fmThreeBase');
  420. if (fmThreeBase) {
  421. const screenObj1 = fmThreeBase.getObjectByName('对象192');
  422. const screenObj2 = fmThreeBase.getObjectByName('对象231');
  423. if (screenObj1) screenObj1.visible = false;
  424. if (screenObj2) screenObj2.visible = false;
  425. }
  426. }
  427. mountedThree(playerDom) {
  428. this.group = new THREE.Object3D();
  429. this.group.name = this.modelName;
  430. return new Promise((resolve) => {
  431. this.model.setGLTFModel(['fm-three-tl'], this.group).then(() => {
  432. this.group.name = 'fm2';
  433. this.setModalPosition();
  434. this.resetModal();
  435. // 初始化左右摇摆动画;
  436. this.initAnimation();
  437. // this.drawHots();
  438. this.addLight();
  439. // this.deviceDetailCard();
  440. this.model.animate();
  441. this.backDamperOpenMesh = this.group.getObjectByName('Dampler_open_2_3');
  442. if (this.backDamperOpenMesh) this.backDamperOpenMesh.visible = false;
  443. this.backDamperClosedMesh = this.group.getObjectByName('Damper_Closed_3_3');
  444. if (this.backDamperClosedMesh) this.backDamperClosedMesh.visible = true;
  445. this.centerDamperOpenMesh = this.group.getObjectByName('Dampler_open_1');
  446. if (this.centerDamperOpenMesh) this.centerDamperOpenMesh.visible = false;
  447. this.centerDamperClosedMesh = this.group.getObjectByName('Damper_Closed_1');
  448. if (this.centerDamperClosedMesh) this.centerDamperClosedMesh.visible = true;
  449. this.frontDamperOpenMesh = this.group.getObjectByName('Damper_Open_2');
  450. if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = false;
  451. this.frontDamperClosedMesh = this.group.getObjectByName('Damper_Closed_2');
  452. if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = true;
  453. resolve(null);
  454. // this.initCamera(playerDom);
  455. });
  456. });
  457. }
  458. destroy() {
  459. if (this.model) {
  460. if (this.mixers) {
  461. const fmGroup = this.group?.getObjectByName('fmThree');
  462. this.mixers.uncacheClip(this.clipActionArr.frontDoor.getClip());
  463. this.mixers.uncacheClip(this.clipActionArr.backDoor.getClip());
  464. this.mixers.uncacheAction(this.clipActionArr.frontDoor.getClip(), fmGroup);
  465. this.mixers.uncacheAction(this.clipActionArr.backDoor.getClip(), fmGroup);
  466. this.mixers.uncacheRoot(fmGroup);
  467. if (this.model.animations[0]) this.model.animations[0].tracks = [];
  468. }
  469. this.model.clearGroup(this.group);
  470. this.clipActionArr.backDoor = undefined;
  471. this.clipActionArr.frontDoor = undefined;
  472. this.mixers = undefined;
  473. }
  474. }
  475. }
  476. export default FmThreeTl;