balancePress.three.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. import * as THREE from 'three';
  2. import { CSS3DObject } from 'three/examples/jsm/renderers/CSS3DRenderer.js';
  3. import { animateCamera, getTextCanvas, renderVideo, updateAxisCenter } from '/@/utils/threejs/util';
  4. import UseThree from '../../../../utils/threejs/useThree';
  5. import Smoke from '/@/views/vent/comment/threejs/Smoke';
  6. import gsap from 'gsap';
  7. import useEvent from '../../../../utils/threejs/useEvent';
  8. // import * as dat from 'dat.gui';
  9. // const gui = new dat.GUI();
  10. // gui.domElement.style = 'position:absolute;top:100px;left:10px;z-index:99999999999999';
  11. const modelName = 'jbfj-hd';
  12. // 模型对象、 文字对象
  13. let model,
  14. group: THREE.Object3D | undefined = new THREE.Object3D(),
  15. player1,
  16. topSmoke: Smoke | undefined,
  17. downSmoke: Smoke | undefined,
  18. playerStartClickTime1 = new Date().getTime();
  19. const { mouseDownFn } = useEvent();
  20. // 打灯光
  21. const addLight = (scene) => {
  22. const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
  23. directionalLight.position.set(106, -348, 19.9);
  24. group?.add(directionalLight);
  25. directionalLight.target = <THREE.Object3D>group;
  26. // gui.add(directionalLight.position, 'x', -1000, 1000);
  27. // gui.add(directionalLight.position, 'y', -1000, 1000);
  28. // gui.add(directionalLight.position, 'z', -1000, 1000);
  29. // const pointLight2 = new THREE.PointLight(0xffeeee, 1.5, 100);
  30. // pointLight2.position.set(-120, 16, -33);
  31. // pointLight2.shadow.bias = 0.05;
  32. // scene.add(pointLight2);
  33. //
  34. // const pointLight3 = new THREE.PointLight(0xffffff, 1, 40);
  35. // pointLight3.position.set(-66, 40, 1);
  36. // pointLight3.shadow.bias = 0.05;
  37. // scene.add(pointLight3);
  38. //
  39. // const pointLight4 = new THREE.PointLight(0xffeeee, 0.6, 230);
  40. // pointLight4.position.set(-18, 30, 12);
  41. // pointLight4.shadow.bias = 0.05;
  42. // scene.add(pointLight4);
  43. //
  44. // const pointLight5 = new THREE.PointLight(0xffffff, 0.8, 90);
  45. // pointLight5.position.set(-57, 7, -30);
  46. // pointLight5.shadow.bias = 0.05;
  47. // scene.add(pointLight5);
  48. //
  49. // const pointLight6 = new THREE.PointLight(0xffffff, 0.8, 270);
  50. // pointLight6.position.set(72, -33, 11.4);
  51. // pointLight6.shadow.bias = 0.05;
  52. // scene.add(pointLight6);
  53. // const pointLight7 = new THREE.PointLight(0xffffff, 0.8, 500);
  54. // pointLight7.position.set(-20, -43, 12);
  55. // pointLight7.shadow.bias = -0.05;
  56. // scene.add(pointLight7);
  57. const spotLight = new THREE.SpotLight();
  58. spotLight.angle = Math.PI / 16;
  59. spotLight.penumbra = 0;
  60. spotLight.castShadow = true;
  61. spotLight.position.set(0, 463, 687);
  62. scene.add(spotLight);
  63. spotLight.shadow.camera.near = 0.5; // default
  64. spotLight.shadow.camera.far = 1000; // default
  65. spotLight.shadow.focus = 1;
  66. spotLight.shadow.bias = -0.000002;
  67. spotLight.target = <THREE.Object3D>group;
  68. // gui.add(pointLight6.position, 'x', -200, 200);
  69. // gui.add(pointLight6.position, 'y', -200, 200);
  70. // gui.add(pointLight6.position, 'z', -200, 200);
  71. // gui.add(pointLight6, 'distance', 0, 500);
  72. };
  73. // 重置摄像头
  74. const resetCamera = () => {
  75. // model.camera.position.set(0, 30, 80);
  76. model.camera.position.set(0, -1000, 100);
  77. // model.camera.fov = 20
  78. model.camera.far = 1000;
  79. model.orbitControls?.update();
  80. model.camera.updateProjectionMatrix();
  81. };
  82. // 设置模型位置
  83. const setModalPosition = () => {
  84. if (group) {
  85. group.position.set(0, 13, -50);
  86. group.rotation.y = Math.PI / 2;
  87. }
  88. };
  89. const setControls = () => {
  90. model.orbitControls.panSpeed = 0.5;
  91. model.orbitControls.rotateSpeed = 0.5;
  92. model.orbitControls.maxPolarAngle = Math.PI / 2.4;
  93. model.orbitControls.minPolarAngle = Math.PI / 3;
  94. };
  95. // 切换局部通风机类型
  96. export const setModelType = (type) => {
  97. return new Promise((resolve) => {
  98. // 显示双道风窗
  99. setTimeout(async () => {
  100. const oldCameraPosition = { x: 500, y: 100, z: 500 };
  101. await animateCamera(oldCameraPosition, oldCameraPosition, { x: 0, y: 20, z: 80 }, { x: 0, y: 0, z: 0 }, model, 0.8);
  102. resolve(null);
  103. }, 300);
  104. });
  105. };
  106. /* 添加监控数据 */
  107. export const addText = (selectData) => {
  108. if (!group) {
  109. return;
  110. }
  111. const textArr = [
  112. {
  113. text: `煤矿巷道远程局部风机系统`,
  114. font: 'normal 30px Arial',
  115. color: '#009900',
  116. strokeStyle: '#002200',
  117. x: 50,
  118. y: 110,
  119. },
  120. {
  121. text: `进风量(m³/min):`,
  122. font: 'normal 30px Arial',
  123. color: '#009900',
  124. strokeStyle: '#002200',
  125. x: 0,
  126. y: 165,
  127. },
  128. {
  129. text: `${selectData.frontRearDP}`,
  130. font: 'normal 30px Arial',
  131. color: '#009900',
  132. strokeStyle: '#002200',
  133. x: 290,
  134. y: 165,
  135. },
  136. {
  137. text: `供风量(m³/min): `,
  138. font: 'normal 30px Arial',
  139. color: '#009900',
  140. strokeStyle: '#002200',
  141. x: 0,
  142. y: 220,
  143. },
  144. {
  145. text: ` ${selectData.sourcePressure}`,
  146. font: 'normal 30px Arial',
  147. color: '#009900',
  148. strokeStyle: '#002200',
  149. x: 280,
  150. y: 220,
  151. },
  152. {
  153. text: `故障诊断:`,
  154. font: 'normal 30px Arial',
  155. color: '#009900',
  156. strokeStyle: '#002200',
  157. x: 0,
  158. y: 275,
  159. },
  160. {
  161. text: `${selectData.warnLevel_str ? selectData.warnLevel_str : '-'}`,
  162. font: 'normal 30px Arial',
  163. color: '#009900',
  164. strokeStyle: '#002200',
  165. x: 280,
  166. y: 275,
  167. },
  168. {
  169. text: `煤炭科学技术研究院有限公司研制`,
  170. font: 'normal 28px Arial',
  171. color: '#009900',
  172. strokeStyle: '#002200',
  173. x: 20,
  174. y: 325,
  175. },
  176. ];
  177. getTextCanvas(526, 346, textArr, '').then((canvas: HTMLCanvasElement) => {
  178. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  179. const textMaterial = new THREE.MeshBasicMaterial({
  180. // 关于材质并未讲解 实操即可熟悉 这里是漫反射类似纸张的材质,对应的就有高光类似金属的材质.
  181. map: textMap, // 设置纹理贴图
  182. transparent: true,
  183. side: THREE.FrontSide, // 这里是双面渲染的意思
  184. });
  185. textMaterial.blending = THREE.CustomBlending;
  186. const monitorPlane = group?.getObjectByName('monitorText');
  187. if (monitorPlane) {
  188. monitorPlane.material = textMaterial;
  189. } else {
  190. const planeGeometry = new THREE.PlaneGeometry(526, 346); // 平面3维几何体PlaneGeometry
  191. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  192. planeMesh.name = 'monitorText';
  193. planeMesh.scale.set(0.0135, 0.0135, 0.0135);
  194. planeMesh.rotation.y = -Math.PI / 2;
  195. planeMesh.position.set(-84.79, 0.82, 17.01);
  196. group?.add(planeMesh);
  197. }
  198. });
  199. };
  200. // // css3D文字
  201. export const addCssText = () => {
  202. if (!group?.getObjectByName('text1')) {
  203. const element = document.getElementById('inputBox') as HTMLElement;
  204. const fanLocalCSS3D = new CSS3DObject(element);
  205. fanLocalCSS3D.name = 'text1';
  206. fanLocalCSS3D.scale.set(0.04, 0.04, 0.04);
  207. fanLocalCSS3D.rotation.y = -Math.PI / 2;
  208. fanLocalCSS3D.position.set(-85.68, 5.97, -17.74);
  209. group?.add(fanLocalCSS3D);
  210. }
  211. if (!group?.getObjectByName('text2')) {
  212. const element = document.getElementById('outBox') as HTMLElement;
  213. const fanLocalCSS3D = new CSS3DObject(element);
  214. fanLocalCSS3D.name = 'text2';
  215. fanLocalCSS3D.scale.set(0.1, 0.1, 0.1);
  216. fanLocalCSS3D.rotation.y = -Math.PI / 2;
  217. fanLocalCSS3D.position.set(74.63, 13.54, 3.84);
  218. group?.add(fanLocalCSS3D);
  219. }
  220. if (!group?.getObjectByName('text3')) {
  221. const element = document.getElementById('returnBox') as HTMLElement;
  222. const fanLocalCSS3D = new CSS3DObject(element);
  223. fanLocalCSS3D.name = 'text3';
  224. fanLocalCSS3D.scale.set(0.1, 0.1, 0.1);
  225. fanLocalCSS3D.rotation.y = -Math.PI / 2;
  226. fanLocalCSS3D.position.set(35.28, 10.05, -37.23);
  227. group?.add(fanLocalCSS3D);
  228. }
  229. if (!group?.getObjectByName('text4')) {
  230. const element = document.getElementById('gateBox') as HTMLElement;
  231. if (element) {
  232. element.innerHTML = '';
  233. const fanLocalCSS3D = new CSS3DObject(element);
  234. fanLocalCSS3D.name = 'text4';
  235. fanLocalCSS3D.scale.set(0.04, 0.04, 0.04);
  236. fanLocalCSS3D.rotation.y = -Math.PI / 2;
  237. fanLocalCSS3D.position.set(-73.13, 8.44, -23.52);
  238. group?.add(fanLocalCSS3D);
  239. }
  240. }
  241. if (!group?.getObjectByName('text5')) {
  242. const element = document.getElementById('windownBox') as HTMLElement;
  243. if (element) {
  244. // element.innerHTML = '';
  245. const fanLocalCSS3D = new CSS3DObject(element);
  246. fanLocalCSS3D.name = 'text5';
  247. fanLocalCSS3D.scale.set(0.07, 0.07, 0.07);
  248. fanLocalCSS3D.rotation.y = -Math.PI / 2;
  249. fanLocalCSS3D.position.set(-28.44, 9.78, -40.42);
  250. group?.add(fanLocalCSS3D);
  251. }
  252. }
  253. };
  254. export const playSmoke = (controlType, deviceType, frequency, state) => {
  255. if (frequency) {
  256. setSmokeFrequency(deviceType, frequency);
  257. }
  258. if (controlType === 'startSmoke') {
  259. runFly(deviceType, state);
  260. }
  261. };
  262. const initFly = async () => {
  263. const topCurve = [
  264. {
  265. path0: new THREE.Vector3(-94.84, 2.359, 3.61),
  266. path1: new THREE.Vector3(-85.678, 2.359, 3.61),
  267. isSpread: true,
  268. spreadDirection: -1, //
  269. },
  270. {
  271. path0: new THREE.Vector3(-85.678, 2.352, 3.66),
  272. path1: new THREE.Vector3(-85.636, 2.353, -3.829),
  273. isSpread: false,
  274. spreadDirection: 0,
  275. },
  276. {
  277. path0: new THREE.Vector3(-85.636, 2.353, -3.829),
  278. path1: new THREE.Vector3(-85.636, 1.026, -5.881),
  279. isSpread: false,
  280. spreadDirection: 0,
  281. },
  282. {
  283. path0: new THREE.Vector3(-85.636, 1.026, -5.881),
  284. path1: new THREE.Vector3(-85.618, 0.887, -12.862),
  285. isSpread: false,
  286. spreadDirection: 0,
  287. },
  288. {
  289. path0: new THREE.Vector3(-85.618, 0.827, -12.962),
  290. path1: new THREE.Vector3(80.404, 0.827, -12.962),
  291. isSpread: false,
  292. spreadDirection: 0,
  293. },
  294. {
  295. path0: new THREE.Vector3(80.404, 0.827, -12.962),
  296. path1: new THREE.Vector3(93.164, 0.85, -12.962),
  297. isSpread: true,
  298. spreadDirection: 1, // 1是由小变大,-1是由大变小
  299. },
  300. ];
  301. const downCurve = [
  302. {
  303. path0: new THREE.Vector3(-94.84, -0.388, 3.61),
  304. path1: new THREE.Vector3(-85.678, -0.393, 3.61),
  305. isSpread: true,
  306. spreadDirection: -1, //
  307. },
  308. {
  309. path0: new THREE.Vector3(-85.678, -0.393, 3.275),
  310. path1: new THREE.Vector3(-85.636, -0.392, -3.829),
  311. isSpread: false,
  312. spreadDirection: 0,
  313. },
  314. {
  315. path0: new THREE.Vector3(-85.636, -0.392, -3.829),
  316. path1: new THREE.Vector3(-85.636, 0.926, -5.881),
  317. isSpread: false,
  318. spreadDirection: 0,
  319. },
  320. {
  321. path0: new THREE.Vector3(-85.636, 1.026, -5.881),
  322. path1: new THREE.Vector3(-85.618, 0.887, -12.862),
  323. isSpread: false,
  324. spreadDirection: 0,
  325. },
  326. {
  327. path0: new THREE.Vector3(-85.618, 0.887, -12.962),
  328. path1: new THREE.Vector3(80.404, 0.887, -12.962),
  329. isSpread: false,
  330. spreadDirection: 0,
  331. },
  332. {
  333. path0: new THREE.Vector3(80.404, 0.887, -12.962),
  334. path1: new THREE.Vector3(93.164, 0.91, -12.962),
  335. isSpread: true,
  336. spreadDirection: 1, // 1是由小变大,-1是由大变小
  337. },
  338. ];
  339. // const windowCurve = new THREE.CatmullRomCurve3([
  340. // new THREE.Vector3(83.479, 0.052, -16.487),
  341. // new THREE.Vector3(-90.436, 0.0, -16.32),
  342. // new THREE.Vector3(-90.712, 0.0, -16.395),
  343. // new THREE.Vector3(-90.77, 0.0, -16.663),
  344. // new THREE.Vector3(-90.77, 0.0, -36.015),
  345. // ]);
  346. // const gateCurve = new THREE.CatmullRomCurve3([
  347. // new THREE.Vector3(83.479, 0.052, -16.487),
  348. // new THREE.Vector3(-30.267, 0.013, -16.458),
  349. // new THREE.Vector3(-30.62, 0.013, -16.523),
  350. // new THREE.Vector3(-30.727, 0.013, -16.831),
  351. // new THREE.Vector3(-30.727, 0.013, -35.84),
  352. // ]);
  353. if (!topSmoke) {
  354. topSmoke = new Smoke('/model/img/texture-smoke.png', '#ffffff', 0, 0.8, 0.5, 400);
  355. topSmoke.setPath(topCurve);
  356. await topSmoke.setPoints();
  357. group?.add(topSmoke.points);
  358. }
  359. if (!downSmoke) {
  360. downSmoke = new Smoke('/model/img/texture-smoke.png', '#ffffff', 0, 0.8, 0.5, 400);
  361. downSmoke.setPath(downCurve);
  362. await downSmoke.setPoints();
  363. group?.add(downSmoke.points);
  364. }
  365. // if (!group.getObjectByName('gateFly')) {
  366. // }
  367. };
  368. const runFly = (deviceType, state) => {
  369. if (!downSmoke || !topSmoke) return;
  370. if (state === 'open') {
  371. if (deviceType === 'top') {
  372. if (downSmoke.frameId) {
  373. downSmoke.stopSmoke();
  374. }
  375. topSmoke.startSmoke();
  376. } else {
  377. if (topSmoke.frameId) {
  378. topSmoke.stopSmoke();
  379. }
  380. downSmoke.startSmoke();
  381. }
  382. } else {
  383. if (downSmoke.frameId) {
  384. downSmoke.stopSmoke();
  385. }
  386. if (topSmoke.frameId) {
  387. topSmoke.stopSmoke();
  388. }
  389. }
  390. };
  391. // 调频 30-50 (life 300 - 800) , 25 = (800 - 300)/ 20
  392. const setSmokeFrequency = (deviceType, frequency) => {
  393. if (!downSmoke || !topSmoke) return;
  394. const life = (frequency - 30) * 25;
  395. const duration = (Math.abs(life - topSmoke.life) / 500) * 25;
  396. let smoke;
  397. if (deviceType === 'top') {
  398. smoke = topSmoke;
  399. } else {
  400. smoke = downSmoke;
  401. }
  402. gsap.to(smoke, {
  403. life: life,
  404. duration: duration,
  405. ease: 'easeInCubic',
  406. overwrite: true,
  407. });
  408. };
  409. const clearFly = () => {
  410. if (!downSmoke || !topSmoke) return;
  411. topSmoke.clearSmoke();
  412. downSmoke.clearSmoke();
  413. };
  414. // 初始化事件
  415. const startAnimation = () => {
  416. // 定义鼠标点击事件
  417. model.canvasContainer?.addEventListener('mousedown', mouseEvent.bind(null));
  418. model.canvasContainer?.addEventListener('pointerup', (event) => {
  419. event.stopPropagation();
  420. });
  421. };
  422. // 鼠标点击、松开事件
  423. const mouseEvent = (event) => {
  424. if (event.button == 0) {
  425. mouseDownFn(<UseThree>model, <THREE.Object3D>group, event, (intersects) => {
  426. intersects.find((intersect) => {
  427. const mesh = intersect.object;
  428. if (mesh.name === 'player1') {
  429. if (new Date().getTime() - playerStartClickTime1 < 400) {
  430. // 双击,视频放大
  431. if (player1) {
  432. player1.requestFullscreen();
  433. }
  434. }
  435. playerStartClickTime1 = new Date().getTime();
  436. return true;
  437. }
  438. });
  439. });
  440. console.log('摄像头控制信息', model?.orbitControls, model?.camera);
  441. }
  442. };
  443. export const mountedThree = (playerVal1) => {
  444. player1 = playerVal1;
  445. return new Promise((resolve) => {
  446. model = new UseThree('#fanLocal3D', '#fanLocal3DCSS');
  447. model.setEnvMap('test1');
  448. resetCamera();
  449. model.setGLTFModel([modelName]).then(async (gltf) => {
  450. group = gltf[0];
  451. model.scene?.add(group);
  452. setModalPosition();
  453. setControls();
  454. await initFly();
  455. model.animate();
  456. addLight(model.scene);
  457. const videoPlayer1 = document.getElementById('jb-player1')?.getElementsByClassName('vjs-tech')[0];
  458. if (videoPlayer1) {
  459. const mesh = renderVideo(group, videoPlayer1, 'player1');
  460. if (mesh) {
  461. mesh.scale.set(0.222, 0.19, 0.2);
  462. mesh.position.set(-84.87, 0.298, 24.76);
  463. mesh.rotation.y = -Math.PI / 2;
  464. group?.add(mesh);
  465. }
  466. } else {
  467. const textArr = [
  468. {
  469. text: `无信号输入`,
  470. font: 'normal 40px Arial',
  471. color: '#009900',
  472. strokeStyle: '#002200',
  473. x: 170,
  474. y: 40,
  475. },
  476. ];
  477. getTextCanvas(560, 346, textArr, '').then((canvas: HTMLCanvasElement) => {
  478. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  479. const textMaterial = new THREE.MeshBasicMaterial({
  480. map: textMap, // 设置纹理贴图
  481. transparent: true,
  482. side: THREE.DoubleSide, // 这里是双面渲染的意思
  483. });
  484. textMaterial.blending = THREE.CustomBlending;
  485. const monitorPlane = group?.getObjectByName('noPlayer');
  486. if (monitorPlane) {
  487. monitorPlane.material = textMaterial;
  488. } else {
  489. const planeGeometry = new THREE.PlaneGeometry(100, 100); // 平面3维几何体PlaneGeometry
  490. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  491. if (!videoPlayer1) {
  492. planeMesh.name = 'noPlayer';
  493. planeMesh.scale.set(0.07, 0.05, 0.07);
  494. planeMesh.position.set(-84.82, -1.53, 24.94);
  495. planeMesh.rotation.y = -Math.PI / 2;
  496. group?.add(planeMesh.clone());
  497. }
  498. }
  499. });
  500. }
  501. startAnimation();
  502. resolve(model);
  503. });
  504. });
  505. };
  506. export const destroy = () => {
  507. if (model) {
  508. model.isRender = false;
  509. clearFly();
  510. topSmoke = undefined;
  511. downSmoke = undefined;
  512. group = undefined;
  513. model.destroy();
  514. model = undefined;
  515. }
  516. };