balancePress.three.ts 16 KB

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