shuangdaoFcYjl.threejs.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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.rearArea
  84. ? Number(`${selectData.rearArea}`).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. }
  154. /* 提取风门序列帧,初始化前后门动画 */
  155. initAnimation() {
  156. const sdFcModal = this.group.getObjectByName('sdFc-yjl');
  157. if (!sdFcModal) return;
  158. // 初始化窗的动画
  159. const fengchuang1 = sdFcModal?.getObjectByName('FengChuang');
  160. const fengchuang2 = sdFcModal?.getObjectByName('FengChuang1');
  161. const meshArr01: THREE.Mesh[] = [];
  162. const meshArr02: THREE.Mesh[] = [];
  163. if (fengchuang1 && fengchuang2) {
  164. fengchuang1.getObjectByName('chuang')?.children.forEach((item: THREE.Object3D) => {
  165. item.children.forEach((obj: THREE.Mesh) => {
  166. if (obj.name && obj.name.startsWith('shanye')) {
  167. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  168. meshArr01.push(obj);
  169. }
  170. });
  171. });
  172. fengchuang2.getObjectByName('chuang001')?.children.forEach((item: THREE.Object3D) => {
  173. item.children.forEach((obj: THREE.Mesh) => {
  174. if (obj.name && obj.name.startsWith('d__FCshanye')) {
  175. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  176. meshArr02.push(obj);
  177. }
  178. });
  179. });
  180. this.windowsActionArr.frontWindow = meshArr01;
  181. this.windowsActionArr.backWindow = meshArr02;
  182. }
  183. // // 初始化门的动画
  184. // this.mixers = new THREE.AnimationMixer(sdFcModal);
  185. // const frontDoor = sdFcModal?.animations[0];
  186. // const frontClipAction = this.mixers.clipAction(frontDoor, sdFcModal);
  187. // frontClipAction.clampWhenFinished = true;
  188. // frontClipAction.loop = THREE.LoopOnce;
  189. // this.clipActionArr.frontDoor = frontClipAction;
  190. // const backDoor = sdFcModal?.animations[1];
  191. // const backClipAction = this.mixers.clipAction(backDoor, sdFcModal);
  192. // backClipAction.clampWhenFinished = true;
  193. // backClipAction.loop = THREE.LoopOnce;
  194. // this.clipActionArr.backDoor = backClipAction;
  195. }
  196. /* 点击风窗,风窗全屏 */
  197. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  198. if (this.animationTimer) {
  199. clearTimeout(this.animationTimer);
  200. this.animationTimer = null;
  201. }
  202. // 判断是否点击到视频
  203. intersects.find((intersect) => {
  204. const mesh = intersect.object;
  205. return false;
  206. });
  207. }
  208. mouseUpModel() {}
  209. play(rotationParam, flag) {
  210. if (this.windowsActionArr.frontWindow.length <= 0 || this.windowsActionArr.backWindow.length <= 0) {
  211. return;
  212. }
  213. if (flag === 1) {
  214. // 前风窗动画
  215. this.windowsActionArr.frontWindow.forEach((mesh) => {
  216. gsap.to(mesh.rotation, {
  217. y: THREE.MathUtils.degToRad(rotationParam.frontDeg1),
  218. duration: (1 / 9) * Math.abs(rotationParam.frontDeg1 - mesh.rotation.y),
  219. overwrite: true,
  220. });
  221. });
  222. } else if (flag === 2) {
  223. // 后风窗动画
  224. this.windowsActionArr.backWindow.forEach((mesh) => {
  225. gsap.to(mesh.rotation, {
  226. y: THREE.MathUtils.degToRad(rotationParam.backDeg1),
  227. duration: (1 / 9) * Math.abs(rotationParam.backDeg1 - mesh.rotation.y),
  228. overwrite: true,
  229. });
  230. });
  231. } else if (flag === 0) {
  232. ([...this.windowsActionArr.frontWindow, ...this.windowsActionArr.backWindow] as THREE.Mesh[]).forEach((mesh) => {
  233. gsap.to(mesh.rotation, {
  234. y: 0,
  235. overwrite: true,
  236. });
  237. });
  238. }
  239. }
  240. // // 播放动画
  241. // doorPlay(handlerState, timeScale = 0.01) {
  242. // if (this.clipActionArr.frontDoor && this.clipActionArr.backDoor) {
  243. // let handler = () => {};
  244. // switch (handlerState) {
  245. // case 1: // 打开前门
  246. // handler = () => {
  247. // this.clipActionArr.frontDoor.paused = true;
  248. // this.clipActionArr.frontDoor.reset();
  249. // this.clipActionArr.frontDoor.time = 1.2;
  250. // this.clipActionArr.frontDoor.timeScale = timeScale;
  251. // // this.clipActionArr.frontDoor.clampWhenFinished = true;
  252. // this.clipActionArr.frontDoor.play();
  253. // this.fmClock.start();
  254. // };
  255. // break;
  256. // case 2: // 关闭前门
  257. // handler = () => {
  258. // this.clipActionArr.frontDoor.paused = true;
  259. // this.clipActionArr.frontDoor.reset(); //
  260. // this.clipActionArr.frontDoor.time = 3;
  261. // this.clipActionArr.frontDoor.timeScale = -timeScale;
  262. // // this.clipActionArr.frontDoor.clampWhenFinished = true;
  263. // this.clipActionArr.frontDoor.play();
  264. // this.fmClock.start();
  265. // };
  266. // break;
  267. // case 3: // 打开后门
  268. // handler = () => {
  269. // this.clipActionArr.backDoor.paused = true;
  270. // this.clipActionArr.backDoor.reset();
  271. // this.clipActionArr.backDoor.time = 1.2;
  272. // this.clipActionArr.backDoor.timeScale = timeScale;
  273. // // this.clipActionArr.backDoor.clampWhenFinished = true;
  274. // this.clipActionArr.backDoor.play();
  275. // this.fmClock.start();
  276. // };
  277. // break;
  278. // case 4: // 关闭后门
  279. // handler = () => {
  280. // this.clipActionArr.backDoor.paused = true;
  281. // this.clipActionArr.backDoor.reset();
  282. // this.clipActionArr.backDoor.time = 3;
  283. // this.clipActionArr.backDoor.timeScale = -timeScale;
  284. // // this.clipActionArr.backDoor.clampWhenFinished = true;
  285. // this.clipActionArr.backDoor.play();
  286. // this.fmClock.start();
  287. // };
  288. // break;
  289. // // case 5: // 打开前后门
  290. // // handler = () => {
  291. // // this.clipActionArr.backDoor.paused = true;
  292. // // this.clipActionArr.frontDoor.paused = true;
  293. // // this.clipActionArr.frontDoor.reset();
  294. // // this.clipActionArr.frontDoor.time = 0;
  295. // // this.clipActionArr.frontDoor.timeScale = 0.01;
  296. // // this.clipActionArr.frontDoor.clampWhenFinished = true;
  297. // // this.clipActionArr.frontDoor.play();
  298. // // this.clipActionArr.backDoor.reset();
  299. // // this.clipActionArr.backDoor.time = 0;
  300. // // this.clipActionArr.backDoor.timeScale = 0.01;
  301. // // this.clipActionArr.backDoor.clampWhenFinished = true;
  302. // // this.clipActionArr.backDoor.play();
  303. // // this.frontClock.start();
  304. // // this.backClock.start();
  305. // // };
  306. // // break;
  307. // // case 6: // 关闭前后门
  308. // // handler = () => {
  309. // // debugger;
  310. // // this.clipActionArr.backDoor.paused = true;
  311. // // this.clipActionArr.frontDoor.paused = true;
  312. // // this.clipActionArr.frontDoor.reset();
  313. // // this.clipActionArr.frontDoor.time = 4;
  314. // // this.clipActionArr.frontDoor.timeScale = -0.01;
  315. // // this.clipActionArr.frontDoor.clampWhenFinished = true;
  316. // // this.clipActionArr.frontDoor.play();
  317. // // this.clipActionArr.backDoor.reset();
  318. // // this.clipActionArr.backDoor.time = 4;
  319. // // this.clipActionArr.backDoor.timeScale = -0.01;
  320. // // this.clipActionArr.backDoor.clampWhenFinished = true;
  321. // // this.clipActionArr.backDoor.play();
  322. // // this.frontClock.start();
  323. // // this.backClock.start();
  324. // // };
  325. // // break;
  326. // default:
  327. // }
  328. // handler();
  329. // // model.clock.start();
  330. // // const honglvdeng = group.getObjectByName('honglvdeng');
  331. // // const material = honglvdeng.material;
  332. // // setTimeout(() => {
  333. // // if (handlerState === 2 || handlerState === 4 || handlerState === 6) {
  334. // // material.color = new THREE.Color(0x00ff00);
  335. // // } else {
  336. // // material.color = new THREE.Color(0xff0000);
  337. // // }
  338. // // }, 1000);
  339. // }
  340. // }
  341. mountedThree(playerDom) {
  342. return new Promise((resolve) => {
  343. this.model.setGLTFModel(['sdFc-yjl'], this.group).then(() => {
  344. console.log(this.group);
  345. this.setModalPosition();
  346. this.initAnimation();
  347. resolve(null);
  348. });
  349. });
  350. }
  351. destroy() {
  352. this.model.clearGroup(this.group);
  353. this.windowsActionArr.frontWindow = [];
  354. this.windowsActionArr.backWindow = [];
  355. this.model = null;
  356. this.group = null;
  357. }
  358. }
  359. export default doubleWindowYjl;