fanLocal.three.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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 fcFan from './fcfanLocal.three';
  6. import fmFan from './fmfanLocal.three';
  7. import Smoke from '/@/views/vent/comment/threejs/Smoke';
  8. import gsap from 'gsap';
  9. import useEvent from '../../../../utils/threejs/useEvent';
  10. // import * as dat from 'dat.gui';
  11. // const gui = new dat.GUI();
  12. // gui.domElement.style = 'position:absolute;top:100px;left:10px;z-index:99999999999999';
  13. const modelName = 'jbfj-hd';
  14. // 模型对象、 文字对象
  15. let model: UseThree | undefined,
  16. group: THREE.Object3D | undefined,
  17. fcFanObj: fcFan | undefined,
  18. fmFanObj: fmFan | undefined,
  19. player1,
  20. fanType,
  21. topSmoke: Smoke | undefined,
  22. downSmoke: Smoke | undefined,
  23. topLife: number | undefined,
  24. downLife: number | undefined,
  25. playerStartClickTime1 = new Date().getTime();
  26. const { mouseDownFn, mousemoveFn, mouseUpFn } = useEvent();
  27. // 打灯光
  28. const addLight = () => {
  29. const directionalLight = new THREE.DirectionalLight(0xffffff, 1.2);
  30. directionalLight.position.set(-564, 602, -261);
  31. group?.add(directionalLight);
  32. // directionalLight.target = group;
  33. // const spotLight = new THREE.SpotLight();
  34. // spotLight.angle = Math.PI / 16;
  35. // spotLight.penumbra = 0;
  36. // spotLight.castShadow = true;
  37. // spotLight.position.set(0, 463, 687);
  38. // scene.add(spotLight);
  39. // spotLight.shadow.camera.near = 0.5; // default
  40. // spotLight.shadow.camera.far = 1000; // default
  41. // spotLight.shadow.focus = 1;
  42. // spotLight.shadow.bias = -0.000002;
  43. // spotLight.target = group;
  44. // const pointLight6 = new THREE.PointLight(0xffffff, 1, 500);
  45. // pointLight6.position.set(-131, 64, -1);
  46. // pointLight6.shadow.bias = 0.05;
  47. // group?.add(pointLight6);
  48. // gui.add(directionalLight.position, 'x', -1000, 1000);
  49. // gui.add(directionalLight.position, 'y', -1000, 1000);
  50. // gui.add(directionalLight.position, 'z', -1000, 1000);
  51. // gui.add(pointLight6.position, 'x', -500, 500);
  52. // gui.add(pointLight6.position, 'y', -500, 500);
  53. // gui.add(pointLight6.position, 'z', -500, 500);
  54. // gui.add(pointLight6, 'distance', 0, 500);
  55. };
  56. // 重置摄像头
  57. const resetCamera = () => {
  58. model.camera.position.set(0, -1000, 100);
  59. model.camera.far = 1000;
  60. model.orbitControls?.update();
  61. model.camera.updateProjectionMatrix();
  62. };
  63. // 设置模型位置
  64. const setModalPosition = () => {
  65. if (!group) return;
  66. group.position.set(0, 10, -50);
  67. group.rotation.y = Math.PI / 2;
  68. };
  69. const setControls = () => {
  70. model.orbitControls.panSpeed = 0.5;
  71. model.orbitControls.rotateSpeed = 0.5;
  72. model.orbitControls.maxPolarAngle = Math.PI / 2.4;
  73. model.orbitControls.minPolarAngle = Math.PI / 3;
  74. };
  75. // 切换局部通风机类型
  76. export const setModelType = (type) => {
  77. fanType = type;
  78. return new Promise((resolve) => {
  79. // 显示双道风窗
  80. let childGroup;
  81. if (fanType === 'fm' && fcFanObj && fcFanObj.group) {
  82. if (group.getObjectByName('jbfj-fc')) {
  83. group.remove(fcFanObj.group);
  84. }
  85. childGroup = fmFanObj.group;
  86. (group?.getObjectByName('text5')).visible = false;
  87. (group?.getObjectByName('text4')).visible = true;
  88. } else if (fanType === 'fc' && fmFanObj && fmFanObj.group) {
  89. // 显示单道风窗
  90. if (group.getObjectByName('jbfj-fm')) {
  91. group.remove(fmFanObj.group);
  92. }
  93. childGroup = fcFanObj.group;
  94. (group?.getObjectByName('text5')).visible = true;
  95. (group?.getObjectByName('text4')).visible = false;
  96. }
  97. setTimeout(async () => {
  98. group?.add(childGroup);
  99. const oldCameraPosition = { x: 615, y: 275, z: 744 };
  100. await animateCamera(oldCameraPosition, { x: 0, y: 0, z: 0 }, { x: 0.08, y: 21.73, z: 78.45 }, { x: 0.13, y: -0.82, z: 0.236 }, model, 0.8);
  101. resolve(null);
  102. }, 300);
  103. });
  104. };
  105. /* 添加监控数据 */
  106. export const addText = (selectData) => {
  107. if (!group) {
  108. return;
  109. }
  110. const textArr = [
  111. {
  112. text: `煤矿巷道远程局部风机系统`,
  113. font: 'normal 30px Arial',
  114. color: '#009900',
  115. strokeStyle: '#002200',
  116. x: 50,
  117. y: 110,
  118. },
  119. {
  120. text: `进风量(m³/min):`,
  121. font: 'normal 30px Arial',
  122. color: '#009900',
  123. strokeStyle: '#002200',
  124. x: 0,
  125. y: 165,
  126. },
  127. {
  128. text: `${selectData.windQuantity1}`,
  129. font: 'normal 30px Arial',
  130. color: '#009900',
  131. strokeStyle: '#002200',
  132. x: 290,
  133. y: 165,
  134. },
  135. {
  136. text: `供风量(m³/min): `,
  137. font: 'normal 30px Arial',
  138. color: '#009900',
  139. strokeStyle: '#002200',
  140. x: 0,
  141. y: 220,
  142. },
  143. {
  144. text: ` ${selectData.windQuantity2}`,
  145. font: 'normal 30px Arial',
  146. color: '#009900',
  147. strokeStyle: '#002200',
  148. x: 280,
  149. y: 220,
  150. },
  151. {
  152. text: `故障诊断:`,
  153. font: 'normal 30px Arial',
  154. color: '#009900',
  155. strokeStyle: '#002200',
  156. x: 0,
  157. y: 275,
  158. },
  159. {
  160. text: `${selectData.fault}`,
  161. font: 'normal 30px Arial',
  162. color: '#009900',
  163. strokeStyle: '#002200',
  164. x: 280,
  165. y: 275,
  166. },
  167. {
  168. text: `煤炭科学技术研究院有限公司研制`,
  169. font: 'normal 28px Arial',
  170. color: '#009900',
  171. strokeStyle: '#002200',
  172. x: 20,
  173. y: 325,
  174. },
  175. ];
  176. getTextCanvas(526, 346, textArr, '').then((canvas: HTMLCanvasElement) => {
  177. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  178. const textMaterial = new THREE.MeshBasicMaterial({
  179. // 关于材质并未讲解 实操即可熟悉 这里是漫反射类似纸张的材质,对应的就有高光类似金属的材质.
  180. map: textMap, // 设置纹理贴图
  181. transparent: true,
  182. side: THREE.FrontSide, // 这里是双面渲染的意思
  183. });
  184. textMaterial.blending = THREE.CustomBlending;
  185. const monitorPlane = group.getObjectByName('monitorText');
  186. if (monitorPlane) {
  187. monitorPlane.material = textMaterial;
  188. } else {
  189. const planeGeometry = new THREE.PlaneGeometry(526, 346); // 平面3维几何体PlaneGeometry
  190. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  191. planeMesh.name = 'monitorText';
  192. planeMesh.scale.set(0.0135, 0.0135, 0.0135);
  193. planeMesh.rotation.y = -Math.PI / 2;
  194. planeMesh.position.set(-84.79, 0.82, 17.01);
  195. group.add(planeMesh);
  196. }
  197. });
  198. };
  199. // // css3D文字
  200. export const addCssText = () => {
  201. if (!group) return;
  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. export const playSmoke = (selectData) => {
  263. if (selectData['Fan1StartStatus'] == 1) {
  264. // 主风机打开
  265. setSmokeFrequency('top', selectData['Fan1fHz']);
  266. runFly('top', 'open');
  267. } else {
  268. // 备风机关闭
  269. runFly('top', 'close');
  270. }
  271. if (selectData['Fan2StartStatus'] == 1) {
  272. // 备风机打开
  273. setSmokeFrequency('down', selectData['Fan2fHz']);
  274. runFly('down', 'open');
  275. } else {
  276. // 备风机关闭
  277. runFly('down', 'close');
  278. }
  279. // if (frequency) {
  280. // setSmokeFrequency(deviceType, frequency);
  281. // }
  282. // if (controlType === 'startSmoke') {
  283. // runFly(deviceType, state);
  284. // }
  285. };
  286. const initFly = async () => {
  287. const topCurve = [
  288. {
  289. path0: new THREE.Vector3(-89.84, 2.359, 4.91),
  290. path1: new THREE.Vector3(-85.678, 2.359, 3.61),
  291. isSpread: true,
  292. spreadDirection: -1, //
  293. },
  294. {
  295. path0: new THREE.Vector3(-85.678, 2.352, 3.66),
  296. path1: new THREE.Vector3(-85.636, 2.353, -3.829),
  297. isSpread: false,
  298. spreadDirection: 0,
  299. },
  300. {
  301. path0: new THREE.Vector3(-85.636, 2.353, -3.829),
  302. path1: new THREE.Vector3(-85.636, 1.026, -5.881),
  303. isSpread: false,
  304. spreadDirection: 0,
  305. },
  306. {
  307. path0: new THREE.Vector3(-85.636, 1.026, -5.881),
  308. path1: new THREE.Vector3(-85.618, 0.887, -12.862),
  309. isSpread: false,
  310. spreadDirection: 0,
  311. },
  312. {
  313. path0: new THREE.Vector3(-85.618, 0.827, -12.962),
  314. path1: new THREE.Vector3(80.404, 0.827, -12.962),
  315. isSpread: false,
  316. spreadDirection: 0,
  317. },
  318. {
  319. path0: new THREE.Vector3(80.404, 0.827, -12.962),
  320. path1: new THREE.Vector3(93.164, 0.85, -12.962),
  321. isSpread: true,
  322. spreadDirection: 1, // 1是由小变大,-1是由大变小
  323. },
  324. ];
  325. const downCurve = [
  326. {
  327. path0: new THREE.Vector3(-94.84, -0.388, 3.61),
  328. path1: new THREE.Vector3(-85.678, -0.393, 3.61),
  329. isSpread: true,
  330. spreadDirection: -1, //
  331. },
  332. {
  333. path0: new THREE.Vector3(-85.678, -0.393, 3.275),
  334. path1: new THREE.Vector3(-85.636, -0.392, -3.829),
  335. isSpread: false,
  336. spreadDirection: 0,
  337. },
  338. {
  339. path0: new THREE.Vector3(-85.636, -0.392, -3.829),
  340. path1: new THREE.Vector3(-85.636, 0.926, -5.881),
  341. isSpread: false,
  342. spreadDirection: 0,
  343. },
  344. {
  345. path0: new THREE.Vector3(-85.636, 1.026, -5.881),
  346. path1: new THREE.Vector3(-85.618, 0.887, -12.862),
  347. isSpread: false,
  348. spreadDirection: 0,
  349. },
  350. {
  351. path0: new THREE.Vector3(-85.618, 0.887, -12.962),
  352. path1: new THREE.Vector3(80.404, 0.887, -12.962),
  353. isSpread: false,
  354. spreadDirection: 0,
  355. },
  356. {
  357. path0: new THREE.Vector3(80.404, 0.887, -12.962),
  358. path1: new THREE.Vector3(93.164, 0.91, -12.962),
  359. isSpread: true,
  360. spreadDirection: 1, // 1是由小变大,-1是由大变小
  361. },
  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. };
  376. const runFly = (deviceType, state) => {
  377. if (state === 'open') {
  378. if (deviceType === 'top') {
  379. if (downSmoke.frameId) {
  380. downSmoke.stopSmoke();
  381. }
  382. topSmoke.startSmoke();
  383. } else {
  384. if (topSmoke.frameId) {
  385. topSmoke.stopSmoke();
  386. }
  387. downSmoke.startSmoke();
  388. }
  389. } else {
  390. if (downSmoke.frameId) {
  391. downSmoke.stopSmoke();
  392. }
  393. if (topSmoke.frameId) {
  394. topSmoke.stopSmoke();
  395. }
  396. }
  397. };
  398. // 调频 30-50 (life 300 - 800) , 25 = (800 - 300)/ 20
  399. const setSmokeFrequency = (deviceType, frequency) => {
  400. const life = (frequency - 30) * 25;
  401. let duration = 0;
  402. let smoke;
  403. if (deviceType === 'top') {
  404. if (topLife == life) {
  405. return;
  406. }
  407. smoke = topSmoke;
  408. duration = (Math.abs(life - smoke.life) / 500) * 25;
  409. } else {
  410. if (downLife == life) {
  411. return;
  412. }
  413. smoke = downSmoke;
  414. duration = (Math.abs(life - smoke.life) / 500) * 25;
  415. }
  416. if (smoke) {
  417. gsap.to(smoke, {
  418. life: life,
  419. duration: duration,
  420. ease: 'easeInCubic',
  421. overwrite: true,
  422. });
  423. }
  424. };
  425. const clearFly = () => {
  426. if (topSmoke) topSmoke.clearSmoke();
  427. if (downSmoke) downSmoke.clearSmoke();
  428. };
  429. // 初始化事件
  430. const startAnimation = () => {
  431. // 定义鼠标点击事件
  432. model.canvasContainer?.addEventListener('mousedown', mouseEvent.bind(null));
  433. model.canvasContainer?.addEventListener('pointerup', mouseUp);
  434. };
  435. // 鼠标点击、松开事件
  436. const mouseEvent = (event) => {
  437. if (event.button == 0) {
  438. model?.canvasContainer?.addEventListener('mousemove', mousemove);
  439. mouseDownFn(<UseThree>model, <THREE.Object3D>group, event, (intersects) => {
  440. intersects.find((intersect) => {
  441. const mesh = intersect.object;
  442. if (mesh.name === 'player1') {
  443. if (new Date().getTime() - playerStartClickTime1 < 400) {
  444. // 双击,视频放大
  445. if (player1) {
  446. player1.requestFullscreen();
  447. }
  448. }
  449. playerStartClickTime1 = new Date().getTime();
  450. return true;
  451. }
  452. });
  453. });
  454. console.log('摄像头控制信息', model?.orbitControls, model?.camera);
  455. }
  456. };
  457. const mouseUp = () => {
  458. if (!model) return;
  459. mouseUpFn(model);
  460. model.canvasContainer?.removeEventListener('mousemove', mousemove);
  461. };
  462. const mousemove = () => {
  463. mousemoveFn();
  464. };
  465. export const mountedThree = (playerVal1) => {
  466. player1 = playerVal1;
  467. return new Promise((resolve) => {
  468. model = new UseThree('#fanLocal3D', '#fanLocal3DCSS');
  469. model.setEnvMap('test1');
  470. model.renderer.toneMappingExposure = 1.0;
  471. if (model.renderer) {
  472. model.renderer.sortObjects = true;
  473. }
  474. resetCamera();
  475. model.setGLTFModel([modelName]).then(async (gltf) => {
  476. group = <THREE.Object3D>gltf[0];
  477. const Fengtongbu01 = group?.getObjectByName('Cylinder1054') as THREE.Mesh;
  478. if (Fengtongbu01) {
  479. const textMaterial = new THREE.MeshBasicMaterial({
  480. color: '#000',
  481. transparent: true,
  482. opacity: 0.3,
  483. side: THREE.DoubleSide, // 这里是双面渲染的意思
  484. });
  485. Fengtongbu01.material = textMaterial;
  486. Fengtongbu01.renderOrder = 300;
  487. }
  488. model?.scene?.add(group);
  489. setModalPosition();
  490. setControls();
  491. await initFly();
  492. model?.animate();
  493. addLight(model?.scene);
  494. fcFanObj = new fcFan(model);
  495. await fcFanObj.mountedThree();
  496. fmFanObj = new fmFan(model);
  497. await fmFanObj.mountedThree();
  498. const videoPlayer1 = document.getElementById('jb-player1')?.getElementsByClassName('vjs-tech')[0];
  499. if (videoPlayer1) {
  500. const mesh = renderVideo(group, videoPlayer1, 'player1');
  501. if (mesh) {
  502. mesh.scale.set(0.222, 0.19, 0.2);
  503. mesh.position.set(-84.87, 0.298, 24.76);
  504. mesh.rotation.y = -Math.PI / 2;
  505. group.add(mesh);
  506. }
  507. } else {
  508. const textArr = [
  509. {
  510. text: `无信号输入`,
  511. font: 'normal 40px Arial',
  512. color: '#009900',
  513. strokeStyle: '#002200',
  514. x: 170,
  515. y: 40,
  516. },
  517. ];
  518. getTextCanvas(560, 346, textArr, '').then((canvas: HTMLCanvasElement) => {
  519. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  520. const textMaterial = new THREE.MeshBasicMaterial({
  521. map: textMap, // 设置纹理贴图
  522. transparent: true,
  523. side: THREE.DoubleSide, // 这里是双面渲染的意思
  524. });
  525. textMaterial.blending = THREE.CustomBlending;
  526. const monitorPlane = group?.getObjectByName('noPlayer');
  527. if (monitorPlane) {
  528. monitorPlane.material = textMaterial;
  529. } else {
  530. const planeGeometry = new THREE.PlaneGeometry(100, 100); // 平面3维几何体PlaneGeometry
  531. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  532. if (!videoPlayer1) {
  533. planeMesh.name = 'noPlayer';
  534. planeMesh.scale.set(0.07, 0.05, 0.07);
  535. planeMesh.position.set(-84.82, -1.53, 24.94);
  536. planeMesh.rotation.y = -Math.PI / 2;
  537. group?.add(planeMesh.clone());
  538. }
  539. }
  540. });
  541. }
  542. startAnimation();
  543. resolve(model);
  544. });
  545. });
  546. };
  547. export const destroy = () => {
  548. if (model) {
  549. model.isRender = false;
  550. console.log('场景销毁后信息----------->', model.renderer?.info);
  551. clearFly();
  552. topSmoke = undefined;
  553. downSmoke = undefined;
  554. if (fcFanObj) fcFanObj.destroy();
  555. if (fmFanObj) fmFanObj.destroy();
  556. group = undefined;
  557. model.destroy();
  558. model = undefined;
  559. }
  560. };