shuangdaoFcBlt.threejs.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import * as THREE from 'three';
  2. import { getTextCanvas, renderVideo } from '/@/utils/threejs/util';
  3. import gsap from 'gsap';
  4. class sdFc_3 {
  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. constructor(model) {
  16. this.model = model;
  17. // this.group.name = 'ddFc';
  18. }
  19. // // 重置摄像头
  20. // const resetCamera = () => {
  21. // this.model.camera.position.set(30.328, 58.993, 148.315);
  22. // this.model.camera.rotation.set(-27.88, 14.35, 7.47);
  23. // this.model.orbitControls?.update();
  24. // this.model.camera.updateProjectionMatrix();
  25. // };
  26. addLight = () => {};
  27. // 设置模型位置
  28. setModalPosition() {
  29. this.group?.scale.set(22, 22, 22);
  30. this.group?.position.set(-35, 25, 15);
  31. }
  32. addMonitorText(selectData) {
  33. if (!this.group) {
  34. return;
  35. }
  36. const screenDownText = VENT_PARAM['modalText']
  37. ? VENT_PARAM['modalText']
  38. : History_Type['type'] == 'remote'
  39. ? `国能神东煤炭集团监制`
  40. : '煤炭科学技术研究院有限公司研制';
  41. const screenDownTextX = 125 - (screenDownText.length - 10) * 10;
  42. const textArr = [
  43. {
  44. text: `远程定量调节自动风窗`,
  45. font: 'normal 30px Arial',
  46. color: '#009900',
  47. strokeStyle: '#002200',
  48. x: 120,
  49. y: 90,
  50. },
  51. {
  52. text: `${selectData.OpenDegree1 ? '前窗开度值(°)' : selectData.forntArea ? '前窗过风面积(㎡)' : '前窗过风面积(㎡)'}:`,
  53. font: 'normal 30px Arial',
  54. color: '#009900',
  55. strokeStyle: '#002200',
  56. x: 5,
  57. y: 145,
  58. },
  59. {
  60. text: selectData.OpenDegree1
  61. ? Number(`${selectData.OpenDegree1}`).toFixed(2)
  62. : selectData.forntArea
  63. ? Number(`${selectData.forntArea}`).toFixed(2)
  64. : '-',
  65. font: 'normal 30px Arial',
  66. color: '#009900',
  67. strokeStyle: '#002200',
  68. x: 330,
  69. y: 145,
  70. },
  71. {
  72. text: `${selectData.OpenDegree2 ? '后窗开度值(°)' : selectData.forntArea ? '后窗过风面积(㎡)' : '后窗过风面积(㎡)'}:`,
  73. font: 'normal 30px Arial',
  74. color: '#009900',
  75. strokeStyle: '#002200',
  76. x: 5,
  77. y: 200,
  78. },
  79. {
  80. text: selectData.OpenDegree2
  81. ? Number(`${selectData.OpenDegree2}`).toFixed(2)
  82. : selectData.rearArea
  83. ? Number(`${selectData.rearArea}`).toFixed(2)
  84. : '-',
  85. font: 'normal 30px Arial',
  86. color: '#009900',
  87. strokeStyle: '#002200',
  88. x: 330,
  89. y: 200,
  90. },
  91. {
  92. text: `${selectData.frontRearDP ? '风窗压差(Pa)' : selectData.windSpeed ? '风速(m/s)' : '通信状态'}:`,
  93. font: 'normal 30px Arial',
  94. color: '#009900',
  95. strokeStyle: '#002200',
  96. x: 5,
  97. y: 256,
  98. },
  99. {
  100. text: `${
  101. selectData.frontRearDP
  102. ? selectData.frontRearDP
  103. : selectData.windSpeed
  104. ? selectData.windSpeed
  105. : selectData.netStatus == '0'
  106. ? '断开'
  107. : '连接'
  108. }`,
  109. font: 'normal 30px Arial',
  110. color: '#009900',
  111. strokeStyle: '#002200',
  112. x: 330,
  113. y: 256,
  114. },
  115. {
  116. text: screenDownText,
  117. font: 'normal 28px Arial',
  118. color: '#009900',
  119. strokeStyle: '#002200',
  120. x: screenDownTextX,
  121. y: 303,
  122. },
  123. ];
  124. getTextCanvas(726, 546, textArr, '').then((canvas: HTMLCanvasElement) => {
  125. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  126. const textMaterial = new THREE.MeshBasicMaterial({
  127. // 关于材质并未讲解 实操即可熟悉 这里是漫反射类似纸张的材质,对应的就有高光类似金属的材质.
  128. map: textMap, // 设置纹理贴图
  129. transparent: true,
  130. side: THREE.DoubleSide, // 这里是双面渲染的意思
  131. });
  132. textMap.dispose();
  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(726, 546); // 平面3维几何体PlaneGeometry
  139. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  140. planeMesh.name = 'monitorText';
  141. planeMesh.scale.set(0.002, 0.002, 0.002);
  142. planeMesh.position.set(4.19, 0.448, -0.27);
  143. this.group?.add(planeMesh);
  144. }
  145. });
  146. }
  147. /* 提取风门序列帧,初始化前后门动画 */
  148. initAnimation() {
  149. const meshArr01: THREE.Object3D[] = [];
  150. const meshArr02: THREE.Object3D[] = [];
  151. const fc = this.group.getObjectByName('sdFc')?.getObjectByName('FengChuang_2_ShuangDao');
  152. const fcObj1 = fc?.getObjectByName('FengChuang_1_1');
  153. const fcObj2 = fc?.getObjectByName('FengChuang_2_1');
  154. const frontObjNames = ['FengChuang_1_2', 'FengChuang_1_3', 'FengChuang_1_4', 'FengChuang_1_5'];
  155. const backObjNames = ['FengChuang_2_2', 'FengChuang_2_3', 'FengChuang_2_4', 'FengChuang_2_5'];
  156. frontObjNames.filter((name) => {
  157. const obj = fcObj1?.getObjectByName(name);
  158. if (obj) {
  159. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  160. meshArr01.push(obj);
  161. }
  162. });
  163. backObjNames.filter((name) => {
  164. const obj = fcObj2?.getObjectByName(name);
  165. if (obj) {
  166. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  167. meshArr02.push(obj);
  168. }
  169. });
  170. this.windowsActionArr.frontWindow = meshArr01;
  171. this.windowsActionArr.backWindow = meshArr02;
  172. }
  173. play(rotationParam, flag) {
  174. if (this.windowsActionArr.frontWindow.length <= 0 || this.windowsActionArr.backWindow.length <= 0) {
  175. return;
  176. }
  177. if (flag === 1) {
  178. // 前风窗动画
  179. this.windowsActionArr.frontWindow.forEach((mesh) => {
  180. gsap.to(mesh.rotation, {
  181. y: THREE.MathUtils.degToRad(rotationParam.frontDeg1),
  182. duration: (1 / 9) * Math.abs(rotationParam.frontDeg1 - mesh.rotation.y),
  183. overwrite: true,
  184. });
  185. });
  186. } else if (flag === 2) {
  187. // 后风窗动画
  188. this.windowsActionArr.backWindow.forEach((mesh) => {
  189. gsap.to(mesh.rotation, {
  190. y: THREE.MathUtils.degToRad(rotationParam.backDeg1),
  191. duration: (1 / 9) * Math.abs(rotationParam.backDeg1 - mesh.rotation.y),
  192. overwrite: true,
  193. });
  194. });
  195. } else if (flag === 0) {
  196. ([...this.windowsActionArr.frontWindow, ...this.windowsActionArr.backWindow] as THREE.Mesh[]).forEach((mesh) => {
  197. gsap.to(mesh.rotation, {
  198. y: 0,
  199. overwrite: true,
  200. });
  201. });
  202. }
  203. }
  204. /* 点击风窗,风窗全屏 */
  205. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  206. if (this.animationTimer) {
  207. clearTimeout(this.animationTimer);
  208. this.animationTimer = null;
  209. }
  210. // 判断是否点击到视频
  211. intersects.find((intersect) => {
  212. const mesh = intersect.object;
  213. if (mesh.name === 'player1') {
  214. if (new Date().getTime() - this.playerStartClickTime1 < 400) {
  215. // 双击,视频放大
  216. if (this.player1) {
  217. this.player1.requestFullscreen();
  218. }
  219. }
  220. this.playerStartClickTime1 = new Date().getTime();
  221. return true;
  222. }
  223. return false;
  224. });
  225. }
  226. mouseUpModel() {}
  227. /* 风门动画 */
  228. render() {
  229. if (!this.model) {
  230. return;
  231. }
  232. }
  233. mountedThree() {
  234. return new Promise((resolve) => {
  235. this.model.setGLTFModel('sdFc-dm').then((gltf) => {
  236. const fcModal = gltf[0];
  237. fcModal.name = 'sdFc';
  238. this.group?.add(fcModal);
  239. this.setModalPosition();
  240. this.initAnimation();
  241. this.addLight();
  242. resolve(null);
  243. });
  244. });
  245. }
  246. destroy() {
  247. this.model.clearGroup(this.group);
  248. this.windowsActionArr.frontWindow = undefined;
  249. this.model = null;
  250. this.group = null;
  251. }
  252. }
  253. export default sdFc_3;