mainWind.threejs.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. import * as THREE from 'three';
  2. import { CSS3DObject } from 'three/examples/jsm/renderers/CSS3DRenderer.js';
  3. import Smoke from '/@/views/vent/comment/threejs/Smoke';
  4. import { renderVideo } from '/@/utils/threejs/util';
  5. import gsap from 'gsap';
  6. class mainWindRect {
  7. model;
  8. modelName = 'main';
  9. group: THREE.Group | null = null; // 主通风机场景
  10. motorGroup1: THREE.Group | null = null; //电机
  11. motorGroup2: THREE.Group | null = null; //电机
  12. airJin1: THREE.Mesh | null = null; //风向箭头
  13. airJin2: THREE.Mesh | null = null; //风向箭头
  14. airChu1: THREE.Mesh | null = null; //风向箭头
  15. airChu2: THREE.Mesh | null = null; //风向箭头
  16. gearFront = {
  17. gear1: null, //扇叶
  18. gear2: null, //扇叶
  19. gear1Direction: -1,
  20. gear2Direction: 1,
  21. gearFrameId: undefined,
  22. gearRotateFactor: 0.5,
  23. endGearRotateFactor: 3,
  24. };
  25. gearBack = {
  26. gear1: null, //扇叶
  27. gear2: null, //扇叶
  28. gear1Direction: -1, // 扇叶转动方向
  29. gear2Direction: 1, // 扇叶转动方向
  30. gearFrameId: undefined,
  31. gearRotateFactor: 0.5, // 扇叶转动因素
  32. endGearRotateFactor: 3, // 扇叶最终转动速度因素
  33. };
  34. oldMaterial: THREE.Material = new THREE.MeshStandardMaterial();
  35. // smoke;
  36. frontSmoke: Smoke | null = null; // 前面风流对象
  37. backSmoke: Smoke | null = null; // 后面风流对象
  38. player1; // 视频播放器
  39. playerStartClickTime1 = new Date().getTime();
  40. frontWindowGroup;
  41. backWindowGroup;
  42. windowAngle = 0;
  43. fbmAnimationClip: THREE.AnimationClip | null = null;
  44. fbmMixers: THREE.AnimationMixer | null = null;
  45. fbmOpenAction: THREE.AnimationAction | null = null;
  46. constructor(model, playerVal1) {
  47. this.model = model;
  48. this.player1 = playerVal1;
  49. }
  50. // 添加 cssObject
  51. addCssText() {
  52. if (!this.group) {
  53. return;
  54. }
  55. const ztfjGroup = this.group.getObjectByName('ztfj');
  56. if (!this.group.getObjectByName('monitorText1')) {
  57. const worldPosition = new THREE.Vector3();
  58. ztfjGroup?.getObjectByName('Cylinder1042')?.getWorldPosition(worldPosition);
  59. const element = document.getElementById('inputBox') as HTMLElement;
  60. if (element) {
  61. const mainCSS3D = new CSS3DObject(element);
  62. mainCSS3D.name = 'monitorText1';
  63. mainCSS3D.scale.set(0.09, 0.09, 0.09);
  64. // mainCSS3D.position.set(23.78, 18.18, -6.85);
  65. mainCSS3D.position.set(worldPosition.x + 12, worldPosition.y - 10, worldPosition.z - 20);
  66. mainCSS3D.lookAt(worldPosition.x + 12, worldPosition.y - 0, worldPosition.z + 2);
  67. this.group.add(mainCSS3D);
  68. }
  69. }
  70. if (!this.group.getObjectByName('monitorText2')) {
  71. const worldPosition = new THREE.Vector3();
  72. ztfjGroup?.getObjectByName('Cylinder396')?.getWorldPosition(worldPosition);
  73. const element = document.getElementById('inputBox1') as HTMLElement;
  74. if (element) {
  75. const mainCSS3D = new CSS3DObject(element);
  76. mainCSS3D.name = 'monitorText2';
  77. mainCSS3D.scale.set(0.09, 0.09, 0.09);
  78. // mainCSS3D.position.set(23.78, 18.18, 16.82);
  79. mainCSS3D.position.set(worldPosition.x + 12, worldPosition.y - 10, worldPosition.z - 20);
  80. mainCSS3D.lookAt(worldPosition.x + 12, worldPosition.y - 0, worldPosition.z + 2);
  81. // mainCSS3D.lookAt(23.78, 20, 20.82);
  82. this.group.add(mainCSS3D);
  83. }
  84. }
  85. if (!this.group.getObjectByName('monitorText4')) {
  86. const worldPosition = new THREE.Vector3();
  87. const fbmGroup = this.group?.getObjectByName('fbm') as THREE.Group;
  88. if (fbmGroup) {
  89. fbmGroup?.getObjectByName('Box022')?.getWorldPosition(worldPosition);
  90. const element = document.getElementById('fbm') as HTMLElement;
  91. if (element) {
  92. const mainCSS3D = new CSS3DObject(element);
  93. mainCSS3D.name = 'monitorText4';
  94. mainCSS3D.scale.set(0.07, 0.07, 0.07);
  95. mainCSS3D.position.set(worldPosition.x + 20, worldPosition.y - 8, worldPosition.z - 20);
  96. mainCSS3D.lookAt(worldPosition.x + 20, worldPosition.y - 0, worldPosition.z + 2);
  97. this.group.add(mainCSS3D);
  98. }
  99. }
  100. }
  101. }
  102. clearCssText() {
  103. if (this.group) {
  104. const mainCSS3D1 = this.group.getObjectByName('monitorText1');
  105. const mainCSS3D2 = this.group.getObjectByName('monitorText2');
  106. if (mainCSS3D1) this.group.remove(mainCSS3D1);
  107. if (mainCSS3D2) this.group.remove(mainCSS3D2);
  108. }
  109. }
  110. addEcharts() {
  111. const echartsBox = document.getElementById('fan-echarts');
  112. if (echartsBox) {
  113. const canvasObj = echartsBox.getElementsByTagName('canvas')[0];
  114. // 将canvas 纹理转换为材质
  115. const echartsMap = new THREE.CanvasTexture(canvasObj); // 关键一步
  116. const echartsMaterial = new THREE.MeshBasicMaterial({
  117. map: echartsMap, // 设置纹理贴图
  118. transparent: true,
  119. side: THREE.FrontSide, // 这里是双面渲染的意思
  120. });
  121. echartsMaterial.blending = THREE.CustomBlending;
  122. const monitorPlane = this.group?.getObjectByName('monitorEcharts');
  123. if (monitorPlane) {
  124. monitorPlane.material = echartsMaterial;
  125. } else {
  126. const planeGeometry = new THREE.PlaneGeometry(17.6, 9.9); // 平面3维几何体PlaneGeometry
  127. const planeMesh = new THREE.Mesh(planeGeometry, echartsMaterial);
  128. planeMesh.name = 'monitorEcharts';
  129. planeMesh.scale.set(1, 1, 1);
  130. planeMesh.position.set(-47.38, 13.227, -21.79);
  131. this.group?.add(planeMesh);
  132. }
  133. }
  134. }
  135. initAnimation() {}
  136. startAnimation() {}
  137. /* 更新动画 */
  138. render() {
  139. if (!this.model) {
  140. return;
  141. }
  142. if (this.fbmMixers) this.fbmMixers?.update(1 / 25);
  143. }
  144. /* 点击风窗,风窗全屏 */
  145. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  146. // 判断是否点击到视频
  147. intersects.find((intersect) => {
  148. const mesh = intersect.object;
  149. if (mesh.name === 'player1') {
  150. if (new Date().getTime() - this.playerStartClickTime1 < 400) {
  151. // 双击,视频放大
  152. if (this.player1) {
  153. this.player1.requestFullscreen();
  154. }
  155. }
  156. this.playerStartClickTime1 = new Date().getTime();
  157. return true;
  158. }
  159. return false;
  160. });
  161. }
  162. mouseUpModel() {}
  163. async setDeviceFrequency(deviceType, state, frequencyVal?) {
  164. // 调节频率
  165. if (frequencyVal) {
  166. this.resetSmokeParam(deviceType, frequencyVal, 0);
  167. }
  168. this.openOrCloseValve(deviceType, state, 0);
  169. this.startGearAnimation(deviceType, state, '', 0);
  170. if (deviceType === 'front') {
  171. this.frontSmoke?.startSmoke();
  172. } else {
  173. this.backSmoke?.startSmoke();
  174. }
  175. setTimeout(() => {
  176. this.lookMotor(deviceType, state, 10);
  177. }, 2000);
  178. }
  179. async openDevice(deviceType, smokeDirection, frequencyVal, duration?) {
  180. if (smokeDirection) {
  181. this.setSmokeDirection(deviceType, smokeDirection);
  182. }
  183. let smoke;
  184. if (deviceType === 'front') {
  185. smoke = this.frontSmoke;
  186. } else {
  187. smoke = this.backSmoke;
  188. }
  189. if (!smoke.frameId) {
  190. await this.lookMotor(deviceType, 'open', duration);
  191. await this.openOrCloseValve(deviceType, 'open', duration);
  192. this.startGearAnimation(deviceType, 'open', smokeDirection, frequencyVal, duration);
  193. smoke.startSmoke(duration);
  194. }
  195. }
  196. async closeDevice(deviceType, flag = true) {
  197. let smoke;
  198. if (deviceType === 'front') {
  199. smoke = this.frontSmoke;
  200. } else if (deviceType === 'back') {
  201. smoke = this.backSmoke;
  202. }
  203. if (smoke && smoke.frameId) {
  204. if (flag) {
  205. smoke.stopSmoke();
  206. await this.openOrCloseValve(deviceType, 'close');
  207. this.startGearAnimation(deviceType, 'close', '', null);
  208. await this.lookMotor(deviceType, 'close');
  209. } else {
  210. smoke.stopSmoke(0);
  211. await this.openOrCloseValve(deviceType, 'close', 0);
  212. this.startGearAnimation(deviceType, 'close', '', null, 0);
  213. await this.lookMotor(deviceType, 'close', 0);
  214. }
  215. }
  216. }
  217. async setSmokeDirection(deviceType, smokeDirection) {
  218. const windowPositivePath = [
  219. {
  220. path0: new THREE.Vector3(4.441, 20.267, 3.614),
  221. path1: new THREE.Vector3(5.041, 6.806, 3.614),
  222. isSpread: true,
  223. spreadDirection: -1, //
  224. },
  225. {
  226. path0: new THREE.Vector3(7.441, 0.806, 3.614),
  227. path1: new THREE.Vector3(41.583, 1.485, 3.614),
  228. isSpread: false,
  229. spreadDirection: 0, //
  230. },
  231. {
  232. path0: new THREE.Vector3(41.583, 1.485, 3.614),
  233. path1: new THREE.Vector3(42.741, 5.364, 3.614),
  234. isSpread: false,
  235. spreadDirection: 0,
  236. },
  237. {
  238. path0: new THREE.Vector3(42.741, 5.364, 3.614),
  239. path1: new THREE.Vector3(44.741, 17.267, 3.614),
  240. isSpread: true,
  241. spreadDirection: 1, // 1是由小变大(出),-1是由大变小(进)
  242. },
  243. ];
  244. const windowInversePath = [
  245. {
  246. path0: new THREE.Vector3(44.741, 17.267, 3.614),
  247. path1: new THREE.Vector3(42.741, 5.364, 3.614),
  248. isSpread: true,
  249. spreadDirection: -1, //
  250. },
  251. {
  252. path0: new THREE.Vector3(42.741, 5.364, 3.614),
  253. path1: new THREE.Vector3(41.583, 1.485, 3.614),
  254. isSpread: false,
  255. spreadDirection: 0, //
  256. },
  257. {
  258. path0: new THREE.Vector3(41.583, 1.485, 3.614),
  259. path1: new THREE.Vector3(7.441, 0.806, 3.614),
  260. isSpread: false,
  261. spreadDirection: 0, // 1是由小变大,-1是由大变小
  262. },
  263. {
  264. path0: new THREE.Vector3(4.441, 17.267, 3.614),
  265. path1: new THREE.Vector3(5.041, 6.806, 3.614),
  266. isSpread: true,
  267. spreadDirection: 1, //
  268. },
  269. ];
  270. const tubPositivePath = [
  271. {
  272. path0: new THREE.Vector3(7.441, 0.806, 3.614),
  273. path1: new THREE.Vector3(44.583, 1.485, 3.614),
  274. isSpread: false,
  275. spreadDirection: 0, //
  276. },
  277. {
  278. path0: new THREE.Vector3(44.583, 1.485, 3.614),
  279. path1: new THREE.Vector3(45.741, 5.364, 3.614),
  280. isSpread: false,
  281. spreadDirection: 0,
  282. },
  283. {
  284. path0: new THREE.Vector3(45.741, 5.364, 3.614),
  285. path1: new THREE.Vector3(47.741, 17.267, 3.614),
  286. isSpread: true,
  287. spreadDirection: 1, // 1是由小变大(出),-1是由大变小(进)
  288. },
  289. ];
  290. const tubInversePath = [
  291. {
  292. path0: new THREE.Vector3(47.741, 17.267, 3.614),
  293. path1: new THREE.Vector3(45.741, 5.364, 3.614),
  294. isSpread: true,
  295. spreadDirection: -1, //
  296. },
  297. {
  298. path0: new THREE.Vector3(45.741, 5.364, 3.614),
  299. path1: new THREE.Vector3(44.583, 1.485, 3.614),
  300. isSpread: false,
  301. spreadDirection: 0, //
  302. },
  303. {
  304. path0: new THREE.Vector3(44.583, 1.485, 3.614),
  305. path1: new THREE.Vector3(7.441, 0.806, 3.614),
  306. isSpread: false,
  307. spreadDirection: 0, // 1是由小变大,-1是由大变小
  308. },
  309. ];
  310. let smoke;
  311. if (deviceType === 'front') {
  312. smoke = this.frontSmoke;
  313. } else if (deviceType === 'back') {
  314. smoke = this.backSmoke;
  315. }
  316. switch (smokeDirection) {
  317. case 'tubPositivePath': // 风筒正
  318. smoke.setPath(tubPositivePath);
  319. break;
  320. case 'tubInversePath': // 风筒反
  321. smoke.setPath(tubInversePath);
  322. break;
  323. case 'windowPositivePath': // 风窗正
  324. smoke.setPath(windowPositivePath);
  325. break;
  326. case 'windowInversePath': // 风窗反
  327. smoke.setPath(windowInversePath);
  328. break;
  329. }
  330. }
  331. /* 播放气流动画 */
  332. /**
  333. *
  334. * @param controlType // 设备控制类型
  335. * @param deviceType //前后风机
  336. * @param frequencyVal // 风机运行频率
  337. * @param state // 打开、关闭状态
  338. */
  339. async playSmoke(controlType, deviceType, frequencyVal, state, smokeDirection) {
  340. if (frequencyVal) {
  341. this.resetSmokeParam(deviceType, frequencyVal);
  342. }
  343. if (controlType === 'startSmoke') {
  344. if (state === 'stop') {
  345. await this.closeDevice(deviceType);
  346. } else {
  347. // 开启时需要设置方向
  348. await this.openDevice(deviceType, smokeDirection, frequencyVal);
  349. }
  350. } else if (controlType === 'changeDirection') {
  351. // 改变扇叶转动方向、反风
  352. this.startGearAnimation(deviceType, 'changeDirection', smokeDirection, frequencyVal);
  353. let smoke;
  354. if (deviceType === 'front') {
  355. smoke = this.frontSmoke;
  356. } else {
  357. smoke = this.backSmoke;
  358. }
  359. if (smoke && smoke.frameId) {
  360. await smoke.stopSmoke();
  361. await this.setSmokeDirection(deviceType, smokeDirection);
  362. smoke.startSmoke();
  363. }
  364. } else if (controlType === 'frequency') {
  365. this.startGearAnimation(deviceType, 'frequency', smokeDirection, frequencyVal);
  366. } else if (controlType === 'initiatePlay') {
  367. this.openDevice(deviceType, smokeDirection, frequencyVal, 0);
  368. } else if (controlType === 'changeSmoke') {
  369. //
  370. }
  371. }
  372. stopSmoke() {
  373. this.closeDevice('front', false);
  374. this.closeDevice('back', false);
  375. }
  376. /* 打开或关闭蝶阀 */
  377. openOrCloseValve(deviceType, flag, duration = 3) {
  378. const ztfjGroup = this.group?.getObjectByName('ztfj');
  379. return new Promise((resolve) => {
  380. let diefa;
  381. if (deviceType == 'front') {
  382. diefa = ztfjGroup?.getObjectByName('butterfly_valve001') as THREE.Mesh;
  383. } else {
  384. diefa = ztfjGroup?.getObjectByName('butterfly_valve002') as THREE.Mesh;
  385. }
  386. let rotationY;
  387. if (flag == 'open') {
  388. rotationY = 0;
  389. } else {
  390. rotationY = Math.PI / 2;
  391. }
  392. if (diefa) {
  393. gsap.to(diefa.rotation, {
  394. y: rotationY,
  395. duration: duration,
  396. ease: 'none',
  397. onComplete: function () {
  398. resolve(null);
  399. },
  400. });
  401. }
  402. });
  403. }
  404. /* 风流调频, 范围1-50 */
  405. // opacityFactor (0.4 300)
  406. // life 最小 300, 最大 50
  407. // speedFactor 最大0, 最小100
  408. resetSmokeParam(deviceType, frequency, duration = 5) {
  409. if (frequency < 1) frequency = 1;
  410. if (frequency > 50) frequency = 50;
  411. let smoke;
  412. if (deviceType === 'front') {
  413. smoke = this.frontSmoke;
  414. } else {
  415. smoke = this.backSmoke;
  416. }
  417. const opacityFactor = (frequency / 50) * 0.8;
  418. duration = (Number(Math.abs(smoke.opacityFactor - opacityFactor).toFixed(1)) / 0.8) * 5;
  419. const life = (-250 / 50) * frequency + 300;
  420. gsap.to(smoke, {
  421. opacityFactor: opacityFactor,
  422. life: life,
  423. duration: duration,
  424. ease: 'easeInCirc',
  425. overwrite: true,
  426. });
  427. }
  428. /* 显示电机 */
  429. lookMotor(deviceType, flag, duration = 5) {
  430. return new Promise((resolve) => {
  431. const ztfjGroup = this.group?.getObjectByName('ztfj');
  432. let mesh, mesh1, mesh2, motorGroup;
  433. mesh1 = ztfjGroup?.getObjectByName('TWO00'); //前
  434. mesh2 = ztfjGroup?.getObjectByName('ONE00'); //后
  435. if (deviceType == 'front') {
  436. mesh = mesh1;
  437. motorGroup = this.motorGroup2;
  438. } else {
  439. mesh = mesh2;
  440. motorGroup = this.motorGroup1;
  441. }
  442. if (mesh && motorGroup) {
  443. if (flag == 'open') {
  444. mesh.material.depthWrite = false;
  445. mesh.material.depthTest = false;
  446. motorGroup.visible = true;
  447. gsap.to(mesh.material, {
  448. opacity: 0.1,
  449. duration: duration,
  450. overwrite: true,
  451. onComplete: function () {
  452. // mesh.material.color = '#000';
  453. resolve(null);
  454. },
  455. });
  456. } else {
  457. const opacity = mesh.material.opacity;
  458. Object.assign(mesh.material, this.oldMaterial, { opacity: opacity });
  459. mesh.material.depthWrite = true;
  460. mesh.material.depthTest = true;
  461. gsap.to(mesh.material, {
  462. opacity: 1,
  463. duration: 1,
  464. overwrite: true,
  465. onComplete: function () {
  466. resolve(null);
  467. },
  468. });
  469. }
  470. }
  471. });
  472. }
  473. /* 齿轮转动动画 1 - 50 最大3 */
  474. startGearAnimation(deviceType, flag, smokeDirection, frequencyVal, duration = 8) {
  475. let gearObj, gearDirection;
  476. if (deviceType === 'front') {
  477. gearObj = this.gearFront;
  478. } else {
  479. gearObj = this.gearBack;
  480. }
  481. if (smokeDirection === 'tubPositivePath') {
  482. gearDirection = 1;
  483. } else if (smokeDirection === 'tubInversePath') {
  484. gearDirection = -1;
  485. }
  486. if (frequencyVal) {
  487. const endGearRotateFactor = (3 / 50) * frequencyVal;
  488. duration = (8 / 3) * Math.abs(gearObj.endGearRotateFactor - endGearRotateFactor);
  489. gearObj.endGearRotateFactor = endGearRotateFactor;
  490. }
  491. const gearAnimation = () => {
  492. gsap.to(gearObj, {
  493. gearRotateFactor: gearObj.endGearRotateFactor,
  494. duration: duration,
  495. ease: 'easeInCubic',
  496. repeat: 0,
  497. overwrite: true,
  498. });
  499. const clock = new THREE.Clock(); // 时钟
  500. const h = () => {
  501. if (gearObj.gear1 && gearObj.gear2) {
  502. gearObj.gearFrameId = requestAnimationFrame(h);
  503. const dt = clock.getDelta();
  504. gearObj.gear1.rotation.x += dt * gearObj.gearRotateFactor * gearObj.gear1Direction;
  505. gearObj.gear2.rotation.x += dt * gearObj.gearRotateFactor * gearObj.gear2Direction;
  506. }
  507. };
  508. h();
  509. };
  510. if (flag === 'changeDirection') {
  511. if (gearDirection == -1 * gearObj.gear1Direction) {
  512. // 齿轮正在转,需要停止后再反方向转
  513. gsap.to(gearObj, {
  514. gearRotateFactor: 0,
  515. duration: duration,
  516. ease: 'easeInCubic',
  517. repeat: 0,
  518. onComplete: function () {
  519. window.cancelAnimationFrame(gearObj.gearFrameId);
  520. gearObj.gearFrameId = undefined;
  521. gearObj.gear1Direction = -1 * gearObj.gear1Direction;
  522. gearObj.gear2Direction = -1 * gearObj.gear2Direction;
  523. gearAnimation();
  524. },
  525. });
  526. }
  527. } else if (flag === 'open') {
  528. gearObj.gear1Direction = gearDirection;
  529. gearObj.gear2Direction = -1 * gearDirection;
  530. gearAnimation();
  531. } else if (flag === 'close') {
  532. gsap.to(gearObj, {
  533. gearRotateFactor: 0,
  534. duration: duration,
  535. ease: 'easeInCubic',
  536. repeat: 0,
  537. overwrite: true,
  538. onComplete: function () {
  539. window.cancelAnimationFrame(gearObj.gearFrameId);
  540. gearObj.gearFrameId = undefined;
  541. },
  542. });
  543. } else if (flag === 'frequency') {
  544. gsap.to(gearObj, {
  545. gearRotateFactor: gearObj.endGearRotateFactor,
  546. duration: duration,
  547. ease: 'easeInCubic',
  548. repeat: 0,
  549. overwrite: true,
  550. });
  551. }
  552. }
  553. /* 初始化口上面的气体 */
  554. initSmokeMass() {
  555. if (!this.frontSmoke) {
  556. this.frontSmoke = new Smoke('/model/img/texture-smoke.png', '#ffffff', 0, 0.4, 1.8, 100);
  557. }
  558. if (!this.backSmoke) {
  559. this.backSmoke = new Smoke('/model/img/texture-smoke.png', '#ffffff', 0, 0.4, 1.8, 100);
  560. }
  561. }
  562. /* 设置气流位置 */
  563. async setSmokePosition() {
  564. if (this.frontSmoke) {
  565. await this.frontSmoke.setPoints();
  566. this.frontSmoke.points.name = 'frontSmoke';
  567. this.group?.add(this.frontSmoke.points);
  568. // this.frontSmoke.points.position.set(-2.51, 2.51, 8.25);
  569. this.frontSmoke.points.position.set(-2.51, 2.51, 13.25);
  570. }
  571. if (this.backSmoke) {
  572. await this.backSmoke.setPoints();
  573. this.backSmoke.points.name = 'backSmoke';
  574. this.group?.add(this.backSmoke.points);
  575. // this.backSmoke.points.position.set(-2.2, 3.13, -7.8);
  576. this.backSmoke.points.position.set(-2.2, 3.13, -2.8);
  577. }
  578. }
  579. /** 初始化电机 */
  580. async initMotor() {
  581. // 前电机
  582. const motorGltf1 = await this.model.setGLTFModel('dj1');
  583. this.motorGroup1 = motorGltf1[0] as THREE.Group;
  584. this.motorGroup1?.position.set(0, 0, 5);
  585. this.motorGroup1.visible = false;
  586. this.motorGroup1.traverse((item) => {
  587. if (item instanceof THREE.Object3D) {
  588. item.renderOrder = -1;
  589. if (item.name === 'fan_blade003') {
  590. // @ts-ignore
  591. this.gearBack.gear1 = item as THREE.Group;
  592. } else if (item.name === 'fan_blade005') {
  593. // @ts-ignore
  594. this.gearBack.gear2 = item as THREE.Group;
  595. }
  596. }
  597. });
  598. this.motorGroup1.renderOrder = -1;
  599. this.group?.add(this.motorGroup1);
  600. // 后电机
  601. const motorGltf2 = await this.model.setGLTFModel('dj2');
  602. this.motorGroup2 = motorGltf2[0] as THREE.Group;
  603. this.motorGroup2?.position.set(0, 0, 5);
  604. this.motorGroup2.visible = false;
  605. this.motorGroup2.traverse((item) => {
  606. if (item instanceof THREE.Object3D) {
  607. item.renderOrder = -1;
  608. if (item.name === 'fan_blade007') {
  609. // @ts-ignore
  610. this.gearFront.gear1 = item as THREE.Group;
  611. } else if (item.name === 'fan_blade006') {
  612. // @ts-ignore
  613. this.gearFront.gear2 = item as THREE.Group;
  614. }
  615. }
  616. });
  617. this.motorGroup2.renderOrder = -1;
  618. this.group?.add(this.motorGroup2);
  619. }
  620. openOrCloseWindow(deviceType, flag) {
  621. const _this = this;
  622. let endAngle = 0,
  623. windowGroup;
  624. if (deviceType === 'front') {
  625. windowGroup = this.frontWindowGroup;
  626. }
  627. if (deviceType === 'back') {
  628. windowGroup = this.backWindowGroup;
  629. }
  630. if (flag == 'openWindow') {
  631. // 打开风窗
  632. endAngle = 1;
  633. } else {
  634. // 关闭风窗
  635. endAngle = 0;
  636. }
  637. if (windowGroup)
  638. gsap.to(this, {
  639. windowAngle: endAngle,
  640. duration: Math.abs(endAngle - this.windowAngle) * 10,
  641. ease: 'none',
  642. onUpdate: function () {
  643. windowGroup.children.forEach((mesh) => {
  644. mesh.rotation.z = _this.windowAngle;
  645. });
  646. },
  647. });
  648. }
  649. /** 初始化风窗 */
  650. initWindow() {
  651. if (!this.group) return;
  652. this.frontWindowGroup = new THREE.Group();
  653. this.frontWindowGroup.name = 'frontWindowGroup';
  654. this.backWindowGroup = new THREE.Group();
  655. this.backWindowGroup.name = 'backWindowGroup';
  656. if (this.group && this.group?.children.length > 0) {
  657. for (let i = this.group?.children.length - 1; i >= 0; i--) {
  658. const obj = this.group?.children[i];
  659. if (obj.type === 'Mesh' && obj.name && obj.name.startsWith('TC')) {
  660. const mesh = obj.clone();
  661. if (obj.name.startsWith('TC1')) {
  662. this.backWindowGroup.add(mesh);
  663. } else if (obj.name.startsWith('TC2')) {
  664. this.frontWindowGroup.add(mesh);
  665. }
  666. obj.removeFromParent();
  667. this.group?.remove(obj);
  668. }
  669. }
  670. }
  671. this.group?.add(this.backWindowGroup);
  672. this.group?.add(this.frontWindowGroup);
  673. }
  674. initFbmAnimation() {
  675. const fbmGroup = this.group?.getObjectByName('fbm') as THREE.Group;
  676. if (fbmGroup) {
  677. this.fbmAnimationClip = fbmGroup.animations[0];
  678. this.fbmMixers = new THREE.AnimationMixer(fbmGroup);
  679. this.fbmOpenAction = this.fbmMixers.clipAction(this.fbmAnimationClip);
  680. this.fbmOpenAction.loop = THREE.LoopOnce;
  681. this.fbmOpenAction.clampWhenFinished = true;
  682. this.fbmMixers.timeScale = 0.15;
  683. }
  684. }
  685. playAnimation(flag, duration?) {
  686. if (this.fbmOpenAction && this.fbmMixers && this.fbmAnimationClip) {
  687. if (duration == 0) {
  688. if (flag == 'open') {
  689. this.fbmOpenAction.reset();
  690. this.fbmOpenAction.time = this.fbmAnimationClip.duration;
  691. this.fbmOpenAction.timeScale = 1;
  692. } else {
  693. this.fbmOpenAction.reset();
  694. this.fbmOpenAction.time = 0;
  695. this.fbmOpenAction.timeScale = -1;
  696. }
  697. this.fbmOpenAction.play();
  698. } else {
  699. if (flag == 'open') {
  700. this.fbmOpenAction.reset();
  701. this.fbmOpenAction.time = 0;
  702. this.fbmOpenAction.timeScale = 1;
  703. this.fbmOpenAction.play();
  704. } else {
  705. this.fbmOpenAction.reset();
  706. this.fbmOpenAction.time = this.fbmAnimationClip.duration;
  707. this.fbmOpenAction.timeScale = -1;
  708. this.fbmOpenAction.play();
  709. }
  710. }
  711. }
  712. }
  713. mountedThree() {
  714. this.group = new THREE.Group();
  715. return new Promise(async (resolve) => {
  716. this.model.setGLTFModel(['bg1', 'fbm', 'ztfj', 'ztfj-fc'], this.group).then(async () => {
  717. // this.group = gltf[0];
  718. this.group?.position.set(-0.44, 19.88, 22.37);
  719. this.initSmokeMass();
  720. await this.setSmokePosition();
  721. const ztfjGroup = this.group?.getObjectByName('ztfj') as THREE.Group;
  722. const fbmGroup = this.group?.getObjectByName('fbm') as THREE.Group;
  723. const fcGroup = this.group?.getObjectByName('ztfj-fc') as THREE.Group;
  724. if (ztfjGroup) {
  725. ztfjGroup.position.z = ztfjGroup.position.z + 5;
  726. const airJinGroup = ztfjGroup.getObjectByName('JianTou1_Jin') as THREE.Object3D;
  727. const airChuGroup = ztfjGroup.getObjectByName('JianTou2_Chu') as THREE.Object3D;
  728. this.airJin1 = airJinGroup.getObjectByName('Jin_1') as THREE.Mesh;
  729. this.airJin2 = airJinGroup.getObjectByName('Jin_2') as THREE.Mesh;
  730. this.airChu1 = airChuGroup.getObjectByName('Chu_1') as THREE.Mesh;
  731. this.airChu2 = airChuGroup.getObjectByName('Chu_2') as THREE.Mesh;
  732. }
  733. if (fbmGroup) fbmGroup.position.z = fbmGroup.position.z + 5;
  734. if (fcGroup) fcGroup.position.z = fcGroup.position.z + 5;
  735. const mesh = ztfjGroup?.getObjectByName('jizu06') as THREE.Mesh; //前
  736. if (mesh && mesh.material) this.oldMaterial = mesh.material as THREE.MeshStandardMaterial;
  737. await this.initMotor();
  738. resolve(null);
  739. this.initWindow();
  740. this.initFbmAnimation();
  741. });
  742. });
  743. }
  744. destroy() {
  745. this.frontSmoke.clearSmoke();
  746. this.backSmoke.clearSmoke();
  747. const fbmGroup = this.group?.getObjectByName('fbm') as THREE.Group;
  748. if (this.fbmMixers && this.fbmAnimationClip && this.fbmOpenAction && fbmGroup) {
  749. this.fbmMixers.uncacheClip(this.fbmAnimationClip);
  750. this.fbmMixers.uncacheAction(this.fbmOpenAction.getClip(), fbmGroup);
  751. this.fbmMixers.uncacheRoot(fbmGroup);
  752. this.fbmAnimationClip.tracks = [];
  753. this.fbmAnimationClip = undefined;
  754. this.fbmOpenAction = undefined;
  755. }
  756. this.model.clearGroup(this.motorGroup1);
  757. this.model.clearGroup(this.motorGroup2);
  758. this.model.clearGroup(this.group);
  759. this.motorGroup1 = undefined;
  760. this.motorGroup2 = undefined;
  761. this.gearFront.gear1 = undefined;
  762. this.gearFront.gear2 = undefined;
  763. this.gearBack.gear1 = undefined;
  764. this.gearBack.gear2 = undefined;
  765. this.frontSmoke = undefined;
  766. this.backSmoke = undefined;
  767. this.model = undefined;
  768. this.group = undefined;
  769. }
  770. }
  771. export default mainWindRect;