1
0

shuangdaoFcYjl.threejs.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. import * as THREE from 'three';
  2. import { getTextCanvas, renderVideo } from '/@/utils/threejs/util';
  3. import gsap from 'gsap';
  4. class doubleWindowYjl {
  5. model;
  6. modelName = 'sdFc';
  7. group: THREE.Object3D = new THREE.Object3D();
  8. animationTimer;
  9. isLRAnimation = true;
  10. direction = 1;
  11. windowsActionArr = {
  12. frontWindow: <THREE.Mesh[]>[],
  13. backWindow: <THREE.Mesh[]>[],
  14. };
  15. playerStartClickTime1 = new Date().getTime();
  16. playerStartClickTime2 = new Date().getTime();
  17. mixers: THREE.AnimationMixer | undefined;
  18. // fmClock = new THREE.Clock();
  19. clipActionArr = {
  20. frontDoor: null as unknown as THREE.AnimationAction,
  21. backDoor: null as unknown as THREE.AnimationAction,
  22. };
  23. constructor(model) {
  24. this.model = model;
  25. // this.group.name = this.modelName;
  26. }
  27. // 设置模型位置
  28. setModalPosition() {
  29. // this.group.getObjectByName(this.modelName)?.scale.set(9, 9, 9);
  30. // this.group?.scale.set(22, 22, 22);
  31. // this.group?.position.set(-15, 25, 15);
  32. }
  33. addMonitorText(selectData) {
  34. if (!this.group) {
  35. return;
  36. }
  37. const screenDownText = VENT_PARAM['modalText']
  38. ? VENT_PARAM['modalText']
  39. : History_Type['type'] == 'remote'
  40. ? `国能神东煤炭集团监制`
  41. : '煤炭科学技术研究院有限公司研制';
  42. const screenDownTextX = 120 - (screenDownText.length - 10) * 6;
  43. const textArr = [
  44. {
  45. text: `远程定量调节自动风窗`,
  46. font: 'normal 30px Arial',
  47. color: '#009900',
  48. strokeStyle: '#002200',
  49. x: 95,
  50. y: 97,
  51. },
  52. {
  53. text: `${selectData.OpenDegree1 ? '前窗开度值(%)' : selectData.forntArea ? '前窗过风面积(㎡)' : '前窗过风面积(㎡)'}:`,
  54. font: 'normal 28px Arial',
  55. color: '#009900',
  56. strokeStyle: '#002200',
  57. x: 5,
  58. y: 150,
  59. },
  60. {
  61. text: selectData.OpenDegree1
  62. ? Number(`${selectData.OpenDegree1}`).toFixed(2)
  63. : selectData.forntArea
  64. ? Number(`${selectData.forntArea}`).toFixed(2)
  65. : '-',
  66. font: 'normal 28px Arial',
  67. color: '#009900',
  68. strokeStyle: '#002200',
  69. x: 330,
  70. y: 150,
  71. },
  72. {
  73. text: `${selectData.OpenDegree2 ? '后窗开度值(%)' : selectData.forntArea ? '后窗过风面积(㎡)' : '后窗过风面积(㎡)'}:`,
  74. font: 'normal 28px Arial',
  75. color: '#009900',
  76. strokeStyle: '#002200',
  77. x: 5,
  78. y: 210,
  79. },
  80. {
  81. text: selectData.OpenDegree2
  82. ? Number(`${selectData.OpenDegree2}`).toFixed(2)
  83. : selectData.backArea
  84. ? Number(`${selectData.backArea}`).toFixed(2)
  85. : '-',
  86. font: 'normal 28px Arial',
  87. color: '#009900',
  88. strokeStyle: '#002200',
  89. x: 325,
  90. y: 210,
  91. },
  92. {
  93. text: `${selectData.frontRearDP ? '风窗压差(Pa)' : selectData.windSpeed ? '风速(m/s)' : '通信状态'}:`,
  94. font: 'normal 28px Arial',
  95. color: '#009900',
  96. strokeStyle: '#002200',
  97. x: 5,
  98. y: 266,
  99. },
  100. {
  101. text: `${
  102. selectData.frontRearDP
  103. ? selectData.frontRearDP
  104. : selectData.windSpeed
  105. ? selectData.windSpeed
  106. : selectData.netStatus == '0'
  107. ? '断开'
  108. : '连接'
  109. }`,
  110. font: 'normal 28px Arial',
  111. color: '#009900',
  112. strokeStyle: '#002200',
  113. x: 330,
  114. y: 266,
  115. },
  116. {
  117. text: screenDownText,
  118. font: 'normal 28px Arial',
  119. color: '#009900',
  120. strokeStyle: '#002200',
  121. x: screenDownTextX,
  122. y: 322,
  123. },
  124. ];
  125. getTextCanvas(570, 346, textArr, '').then((canvas: HTMLCanvasElement) => {
  126. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  127. const textMaterial = new THREE.MeshBasicMaterial({
  128. // 关于材质并未讲解 实操即可熟悉 这里是漫反射类似纸张的材质,对应的就有高光类似金属的材质.
  129. map: textMap, // 设置纹理贴图
  130. transparent: true,
  131. side: THREE.DoubleSide, // 这里是双面渲染的意思
  132. });
  133. textMaterial.blending = THREE.CustomBlending;
  134. const monitorPlane = this.group?.getObjectByName('monitorText');
  135. if (monitorPlane) {
  136. monitorPlane.material = textMaterial;
  137. } else {
  138. const planeGeometry = new THREE.PlaneGeometry(570, 346); // 平面3维几何体PlaneGeometry
  139. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  140. planeMesh.name = 'monitorText';
  141. planeMesh.scale.set(0.0018, 0.0018, 0.0018);
  142. planeMesh.position.set(4.04, 0.178, -0.2);
  143. this.group?.add(planeMesh);
  144. }
  145. textMap.dispose();
  146. });
  147. }
  148. /* 风门动画 */
  149. render() {
  150. if (!this.model) {
  151. return;
  152. }
  153. if (this.isLRAnimation && this.group) {
  154. // 左右摇摆动画
  155. if (Math.abs(this.group.rotation.y) >= 0.2) {
  156. this.direction = -this.direction;
  157. this.group.rotation.y += 0.00002 * 30 * this.direction;
  158. } else {
  159. this.group.rotation.y += 0.00002 * 30 * this.direction;
  160. }
  161. }
  162. // if (this.mixers && this.fmClock.running) {
  163. // this.mixers.update(2);
  164. // }
  165. }
  166. /* 提取风门序列帧,初始化前后门动画 */
  167. initAnimation() {
  168. const sdFcModal = this.group.getObjectByName('sdFc-yjl');
  169. if (!sdFcModal) return;
  170. // 初始化窗的动画
  171. const fengchuang1 = sdFcModal?.getObjectByName('FengChuang');
  172. const fengchuang2 = sdFcModal?.getObjectByName('FengChuang1');
  173. const meshArr01: THREE.Mesh[] = [];
  174. const meshArr02: THREE.Mesh[] = [];
  175. if (fengchuang1 && fengchuang2) {
  176. fengchuang1.getObjectByName('chuang')?.children.forEach((item: THREE.Object3D) => {
  177. item.children.forEach((obj: THREE.Mesh) => {
  178. if (obj.name && obj.name.startsWith('shanye')) {
  179. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  180. meshArr01.push(obj);
  181. }
  182. });
  183. });
  184. fengchuang2.getObjectByName('chuang001')?.children.forEach((item: THREE.Object3D) => {
  185. item.children.forEach((obj: THREE.Mesh) => {
  186. if (obj.name && obj.name.startsWith('d__FCshanye')) {
  187. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  188. meshArr02.push(obj);
  189. }
  190. });
  191. });
  192. this.windowsActionArr.frontWindow = meshArr01;
  193. this.windowsActionArr.backWindow = meshArr02;
  194. }
  195. // // 初始化门的动画
  196. // this.mixers = new THREE.AnimationMixer(sdFcModal);
  197. // const frontDoor = sdFcModal?.animations[0];
  198. // const frontClipAction = this.mixers.clipAction(frontDoor, sdFcModal);
  199. // frontClipAction.clampWhenFinished = true;
  200. // frontClipAction.loop = THREE.LoopOnce;
  201. // this.clipActionArr.frontDoor = frontClipAction;
  202. // const backDoor = sdFcModal?.animations[1];
  203. // const backClipAction = this.mixers.clipAction(backDoor, sdFcModal);
  204. // backClipAction.clampWhenFinished = true;
  205. // backClipAction.loop = THREE.LoopOnce;
  206. // this.clipActionArr.backDoor = backClipAction;
  207. }
  208. /* 点击风窗,风窗全屏 */
  209. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  210. this.isLRAnimation = false;
  211. if (this.animationTimer) {
  212. clearTimeout(this.animationTimer);
  213. this.animationTimer = null;
  214. }
  215. // 判断是否点击到视频
  216. intersects.find((intersect) => {
  217. const mesh = intersect.object;
  218. return false;
  219. });
  220. }
  221. mouseUpModel() {
  222. // 10s后开始摆动
  223. if (!this.animationTimer && !this.isLRAnimation) {
  224. this.animationTimer = setTimeout(() => {
  225. this.isLRAnimation = true;
  226. }, 10000);
  227. }
  228. }
  229. play(rotationParam, flag) {
  230. if (this.windowsActionArr.frontWindow.length <= 0 || this.windowsActionArr.backWindow.length <= 0) {
  231. return;
  232. }
  233. if (flag === 1) {
  234. // 前风窗动画
  235. this.windowsActionArr.frontWindow.forEach((mesh) => {
  236. gsap.to(mesh.rotation, {
  237. y: THREE.MathUtils.degToRad(rotationParam.frontDeg1),
  238. duration: (1 / 9) * Math.abs(rotationParam.frontDeg1 - mesh.rotation.y),
  239. overwrite: true,
  240. });
  241. });
  242. } else if (flag === 2) {
  243. // 后风窗动画
  244. this.windowsActionArr.backWindow.forEach((mesh) => {
  245. gsap.to(mesh.rotation, {
  246. y: THREE.MathUtils.degToRad(rotationParam.backDeg1),
  247. duration: (1 / 9) * Math.abs(rotationParam.backDeg1 - mesh.rotation.y),
  248. overwrite: true,
  249. });
  250. });
  251. } else if (flag === 0) {
  252. ([...this.windowsActionArr.frontWindow, ...this.windowsActionArr.backWindow] as THREE.Mesh[]).forEach((mesh) => {
  253. gsap.to(mesh.rotation, {
  254. y: 0,
  255. overwrite: true,
  256. });
  257. });
  258. }
  259. }
  260. // // 播放动画
  261. // doorPlay(handlerState, timeScale = 0.01) {
  262. // if (this.clipActionArr.frontDoor && this.clipActionArr.backDoor) {
  263. // let handler = () => {};
  264. // switch (handlerState) {
  265. // case 1: // 打开前门
  266. // handler = () => {
  267. // this.clipActionArr.frontDoor.paused = true;
  268. // this.clipActionArr.frontDoor.reset();
  269. // this.clipActionArr.frontDoor.time = 1.2;
  270. // this.clipActionArr.frontDoor.timeScale = timeScale;
  271. // // this.clipActionArr.frontDoor.clampWhenFinished = true;
  272. // this.clipActionArr.frontDoor.play();
  273. // this.fmClock.start();
  274. // };
  275. // break;
  276. // case 2: // 关闭前门
  277. // handler = () => {
  278. // this.clipActionArr.frontDoor.paused = true;
  279. // this.clipActionArr.frontDoor.reset(); //
  280. // this.clipActionArr.frontDoor.time = 3;
  281. // this.clipActionArr.frontDoor.timeScale = -timeScale;
  282. // // this.clipActionArr.frontDoor.clampWhenFinished = true;
  283. // this.clipActionArr.frontDoor.play();
  284. // this.fmClock.start();
  285. // };
  286. // break;
  287. // case 3: // 打开后门
  288. // handler = () => {
  289. // this.clipActionArr.backDoor.paused = true;
  290. // this.clipActionArr.backDoor.reset();
  291. // this.clipActionArr.backDoor.time = 1.2;
  292. // this.clipActionArr.backDoor.timeScale = timeScale;
  293. // // this.clipActionArr.backDoor.clampWhenFinished = true;
  294. // this.clipActionArr.backDoor.play();
  295. // this.fmClock.start();
  296. // };
  297. // break;
  298. // case 4: // 关闭后门
  299. // handler = () => {
  300. // this.clipActionArr.backDoor.paused = true;
  301. // this.clipActionArr.backDoor.reset();
  302. // this.clipActionArr.backDoor.time = 3;
  303. // this.clipActionArr.backDoor.timeScale = -timeScale;
  304. // // this.clipActionArr.backDoor.clampWhenFinished = true;
  305. // this.clipActionArr.backDoor.play();
  306. // this.fmClock.start();
  307. // };
  308. // break;
  309. // // case 5: // 打开前后门
  310. // // handler = () => {
  311. // // this.clipActionArr.backDoor.paused = true;
  312. // // this.clipActionArr.frontDoor.paused = true;
  313. // // this.clipActionArr.frontDoor.reset();
  314. // // this.clipActionArr.frontDoor.time = 0;
  315. // // this.clipActionArr.frontDoor.timeScale = 0.01;
  316. // // this.clipActionArr.frontDoor.clampWhenFinished = true;
  317. // // this.clipActionArr.frontDoor.play();
  318. // // this.clipActionArr.backDoor.reset();
  319. // // this.clipActionArr.backDoor.time = 0;
  320. // // this.clipActionArr.backDoor.timeScale = 0.01;
  321. // // this.clipActionArr.backDoor.clampWhenFinished = true;
  322. // // this.clipActionArr.backDoor.play();
  323. // // this.frontClock.start();
  324. // // this.backClock.start();
  325. // // };
  326. // // break;
  327. // // case 6: // 关闭前后门
  328. // // handler = () => {
  329. // // debugger;
  330. // // this.clipActionArr.backDoor.paused = true;
  331. // // this.clipActionArr.frontDoor.paused = true;
  332. // // this.clipActionArr.frontDoor.reset();
  333. // // this.clipActionArr.frontDoor.time = 4;
  334. // // this.clipActionArr.frontDoor.timeScale = -0.01;
  335. // // this.clipActionArr.frontDoor.clampWhenFinished = true;
  336. // // this.clipActionArr.frontDoor.play();
  337. // // this.clipActionArr.backDoor.reset();
  338. // // this.clipActionArr.backDoor.time = 4;
  339. // // this.clipActionArr.backDoor.timeScale = -0.01;
  340. // // this.clipActionArr.backDoor.clampWhenFinished = true;
  341. // // this.clipActionArr.backDoor.play();
  342. // // this.frontClock.start();
  343. // // this.backClock.start();
  344. // // };
  345. // // break;
  346. // default:
  347. // }
  348. // handler();
  349. // // model.clock.start();
  350. // // const honglvdeng = group.getObjectByName('honglvdeng');
  351. // // const material = honglvdeng.material;
  352. // // setTimeout(() => {
  353. // // if (handlerState === 2 || handlerState === 4 || handlerState === 6) {
  354. // // material.color = new THREE.Color(0x00ff00);
  355. // // } else {
  356. // // material.color = new THREE.Color(0xff0000);
  357. // // }
  358. // // }, 1000);
  359. // }
  360. // }
  361. mountedThree(playerDom) {
  362. return new Promise((resolve) => {
  363. this.model.setGLTFModel(['sdFc-yjl'], this.group).then(() => {
  364. console.log(this.group);
  365. this.setModalPosition();
  366. this.initAnimation();
  367. resolve(null);
  368. });
  369. });
  370. }
  371. destroy() {
  372. this.model.clearGroup(this.group);
  373. this.windowsActionArr.frontWindow = [];
  374. this.windowsActionArr.backWindow = [];
  375. this.model = null;
  376. this.group = null;
  377. }
  378. }
  379. export default doubleWindowYjl;