gate.threejs.three.tl.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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 FmHsw3 {
  9. modelName = 'fmHsw3';
  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. // 风门开关动画
  194. if (this.mixers && this.fmClock.running) this.mixers.update(2);
  195. }
  196. /* 点击风窗,风窗全屏 */
  197. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  198. if (this.animationTimer) {
  199. clearTimeout(this.animationTimer);
  200. this.animationTimer = null;
  201. }
  202. // 判断是否点击到视频
  203. intersects.find((intersect) => {
  204. const mesh = intersect.object;
  205. console.log('[ 点击事件 ] >');
  206. return false;
  207. });
  208. }
  209. mouseUpModel() {}
  210. /* 提取风门序列帧,初始化前后门动画 */
  211. initAnimation() {
  212. const fmGroup = this.group?.getObjectByName('fm-three-tl');
  213. if (fmGroup) {
  214. const tracks = fmGroup.animations[0].tracks;
  215. const fontTracks: any[] = [],
  216. centerTracks: any[] = [],
  217. backTracks: any[] = [];
  218. console.log('风门帧', tracks);
  219. for (let i = 0; i < tracks.length; i++) {
  220. const track = tracks[i];
  221. if (track.name.startsWith('qianmen')) {
  222. fontTracks.push(track);
  223. } else if (track.name.startsWith('houmen') && !track.name.includes('_3')) {
  224. centerTracks.push(track);
  225. } else {
  226. backTracks.push(track);
  227. }
  228. }
  229. this.mixers = new THREE.AnimationMixer(fmGroup);
  230. const frontDoor = new THREE.AnimationClip('frontDoor', 4, fontTracks);
  231. const frontClipAction = this.mixers.clipAction(frontDoor, fmGroup);
  232. frontClipAction.clampWhenFinished = true;
  233. frontClipAction.loop = THREE.LoopOnce;
  234. this.clipActionArr.frontDoor = frontClipAction;
  235. const backDoor = new THREE.AnimationClip('backDoor', 4, backTracks);
  236. const backClipAction = this.mixers.clipAction(backDoor, fmGroup);
  237. backClipAction.clampWhenFinished = true;
  238. backClipAction.loop = THREE.LoopOnce;
  239. this.clipActionArr.backDoor = backClipAction;
  240. const centerDoor = new THREE.AnimationClip('centerDoor', 4, centerTracks);
  241. const centerClipAction = this.mixers.clipAction(centerDoor, fmGroup);
  242. centerClipAction.clampWhenFinished = true;
  243. centerClipAction.loop = THREE.LoopOnce;
  244. this.clipActionArr.centerDoor = centerClipAction;
  245. }
  246. }
  247. deviceDetailCard(position = { x: 0, y: 0, z: 0 }) {
  248. const element = document.getElementById('deviceCard') as HTMLElement;
  249. this.deviceDetailCSS3D = new CSS2DObject(element);
  250. this.deviceDetailCSS3D.name = 'deviceCard';
  251. this.deviceDetailCSS3D.position.set(position.x, position.y, position.z);
  252. this.deviceDetailCSS3D.visible = false;
  253. // this.model.scene.add(this.deviceDetailCSS3D);
  254. this.group.add(this.deviceDetailCSS3D);
  255. }
  256. // 播放动画
  257. play(handlerState, timeScale = 0.01) {
  258. if (this.clipActionArr.frontDoor && this.clipActionArr.backDoor) {
  259. let handler = () => {};
  260. switch (handlerState) {
  261. case 1: // 打开前门
  262. handler = () => {
  263. this.clipActionArr.frontDoor.paused = true;
  264. this.clipActionArr.frontDoor.reset();
  265. this.clipActionArr.frontDoor.time = 1.2;
  266. this.clipActionArr.frontDoor.timeScale = timeScale;
  267. this.clipActionArr.frontDoor.clampWhenFinished = true;
  268. this.clipActionArr.frontDoor.play();
  269. this.fmClock.start();
  270. // 显示打开前门文字
  271. if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = true;
  272. if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = false;
  273. };
  274. break;
  275. case 2: // 关闭前门
  276. handler = () => {
  277. this.clipActionArr.frontDoor.paused = true;
  278. this.clipActionArr.frontDoor.reset(); //
  279. this.clipActionArr.frontDoor.time = 4;
  280. this.clipActionArr.frontDoor.timeScale = -timeScale;
  281. this.clipActionArr.frontDoor.clampWhenFinished = true;
  282. this.clipActionArr.frontDoor.play();
  283. this.fmClock.start();
  284. // 显示打开前门文字
  285. if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = false;
  286. if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = true;
  287. };
  288. break;
  289. case 3: // 打开后门
  290. handler = () => {
  291. this.clipActionArr.backDoor.paused = true;
  292. this.clipActionArr.backDoor.reset();
  293. this.clipActionArr.backDoor.time = 1.2;
  294. this.clipActionArr.backDoor.timeScale = timeScale;
  295. this.clipActionArr.backDoor.clampWhenFinished = true;
  296. this.clipActionArr.backDoor.play();
  297. this.fmClock.start();
  298. if (this.backDamperOpenMesh) this.backDamperOpenMesh.visible = true;
  299. if (this.backDamperClosedMesh) this.backDamperClosedMesh.visible = false;
  300. };
  301. break;
  302. case 4: // 关闭后门
  303. handler = () => {
  304. this.clipActionArr.backDoor.paused = true;
  305. this.clipActionArr.backDoor.reset();
  306. this.clipActionArr.backDoor.time = 4;
  307. this.clipActionArr.backDoor.timeScale = -timeScale;
  308. this.clipActionArr.backDoor.clampWhenFinished = true;
  309. this.clipActionArr.backDoor.play();
  310. this.fmClock.start();
  311. if (this.backDamperOpenMesh) this.backDamperOpenMesh.visible = false;
  312. if (this.backDamperClosedMesh) this.backDamperClosedMesh.visible = true;
  313. };
  314. break;
  315. case 8: // 打开后中间门
  316. handler = () => {
  317. this.clipActionArr.centerDoor.paused = true;
  318. this.clipActionArr.centerDoor.reset();
  319. this.clipActionArr.centerDoor.time = 1.2;
  320. this.clipActionArr.centerDoor.timeScale = timeScale;
  321. this.clipActionArr.centerDoor.clampWhenFinished = true;
  322. this.clipActionArr.centerDoor.play();
  323. this.fmClock.start();
  324. if (this.centerDamperOpenMesh) this.centerDamperOpenMesh.visible = true;
  325. if (this.centerDamperClosedMesh) this.centerDamperClosedMesh.visible = false;
  326. };
  327. break;
  328. case 9: // 关闭中间门
  329. handler = () => {
  330. this.clipActionArr.centerDoor.paused = true;
  331. this.clipActionArr.centerDoor.reset();
  332. this.clipActionArr.centerDoor.time = 4;
  333. this.clipActionArr.centerDoor.timeScale = -timeScale;
  334. this.clipActionArr.centerDoor.clampWhenFinished = true;
  335. this.clipActionArr.centerDoor.play();
  336. this.fmClock.start();
  337. if (this.centerDamperOpenMesh) this.centerDamperOpenMesh.visible = false;
  338. if (this.centerDamperClosedMesh) this.centerDamperClosedMesh.visible = true;
  339. };
  340. break;
  341. default:
  342. }
  343. handler();
  344. // model.clock.start();
  345. // const honglvdeng = group.getObjectByName('honglvdeng');
  346. // const material = honglvdeng.material;
  347. // setTimeout(() => {
  348. // if (handlerState === 2 || handlerState === 4 || handlerState === 6) {
  349. // material.color = new THREE.Color(0x00ff00);
  350. // } else {
  351. // material.color = new THREE.Color(0xff0000);
  352. // }
  353. // }, 1000);
  354. }
  355. }
  356. async initCamera(dom1?) {
  357. const videoPlayer1 = dom1;
  358. let monitorPlane: THREE.Mesh | null = null;
  359. const canvas = await getTextCanvas(320, 180, '', 'noSinge.png');
  360. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  361. const textMaterial = new THREE.MeshBasicMaterial({
  362. map: textMap, // 设置纹理贴图
  363. transparent: true,
  364. side: THREE.DoubleSide, // 这里是双面渲染的意思
  365. });
  366. textMaterial.blending = THREE.CustomBlending;
  367. monitorPlane = this.group?.getObjectByName('noPlayer');
  368. if (monitorPlane) {
  369. monitorPlane.material = textMaterial;
  370. } else {
  371. const planeGeometry = new THREE.PlaneGeometry(100, 100); // 平面3维几何体PlaneGeometry
  372. monitorPlane = new THREE.Mesh(planeGeometry, textMaterial);
  373. textMaterial.dispose();
  374. planeGeometry.dispose();
  375. }
  376. const videoPlayer = this.group.getObjectByName('player1');
  377. if (videoPlayer) {
  378. this.model.clearMesh(videoPlayer);
  379. this.group.remove(videoPlayer);
  380. }
  381. const noPlayer1 = this.group.getObjectByName('noPlayer1');
  382. if (noPlayer1) {
  383. this.model.clearMesh(noPlayer1);
  384. this.group.remove(noPlayer1);
  385. }
  386. if (!videoPlayer1 && videoPlayer1 === null) {
  387. monitorPlane.name = 'noPlayer1';
  388. monitorPlane.scale.set(0.0085, 0.0056, 0.012);
  389. monitorPlane.position.set(-4.23, 0.02, -0.39);
  390. this.group?.add(monitorPlane);
  391. } else if (videoPlayer1) {
  392. const mesh = renderVideo(this.group, videoPlayer1, 'player1');
  393. if (mesh) {
  394. mesh?.scale.set(-0.028, 0.0285, 1);
  395. mesh?.position.set(-4.238, 0.02, -0.4);
  396. mesh.rotation.y = -Math.PI;
  397. this.group.add(mesh);
  398. }
  399. }
  400. }
  401. resetModal() {
  402. const fmThreeBase = this.group.getObjectByName('fmThreeBase');
  403. if (fmThreeBase) {
  404. const screenObj1 = fmThreeBase.getObjectByName('对象192');
  405. const screenObj2 = fmThreeBase.getObjectByName('对象231');
  406. if (screenObj1) screenObj1.visible = false;
  407. if (screenObj2) screenObj2.visible = false;
  408. }
  409. }
  410. mountedThree(playerDom) {
  411. this.group = new THREE.Object3D();
  412. this.group.name = this.modelName;
  413. return new Promise((resolve) => {
  414. this.model.setGLTFModel(['fmHsw3'], this.group).then(() => {
  415. this.group.name = 'fm2';
  416. this.setModalPosition();
  417. this.resetModal();
  418. // 初始化左右摇摆动画;
  419. this.initAnimation();
  420. // this.drawHots();
  421. this.addLight();
  422. // this.deviceDetailCard();
  423. this.model.animate();
  424. this.backDamperOpenMesh = this.group.getObjectByName('Dampler_open_2_3');
  425. if (this.backDamperOpenMesh) this.backDamperOpenMesh.visible = false;
  426. this.backDamperClosedMesh = this.group.getObjectByName('Damper_Closed_3_3');
  427. if (this.backDamperClosedMesh) this.backDamperClosedMesh.visible = true;
  428. this.centerDamperOpenMesh = this.group.getObjectByName('Dampler_open_1');
  429. if (this.centerDamperOpenMesh) this.centerDamperOpenMesh.visible = false;
  430. this.centerDamperClosedMesh = this.group.getObjectByName('Damper_Closed_1');
  431. if (this.centerDamperClosedMesh) this.centerDamperClosedMesh.visible = true;
  432. this.frontDamperOpenMesh = this.group.getObjectByName('Damper_Open_2');
  433. if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = false;
  434. this.frontDamperClosedMesh = this.group.getObjectByName('Damper_Closed_2');
  435. if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = true;
  436. resolve(null);
  437. // this.initCamera(playerDom);
  438. });
  439. });
  440. }
  441. destroy() {
  442. if (this.model) {
  443. if (this.mixers) {
  444. const fmGroup = this.group?.getObjectByName('fmThree');
  445. this.mixers.uncacheClip(this.clipActionArr.frontDoor.getClip());
  446. this.mixers.uncacheClip(this.clipActionArr.backDoor.getClip());
  447. this.mixers.uncacheAction(this.clipActionArr.frontDoor.getClip(), fmGroup);
  448. this.mixers.uncacheAction(this.clipActionArr.backDoor.getClip(), fmGroup);
  449. this.mixers.uncacheRoot(fmGroup);
  450. if (this.model.animations[0]) this.model.animations[0].tracks = [];
  451. }
  452. this.model.clearGroup(this.group);
  453. this.clipActionArr.backDoor = undefined;
  454. this.clipActionArr.frontDoor = undefined;
  455. this.mixers = undefined;
  456. }
  457. }
  458. }
  459. export default FmHsw3;