fanLocal.three.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. import * as THREE from 'three';
  2. import { CSS3DObject } from 'three/examples/jsm/renderers/CSS3DRenderer.js';
  3. import { animateCamera, getTextCanvas, renderVideo } from '/@/utils/threejs/util';
  4. import UseThree from '../../../../hooks/core/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. const modelName = 'jbfj_hd';
  10. // 模型对象、 文字对象
  11. let model, group, fcFanObj, fmFanObj, player1, fanType, topSmoke, downSmoke;
  12. // 打灯光
  13. const addLight = (scene) => {
  14. // const pointLight2 = new THREE.PointLight(0xffeeee, 1.5, 100);
  15. // pointLight2.position.set(-120, 16, -33);
  16. // pointLight2.shadow.bias = 0.05;
  17. // scene.add(pointLight2);
  18. //
  19. // const pointLight3 = new THREE.PointLight(0xffffff, 1, 40);
  20. // pointLight3.position.set(-66, 40, 1);
  21. // pointLight3.shadow.bias = 0.05;
  22. // scene.add(pointLight3);
  23. //
  24. // const pointLight4 = new THREE.PointLight(0xffeeee, 0.6, 230);
  25. // pointLight4.position.set(-18, 30, 12);
  26. // pointLight4.shadow.bias = 0.05;
  27. // scene.add(pointLight4);
  28. //
  29. // const pointLight5 = new THREE.PointLight(0xffffff, 0.8, 90);
  30. // pointLight5.position.set(-57, 7, -30);
  31. // pointLight5.shadow.bias = 0.05;
  32. // scene.add(pointLight5);
  33. //
  34. // const pointLight6 = new THREE.PointLight(0xffffff, 0.8, 270);
  35. // pointLight6.position.set(72, -33, 11.4);
  36. // pointLight6.shadow.bias = 0.05;
  37. // scene.add(pointLight6);
  38. const pointLight7 = new THREE.PointLight(0xffffff, 0.8, 500);
  39. pointLight7.position.set(-20, -43, 12);
  40. pointLight7.shadow.bias = -0.05;
  41. scene.add(pointLight7);
  42. const spotLight = new THREE.SpotLight();
  43. spotLight.angle = Math.PI / 16;
  44. spotLight.penumbra = 0;
  45. spotLight.castShadow = true;
  46. spotLight.position.set(0, 463, 687);
  47. scene.add(spotLight);
  48. spotLight.shadow.camera.near = 0.5; // default
  49. spotLight.shadow.camera.far = 1000; // default
  50. spotLight.shadow.focus = 1;
  51. spotLight.shadow.bias = -0.000002;
  52. // gui.add(pointLight6.position, 'x', -200, 200);
  53. // gui.add(pointLight6.position, 'y', -200, 200);
  54. // gui.add(pointLight6.position, 'z', -200, 200);
  55. // gui.add(pointLight6, 'distance', 0, 500);
  56. };
  57. // 重置摄像头
  58. const resetCamera = () => {
  59. // model.camera.position.set(0, 30, 80);
  60. model.camera.position.set(0, -1000, 100);
  61. // model.camera.fov = 20
  62. model.camera.far = 1000;
  63. model.orbitControls?.update();
  64. model.camera.updateProjectionMatrix();
  65. };
  66. // 设置模型位置
  67. const setModalPosition = () => {
  68. group.position.set(0, 18, -50);
  69. group.rotation.y = Math.PI / 2;
  70. };
  71. const setControls = () => {
  72. model.orbitControls.panSpeed = 0.5;
  73. model.orbitControls.rotateSpeed = 0.5;
  74. model.orbitControls.maxPolarAngle = Math.PI / 2.4;
  75. model.orbitControls.minPolarAngle = Math.PI / 3;
  76. };
  77. // 切换局部通风机类型
  78. export const setModelType = (type) => {
  79. fanType = type;
  80. return new Promise((resolve) => {
  81. // 显示双道风窗
  82. if (fanType === 'fm') {
  83. if (group.getObjectByName('jbfj_fc')) {
  84. group.remove(fcFanObj.group);
  85. }
  86. setTimeout(async () => {
  87. const position = fmFanObj.group;
  88. const oldCameraPosition = { x: 500, y: 100, z: 500 };
  89. await animateCamera(
  90. oldCameraPosition,
  91. oldCameraPosition,
  92. { x: 0, y: 30, z: 80 },
  93. { x: position.x, y: position.y, z: position.z },
  94. model,
  95. 0.8
  96. );
  97. group.add(fmFanObj.group);
  98. resolve(null);
  99. }, 300);
  100. } else if (fanType === 'fc') {
  101. // 显示单道风窗
  102. if (group.getObjectByName('jbfj_fm')) {
  103. group.remove(fmFanObj.group);
  104. }
  105. setTimeout(async () => {
  106. const position = fcFanObj.group;
  107. const oldCameraPosition = { x: 500, y: 100, z: 500 };
  108. await animateCamera(
  109. oldCameraPosition,
  110. oldCameraPosition,
  111. { x: 0, y: 30, z: 80 },
  112. { x: position.x, y: position.y, z: position.z },
  113. model,
  114. 0.8
  115. );
  116. group.add(fcFanObj.group);
  117. resolve(null);
  118. }, 300);
  119. }
  120. });
  121. };
  122. /* 添加监控数据 */
  123. export const addText = (selectData) => {
  124. if (!group) {
  125. return;
  126. }
  127. const textArr = [
  128. {
  129. text: `煤矿巷道远程局部风机系统`,
  130. font: 'normal 30px Arial',
  131. color: '#009900',
  132. strokeStyle: '#002200',
  133. x: 50,
  134. y: 110,
  135. },
  136. {
  137. text: `进风量(m³/min):`,
  138. font: 'normal 30px Arial',
  139. color: '#009900',
  140. strokeStyle: '#002200',
  141. x: 0,
  142. y: 165,
  143. },
  144. {
  145. text: `${selectData.frontRearDP}`,
  146. font: 'normal 30px Arial',
  147. color: '#009900',
  148. strokeStyle: '#002200',
  149. x: 290,
  150. y: 165,
  151. },
  152. {
  153. text: `供风量(m³/min): `,
  154. font: 'normal 30px Arial',
  155. color: '#009900',
  156. strokeStyle: '#002200',
  157. x: 0,
  158. y: 220,
  159. },
  160. {
  161. text: ` ${selectData.sourcePressure}`,
  162. font: 'normal 30px Arial',
  163. color: '#009900',
  164. strokeStyle: '#002200',
  165. x: 280,
  166. y: 220,
  167. },
  168. {
  169. text: `故障诊断:`,
  170. font: 'normal 30px Arial',
  171. color: '#009900',
  172. strokeStyle: '#002200',
  173. x: 0,
  174. y: 275,
  175. },
  176. {
  177. text: `${selectData.fault}`,
  178. font: 'normal 30px Arial',
  179. color: '#009900',
  180. strokeStyle: '#002200',
  181. x: 280,
  182. y: 275,
  183. },
  184. {
  185. text: `煤炭科学技术研究院有限公司研制`,
  186. font: 'normal 28px Arial',
  187. color: '#009900',
  188. strokeStyle: '#002200',
  189. x: 20,
  190. y: 325,
  191. },
  192. ];
  193. getTextCanvas(526, 346, textArr, '').then((canvas: HTMLCanvasElement) => {
  194. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  195. const textMaterial = new THREE.MeshBasicMaterial({
  196. // 关于材质并未讲解 实操即可熟悉 这里是漫反射类似纸张的材质,对应的就有高光类似金属的材质.
  197. map: textMap, // 设置纹理贴图
  198. transparent: true,
  199. side: THREE.FrontSide, // 这里是双面渲染的意思
  200. });
  201. textMaterial.blending = THREE.CustomBlending;
  202. const monitorPlane = group.getObjectByName('monitorText');
  203. if (monitorPlane) {
  204. monitorPlane.material = textMaterial;
  205. } else {
  206. const planeGeometry = new THREE.PlaneGeometry(526, 346); // 平面3维几何体PlaneGeometry
  207. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  208. planeMesh.name = 'monitorText';
  209. planeMesh.scale.set(0.0135, 0.0135, 0.0135);
  210. planeMesh.rotation.y = -Math.PI / 2;
  211. planeMesh.position.set(-84.79, 0.82, 17.01);
  212. group.add(planeMesh);
  213. }
  214. });
  215. };
  216. // // css3D文字
  217. export const addCssText = () => {
  218. if (!group.getObjectByName('text1')) {
  219. const element = document.getElementById('inputBox') as HTMLElement;
  220. const fanLocalCSS3D = new CSS3DObject(element);
  221. fanLocalCSS3D.name = 'text1';
  222. fanLocalCSS3D.scale.set(0.04, 0.04, 0.04);
  223. fanLocalCSS3D.rotation.y = -Math.PI / 2;
  224. fanLocalCSS3D.position.set(-82.96, 5.97, -0.84);
  225. group.add(fanLocalCSS3D);
  226. }
  227. if (!group.getObjectByName('text2')) {
  228. const element = document.getElementById('outBox') as HTMLElement;
  229. const fanLocalCSS3D = new CSS3DObject(element);
  230. fanLocalCSS3D.name = 'text2';
  231. fanLocalCSS3D.scale.set(0.1, 0.1, 0.1);
  232. fanLocalCSS3D.rotation.y = -Math.PI / 2;
  233. fanLocalCSS3D.position.set(74.63, 13.54, 3.84);
  234. group.add(fanLocalCSS3D);
  235. }
  236. if (!group.getObjectByName('text3')) {
  237. const element = document.getElementById('returnBox') as HTMLElement;
  238. const fanLocalCSS3D = new CSS3DObject(element);
  239. fanLocalCSS3D.name = 'text3';
  240. fanLocalCSS3D.scale.set(0.1, 0.1, 0.1);
  241. fanLocalCSS3D.rotation.y = -Math.PI / 2;
  242. fanLocalCSS3D.position.set(74.63, 10.05, -37.23);
  243. group.add(fanLocalCSS3D);
  244. }
  245. if (!group.getObjectByName('text4')) {
  246. const element = document.getElementById('gateBox') as HTMLElement;
  247. const fanLocalCSS3D = new CSS3DObject(element);
  248. fanLocalCSS3D.name = 'text4';
  249. fanLocalCSS3D.scale.set(0.04, 0.04, 0.04);
  250. fanLocalCSS3D.rotation.y = -Math.PI / 2;
  251. fanLocalCSS3D.position.set(-73.13, 8.44, -23.52);
  252. group.add(fanLocalCSS3D);
  253. }
  254. if (!group.getObjectByName('text5')) {
  255. const element = document.getElementById('windownBox') as HTMLElement;
  256. const fanLocalCSS3D = new CSS3DObject(element);
  257. fanLocalCSS3D.name = 'text5';
  258. fanLocalCSS3D.scale.set(0.07, 0.07, 0.07);
  259. fanLocalCSS3D.rotation.y = -Math.PI / 2;
  260. fanLocalCSS3D.position.set(-28.44, 9.78, -40.42);
  261. group.add(fanLocalCSS3D);
  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 = () => {
  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. 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. downSmoke.setPoints();
  373. group?.add(downSmoke.points);
  374. }
  375. // if (!group.getObjectByName('gateFly')) {
  376. // }
  377. };
  378. const runFly = (deviceType, state) => {
  379. if (state === 'open') {
  380. if (deviceType === 'top') {
  381. if (downSmoke.frameId) {
  382. downSmoke.stopSmoke();
  383. }
  384. topSmoke.startSmoke();
  385. } else {
  386. if (topSmoke.frameId) {
  387. topSmoke.stopSmoke();
  388. }
  389. downSmoke.startSmoke();
  390. }
  391. } else {
  392. if (downSmoke.frameId) {
  393. downSmoke.stopSmoke();
  394. }
  395. if (topSmoke.frameId) {
  396. topSmoke.stopSmoke();
  397. }
  398. }
  399. };
  400. // 调频 30-50 (life 300 - 800) , 25 = (800 - 300)/ 20
  401. const setSmokeFrequency = (deviceType, frequency) => {
  402. const life = (frequency - 30) * 25;
  403. const duration = (Math.abs(life - topSmoke.life) / 500) * 25;
  404. let smoke;
  405. if (deviceType === 'top') {
  406. smoke = topSmoke;
  407. } else {
  408. smoke = downSmoke;
  409. }
  410. gsap.to(smoke, {
  411. life: life,
  412. duration: duration,
  413. ease: 'easeInCubic',
  414. overwrite: true,
  415. });
  416. };
  417. const clearFly = () => {
  418. if (topSmoke) topSmoke.clearSmoke();
  419. if (downSmoke) downSmoke.clearSmoke();
  420. };
  421. export const mountedThree = (playerVal1) => {
  422. player1 = playerVal1;
  423. return new Promise((resolve) => {
  424. model = new UseThree('#fanLocal3D', '#fanLocal3DCSS');
  425. model.setEnvMap('test1');
  426. model.renderer.toneMappingExposure = 0.8;
  427. addLight(model.scene);
  428. resetCamera();
  429. model.setModel(modelName).then(async (gltf) => {
  430. group = gltf.scene;
  431. model.scene?.add(group);
  432. if (gltf.animations && gltf.animations.length > 0) {
  433. model.mixers = [];
  434. model.animations = [];
  435. gltf.animations.forEach((animation) => {
  436. const mixer = new THREE.AnimationMixer(group);
  437. model.mixers.push(mixer);
  438. model.animations.push(animation);
  439. });
  440. }
  441. setModalPosition();
  442. setControls();
  443. initFly();
  444. model.animate();
  445. fcFanObj = new fcFan(model);
  446. await fcFanObj.mountedThree();
  447. fmFanObj = new fmFan(model);
  448. await fmFanObj.mountedThree();
  449. const videoPlayer1 = document.getElementById('jb-player1')?.getElementsByClassName('vjs-tech')[0];
  450. if (videoPlayer1) {
  451. const mesh = renderVideo(group, videoPlayer1, 'player1');
  452. mesh.scale.set(0.222, 0.19, 0.2);
  453. mesh.position.set(-84.87, 0.298, 24.76);
  454. mesh.rotation.y = -Math.PI / 2;
  455. group.add(mesh);
  456. }
  457. resolve(model);
  458. });
  459. });
  460. };
  461. export const destroy = () => {
  462. if (model) {
  463. clearFly();
  464. if (fcFanObj) fcFanObj.destroy();
  465. if (fmFanObj) fmFanObj.destroy();
  466. model.deleteModal();
  467. model = null;
  468. group = null;
  469. topSmoke = null;
  470. downSmoke = null;
  471. }
  472. };