gate.threejs.three.hsw.ts 19 KB

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