shuangdaoFcHj.threejs.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. import * as THREE from 'three';
  2. import { getTextCanvas, renderVideo } from '/@/utils/threejs/util';
  3. import gsap from 'gsap';
  4. class sdFc_4 {
  5. model;
  6. modelName = 'sdFc';
  7. group: THREE.Object3D = new THREE.Object3D();
  8. animationTimer;
  9. direction = 1;
  10. windowsActionArr = {
  11. frontWindow1: <THREE.Mesh[]>[],
  12. frontWindow2: <THREE.Mesh[]>[],
  13. backWindow1: <THREE.Mesh[]>[],
  14. backWindow2: <THREE.Mesh[]>[],
  15. };
  16. constructor(model) {
  17. this.model = model;
  18. // this.group.name = 'ddFc';
  19. }
  20. // // 重置摄像头
  21. // const resetCamera = () => {
  22. // this.model.camera.position.set(30.328, 58.993, 148.315);
  23. // this.model.camera.rotation.set(-27.88, 14.35, 7.47);
  24. // this.model.orbitControls?.update();
  25. // this.model.camera.updateProjectionMatrix();
  26. // };
  27. addLight = () => {};
  28. // 设置模型位置
  29. setModalPosition() {
  30. this.group?.scale.set(22, 22, 22);
  31. this.group?.position.set(-35, 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 = 125 - (screenDownText.length - 10) * 10;
  43. const textArr = [
  44. {
  45. text: `远程定量调节自动风窗`,
  46. font: 'normal 30px Arial',
  47. color: '#009900',
  48. strokeStyle: '#002200',
  49. x: 120,
  50. y: 90,
  51. },
  52. {
  53. text: `${selectData.OpenDegree1 ? '前窗开度值(°)' : selectData.frontArea ? '前窗过风面积(㎡)' : '前窗过风面积(㎡)'}:`,
  54. font: 'normal 30px Arial',
  55. color: '#009900',
  56. strokeStyle: '#002200',
  57. x: 5,
  58. y: 145,
  59. },
  60. {
  61. text: selectData.OpenDegree1
  62. ? Number(`${selectData.OpenDegree1}`).toFixed(2)
  63. : selectData.frontArea
  64. ? Number(`${selectData.frontArea}`).toFixed(2)
  65. : '-',
  66. font: 'normal 30px Arial',
  67. color: '#009900',
  68. strokeStyle: '#002200',
  69. x: 330,
  70. y: 145,
  71. },
  72. {
  73. text: `${selectData.OpenDegree2 ? '后窗开度值(°)' : selectData.rearArea ? '后窗过风面积(㎡)' : '后窗过风面积(㎡)'}:`,
  74. font: 'normal 30px Arial',
  75. color: '#009900',
  76. strokeStyle: '#002200',
  77. x: 5,
  78. y: 200,
  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 30px Arial',
  87. color: '#009900',
  88. strokeStyle: '#002200',
  89. x: 330,
  90. y: 200,
  91. },
  92. {
  93. text: `${selectData.frontRearDP ? '风窗压差(Pa)' : selectData.windSpeed ? '风速(m/s)' : '通信状态'}:`,
  94. font: 'normal 30px Arial',
  95. color: '#009900',
  96. strokeStyle: '#002200',
  97. x: 5,
  98. y: 256,
  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 30px Arial',
  111. color: '#009900',
  112. strokeStyle: '#002200',
  113. x: 330,
  114. y: 256,
  115. },
  116. {
  117. text: screenDownText,
  118. font: 'normal 28px Arial',
  119. color: '#009900',
  120. strokeStyle: '#002200',
  121. x: screenDownTextX,
  122. y: 303,
  123. },
  124. ];
  125. getTextCanvas(726, 546, 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. textMap.dispose();
  134. textMaterial.blending = THREE.CustomBlending;
  135. const monitorPlane = this.group?.getObjectByName('monitorText');
  136. if (monitorPlane) {
  137. monitorPlane.material = textMaterial;
  138. } else {
  139. const planeGeometry = new THREE.PlaneGeometry(726, 546); // 平面3维几何体PlaneGeometry
  140. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  141. planeMesh.name = 'monitorText';
  142. planeMesh.scale.set(0.002, 0.002, 0.002);
  143. planeMesh.position.set(4.19, 0.448, -0.27);
  144. this.group?.add(planeMesh);
  145. }
  146. });
  147. }
  148. /* 提取风门序列帧,初始化前后门动画 */
  149. initAnimation() {
  150. const fcGroup = this.group?.getObjectByName('ShuangDaoTiaoJieFengChuang_1');
  151. if (fcGroup) {
  152. const fontGroup = fcGroup.getObjectByName('FengChuang_1');
  153. const backGroup = fcGroup.getObjectByName('FengChuang_2');
  154. const JuanLian1 = fontGroup?.getObjectByName('JuanLian_1') as THREE.Mesh;
  155. JuanLian1.scale.set(1, -1, 1);
  156. JuanLian1.position.setY(0.9);
  157. const JuanLian2 = fontGroup?.getObjectByName('JuanLian_2') as THREE.Mesh;
  158. JuanLian2.scale.set(1, -1, 1);
  159. JuanLian2.position.setY(0.9);
  160. const JuanLian3 = backGroup?.getObjectByName('JuanLian_4') as THREE.Mesh;
  161. JuanLian3.scale.set(1, -1, 1);
  162. JuanLian3.position.setY(0.9);
  163. const JuanLian4 = backGroup?.getObjectByName('JuanLian_3') as THREE.Mesh;
  164. JuanLian4.scale.set(1, -1, 1);
  165. JuanLian4.position.setY(0.9);
  166. const meshArr01: THREE.Mesh[] = [JuanLian1];
  167. const meshArr02: THREE.Mesh[] = [JuanLian2];
  168. const meshArr03: THREE.Mesh[] = [JuanLian3];
  169. const meshArr04: THREE.Mesh[] = [JuanLian4];
  170. this.windowsActionArr.frontWindow1 = meshArr01;
  171. this.windowsActionArr.frontWindow2 = meshArr02;
  172. this.windowsActionArr.backWindow1 = meshArr04;
  173. this.windowsActionArr.backWindow2 = meshArr03;
  174. }
  175. }
  176. play(handlerState, rotationParam) {
  177. if (
  178. this.windowsActionArr.frontWindow1 &&
  179. this.windowsActionArr.frontWindow2 &&
  180. this.windowsActionArr.backWindow1 &&
  181. this.windowsActionArr.backWindow2
  182. ) {
  183. switch (handlerState) {
  184. case 1: // 窗1开启动画
  185. this.windowsActionArr.frontWindow1.forEach((mesh) => {
  186. gsap.to(mesh.scale, {
  187. y: -1 + rotationParam.scaleY,
  188. duration: 10 * Math.abs(-1 + rotationParam.scaleY - mesh.scale.y),
  189. overwrite: true,
  190. });
  191. });
  192. break;
  193. case 2: // 窗2开启动画
  194. this.windowsActionArr.frontWindow2.forEach((mesh) => {
  195. gsap.to(mesh.scale, {
  196. y: -1 + rotationParam.scaleY,
  197. duration: 10 * Math.abs(-1 + rotationParam.scaleY - mesh.scale.y),
  198. overwrite: true,
  199. });
  200. });
  201. break;
  202. case 3: // 窗3开启动画
  203. this.windowsActionArr.backWindow1.forEach((mesh) => {
  204. gsap.to(mesh.scale, {
  205. y: -1 + rotationParam.scaleY,
  206. duration: 10 * Math.abs(-1 + rotationParam.scaleY - mesh.scale.y),
  207. overwrite: true,
  208. });
  209. });
  210. break;
  211. case 4: // 窗4开启动画
  212. this.windowsActionArr.backWindow2.forEach((mesh) => {
  213. gsap.to(mesh.scale, {
  214. y: -1 + rotationParam.scaleY,
  215. duration: 10 * Math.abs(-1 + rotationParam.scaleY - mesh.scale.y),
  216. overwrite: true,
  217. });
  218. });
  219. break;
  220. default:
  221. }
  222. }
  223. }
  224. /* 点击风窗,风窗全屏 */
  225. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  226. if (this.animationTimer) {
  227. clearTimeout(this.animationTimer);
  228. this.animationTimer = null;
  229. }
  230. // 判断是否点击到视频
  231. intersects.find((intersect) => {
  232. const mesh = intersect.object;
  233. return false;
  234. });
  235. }
  236. mouseUpModel() {}
  237. /* 风门动画 */
  238. render() {
  239. if (!this.model) {
  240. return;
  241. }
  242. if (this.fontMixers1 && this.frontClock1.running) {
  243. this.fontMixers1.update(2);
  244. }
  245. }
  246. mountedThree() {
  247. return new Promise((resolve) => {
  248. this.model.setGLTFModel('sdFc-jz').then((gltf) => {
  249. // this.group?.add(gltf[0]);
  250. this.group = gltf[0];
  251. this.group.name = 'sdFc';
  252. this.setModalPosition();
  253. this.initAnimation();
  254. this.addLight();
  255. resolve(null);
  256. });
  257. });
  258. }
  259. destroy() {
  260. this.model.clearGroup(this.group);
  261. this.windowsActionArr.frontWindow = undefined;
  262. this.model = null;
  263. this.group = null;
  264. }
  265. }
  266. export default sdFc_4;