shuangdaoFcBlt.threejs.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. 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.OpenDegree ? '开度值(%)' : selectData.forntArea ? '过风面积(㎡)' : '过风面积(㎡)'}:`,
  53. font: 'normal 30px Arial',
  54. color: '#009900',
  55. strokeStyle: '#002200',
  56. x: 5,
  57. y: 145,
  58. },
  59. {
  60. text: selectData.OpenDegree
  61. ? Number(`${selectData.OpenDegree}`).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.frontRearDP ? '风窗压差(Pa)' : selectData.windSpeed ? '风速(m/s)' : '通信状态'}:`,
  73. font: 'normal 30px Arial',
  74. color: '#009900',
  75. strokeStyle: '#002200',
  76. x: 5,
  77. y: 200,
  78. },
  79. {
  80. text: `${
  81. selectData.frontRearDP
  82. ? selectData.frontRearDP
  83. : selectData.windSpeed
  84. ? selectData.windSpeed
  85. : selectData.netStatus == '0'
  86. ? '断开'
  87. : '连接'
  88. }`,
  89. font: 'normal 30px Arial',
  90. color: '#009900',
  91. strokeStyle: '#002200',
  92. x: 330,
  93. y: 200,
  94. },
  95. {
  96. text: `${selectData.fWindowM3 ? '过风量(m³/min)' : '风窗道数'}: `,
  97. font: 'normal 30px Arial',
  98. color: '#009900',
  99. strokeStyle: '#002200',
  100. x: 5,
  101. y: 256,
  102. },
  103. {
  104. text: `${selectData.fWindowM3 ? selectData.fWindowM3 : selectData.nwindownum}`,
  105. font: 'normal 30px Arial',
  106. color: '#009900',
  107. strokeStyle: '#002200',
  108. x: 330,
  109. y: 256,
  110. },
  111. {
  112. text: screenDownText,
  113. font: 'normal 28px Arial',
  114. color: '#009900',
  115. strokeStyle: '#002200',
  116. x: screenDownTextX,
  117. y: 303,
  118. },
  119. ];
  120. getTextCanvas(726, 546, textArr, '').then((canvas: HTMLCanvasElement) => {
  121. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  122. const textMaterial = new THREE.MeshBasicMaterial({
  123. // 关于材质并未讲解 实操即可熟悉 这里是漫反射类似纸张的材质,对应的就有高光类似金属的材质.
  124. map: textMap, // 设置纹理贴图
  125. transparent: true,
  126. side: THREE.DoubleSide, // 这里是双面渲染的意思
  127. });
  128. textMap.dispose();
  129. textMaterial.blending = THREE.CustomBlending;
  130. const monitorPlane = this.group?.getObjectByName('monitorText');
  131. if (monitorPlane) {
  132. monitorPlane.material = textMaterial;
  133. } else {
  134. const planeGeometry = new THREE.PlaneGeometry(726, 546); // 平面3维几何体PlaneGeometry
  135. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  136. planeMesh.name = 'monitorText';
  137. planeMesh.scale.set(0.002, 0.002, 0.002);
  138. planeMesh.position.set(4.19, 0.448, -0.27);
  139. this.group?.add(planeMesh);
  140. }
  141. });
  142. }
  143. /* 提取风门序列帧,初始化前后门动画 */
  144. initAnimation() {
  145. const meshArr01: THREE.Object3D[] = [];
  146. const meshArr02: THREE.Object3D[] = [];
  147. const fcObj = this.group.getObjectByName('sdFc')?.getObjectByName('FengChuang_2')?.getObjectByName('FengChuang');
  148. const leftObjNames = ['FCshanye03', 'FCshanye04'];
  149. const rightObjNames = ['FCshanye05', 'FCshanye06'];
  150. debugger;
  151. leftObjNames.filter((name) => {
  152. const obj = fcObj?.getObjectByName(name);
  153. if (obj) {
  154. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  155. meshArr01.push(obj);
  156. }
  157. });
  158. rightObjNames.filter((name) => {
  159. const obj = fcObj?.getObjectByName(name);
  160. if (obj) {
  161. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  162. meshArr02.push(obj);
  163. }
  164. });
  165. this.windowsActionArr.frontWindow = meshArr01;
  166. this.windowsActionArr.backWindow = meshArr02;
  167. }
  168. play(rotationParam, flag) {
  169. if (this.windowsActionArr.frontWindow.length <= 0 || this.windowsActionArr.backWindow.length <= 0) {
  170. return;
  171. }
  172. if (flag === 1) {
  173. // 前风窗动画
  174. this.windowsActionArr.frontWindow.forEach((mesh) => {
  175. gsap.to(mesh.rotation, {
  176. y: THREE.MathUtils.degToRad(rotationParam.frontDeg1),
  177. duration: (1 / 9) * Math.abs(rotationParam.frontDeg1 - mesh.rotation.y),
  178. overwrite: true,
  179. });
  180. });
  181. } else if (flag === 2) {
  182. // 后风窗动画
  183. this.windowsActionArr.backWindow.forEach((mesh) => {
  184. gsap.to(mesh.rotation, {
  185. y: THREE.MathUtils.degToRad(rotationParam.backDeg1),
  186. duration: (1 / 9) * Math.abs(rotationParam.backDeg1 - mesh.rotation.y),
  187. overwrite: true,
  188. });
  189. });
  190. } else if (flag === 0) {
  191. ([...this.windowsActionArr.frontWindow, ...this.windowsActionArr.backWindow] as THREE.Mesh[]).forEach((mesh) => {
  192. gsap.to(mesh.rotation, {
  193. y: 0,
  194. overwrite: true,
  195. });
  196. });
  197. }
  198. }
  199. /* 点击风窗,风窗全屏 */
  200. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  201. this.isLRAnimation = false;
  202. if (this.animationTimer) {
  203. clearTimeout(this.animationTimer);
  204. this.animationTimer = null;
  205. }
  206. // 判断是否点击到视频
  207. intersects.find((intersect) => {
  208. const mesh = intersect.object;
  209. if (mesh.name === 'player1') {
  210. if (new Date().getTime() - this.playerStartClickTime1 < 400) {
  211. // 双击,视频放大
  212. if (this.player1) {
  213. this.player1.requestFullscreen();
  214. }
  215. }
  216. this.playerStartClickTime1 = new Date().getTime();
  217. return true;
  218. }
  219. return false;
  220. });
  221. }
  222. mouseUpModel() {
  223. // 10s后开始摆动
  224. if (!this.animationTimer && !this.isLRAnimation) {
  225. this.animationTimer = setTimeout(() => {
  226. this.isLRAnimation = true;
  227. }, 10000);
  228. }
  229. }
  230. /* 风门动画 */
  231. render() {
  232. if (!this.model) {
  233. return;
  234. }
  235. if (this.isLRAnimation && this.group) {
  236. // 左右摇摆动画
  237. if (Math.abs(this.group.rotation.y) >= 0.2) {
  238. this.direction = -this.direction;
  239. this.group.rotation.y += 0.00002 * 30 * this.direction;
  240. } else {
  241. this.group.rotation.y += 0.00002 * 30 * this.direction;
  242. }
  243. }
  244. }
  245. mountedThree(playerDom) {
  246. return new Promise((resolve) => {
  247. this.model.setGLTFModel('sdFc-d').then((gltf) => {
  248. const fcModal = gltf[0];
  249. fcModal.name = 'sdFc';
  250. this.group?.add(fcModal);
  251. this.setModalPosition();
  252. this.initAnimation();
  253. this.addLight();
  254. resolve(null);
  255. });
  256. });
  257. }
  258. destroy() {
  259. this.model.clearGroup(this.group);
  260. this.windowsActionArr.frontWindow = undefined;
  261. this.model = null;
  262. this.group = null;
  263. }
  264. }
  265. export default doubleWindow;