gate.threejs.tj.ssl.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. import * as THREE from 'three';
  2. import { getTextCanvas, renderVideo } from '/@/utils/threejs/util';
  3. import gsap from 'gsap';
  4. class ddFmSsl {
  5. model;
  6. modelName = 'fm-fc-ssl';
  7. group: THREE.Object3D = new THREE.Object3D();
  8. animationTimer;
  9. isLRAnimation = true;
  10. direction = 1;
  11. windowsActionArr = {
  12. frontWindow: [],
  13. backWindow: [],
  14. };
  15. player1;
  16. player2;
  17. playerStartClickTime1 = new Date().getTime();
  18. mixers: THREE.AnimationMixer | undefined;
  19. frontClipAction;
  20. fmClock = new THREE.Clock();
  21. constructor(model) {
  22. this.model = model;
  23. this.group.name = 'fm-fc-ssl';
  24. }
  25. addLight = () => {
  26. if (!this.group || !this.group) return;
  27. const directionalLight = new THREE.DirectionalLight(0xffffff, 2);
  28. directionalLight.position.set(-437, 61, 559);
  29. this.group.add(directionalLight);
  30. const pointLight2 = new THREE.PointLight(0xffffff, 1, 150);
  31. pointLight2.position.set(-101, 34, 16);
  32. pointLight2.shadow.bias = 0.05;
  33. this.group.add(pointLight2);
  34. const pointLight3 = new THREE.PointLight(0xffffff, 1, 150);
  35. pointLight3.position.set(19, 25, -7);
  36. pointLight3.shadow.bias = 0.05;
  37. this.group.add(pointLight3);
  38. const pointLight6 = new THREE.PointLight(0xffffff, 1, 300);
  39. pointLight6.position.set(51, 51, 9);
  40. pointLight6.shadow.bias = 0.05;
  41. this.group.add(pointLight6);
  42. };
  43. // 设置模型位置
  44. setModalPosition() {
  45. this.group?.scale.set(22, 22, 22);
  46. this.group?.position.set(-35, 25, 15);
  47. }
  48. addMonitorText(selectData) {
  49. if (!this.group) {
  50. return;
  51. }
  52. const screenDownText = VENT_PARAM['modalText']
  53. ? VENT_PARAM['modalText']
  54. : History_Type['type'] == 'remote'
  55. ? `国能神东煤炭集团监制`
  56. : '煤科通安(北京)智控科技有限公司研制';
  57. const screenDownTextX = 90 - (screenDownText.length - 10) * 7;
  58. const textArr = [
  59. {
  60. text: `远程定量调节自动风门`,
  61. font: 'normal 30px Arial',
  62. color: '#009900',
  63. strokeStyle: '#002200',
  64. x: 100,
  65. y: 95,
  66. },
  67. {
  68. text: `${selectData.OpenDegree ? '开度值(°)' : selectData.frontArea ? '过风面积(㎡)' : '过风面积(㎡)'}:`,
  69. font: 'normal 30px Arial',
  70. color: '#009900',
  71. strokeStyle: '#002200',
  72. x: 5,
  73. y: 145,
  74. },
  75. {
  76. text: selectData.OpenDegree
  77. ? Number(`${selectData.OpenDegree}`).toFixed(2)
  78. : selectData.frontArea
  79. ? Number(`${selectData.frontArea}`).toFixed(2)
  80. : '-',
  81. font: 'normal 30px Arial',
  82. color: '#009900',
  83. strokeStyle: '#002200',
  84. x: 330,
  85. y: 145,
  86. },
  87. {
  88. text: `${selectData.frontRearDP ? '风窗压差(Pa)' : selectData.windSpeed ? '风速(m/s)' : '通信状态:'}:`,
  89. font: 'normal 30px Arial',
  90. color: '#009900',
  91. strokeStyle: '#002200',
  92. x: 5,
  93. y: 200,
  94. },
  95. {
  96. text: `${
  97. selectData.frontRearDP
  98. ? selectData.frontRearDP
  99. : selectData.windSpeed
  100. ? selectData.windSpeed
  101. : selectData.netStatus == '0'
  102. ? '断开'
  103. : '连接'
  104. }`,
  105. font: 'normal 30px Arial',
  106. color: '#009900',
  107. strokeStyle: '#002200',
  108. x: 330,
  109. y: 200,
  110. },
  111. {
  112. text: `${selectData.fWindowM3 ? '过风量(m³/min)' : '风窗道数'}: `,
  113. font: 'normal 30px Arial',
  114. color: '#009900',
  115. strokeStyle: '#002200',
  116. x: 5,
  117. y: 250,
  118. },
  119. {
  120. text: `${selectData.fWindowM3 ? selectData.fWindowM3 : selectData.nwindownum}`,
  121. font: 'normal 30px Arial',
  122. color: '#009900',
  123. strokeStyle: '#002200',
  124. x: 330,
  125. y: 250,
  126. },
  127. {
  128. text: screenDownText,
  129. font: 'normal 28px Arial',
  130. color: '#009900',
  131. strokeStyle: '#002200',
  132. x: screenDownTextX,
  133. y: 300,
  134. },
  135. ];
  136. getTextCanvas(750, 546, textArr, '').then((canvas: HTMLCanvasElement) => {
  137. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  138. const textMaterial = new THREE.MeshBasicMaterial({
  139. // 关于材质并未讲解 实操即可熟悉 这里是漫反射类似纸张的材质,对应的就有高光类似金属的材质.
  140. map: textMap, // 设置纹理贴图
  141. transparent: true,
  142. side: THREE.DoubleSide, // 这里是双面渲染的意思
  143. });
  144. textMap.dispose();
  145. textMaterial.blending = THREE.CustomBlending;
  146. const monitorPlane = this.group?.getObjectByName('monitorText');
  147. if (monitorPlane) {
  148. monitorPlane.material = textMaterial;
  149. } else {
  150. const planeGeometry = new THREE.PlaneGeometry(526, 346); // 平面3维几何体PlaneGeometry
  151. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  152. planeMesh.name = 'monitorText';
  153. planeMesh.scale.set(0.003, 0.0034, 0.004);
  154. planeMesh.position.set(4.25, 0.41, -0.27);
  155. this.group?.add(planeMesh);
  156. }
  157. });
  158. }
  159. /* 提取风门序列帧,初始化前后门动画 */
  160. initAnimation() {
  161. const modalGroup = this.group?.getObjectByName('fm-fc-ssl');
  162. // 初始化窗得动画
  163. const meshArr01: THREE.Object3D[] = [];
  164. const meshArr02: THREE.Object3D[] = [];
  165. const fmGroup = modalGroup?.getObjectByName('FengMen_DaiChuang');
  166. const fcGroup1 = fmGroup?.getObjectByName('Men_1');
  167. const fcGroup2 = fmGroup?.getObjectByName('Men_2');
  168. if (fcGroup1 && fcGroup2) {
  169. fcGroup1.getObjectByName('shanye_1')?.children.filter((obj) => {
  170. if (obj.name.includes('shanye_1')) {
  171. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  172. meshArr01.push(obj);
  173. }
  174. });
  175. fcGroup2.getObjectByName('shanye_3')?.children.filter((obj) => {
  176. if (obj.name.includes('shanye_1')) {
  177. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  178. meshArr02.push(obj);
  179. }
  180. });
  181. this.windowsActionArr.frontWindow = [...meshArr01];
  182. this.windowsActionArr.backWindow = [...meshArr02];
  183. }
  184. // 初始化门的动画
  185. if (modalGroup && fmGroup) {
  186. const tracks = modalGroup.animations[0].tracks;
  187. const fontTracks: any[] = [];
  188. for (let i = 0; i < tracks.length; i++) {
  189. const track = tracks[i];
  190. if (track.name.startsWith('Men')) {
  191. fontTracks.push(track);
  192. }
  193. }
  194. this.mixers = new THREE.AnimationMixer(fmGroup);
  195. const frontDoor = new THREE.AnimationClip('frontDoor', 15, fontTracks);
  196. this.frontClipAction = this.mixers.clipAction(frontDoor, fmGroup);
  197. this.frontClipAction.clampWhenFinished = true;
  198. // this.frontClipAction.reset();
  199. // this.frontClipAction.time = 0;
  200. // this.frontClipAction.timeScale = 1;
  201. // this.frontClipAction.clampWhenFinished = true;
  202. // this.frontClipAction.loop = THREE.LoopOnce;
  203. // this.frontClipAction.play();
  204. }
  205. }
  206. // 播放动画
  207. play(handlerState, timeScale = 0.01) {
  208. let handler = () => {};
  209. if (this.frontClipAction) {
  210. switch (handlerState) {
  211. case 1: // 打开前门
  212. handler = () => {
  213. this.frontClipAction.paused = true;
  214. this.frontClipAction.reset();
  215. this.frontClipAction.time = 0;
  216. this.frontClipAction.timeScale = timeScale;
  217. this.frontClipAction.play();
  218. this.fmClock.start();
  219. };
  220. break;
  221. case 2: // 关闭前门
  222. handler = () => {
  223. this.frontClipAction.paused = true;
  224. this.frontClipAction.reset(); //
  225. this.frontClipAction.time = 2.5;
  226. this.frontClipAction.timeScale = -timeScale;
  227. this.frontClipAction.play();
  228. this.fmClock.start();
  229. };
  230. break;
  231. default:
  232. }
  233. handler();
  234. }
  235. }
  236. playWindow(rotationParam, flag) {
  237. if (!this.windowsActionArr.frontWindow) {
  238. return;
  239. }
  240. if (flag === 1) {
  241. // 前风窗动画
  242. this.windowsActionArr.frontWindow.forEach((mesh: THREE.Mesh) => {
  243. gsap.to(mesh.rotation, {
  244. z: THREE.MathUtils.degToRad(rotationParam.frontDeg1),
  245. duration: (1 / 9) * Math.abs(rotationParam.frontDeg1 - mesh.rotation.z),
  246. overwrite: true,
  247. });
  248. });
  249. } else if (flag === 2) {
  250. // 后风窗动画
  251. this.windowsActionArr.backWindow.forEach((mesh: THREE.Mesh) => {
  252. gsap.to(mesh.rotation, {
  253. z: THREE.MathUtils.degToRad(rotationParam.backDeg1),
  254. duration: (1 / 9) * Math.abs(rotationParam.backDeg1 - mesh.rotation.z),
  255. overwrite: true,
  256. });
  257. });
  258. } else if (flag === 0) {
  259. ([...this.windowsActionArr.frontWindow] as THREE.Mesh[]).forEach((mesh) => {
  260. gsap.to(mesh.rotation, {
  261. z: THREE.MathUtils.degToRad(90),
  262. overwrite: true,
  263. });
  264. });
  265. }
  266. }
  267. /* 点击风窗,风窗全屏 */
  268. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  269. if (this.animationTimer) {
  270. clearTimeout(this.animationTimer);
  271. this.animationTimer = null;
  272. }
  273. // 判断是否点击到视频
  274. intersects.find((intersect) => {
  275. const mesh = intersect.object;
  276. if (mesh.name === 'player1') {
  277. if (new Date().getTime() - this.playerStartClickTime1 < 400) {
  278. // 双击,视频放大
  279. if (this.player1) {
  280. this.player1.requestFullscreen();
  281. }
  282. }
  283. this.playerStartClickTime1 = new Date().getTime();
  284. return true;
  285. }
  286. return false;
  287. });
  288. }
  289. mouseUpModel() {}
  290. /* 风门动画 */
  291. render() {
  292. if (!this.model) {
  293. return;
  294. }
  295. if (this.mixers && this.fmClock.running) {
  296. this.mixers.update(2);
  297. }
  298. }
  299. mountedThree() {
  300. return new Promise((resolve) => {
  301. this.model.setGLTFModel('fm-fc-ssl').then((gltf) => {
  302. this.group = gltf[0];
  303. this.group.name = 'fm-fc-ssl';
  304. this.setModalPosition();
  305. this.initAnimation();
  306. resolve(null);
  307. });
  308. });
  309. }
  310. destroy() {
  311. if (this.mixers) {
  312. const modalGroup = this.group?.getObjectByName('fm-fc-ssl');
  313. const fmGroup = modalGroup?.getObjectByName('FengMen_ShouDong_1')?.getObjectByName('FengMen_1');
  314. this.mixers.uncacheClip(this.frontClipAction.getClip());
  315. this.mixers.uncacheAction(this.frontClipAction.getClip(), fmGroup);
  316. if (fmGroup) this.mixers.uncacheRoot(fmGroup);
  317. if (this.model.animations[0]) this.model.animations[0].tracks = [];
  318. }
  319. this.model.clearGroup(this.group);
  320. this.frontClipAction = undefined;
  321. this.model = null;
  322. this.group = null;
  323. }
  324. }
  325. export default ddFmSsl;