mainWind.lj.threejs.ts 25 KB

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