fanLocal.three.ts 16 KB

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