gate.threejs.three.ts 17 KB

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