gate.threejs.three.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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 Fm2 {
  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. 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 fmGroup = this.group?.getObjectByName('fmThree');
  237. if (fmGroup) {
  238. const tracks = fmGroup.animations[0].tracks;
  239. const fontTracks: any[] = [],
  240. centerTracks: any[] = [],
  241. backTracks: any[] = [];
  242. for (let i = 0; i < tracks.length; i++) {
  243. const track = tracks[i];
  244. if (track.name.startsWith('door01')) {
  245. fontTracks.push(track);
  246. } else if (track.name.startsWith('door02')) {
  247. centerTracks.push(track);
  248. } else if (track.name.startsWith('door03')) {
  249. backTracks.push(track);
  250. }
  251. }
  252. this.mixers = new THREE.AnimationMixer(fmGroup);
  253. const frontDoor = new THREE.AnimationClip('frontDoor', 4, fontTracks);
  254. const frontClipAction = this.mixers.clipAction(frontDoor, fmGroup);
  255. frontClipAction.clampWhenFinished = true;
  256. frontClipAction.loop = THREE.LoopOnce;
  257. this.clipActionArr.frontDoor = frontClipAction;
  258. const backDoor = new THREE.AnimationClip('backDoor', 4, backTracks);
  259. const backClipAction = this.mixers.clipAction(backDoor, fmGroup);
  260. backClipAction.clampWhenFinished = true;
  261. backClipAction.loop = THREE.LoopOnce;
  262. this.clipActionArr.backDoor = backClipAction;
  263. const centerDoor = new THREE.AnimationClip('centerDoor', 4, centerTracks);
  264. const centerClipAction = this.mixers.clipAction(centerDoor, fmGroup);
  265. centerClipAction.clampWhenFinished = true;
  266. centerClipAction.loop = THREE.LoopOnce;
  267. this.clipActionArr.centerDoor = centerClipAction;
  268. }
  269. }
  270. deviceDetailCard(position = { x: 0, y: 0, z: 0 }) {
  271. const element = document.getElementById('deviceCard') as HTMLElement;
  272. this.deviceDetailCSS3D = new CSS2DObject(element);
  273. this.deviceDetailCSS3D.name = 'deviceCard';
  274. this.deviceDetailCSS3D.position.set(position.x, position.y, position.z);
  275. this.deviceDetailCSS3D.visible = false;
  276. // this.model.scene.add(this.deviceDetailCSS3D);
  277. this.group.add(this.deviceDetailCSS3D);
  278. }
  279. // 播放动画
  280. play(handlerState, timeScale = 0.01) {
  281. if (this.clipActionArr.frontDoor && this.clipActionArr.backDoor) {
  282. let handler = () => {};
  283. switch (handlerState) {
  284. case 1: // 打开前门
  285. handler = () => {
  286. this.clipActionArr.frontDoor.paused = true;
  287. this.clipActionArr.frontDoor.reset();
  288. this.clipActionArr.frontDoor.time = 1.2;
  289. this.clipActionArr.frontDoor.timeScale = timeScale;
  290. this.clipActionArr.frontDoor.clampWhenFinished = true;
  291. this.clipActionArr.frontDoor.play();
  292. this.fmClock.start();
  293. };
  294. break;
  295. case 2: // 关闭前门
  296. handler = () => {
  297. this.clipActionArr.frontDoor.paused = true;
  298. this.clipActionArr.frontDoor.reset(); //
  299. this.clipActionArr.frontDoor.time = 4;
  300. this.clipActionArr.frontDoor.timeScale = -timeScale;
  301. this.clipActionArr.frontDoor.clampWhenFinished = true;
  302. this.clipActionArr.frontDoor.play();
  303. this.fmClock.start();
  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. };
  316. break;
  317. case 4: // 关闭后门
  318. handler = () => {
  319. this.clipActionArr.backDoor.paused = true;
  320. this.clipActionArr.backDoor.reset();
  321. this.clipActionArr.backDoor.time = 4;
  322. this.clipActionArr.backDoor.timeScale = -timeScale;
  323. this.clipActionArr.backDoor.clampWhenFinished = true;
  324. this.clipActionArr.backDoor.play();
  325. this.fmClock.start();
  326. };
  327. break;
  328. case 8: // 打开后门
  329. handler = () => {
  330. this.clipActionArr.centerDoor.paused = true;
  331. this.clipActionArr.centerDoor.reset();
  332. this.clipActionArr.centerDoor.time = 1.2;
  333. this.clipActionArr.centerDoor.timeScale = timeScale;
  334. this.clipActionArr.centerDoor.clampWhenFinished = true;
  335. this.clipActionArr.centerDoor.play();
  336. this.fmClock.start();
  337. };
  338. break;
  339. case 9: // 关闭后门
  340. handler = () => {
  341. this.clipActionArr.centerDoor.paused = true;
  342. this.clipActionArr.centerDoor.reset();
  343. this.clipActionArr.centerDoor.time = 4;
  344. this.clipActionArr.centerDoor.timeScale = -timeScale;
  345. this.clipActionArr.centerDoor.clampWhenFinished = true;
  346. this.clipActionArr.centerDoor.play();
  347. this.fmClock.start();
  348. };
  349. break;
  350. // case 5: // 打开前后门
  351. // handler = () => {
  352. // this.clipActionArr.backDoor.paused = true;
  353. // this.clipActionArr.frontDoor.paused = true;
  354. // this.clipActionArr.frontDoor.reset();
  355. // this.clipActionArr.frontDoor.time = 0;
  356. // this.clipActionArr.frontDoor.timeScale = 0.01;
  357. // this.clipActionArr.frontDoor.clampWhenFinished = true;
  358. // this.clipActionArr.frontDoor.play();
  359. // this.clipActionArr.backDoor.reset();
  360. // this.clipActionArr.backDoor.time = 0;
  361. // this.clipActionArr.backDoor.timeScale = 0.01;
  362. // this.clipActionArr.backDoor.clampWhenFinished = true;
  363. // this.clipActionArr.backDoor.play();
  364. // this.fmClock.start();
  365. // };
  366. // break;
  367. // case 6: // 关闭前后门
  368. // handler = () => {
  369. // this.clipActionArr.backDoor.paused = true;
  370. // this.clipActionArr.frontDoor.paused = true;
  371. // this.clipActionArr.frontDoor.reset();
  372. // this.clipActionArr.frontDoor.time = 4;
  373. // this.clipActionArr.frontDoor.timeScale = -0.01;
  374. // this.clipActionArr.frontDoor.clampWhenFinished = true;
  375. // this.clipActionArr.frontDoor.play();
  376. // this.clipActionArr.backDoor.reset();
  377. // this.clipActionArr.backDoor.time = 4;
  378. // this.clipActionArr.backDoor.timeScale = -0.01;
  379. // this.clipActionArr.backDoor.clampWhenFinished = true;
  380. // this.clipActionArr.backDoor.play();
  381. // this.fmClock.start();
  382. // };
  383. // break;
  384. default:
  385. }
  386. handler();
  387. // model.clock.start();
  388. // const honglvdeng = group.getObjectByName('honglvdeng');
  389. // const material = honglvdeng.material;
  390. // setTimeout(() => {
  391. // if (handlerState === 2 || handlerState === 4 || handlerState === 6) {
  392. // material.color = new THREE.Color(0x00ff00);
  393. // } else {
  394. // material.color = new THREE.Color(0xff0000);
  395. // }
  396. // }, 1000);
  397. }
  398. }
  399. async initCamera(dom1?) {
  400. const videoPlayer1 = dom1;
  401. let monitorPlane: THREE.Mesh | null = null;
  402. const canvas = await getTextCanvas(320, 180, '', 'noSinge.png');
  403. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  404. const textMaterial = new THREE.MeshBasicMaterial({
  405. map: textMap, // 设置纹理贴图
  406. transparent: true,
  407. side: THREE.DoubleSide, // 这里是双面渲染的意思
  408. });
  409. textMaterial.blending = THREE.CustomBlending;
  410. monitorPlane = this.group?.getObjectByName('noPlayer');
  411. if (monitorPlane) {
  412. monitorPlane.material = textMaterial;
  413. } else {
  414. const planeGeometry = new THREE.PlaneGeometry(100, 100); // 平面3维几何体PlaneGeometry
  415. monitorPlane = new THREE.Mesh(planeGeometry, textMaterial);
  416. textMaterial.dispose();
  417. planeGeometry.dispose();
  418. }
  419. const videoPlayer = this.group.getObjectByName('player1');
  420. if (videoPlayer) {
  421. this.model.clearMesh(videoPlayer);
  422. this.group.remove(videoPlayer);
  423. }
  424. const noPlayer1 = this.group.getObjectByName('noPlayer1');
  425. if (noPlayer1) {
  426. this.model.clearMesh(noPlayer1);
  427. this.group.remove(noPlayer1);
  428. }
  429. if (!videoPlayer1 && videoPlayer1 === null) {
  430. monitorPlane.name = 'noPlayer1';
  431. monitorPlane.scale.set(0.0085, 0.0056, 0.012);
  432. monitorPlane.position.set(-4.23, 0.02, -0.39);
  433. this.group?.add(monitorPlane);
  434. } else if (videoPlayer1) {
  435. const mesh = renderVideo(this.group, videoPlayer1, 'player1');
  436. if (mesh) {
  437. mesh?.scale.set(-0.028, 0.0285, 1);
  438. mesh?.position.set(-4.238, 0.02, -0.4);
  439. mesh.rotation.y = -Math.PI;
  440. this.group.add(mesh);
  441. }
  442. }
  443. }
  444. resetModal() {
  445. const fmThreeBase = this.group.getObjectByName('fmThreeBase');
  446. if (fmThreeBase) {
  447. const screenObj1 = fmThreeBase.getObjectByName('对象192');
  448. const screenObj2 = fmThreeBase.getObjectByName('对象231');
  449. if (screenObj1) screenObj1.visible = false;
  450. if (screenObj2) screenObj2.visible = false;
  451. }
  452. }
  453. mountedThree(playerDom) {
  454. this.group = new THREE.Object3D();
  455. this.group.name = this.modelName;
  456. return new Promise((resolve) => {
  457. this.model.setGLTFModel(['fmThreeBase', 'fmThree'], this.group).then(() => {
  458. this.group.name = 'fm2';
  459. this.setModalPosition();
  460. this.resetModal();
  461. // 初始化左右摇摆动画;
  462. this.initAnimation();
  463. // this.drawHots();
  464. this.addLight();
  465. // this.deviceDetailCard();
  466. this.model.animate();
  467. if (this.model.camera && this.model.camera.layers.mask == -1) this.model.camera.layers.toggle(1);
  468. resolve(null);
  469. // this.initCamera(playerDom);
  470. });
  471. });
  472. }
  473. destroy() {
  474. if (this.model) {
  475. if (this.mixers) {
  476. const fmGroup = this.group?.getObjectByName('fmThree');
  477. this.mixers.uncacheClip(this.clipActionArr.frontDoor.getClip());
  478. this.mixers.uncacheClip(this.clipActionArr.backDoor.getClip());
  479. this.mixers.uncacheAction(this.clipActionArr.frontDoor.getClip(), fmGroup);
  480. this.mixers.uncacheAction(this.clipActionArr.backDoor.getClip(), fmGroup);
  481. this.mixers.uncacheRoot(fmGroup);
  482. if (this.model.animations[0]) this.model.animations[0].tracks = [];
  483. }
  484. this.model.clearGroup(this.group);
  485. this.clipActionArr.backDoor = undefined;
  486. this.clipActionArr.frontDoor = undefined;
  487. this.mixers = undefined;
  488. }
  489. }
  490. }
  491. export default Fm2;