mainWind.threejs.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  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. // 调节频率
  161. if (frequencyVal) {
  162. this.resetSmokeParam(deviceType, frequencyVal, 0);
  163. }
  164. this.openOrCloseValve(deviceType, state, 0);
  165. this.startGearAnimation(deviceType, state, '', 0);
  166. if (deviceType === 'front') {
  167. this.frontSmoke?.startSmoke();
  168. } else {
  169. this.backSmoke?.startSmoke();
  170. }
  171. setTimeout(() => {
  172. this.lookMotor(deviceType, state, 10);
  173. }, 2000);
  174. }
  175. async openDevice(deviceType, smokeDirection, frequencyVal, duration?) {
  176. if (smokeDirection) {
  177. this.setSmokeDirection(deviceType, smokeDirection);
  178. }
  179. let smoke;
  180. if (deviceType === 'front') {
  181. smoke = this.frontSmoke;
  182. } else {
  183. smoke = this.backSmoke;
  184. }
  185. if (!smoke.frameId) {
  186. await this.lookMotor(deviceType, 'open', duration);
  187. await this.openOrCloseValve(deviceType, 'open', duration);
  188. this.startGearAnimation(deviceType, 'open', smokeDirection, frequencyVal, duration);
  189. smoke.startSmoke(duration);
  190. }
  191. }
  192. async closeDevice(deviceType, flag = true) {
  193. let smoke;
  194. if (deviceType === 'front') {
  195. smoke = this.frontSmoke;
  196. } else if (deviceType === 'back') {
  197. smoke = this.backSmoke;
  198. }
  199. if (smoke && smoke.frameId) {
  200. if (flag) {
  201. smoke.stopSmoke();
  202. await this.openOrCloseValve(deviceType, 'close');
  203. this.startGearAnimation(deviceType, 'close', '', null);
  204. await this.lookMotor(deviceType, 'close');
  205. } else {
  206. smoke.stopSmoke(0);
  207. await this.openOrCloseValve(deviceType, 'close', 0);
  208. this.startGearAnimation(deviceType, 'close', '', null, 0);
  209. await this.lookMotor(deviceType, 'close', 0);
  210. }
  211. }
  212. }
  213. async setSmokeDirection(deviceType, smokeDirection) {
  214. const windowPositivePath = [
  215. {
  216. path0: new THREE.Vector3(4.441, 20.267, 3.614),
  217. path1: new THREE.Vector3(5.041, 6.806, 3.614),
  218. isSpread: true,
  219. spreadDirection: -1, //
  220. },
  221. {
  222. path0: new THREE.Vector3(7.441, 0.806, 3.614),
  223. path1: new THREE.Vector3(41.583, 1.485, 3.614),
  224. isSpread: false,
  225. spreadDirection: 0, //
  226. },
  227. {
  228. path0: new THREE.Vector3(41.583, 1.485, 3.614),
  229. path1: new THREE.Vector3(42.741, 5.364, 3.614),
  230. isSpread: false,
  231. spreadDirection: 0,
  232. },
  233. {
  234. path0: new THREE.Vector3(42.741, 5.364, 3.614),
  235. path1: new THREE.Vector3(44.741, 17.267, 3.614),
  236. isSpread: true,
  237. spreadDirection: 1, // 1是由小变大(出),-1是由大变小(进)
  238. },
  239. ];
  240. const windowInversePath = [
  241. {
  242. path0: new THREE.Vector3(44.741, 17.267, 3.614),
  243. path1: new THREE.Vector3(42.741, 5.364, 3.614),
  244. isSpread: true,
  245. spreadDirection: -1, //
  246. },
  247. {
  248. path0: new THREE.Vector3(42.741, 5.364, 3.614),
  249. path1: new THREE.Vector3(41.583, 1.485, 3.614),
  250. isSpread: false,
  251. spreadDirection: 0, //
  252. },
  253. {
  254. path0: new THREE.Vector3(41.583, 1.485, 3.614),
  255. path1: new THREE.Vector3(7.441, 0.806, 3.614),
  256. isSpread: false,
  257. spreadDirection: 0, // 1是由小变大,-1是由大变小
  258. },
  259. {
  260. path0: new THREE.Vector3(4.441, 17.267, 3.614),
  261. path1: new THREE.Vector3(5.041, 6.806, 3.614),
  262. isSpread: true,
  263. spreadDirection: 1, //
  264. },
  265. ];
  266. const tubPositivePath = [
  267. {
  268. path0: new THREE.Vector3(7.441, 0.806, 3.614),
  269. path1: new THREE.Vector3(44.583, 1.485, 3.614),
  270. isSpread: false,
  271. spreadDirection: 0, //
  272. },
  273. {
  274. path0: new THREE.Vector3(44.583, 1.485, 3.614),
  275. path1: new THREE.Vector3(45.741, 5.364, 3.614),
  276. isSpread: false,
  277. spreadDirection: 0,
  278. },
  279. {
  280. path0: new THREE.Vector3(45.741, 5.364, 3.614),
  281. path1: new THREE.Vector3(47.741, 17.267, 3.614),
  282. isSpread: true,
  283. spreadDirection: 1, // 1是由小变大(出),-1是由大变小(进)
  284. },
  285. ];
  286. const tubInversePath = [
  287. {
  288. path0: new THREE.Vector3(47.741, 17.267, 3.614),
  289. path1: new THREE.Vector3(45.741, 5.364, 3.614),
  290. isSpread: true,
  291. spreadDirection: -1, //
  292. },
  293. {
  294. path0: new THREE.Vector3(45.741, 5.364, 3.614),
  295. path1: new THREE.Vector3(44.583, 1.485, 3.614),
  296. isSpread: false,
  297. spreadDirection: 0, //
  298. },
  299. {
  300. path0: new THREE.Vector3(44.583, 1.485, 3.614),
  301. path1: new THREE.Vector3(7.441, 0.806, 3.614),
  302. isSpread: false,
  303. spreadDirection: 0, // 1是由小变大,-1是由大变小
  304. },
  305. ];
  306. let smoke;
  307. if (deviceType === 'front') {
  308. smoke = this.frontSmoke;
  309. } else if (deviceType === 'back') {
  310. smoke = this.backSmoke;
  311. }
  312. switch (smokeDirection) {
  313. case 'tubPositivePath': // 风筒正
  314. smoke.setPath(tubPositivePath);
  315. break;
  316. case 'tubInversePath': // 风筒反
  317. smoke.setPath(tubInversePath);
  318. break;
  319. case 'windowPositivePath': // 风窗正
  320. smoke.setPath(windowPositivePath);
  321. break;
  322. case 'windowInversePath': // 风窗反
  323. smoke.setPath(windowInversePath);
  324. break;
  325. }
  326. }
  327. /* 播放气流动画 */
  328. /**
  329. *
  330. * @param controlType // 设备控制类型
  331. * @param deviceType //前后风机
  332. * @param frequencyVal // 风机运行频率
  333. * @param state // 打开、关闭状态
  334. */
  335. async playSmoke(controlType, deviceType, frequencyVal, state, smokeDirection) {
  336. if (frequencyVal) {
  337. this.resetSmokeParam(deviceType, frequencyVal);
  338. }
  339. if (controlType === 'startSmoke') {
  340. if (state === 'stop') {
  341. await this.closeDevice(deviceType);
  342. } else {
  343. // 开启时需要设置方向
  344. await this.openDevice(deviceType, smokeDirection, frequencyVal);
  345. }
  346. } else if (controlType === 'changeDirection') {
  347. // 改变扇叶转动方向、反风
  348. this.startGearAnimation(deviceType, 'changeDirection', smokeDirection, frequencyVal);
  349. let smoke;
  350. if (deviceType === 'front') {
  351. smoke = this.frontSmoke;
  352. } else {
  353. smoke = this.backSmoke;
  354. }
  355. if (smoke && smoke.frameId) {
  356. await smoke.stopSmoke();
  357. await this.setSmokeDirection(deviceType, smokeDirection);
  358. smoke.startSmoke();
  359. }
  360. } else if (controlType === 'frequency') {
  361. this.startGearAnimation(deviceType, 'frequency', smokeDirection, frequencyVal);
  362. } else if (controlType === 'initiatePlay') {
  363. this.openDevice(deviceType, smokeDirection, frequencyVal, 0);
  364. } else if (controlType === 'changeSmoke') {
  365. //
  366. }
  367. }
  368. stopSmoke() {
  369. this.closeDevice('front', false);
  370. this.closeDevice('back', false);
  371. }
  372. /* 打开或关闭蝶阀 */
  373. openOrCloseValve(deviceType, flag, duration = 3) {
  374. const ztfjGroup = this.group?.getObjectByName('ztfj');
  375. return new Promise((resolve) => {
  376. let diefa;
  377. if (deviceType == 'front') {
  378. diefa = ztfjGroup?.getObjectByName('butterfly_valve001') as THREE.Mesh;
  379. } else {
  380. diefa = ztfjGroup?.getObjectByName('butterfly_valve002') as THREE.Mesh;
  381. }
  382. let rotationY;
  383. if (flag == 'open') {
  384. rotationY = 0;
  385. } else {
  386. rotationY = Math.PI / 2;
  387. }
  388. if (diefa) {
  389. gsap.to(diefa.rotation, {
  390. y: rotationY,
  391. duration: duration,
  392. ease: 'none',
  393. onComplete: function () {
  394. resolve(null);
  395. },
  396. });
  397. }
  398. });
  399. }
  400. /* 风流调频, 范围1-50 */
  401. // opacityFactor (0.4 300)
  402. // life 最小 300, 最大 50
  403. // speedFactor 最大0, 最小100
  404. resetSmokeParam(deviceType, frequency, duration = 5) {
  405. if (frequency < 1) frequency = 1;
  406. if (frequency > 50) frequency = 50;
  407. let smoke;
  408. if (deviceType === 'front') {
  409. smoke = this.frontSmoke;
  410. } else {
  411. smoke = this.backSmoke;
  412. }
  413. const opacityFactor = (frequency / 50) * 0.8;
  414. duration = (Number(Math.abs(smoke.opacityFactor - opacityFactor).toFixed(1)) / 0.8) * 5;
  415. const life = (-250 / 50) * frequency + 300;
  416. gsap.to(smoke, {
  417. opacityFactor: opacityFactor,
  418. life: life,
  419. duration: duration,
  420. ease: 'easeInCirc',
  421. overwrite: true,
  422. });
  423. }
  424. /* 显示电机 */
  425. lookMotor(deviceType, flag, duration = 5) {
  426. return new Promise((resolve) => {
  427. const ztfjGroup = this.group?.getObjectByName('ztfj');
  428. let mesh, mesh1, mesh2, motorGroup;
  429. mesh1 = ztfjGroup?.getObjectByName('TWO00'); //前
  430. mesh2 = ztfjGroup?.getObjectByName('ONE00'); //后
  431. if (deviceType == 'front') {
  432. mesh = mesh1;
  433. motorGroup = this.motorGroup2;
  434. } else {
  435. mesh = mesh2;
  436. motorGroup = this.motorGroup1;
  437. }
  438. if (mesh && motorGroup) {
  439. if (flag == 'open') {
  440. mesh.material.depthWrite = false;
  441. mesh.material.depthTest = false;
  442. motorGroup.visible = true;
  443. gsap.to(mesh.material, {
  444. opacity: 0.1,
  445. duration: duration,
  446. overwrite: true,
  447. onComplete: function () {
  448. // mesh.material.color = '#000';
  449. resolve(null);
  450. },
  451. });
  452. } else {
  453. const opacity = mesh.material.opacity;
  454. Object.assign(mesh.material, this.oldMaterial, { opacity: opacity });
  455. mesh.material.depthWrite = true;
  456. mesh.material.depthTest = true;
  457. gsap.to(mesh.material, {
  458. opacity: 1,
  459. duration: 1,
  460. overwrite: true,
  461. onComplete: function () {
  462. resolve(null);
  463. },
  464. });
  465. }
  466. }
  467. });
  468. }
  469. /* 齿轮转动动画 1 - 50 最大3 */
  470. startGearAnimation(deviceType, flag, smokeDirection, frequencyVal, duration = 8) {
  471. let gearObj, gearDirection;
  472. if (deviceType === 'front') {
  473. gearObj = this.gearFront;
  474. } else {
  475. gearObj = this.gearBack;
  476. }
  477. if (smokeDirection === 'tubPositivePath') {
  478. gearDirection = 1;
  479. } else if (smokeDirection === 'tubInversePath') {
  480. gearDirection = -1;
  481. }
  482. if (frequencyVal) {
  483. const endGearRotateFactor = (3 / 50) * frequencyVal;
  484. duration = (8 / 3) * Math.abs(gearObj.endGearRotateFactor - endGearRotateFactor);
  485. gearObj.endGearRotateFactor = endGearRotateFactor;
  486. }
  487. const gearAnimation = () => {
  488. gsap.to(gearObj, {
  489. gearRotateFactor: gearObj.endGearRotateFactor,
  490. duration: duration,
  491. ease: 'easeInCubic',
  492. repeat: 0,
  493. overwrite: true,
  494. });
  495. const clock = new THREE.Clock(); // 时钟
  496. const h = () => {
  497. if (gearObj.gear1 && gearObj.gear2) {
  498. gearObj.gearFrameId = requestAnimationFrame(h);
  499. const dt = clock.getDelta();
  500. gearObj.gear1.rotation.x += dt * gearObj.gearRotateFactor * gearObj.gear1Direction;
  501. gearObj.gear2.rotation.x += dt * gearObj.gearRotateFactor * gearObj.gear2Direction;
  502. }
  503. };
  504. h();
  505. };
  506. if (flag === 'changeDirection') {
  507. if (gearDirection == -1 * gearObj.gear1Direction) {
  508. // 齿轮正在转,需要停止后再反方向转
  509. gsap.to(gearObj, {
  510. gearRotateFactor: 0,
  511. duration: duration,
  512. ease: 'easeInCubic',
  513. repeat: 0,
  514. onComplete: function () {
  515. window.cancelAnimationFrame(gearObj.gearFrameId);
  516. gearObj.gearFrameId = undefined;
  517. gearObj.gear1Direction = -1 * gearObj.gear1Direction;
  518. gearObj.gear2Direction = -1 * gearObj.gear2Direction;
  519. gearAnimation();
  520. },
  521. });
  522. }
  523. } else if (flag === 'open') {
  524. gearObj.gear1Direction = gearDirection;
  525. gearObj.gear2Direction = -1 * gearDirection;
  526. gearAnimation();
  527. } else if (flag === 'close') {
  528. gsap.to(gearObj, {
  529. gearRotateFactor: 0,
  530. duration: duration,
  531. ease: 'easeInCubic',
  532. repeat: 0,
  533. overwrite: true,
  534. onComplete: function () {
  535. window.cancelAnimationFrame(gearObj.gearFrameId);
  536. gearObj.gearFrameId = undefined;
  537. },
  538. });
  539. } else if (flag === 'frequency') {
  540. gsap.to(gearObj, {
  541. gearRotateFactor: gearObj.endGearRotateFactor,
  542. duration: duration,
  543. ease: 'easeInCubic',
  544. repeat: 0,
  545. overwrite: true,
  546. });
  547. }
  548. }
  549. /* 初始化口上面的气体 */
  550. initSmokeMass() {
  551. if (!this.frontSmoke) {
  552. this.frontSmoke = new Smoke('/model/img/texture-smoke.png', '#ffffff', 0, 0.4, 1.8, 100);
  553. }
  554. if (!this.backSmoke) {
  555. this.backSmoke = new Smoke('/model/img/texture-smoke.png', '#ffffff', 0, 0.4, 1.8, 100);
  556. }
  557. }
  558. /* 设置气流位置 */
  559. async setSmokePosition() {
  560. if (this.frontSmoke) {
  561. await this.frontSmoke.setPoints();
  562. this.frontSmoke.points.name = 'frontSmoke';
  563. this.group?.add(this.frontSmoke.points);
  564. // this.frontSmoke.points.position.set(-2.51, 2.51, 8.25);
  565. this.frontSmoke.points.position.set(-2.51, 2.51, 13.25);
  566. }
  567. if (this.backSmoke) {
  568. await this.backSmoke.setPoints();
  569. this.backSmoke.points.name = 'backSmoke';
  570. this.group?.add(this.backSmoke.points);
  571. // this.backSmoke.points.position.set(-2.2, 3.13, -7.8);
  572. this.backSmoke.points.position.set(-2.2, 3.13, -2.8);
  573. }
  574. }
  575. /** 初始化电机 */
  576. async initMotor() {
  577. // 前电机
  578. const motorGltf1 = await this.model.setGLTFModel('dj1');
  579. this.motorGroup1 = motorGltf1[0] as THREE.Group;
  580. this.motorGroup1?.position.set(0, 0, 5);
  581. this.motorGroup1.visible = false;
  582. this.motorGroup1.traverse((item) => {
  583. if (item instanceof THREE.Object3D) {
  584. item.renderOrder = -1;
  585. if (item.name === 'fan_blade003') {
  586. // @ts-ignore
  587. this.gearBack.gear1 = item as THREE.Group;
  588. } else if (item.name === 'fan_blade005') {
  589. // @ts-ignore
  590. this.gearBack.gear2 = item as THREE.Group;
  591. }
  592. }
  593. });
  594. this.motorGroup1.renderOrder = -1;
  595. this.group?.add(this.motorGroup1);
  596. // 后电机
  597. const motorGltf2 = await this.model.setGLTFModel('dj2');
  598. this.motorGroup2 = motorGltf2[0] as THREE.Group;
  599. this.motorGroup2?.position.set(0, 0, 5);
  600. this.motorGroup2.visible = false;
  601. this.motorGroup2.traverse((item) => {
  602. if (item instanceof THREE.Object3D) {
  603. item.renderOrder = -1;
  604. if (item.name === 'fan_blade007') {
  605. // @ts-ignore
  606. this.gearFront.gear1 = item as THREE.Group;
  607. } else if (item.name === 'fan_blade006') {
  608. // @ts-ignore
  609. this.gearFront.gear2 = item as THREE.Group;
  610. }
  611. }
  612. });
  613. this.motorGroup2.renderOrder = -1;
  614. this.group?.add(this.motorGroup2);
  615. }
  616. openOrCloseWindow(deviceType, flag) {
  617. const _this = this;
  618. let endAngle = 0,
  619. windowGroup;
  620. if (deviceType === 'front') {
  621. windowGroup = this.frontWindowGroup;
  622. }
  623. if (deviceType === 'back') {
  624. windowGroup = this.backWindowGroup;
  625. }
  626. if (flag == 'openWindow') {
  627. // 打开风窗
  628. endAngle = 1;
  629. } else {
  630. // 关闭风窗
  631. endAngle = 0;
  632. }
  633. if (windowGroup)
  634. gsap.to(this, {
  635. windowAngle: endAngle,
  636. duration: Math.abs(endAngle - this.windowAngle) * 10,
  637. ease: 'none',
  638. onUpdate: function () {
  639. windowGroup.children.forEach((mesh) => {
  640. mesh.rotation.z = _this.windowAngle;
  641. });
  642. },
  643. });
  644. }
  645. /** 初始化风窗 */
  646. initWindow() {
  647. if (!this.group) return;
  648. this.frontWindowGroup = new THREE.Group();
  649. this.frontWindowGroup.name = 'frontWindowGroup';
  650. this.backWindowGroup = new THREE.Group();
  651. this.backWindowGroup.name = 'backWindowGroup';
  652. if (this.group && this.group?.children.length > 0) {
  653. for (let i = this.group?.children.length - 1; i >= 0; i--) {
  654. const obj = this.group?.children[i];
  655. if (obj.type === 'Mesh' && obj.name && obj.name.startsWith('TC')) {
  656. const mesh = obj.clone();
  657. if (obj.name.startsWith('TC1')) {
  658. this.backWindowGroup.add(mesh);
  659. } else if (obj.name.startsWith('TC2')) {
  660. this.frontWindowGroup.add(mesh);
  661. }
  662. obj.removeFromParent();
  663. this.group?.remove(obj);
  664. }
  665. }
  666. }
  667. this.group?.add(this.backWindowGroup);
  668. this.group?.add(this.frontWindowGroup);
  669. }
  670. initFbmAnimation() {
  671. const fbmGroup = this.group?.getObjectByName('fbm') as THREE.Group;
  672. if (fbmGroup) {
  673. this.fbmAnimationClip = fbmGroup.animations[0];
  674. this.fbmMixers = new THREE.AnimationMixer(fbmGroup);
  675. this.fbmOpenAction = this.fbmMixers.clipAction(this.fbmAnimationClip);
  676. this.fbmOpenAction.loop = THREE.LoopOnce;
  677. this.fbmOpenAction.clampWhenFinished = true;
  678. this.fbmMixers.timeScale = 0.15;
  679. }
  680. }
  681. playAnimation(flag, duration?) {
  682. if (this.fbmOpenAction && this.fbmMixers && this.fbmAnimationClip) {
  683. if (duration == 0) {
  684. if (flag == 'open') {
  685. this.fbmOpenAction.reset();
  686. this.fbmOpenAction.time = this.fbmAnimationClip.duration;
  687. this.fbmOpenAction.timeScale = 1;
  688. } else {
  689. this.fbmOpenAction.reset();
  690. this.fbmOpenAction.time = 0;
  691. this.fbmOpenAction.timeScale = -1;
  692. }
  693. this.fbmOpenAction.play();
  694. } else {
  695. if (flag == 'open') {
  696. this.fbmOpenAction.reset();
  697. this.fbmOpenAction.time = 0;
  698. this.fbmOpenAction.timeScale = 1;
  699. this.fbmOpenAction.play();
  700. } else {
  701. this.fbmOpenAction.reset();
  702. this.fbmOpenAction.time = this.fbmAnimationClip.duration;
  703. this.fbmOpenAction.timeScale = -1;
  704. this.fbmOpenAction.play();
  705. }
  706. }
  707. }
  708. }
  709. mountedThree() {
  710. this.group = new THREE.Group();
  711. return new Promise(async (resolve) => {
  712. this.model.setGLTFModel(['bg1', 'fbm', 'ztfj', 'ztfj-fc'], this.group).then(async () => {
  713. // this.group = gltf[0];
  714. this.group?.position.set(-0.44, 19.88, 22.37);
  715. this.initSmokeMass();
  716. await this.setSmokePosition();
  717. const ztfjGroup = this.group?.getObjectByName('ztfj') as THREE.Group;
  718. const fbmGroup = this.group?.getObjectByName('fbm') as THREE.Group;
  719. const fcGroup = this.group?.getObjectByName('ztfj-fc') as THREE.Group;
  720. if (ztfjGroup) ztfjGroup.position.z = ztfjGroup.position.z + 5;
  721. if (fbmGroup) fbmGroup.position.z = fbmGroup.position.z + 5;
  722. if (fcGroup) fcGroup.position.z = fcGroup.position.z + 5;
  723. const mesh = ztfjGroup?.getObjectByName('jizu06') as THREE.Mesh; //前
  724. if (mesh && mesh.material) this.oldMaterial = mesh.material as THREE.MeshStandardMaterial;
  725. await this.initMotor();
  726. resolve(null);
  727. this.initWindow();
  728. this.initFbmAnimation();
  729. });
  730. });
  731. }
  732. destroy() {
  733. this.frontSmoke.clearSmoke();
  734. this.backSmoke.clearSmoke();
  735. const fbmGroup = this.group?.getObjectByName('fbm') as THREE.Group;
  736. if (this.fbmMixers && this.fbmAnimationClip && this.fbmOpenAction && fbmGroup) {
  737. this.fbmMixers.uncacheClip(this.fbmAnimationClip);
  738. this.fbmMixers.uncacheAction(this.fbmOpenAction.getClip(), fbmGroup);
  739. this.fbmMixers.uncacheRoot(fbmGroup);
  740. this.fbmAnimationClip.tracks = [];
  741. this.fbmAnimationClip = undefined;
  742. this.fbmOpenAction = undefined;
  743. }
  744. this.model.clearGroup(this.motorGroup1);
  745. this.model.clearGroup(this.motorGroup2);
  746. this.model.clearGroup(this.group);
  747. this.motorGroup1 = undefined;
  748. this.motorGroup2 = undefined;
  749. this.gearFront.gear1 = undefined;
  750. this.gearFront.gear2 = undefined;
  751. this.gearBack.gear1 = undefined;
  752. this.gearBack.gear2 = undefined;
  753. this.frontSmoke = undefined;
  754. this.backSmoke = undefined;
  755. this.model = undefined;
  756. this.group = undefined;
  757. }
  758. }
  759. export default mainWindRect;