gate.threejs.three.ts 17 KB

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