gate.threejs.three.ts 19 KB

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