dandaoFcBd3.threejs.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. import * as THREE from 'three';
  2. import { getTextCanvas, renderVideo } from '/@/utils/threejs/util';
  3. import gsap from 'gsap';
  4. class ddFc_7 {
  5. model;
  6. modelName = 'ddFcGroup';
  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 = 'ddFcGroup';
  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('ddFc-bd3');
  162. // 初始化窗得动画
  163. const meshArr01: THREE.Object3D[] = [];
  164. const meshArr02: THREE.Object3D[] = [];
  165. const fmGroup = modalGroup?.getObjectByName('FengMen_ShouDong_1')?.getObjectByName('FengMen_1');
  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. fcGroup1.getObjectByName('shanye_2')?.children.filter((obj) => {
  176. if (obj.name.includes('shanye_2')) {
  177. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  178. meshArr01.push(obj);
  179. }
  180. });
  181. fcGroup2.children.filter((obj) => {
  182. if (obj.name.includes('shanye_3')) {
  183. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  184. meshArr02.push(obj);
  185. }
  186. });
  187. fcGroup2.getObjectByName('shanye_4')?.children.filter((obj) => {
  188. if (obj.name.includes('shanye_4')) {
  189. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  190. meshArr02.push(obj);
  191. }
  192. });
  193. this.windowsActionArr.frontWindow = [...meshArr01];
  194. this.windowsActionArr.backWindow = [...meshArr02];
  195. }
  196. // 初始化门的动画
  197. if (modalGroup && fmGroup) {
  198. const tracks = modalGroup.animations[0].tracks;
  199. const fontTracks: any[] = [];
  200. for (let i = 0; i < tracks.length; i++) {
  201. const track = tracks[i];
  202. if (track.name.startsWith('Men')) {
  203. fontTracks.push(track);
  204. }
  205. }
  206. this.mixers = new THREE.AnimationMixer(fmGroup);
  207. const frontDoor = new THREE.AnimationClip('frontDoor', 15, fontTracks);
  208. this.frontClipAction = this.mixers.clipAction(frontDoor, fmGroup);
  209. this.frontClipAction.clampWhenFinished = true;
  210. // this.frontClipAction.reset();
  211. // this.frontClipAction.time = 0;
  212. // this.frontClipAction.timeScale = 1;
  213. // this.frontClipAction.clampWhenFinished = true;
  214. // this.frontClipAction.loop = THREE.LoopOnce;
  215. // this.frontClipAction.play();
  216. }
  217. }
  218. // 播放动画
  219. play(handlerState, timeScale = 0.01) {
  220. let handler = () => {};
  221. if (this.frontClipAction) {
  222. switch (handlerState) {
  223. case 1: // 打开前门
  224. handler = () => {
  225. this.frontClipAction.paused = true;
  226. this.frontClipAction.reset();
  227. this.frontClipAction.time = 0;
  228. this.frontClipAction.timeScale = timeScale;
  229. this.frontClipAction.play();
  230. this.fmClock.start();
  231. };
  232. break;
  233. case 2: // 关闭前门
  234. handler = () => {
  235. this.frontClipAction.paused = true;
  236. this.frontClipAction.reset(); //
  237. this.frontClipAction.time = 2.5;
  238. this.frontClipAction.timeScale = -timeScale;
  239. this.frontClipAction.play();
  240. this.fmClock.start();
  241. };
  242. break;
  243. default:
  244. }
  245. handler();
  246. }
  247. }
  248. playWindow(rotationParam, flag) {
  249. if (!this.windowsActionArr.frontWindow) {
  250. return;
  251. }
  252. if (flag === 1) {
  253. // 前风窗动画
  254. this.windowsActionArr.frontWindow.forEach((mesh: THREE.Mesh) => {
  255. gsap.to(mesh.rotation, {
  256. z: THREE.MathUtils.degToRad(rotationParam.frontDeg1),
  257. duration: (1 / 9) * Math.abs(rotationParam.frontDeg1 - mesh.rotation.z),
  258. overwrite: true,
  259. });
  260. });
  261. } else if (flag === 2) {
  262. // 后风窗动画
  263. this.windowsActionArr.backWindow.forEach((mesh: THREE.Mesh) => {
  264. gsap.to(mesh.rotation, {
  265. z: THREE.MathUtils.degToRad(rotationParam.backDeg1),
  266. duration: (1 / 9) * Math.abs(rotationParam.backDeg1 - mesh.rotation.z),
  267. overwrite: true,
  268. });
  269. });
  270. } else if (flag === 0) {
  271. ([...this.windowsActionArr.frontWindow] as THREE.Mesh[]).forEach((mesh) => {
  272. gsap.to(mesh.rotation, {
  273. z: THREE.MathUtils.degToRad(90),
  274. overwrite: true,
  275. });
  276. });
  277. }
  278. }
  279. /* 点击风窗,风窗全屏 */
  280. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  281. if (this.animationTimer) {
  282. clearTimeout(this.animationTimer);
  283. this.animationTimer = null;
  284. }
  285. // 判断是否点击到视频
  286. intersects.find((intersect) => {
  287. const mesh = intersect.object;
  288. if (mesh.name === 'player1') {
  289. if (new Date().getTime() - this.playerStartClickTime1 < 400) {
  290. // 双击,视频放大
  291. if (this.player1) {
  292. this.player1.requestFullscreen();
  293. }
  294. }
  295. this.playerStartClickTime1 = new Date().getTime();
  296. return true;
  297. }
  298. return false;
  299. });
  300. }
  301. mouseUpModel() {}
  302. /* 风门动画 */
  303. render() {
  304. if (!this.model) {
  305. return;
  306. }
  307. if (this.mixers && this.fmClock.running) {
  308. this.mixers.update(2);
  309. }
  310. }
  311. mountedThree() {
  312. return new Promise((resolve) => {
  313. this.model.setGLTFModel(['ddFc-bd3'], this.group).then(() => {
  314. console.log(this.group);
  315. this.setModalPosition();
  316. this.initAnimation();
  317. resolve(null);
  318. });
  319. });
  320. }
  321. destroy() {
  322. if (this.mixers) {
  323. const modalGroup = this.group?.getObjectByName('ddFc-bd3');
  324. const fmGroup = modalGroup?.getObjectByName('FengMen_ShouDong_1')?.getObjectByName('FengMen_1');
  325. this.mixers.uncacheClip(this.frontClipAction.getClip());
  326. this.mixers.uncacheAction(this.frontClipAction.getClip(), fmGroup);
  327. if (fmGroup) this.mixers.uncacheRoot(fmGroup);
  328. if (this.model.animations[0]) this.model.animations[0].tracks = [];
  329. }
  330. this.model.clearGroup(this.group);
  331. this.frontClipAction = undefined;
  332. this.model = null;
  333. this.group = null;
  334. }
  335. }
  336. export default ddFc_7;