mainWind.threejs.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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. gearFront = {
  13. gear1: null, //扇叶
  14. gear2: null, //扇叶
  15. gear1Direction: -1,
  16. gear2Direction: 1,
  17. gearFrameId: undefined,
  18. gearRotateFactor: 0.5,
  19. endGearRotateFactor: 3,
  20. };
  21. gearBack = {
  22. gear1: null, //扇叶
  23. gear2: null, //扇叶
  24. gear1Direction: -1, // 扇叶转动方向
  25. gear2Direction: 1, // 扇叶转动方向
  26. gearFrameId: undefined,
  27. gearRotateFactor: 0.5, // 扇叶转动因素
  28. endGearRotateFactor: 3, // 扇叶最终转动速度因素
  29. };
  30. oldMaterial: THREE.Material = new THREE.MeshStandardMaterial();
  31. // smoke;
  32. frontSmoke: Smoke | null = null; // 前面风流对象
  33. backSmoke: Smoke | null = null; // 后面风流对象
  34. player1; // 视频播放器
  35. playerStartClickTime1 = new Date().getTime();
  36. frontWindowGroup;
  37. backWindowGroup;
  38. windowAngle = 0;
  39. fbmAnimationClip: THREE.AnimationClip | null = null;
  40. fbmMixers: THREE.AnimationMixer | null = null;
  41. fbmOpenAction: THREE.AnimationAction | null = null;
  42. constructor(model, playerVal1) {
  43. this.model = model;
  44. this.player1 = playerVal1;
  45. }
  46. // 添加 cssObject
  47. addCssText() {
  48. if (!this.group) {
  49. return;
  50. }
  51. const ztfjGroup = this.group.getObjectByName('ztfj');
  52. if (!this.group.getObjectByName('monitorText1')) {
  53. const worldPosition = new THREE.Vector3();
  54. ztfjGroup?.getObjectByName('Cylinder1042')?.getWorldPosition(worldPosition);
  55. const element = document.getElementById('inputBox') as HTMLElement;
  56. if (element) {
  57. const mainCSS3D = new CSS3DObject(element);
  58. mainCSS3D.name = 'monitorText1';
  59. mainCSS3D.scale.set(0.09, 0.09, 0.09);
  60. // mainCSS3D.position.set(23.78, 18.18, -6.85);
  61. mainCSS3D.position.set(worldPosition.x + 12, worldPosition.y - 10, worldPosition.z - 20);
  62. mainCSS3D.lookAt(worldPosition.x + 12, worldPosition.y - 0, worldPosition.z + 2);
  63. this.group.add(mainCSS3D);
  64. }
  65. }
  66. if (!this.group.getObjectByName('monitorText2')) {
  67. const worldPosition = new THREE.Vector3();
  68. ztfjGroup?.getObjectByName('Cylinder396')?.getWorldPosition(worldPosition);
  69. const element = document.getElementById('inputBox1') as HTMLElement;
  70. if (element) {
  71. const mainCSS3D = new CSS3DObject(element);
  72. mainCSS3D.name = 'monitorText2';
  73. mainCSS3D.scale.set(0.09, 0.09, 0.09);
  74. // mainCSS3D.position.set(23.78, 18.18, 16.82);
  75. mainCSS3D.position.set(worldPosition.x + 12, worldPosition.y - 10, worldPosition.z - 20);
  76. mainCSS3D.lookAt(worldPosition.x + 12, worldPosition.y - 0, worldPosition.z + 2);
  77. // mainCSS3D.lookAt(23.78, 20, 20.82);
  78. this.group.add(mainCSS3D);
  79. }
  80. }
  81. if (!this.group.getObjectByName('monitorText4')) {
  82. const worldPosition = new THREE.Vector3();
  83. const fbmGroup = this.group?.getObjectByName('fbm') as THREE.Group;
  84. if (fbmGroup) {
  85. fbmGroup?.getObjectByName('Box022')?.getWorldPosition(worldPosition);
  86. const element = document.getElementById('fbm') as HTMLElement;
  87. if (element) {
  88. const mainCSS3D = new CSS3DObject(element);
  89. mainCSS3D.name = 'monitorText4';
  90. mainCSS3D.scale.set(0.07, 0.07, 0.07);
  91. mainCSS3D.position.set(worldPosition.x + 20, worldPosition.y - 8, worldPosition.z - 20);
  92. mainCSS3D.lookAt(worldPosition.x + 20, worldPosition.y - 0, worldPosition.z + 2);
  93. this.group.add(mainCSS3D);
  94. }
  95. }
  96. }
  97. }
  98. clearCssText() {
  99. if (this.group) {
  100. const mainCSS3D1 = this.group.getObjectByName('monitorText1');
  101. const mainCSS3D2 = this.group.getObjectByName('monitorText2');
  102. if (mainCSS3D1) this.group.remove(mainCSS3D1);
  103. if (mainCSS3D2) this.group.remove(mainCSS3D2);
  104. }
  105. }
  106. addEcharts() {
  107. const echartsBox = document.getElementById('fan-echarts');
  108. if (echartsBox) {
  109. const canvasObj = echartsBox.getElementsByTagName('canvas')[0];
  110. // 将canvas 纹理转换为材质
  111. const echartsMap = new THREE.CanvasTexture(canvasObj); // 关键一步
  112. const echartsMaterial = new THREE.MeshBasicMaterial({
  113. map: echartsMap, // 设置纹理贴图
  114. transparent: true,
  115. side: THREE.FrontSide, // 这里是双面渲染的意思
  116. });
  117. echartsMaterial.blending = THREE.CustomBlending;
  118. const monitorPlane = this.group?.getObjectByName('monitorEcharts');
  119. if (monitorPlane) {
  120. monitorPlane.material = echartsMaterial;
  121. } else {
  122. const planeGeometry = new THREE.PlaneGeometry(17.6, 9.9); // 平面3维几何体PlaneGeometry
  123. const planeMesh = new THREE.Mesh(planeGeometry, echartsMaterial);
  124. planeMesh.name = 'monitorEcharts';
  125. planeMesh.scale.set(1, 1, 1);
  126. planeMesh.position.set(-47.38, 13.227, -21.79);
  127. this.group?.add(planeMesh);
  128. }
  129. }
  130. }
  131. initAnimation() {}
  132. startAnimation() {}
  133. /* 更新动画 */
  134. render() {
  135. if (!this.model) {
  136. return;
  137. }
  138. if (this.fbmMixers) this.fbmMixers?.update(1 / 25);
  139. }
  140. /* 点击风窗,风窗全屏 */
  141. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  142. // 判断是否点击到视频
  143. intersects.find((intersect) => {
  144. const mesh = intersect.object;
  145. if (mesh.name === 'player1') {
  146. if (new Date().getTime() - this.playerStartClickTime1 < 400) {
  147. // 双击,视频放大
  148. if (this.player1) {
  149. this.player1.requestFullscreen();
  150. }
  151. }
  152. this.playerStartClickTime1 = new Date().getTime();
  153. return true;
  154. }
  155. return false;
  156. });
  157. }
  158. mouseUpModel() {}
  159. async setDeviceFrequency(deviceType, state, frequencyVal?) {
  160. debugger;
  161. // 调节频率
  162. if (frequencyVal) {
  163. this.resetSmokeParam(deviceType, frequencyVal, 0);
  164. }
  165. this.openOrCloseValve(deviceType, state, 0);
  166. this.startGearAnimation(deviceType, state, '', 0);
  167. if (deviceType === 'front') {
  168. this.frontSmoke?.startSmoke();
  169. } else {
  170. this.backSmoke?.startSmoke();
  171. }
  172. setTimeout(() => {
  173. this.lookMotor(deviceType, state, 10);
  174. }, 2000);
  175. }
  176. async openDevice(deviceType, smokeDirection, frequencyVal, duration?) {
  177. if (smokeDirection) {
  178. this.setSmokeDirection(deviceType, smokeDirection);
  179. }
  180. let smoke;
  181. if (deviceType === 'front') {
  182. smoke = this.frontSmoke;
  183. } else {
  184. smoke = this.backSmoke;
  185. }
  186. if (!smoke.frameId) {
  187. await this.lookMotor(deviceType, 'open', duration);
  188. await this.openOrCloseValve(deviceType, 'open', duration);
  189. this.startGearAnimation(deviceType, 'open', smokeDirection, frequencyVal, duration);
  190. debugger;
  191. smoke.startSmoke(duration);
  192. }
  193. }
  194. async closeDevice(deviceType, flag = true) {
  195. let smoke;
  196. if (deviceType === 'front') {
  197. smoke = this.frontSmoke;
  198. } else if (deviceType === 'back') {
  199. smoke = this.backSmoke;
  200. }
  201. if (smoke && smoke.frameId) {
  202. if (flag) {
  203. smoke.stopSmoke();
  204. await this.openOrCloseValve(deviceType, 'close');
  205. this.startGearAnimation(deviceType, 'close', '', null);
  206. await this.lookMotor(deviceType, 'close');
  207. } else {
  208. smoke.stopSmoke(0);
  209. await this.openOrCloseValve(deviceType, 'close', 0);
  210. this.startGearAnimation(deviceType, 'close', '', null, 0);
  211. await this.lookMotor(deviceType, 'close', 0);
  212. }
  213. }
  214. }
  215. async setSmokeDirection(deviceType, smokeDirection) {
  216. const windowPositivePath = [
  217. {
  218. path0: new THREE.Vector3(4.441, 20.267, 3.614),
  219. path1: new THREE.Vector3(5.041, 6.806, 3.614),
  220. isSpread: true,
  221. spreadDirection: -1, //
  222. },
  223. {
  224. path0: new THREE.Vector3(7.441, 0.806, 3.614),
  225. path1: new THREE.Vector3(41.583, 1.485, 3.614),
  226. isSpread: false,
  227. spreadDirection: 0, //
  228. },
  229. {
  230. path0: new THREE.Vector3(41.583, 1.485, 3.614),
  231. path1: new THREE.Vector3(42.741, 5.364, 3.614),
  232. isSpread: false,
  233. spreadDirection: 0,
  234. },
  235. {
  236. path0: new THREE.Vector3(42.741, 5.364, 3.614),
  237. path1: new THREE.Vector3(44.741, 17.267, 3.614),
  238. isSpread: true,
  239. spreadDirection: 1, // 1是由小变大(出),-1是由大变小(进)
  240. },
  241. ];
  242. const windowInversePath = [
  243. {
  244. path0: new THREE.Vector3(44.741, 17.267, 3.614),
  245. path1: new THREE.Vector3(42.741, 5.364, 3.614),
  246. isSpread: true,
  247. spreadDirection: -1, //
  248. },
  249. {
  250. path0: new THREE.Vector3(42.741, 5.364, 3.614),
  251. path1: new THREE.Vector3(41.583, 1.485, 3.614),
  252. isSpread: false,
  253. spreadDirection: 0, //
  254. },
  255. {
  256. path0: new THREE.Vector3(41.583, 1.485, 3.614),
  257. path1: new THREE.Vector3(7.441, 0.806, 3.614),
  258. isSpread: false,
  259. spreadDirection: 0, // 1是由小变大,-1是由大变小
  260. },
  261. {
  262. path0: new THREE.Vector3(4.441, 17.267, 3.614),
  263. path1: new THREE.Vector3(5.041, 6.806, 3.614),
  264. isSpread: true,
  265. spreadDirection: 1, //
  266. },
  267. ];
  268. const tubPositivePath = [
  269. {
  270. path0: new THREE.Vector3(7.441, 0.806, 3.614),
  271. path1: new THREE.Vector3(44.583, 1.485, 3.614),
  272. isSpread: false,
  273. spreadDirection: 0, //
  274. },
  275. {
  276. path0: new THREE.Vector3(44.583, 1.485, 3.614),
  277. path1: new THREE.Vector3(45.741, 5.364, 3.614),
  278. isSpread: false,
  279. spreadDirection: 0,
  280. },
  281. {
  282. path0: new THREE.Vector3(45.741, 5.364, 3.614),
  283. path1: new THREE.Vector3(47.741, 17.267, 3.614),
  284. isSpread: true,
  285. spreadDirection: 1, // 1是由小变大(出),-1是由大变小(进)
  286. },
  287. ];
  288. const tubInversePath = [
  289. {
  290. path0: new THREE.Vector3(47.741, 17.267, 3.614),
  291. path1: new THREE.Vector3(45.741, 5.364, 3.614),
  292. isSpread: true,
  293. spreadDirection: -1, //
  294. },
  295. {
  296. path0: new THREE.Vector3(45.741, 5.364, 3.614),
  297. path1: new THREE.Vector3(44.583, 1.485, 3.614),
  298. isSpread: false,
  299. spreadDirection: 0, //
  300. },
  301. {
  302. path0: new THREE.Vector3(44.583, 1.485, 3.614),
  303. path1: new THREE.Vector3(7.441, 0.806, 3.614),
  304. isSpread: false,
  305. spreadDirection: 0, // 1是由小变大,-1是由大变小
  306. },
  307. ];
  308. let smoke;
  309. if (deviceType === 'front') {
  310. smoke = this.frontSmoke;
  311. } else if (deviceType === 'back') {
  312. smoke = this.backSmoke;
  313. }
  314. switch (smokeDirection) {
  315. case 'tubPositivePath': // 风筒正
  316. smoke.setPath(tubPositivePath);
  317. break;
  318. case 'tubInversePath': // 风筒反
  319. smoke.setPath(tubInversePath);
  320. break;
  321. case 'windowPositivePath': // 风窗正
  322. smoke.setPath(windowPositivePath);
  323. break;
  324. case 'windowInversePath': // 风窗反
  325. smoke.setPath(windowInversePath);
  326. break;
  327. }
  328. }
  329. /* 播放气流动画 */
  330. /**
  331. *
  332. * @param controlType // 设备控制类型
  333. * @param deviceType //前后风机
  334. * @param frequencyVal // 风机运行频率
  335. * @param state // 打开、关闭状态
  336. */
  337. async playSmoke(controlType, deviceType, frequencyVal, state, smokeDirection) {
  338. debugger;
  339. if (frequencyVal) {
  340. this.resetSmokeParam(deviceType, frequencyVal);
  341. }
  342. if (controlType === 'startSmoke') {
  343. if (state === 'stop') {
  344. await this.closeDevice(deviceType);
  345. } else {
  346. // 开启时需要设置方向
  347. await this.openDevice(deviceType, smokeDirection, frequencyVal);
  348. }
  349. } else if (controlType === 'changeDirection') {
  350. // 改变扇叶转动方向、反风
  351. this.startGearAnimation(deviceType, 'changeDirection', smokeDirection, frequencyVal);
  352. let smoke;
  353. if (deviceType === 'front') {
  354. smoke = this.frontSmoke;
  355. } else {
  356. smoke = this.backSmoke;
  357. }
  358. if (smoke && smoke.frameId) {
  359. await smoke.stopSmoke();
  360. await this.setSmokeDirection(deviceType, smokeDirection);
  361. debugger;
  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) ztfjGroup.position.z = ztfjGroup.position.z + 5;
  725. if (fbmGroup) fbmGroup.position.z = fbmGroup.position.z + 5;
  726. if (fcGroup) fcGroup.position.z = fcGroup.position.z + 5;
  727. const mesh = ztfjGroup?.getObjectByName('jizu06') as THREE.Mesh; //前
  728. if (mesh && mesh.material) this.oldMaterial = mesh.material as THREE.MeshStandardMaterial;
  729. await this.initMotor();
  730. resolve(null);
  731. this.initWindow();
  732. this.initFbmAnimation();
  733. });
  734. });
  735. }
  736. destroy() {
  737. this.frontSmoke.clearSmoke();
  738. this.backSmoke.clearSmoke();
  739. const fbmGroup = this.group?.getObjectByName('fbm') as THREE.Group;
  740. if (this.fbmMixers && this.fbmAnimationClip && this.fbmOpenAction && fbmGroup) {
  741. this.fbmMixers.uncacheClip(this.fbmAnimationClip);
  742. this.fbmMixers.uncacheAction(this.fbmOpenAction.getClip(), fbmGroup);
  743. this.fbmMixers.uncacheRoot(fbmGroup);
  744. this.fbmAnimationClip.tracks = [];
  745. this.fbmAnimationClip = undefined;
  746. this.fbmOpenAction = undefined;
  747. }
  748. this.model.clearGroup(this.motorGroup1);
  749. this.model.clearGroup(this.motorGroup2);
  750. this.model.clearGroup(this.group);
  751. this.motorGroup1 = undefined;
  752. this.motorGroup2 = undefined;
  753. this.gearFront.gear1 = undefined;
  754. this.gearFront.gear2 = undefined;
  755. this.gearBack.gear1 = undefined;
  756. this.gearBack.gear2 = undefined;
  757. this.frontSmoke = undefined;
  758. this.backSmoke = undefined;
  759. this.model = undefined;
  760. this.group = undefined;
  761. }
  762. }
  763. export default mainWindRect;