shuangdaoFcHj.threejs.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. import * as THREE from 'three';
  2. import { getTextCanvas, renderVideo } from '/@/utils/threejs/util';
  3. import gsap from 'gsap';
  4. class doubleWindow {
  5. model;
  6. modelName = 'sdFc';
  7. group: THREE.Object3D = new THREE.Object3D();
  8. animationTimer;
  9. direction = 1;
  10. fontMixers1: THREE.AnimationMixer | null = null;
  11. fontMixers2: THREE.AnimationMixer | null = null;
  12. backMixers1: THREE.AnimationMixer | null = null;
  13. backMixers2: THREE.AnimationMixer | null = null;
  14. frontClipWindow1: THREE.AnimationClip | null = null;
  15. frontClipWindow2: THREE.AnimationClip | null = null;
  16. backClipWindow1: THREE.AnimationClip | null = null;
  17. backClipWindow2: THREE.AnimationClip | null = null;
  18. windowsActionArr = {
  19. frontWindow1: <THREE.AnimationAction | null>null,
  20. frontWindow2: <THREE.AnimationAction | null>null,
  21. backWindow1: <THREE.AnimationAction | null>null,
  22. backWindow2: <THREE.AnimationAction | null>null,
  23. };
  24. constructor(model) {
  25. this.model = model;
  26. // this.group.name = 'ddFc';
  27. }
  28. // // 重置摄像头
  29. // const resetCamera = () => {
  30. // this.model.camera.position.set(30.328, 58.993, 148.315);
  31. // this.model.camera.rotation.set(-27.88, 14.35, 7.47);
  32. // this.model.orbitControls?.update();
  33. // this.model.camera.updateProjectionMatrix();
  34. // };
  35. addLight = () => {};
  36. // 设置模型位置
  37. setModalPosition() {
  38. this.group?.scale.set(22, 22, 22);
  39. this.group?.position.set(-35, 25, 15);
  40. }
  41. addMonitorText(selectData) {
  42. if (!this.group) {
  43. return;
  44. }
  45. const screenDownText = VENT_PARAM['modalText']
  46. ? VENT_PARAM['modalText']
  47. : History_Type['type'] == 'remote'
  48. ? `国能神东煤炭集团监制`
  49. : '煤炭科学技术研究院有限公司研制';
  50. const screenDownTextX = 125 - (screenDownText.length - 10) * 10;
  51. const textArr = [
  52. {
  53. text: `远程定量调节自动风窗`,
  54. font: 'normal 30px Arial',
  55. color: '#009900',
  56. strokeStyle: '#002200',
  57. x: 120,
  58. y: 90,
  59. },
  60. {
  61. text: `${selectData.OpenDegree1 ? '前窗开度值(°)' : selectData.forntArea ? '前窗过风面积(㎡)' : '前窗过风面积(㎡)'}:`,
  62. font: 'normal 30px Arial',
  63. color: '#009900',
  64. strokeStyle: '#002200',
  65. x: 5,
  66. y: 145,
  67. },
  68. {
  69. text: selectData.OpenDegree1
  70. ? Number(`${selectData.OpenDegree1}`).toFixed(2)
  71. : selectData.forntArea
  72. ? Number(`${selectData.forntArea}`).toFixed(2)
  73. : '-',
  74. font: 'normal 30px Arial',
  75. color: '#009900',
  76. strokeStyle: '#002200',
  77. x: 330,
  78. y: 145,
  79. },
  80. {
  81. text: `${selectData.OpenDegree2 ? '后窗开度值(°)' : selectData.forntArea ? '后窗过风面积(㎡)' : '后窗过风面积(㎡)'}:`,
  82. font: 'normal 30px Arial',
  83. color: '#009900',
  84. strokeStyle: '#002200',
  85. x: 5,
  86. y: 200,
  87. },
  88. {
  89. text: selectData.OpenDegree2
  90. ? Number(`${selectData.OpenDegree2}`).toFixed(2)
  91. : selectData.rearArea
  92. ? Number(`${selectData.rearArea}`).toFixed(2)
  93. : '-',
  94. font: 'normal 30px Arial',
  95. color: '#009900',
  96. strokeStyle: '#002200',
  97. x: 330,
  98. y: 200,
  99. },
  100. {
  101. text: `${selectData.frontRearDP ? '风窗压差(Pa)' : selectData.windSpeed ? '风速(m/s)' : '通信状态'}:`,
  102. font: 'normal 30px Arial',
  103. color: '#009900',
  104. strokeStyle: '#002200',
  105. x: 5,
  106. y: 256,
  107. },
  108. {
  109. text: `${
  110. selectData.frontRearDP
  111. ? selectData.frontRearDP
  112. : selectData.windSpeed
  113. ? selectData.windSpeed
  114. : selectData.netStatus == '0'
  115. ? '断开'
  116. : '连接'
  117. }`,
  118. font: 'normal 30px Arial',
  119. color: '#009900',
  120. strokeStyle: '#002200',
  121. x: 330,
  122. y: 256,
  123. },
  124. {
  125. text: screenDownText,
  126. font: 'normal 28px Arial',
  127. color: '#009900',
  128. strokeStyle: '#002200',
  129. x: screenDownTextX,
  130. y: 303,
  131. },
  132. ];
  133. getTextCanvas(726, 546, textArr, '').then((canvas: HTMLCanvasElement) => {
  134. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  135. const textMaterial = new THREE.MeshBasicMaterial({
  136. // 关于材质并未讲解 实操即可熟悉 这里是漫反射类似纸张的材质,对应的就有高光类似金属的材质.
  137. map: textMap, // 设置纹理贴图
  138. transparent: true,
  139. side: THREE.DoubleSide, // 这里是双面渲染的意思
  140. });
  141. textMap.dispose();
  142. textMaterial.blending = THREE.CustomBlending;
  143. const monitorPlane = this.group?.getObjectByName('monitorText');
  144. if (monitorPlane) {
  145. monitorPlane.material = textMaterial;
  146. } else {
  147. const planeGeometry = new THREE.PlaneGeometry(726, 546); // 平面3维几何体PlaneGeometry
  148. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  149. planeMesh.name = 'monitorText';
  150. planeMesh.scale.set(0.002, 0.002, 0.002);
  151. planeMesh.position.set(4.19, 0.448, -0.27);
  152. this.group?.add(planeMesh);
  153. }
  154. });
  155. }
  156. /* 提取风门序列帧,初始化前后门动画 */
  157. initAnimation() {
  158. const fcGroup = this.group?.getObjectByName('ShuangDaoTiaoJieFengChuang_1');
  159. if (fcGroup) {
  160. const tracks = fcGroup.animations[0].tracks;
  161. const fontTracks1: any[] = [],
  162. fontTracks2: any[] = [],
  163. backTracks1: any[] = [],
  164. backTracks2: any[] = [];
  165. for (let i = 0; i < tracks.length; i++) {
  166. const track = tracks[i];
  167. if (track.name.startsWith('juan_1')) {
  168. fontTracks1.push(track);
  169. } else if (track.name.startsWith('juan_2')) {
  170. fontTracks2.push(track);
  171. } else if (track.name.startsWith('juan_3')) {
  172. backTracks1.push(track);
  173. } else if (track.name.startsWith('juan_4')) {
  174. backTracks2.push(track);
  175. }
  176. }
  177. const fontGroup1 = fcGroup.getObjectByName('FengChuang_1')?.getObjectByName('ske_1');
  178. const fontGroup2 = fcGroup.getObjectByName('FengChuang_1')?.getObjectByName('ske_2');
  179. const backGroup1 = fcGroup.getObjectByName('FengChuang_2')?.getObjectByName('ske_3');
  180. const backGroup2 = fcGroup.getObjectByName('FengChuang_2')?.getObjectByName('ske_4');
  181. if (fontGroup1 && fontGroup2 && backGroup1 && backGroup2) {
  182. this.fontMixers1 = new THREE.AnimationMixer(fontGroup1);
  183. this.fontMixers2 = new THREE.AnimationMixer(fontGroup1);
  184. this.backMixers1 = new THREE.AnimationMixer(backGroup1);
  185. this.backMixers2 = new THREE.AnimationMixer(backGroup2);
  186. this.frontClipWindow1 = new THREE.AnimationClip('frontWindow1', 22, fontTracks1);
  187. const frontClipAction1 = this.fontMixers1.clipAction(this.frontClipWindow1, fontGroup1);
  188. frontClipAction1.clampWhenFinished = true;
  189. frontClipAction1.loop = THREE.LoopOnce;
  190. this.windowsActionArr.frontWindow1 = frontClipAction1;
  191. this.frontClipWindow2 = new THREE.AnimationClip('frontWindow2', 22, fontTracks2);
  192. const frontClipAction2 = this.fontMixers2.clipAction(this.frontClipWindow2, fontGroup2);
  193. frontClipAction2.clampWhenFinished = true;
  194. frontClipAction2.loop = THREE.LoopOnce;
  195. this.windowsActionArr.frontWindow1 = frontClipAction2;
  196. this.backClipWindow1 = new THREE.AnimationClip('backWindow1', 22, backTracks1);
  197. const backClipAction1 = this.backMixers1.clipAction(this.backClipWindow1, backGroup1);
  198. backClipAction1.clampWhenFinished = true;
  199. backClipAction1.loop = THREE.LoopOnce;
  200. this.windowsActionArr.backWindow1 = backClipAction1;
  201. this.backClipWindow2 = new THREE.AnimationClip('backWindow1', 22, backTracks2);
  202. const backClipAction2 = this.backMixers2.clipAction(this.backClipWindow2, backGroup2);
  203. backClipAction2.clampWhenFinished = true;
  204. backClipAction2.loop = THREE.LoopOnce;
  205. this.windowsActionArr.backWindow1 = backClipAction2;
  206. }
  207. }
  208. }
  209. play(handlerState, timeScale = 0.01) {
  210. let handler = () => {};
  211. if (
  212. this.windowsActionArr.frontWindow1 &&
  213. this.windowsActionArr.frontWindow2 &&
  214. this.windowsActionArr.backWindow1 &&
  215. this.windowsActionArr.backWindow2
  216. ) {
  217. switch (handlerState) {
  218. case 1: // 窗1开启动画
  219. handler = () => {
  220. const frontWindow1 = this.windowsActionArr.frontWindow1;
  221. if (frontWindow1 && this.frontClipWindow1) {
  222. frontWindow1.paused = true;
  223. frontWindow1.reset();
  224. this.frontClipWindow1.duration = 0;
  225. frontWindow1.time = 1.8;
  226. frontWindow1.timeScale = timeScale;
  227. frontWindow1.play();
  228. }
  229. };
  230. break;
  231. case 2: // 窗2开启动画
  232. handler = () => {
  233. const frontWindow2 = this.windowsActionArr.frontWindow2;
  234. if (frontWindow2) {
  235. frontWindow2.paused = true;
  236. frontWindow2.reset();
  237. frontWindow2.time = 4;
  238. frontWindow2.timeScale = -timeScale;
  239. frontWindow2.play();
  240. }
  241. };
  242. break;
  243. case 3: // 窗3开启动画
  244. handler = () => {
  245. const backWindow1 = this.windowsActionArr.backWindow1;
  246. if (backWindow1) {
  247. backWindow1.paused = true;
  248. backWindow1.reset();
  249. backWindow1.time = 1.2;
  250. backWindow1.timeScale = timeScale;
  251. backWindow1.play();
  252. }
  253. };
  254. break;
  255. case 4: // 窗4开启动画
  256. handler = () => {
  257. const backWindow2 = this.windowsActionArr.backWindow2;
  258. if (backWindow2) {
  259. backWindow2.paused = true;
  260. backWindow2.reset();
  261. backWindow2.time = 4;
  262. backWindow2.timeScale = -timeScale;
  263. backWindow2.play();
  264. }
  265. };
  266. break;
  267. default:
  268. }
  269. handler();
  270. }
  271. }
  272. /* 点击风窗,风窗全屏 */
  273. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  274. if (this.animationTimer) {
  275. clearTimeout(this.animationTimer);
  276. this.animationTimer = null;
  277. }
  278. // 判断是否点击到视频
  279. intersects.find((intersect) => {
  280. const mesh = intersect.object;
  281. if (mesh.name === 'player1') {
  282. if (new Date().getTime() - this.playerStartClickTime1 < 400) {
  283. // 双击,视频放大
  284. if (this.player1) {
  285. this.player1.requestFullscreen();
  286. }
  287. }
  288. this.playerStartClickTime1 = new Date().getTime();
  289. return true;
  290. }
  291. return false;
  292. });
  293. }
  294. mouseUpModel() {}
  295. /* 风门动画 */
  296. render() {
  297. if (!this.model) {
  298. return;
  299. }
  300. }
  301. mountedThree(playerDom) {
  302. return new Promise((resolve) => {
  303. this.model.setGLTFModel('sdFc-jz').then((gltf) => {
  304. console.log('gltf', gltf);
  305. const fcModal = gltf[0];
  306. fcModal.name = 'sdFc';
  307. this.group?.add(fcModal);
  308. this.setModalPosition();
  309. this.initAnimation();
  310. this.addLight();
  311. resolve(null);
  312. });
  313. });
  314. }
  315. destroy() {
  316. this.model.clearGroup(this.group);
  317. this.windowsActionArr.frontWindow = undefined;
  318. this.model = null;
  319. this.group = null;
  320. }
  321. }
  322. export default doubleWindow;