balancePress.three.ts 17 KB

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