shuangdaoFcBlt.threejs.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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.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 fcObj = this.group.getObjectByName('sdFc')?.getObjectByName('FengChuang_2')?.getObjectByName('FengChuang');
  152. const leftObjNames = ['FCshanye03', 'FCshanye04'];
  153. const rightObjNames = ['FCshanye05', 'FCshanye06'];
  154. debugger;
  155. leftObjNames.filter((name) => {
  156. const obj = fcObj?.getObjectByName(name);
  157. if (obj) {
  158. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  159. meshArr01.push(obj);
  160. }
  161. });
  162. rightObjNames.filter((name) => {
  163. const obj = fcObj?.getObjectByName(name);
  164. if (obj) {
  165. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  166. meshArr02.push(obj);
  167. }
  168. });
  169. this.windowsActionArr.frontWindow = meshArr01;
  170. this.windowsActionArr.backWindow = meshArr02;
  171. }
  172. play(rotationParam, flag) {
  173. debugger;
  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. this.isLRAnimation = false;
  207. if (this.animationTimer) {
  208. clearTimeout(this.animationTimer);
  209. this.animationTimer = null;
  210. }
  211. // 判断是否点击到视频
  212. intersects.find((intersect) => {
  213. const mesh = intersect.object;
  214. if (mesh.name === 'player1') {
  215. if (new Date().getTime() - this.playerStartClickTime1 < 400) {
  216. // 双击,视频放大
  217. if (this.player1) {
  218. this.player1.requestFullscreen();
  219. }
  220. }
  221. this.playerStartClickTime1 = new Date().getTime();
  222. return true;
  223. }
  224. return false;
  225. });
  226. }
  227. mouseUpModel() {
  228. // 10s后开始摆动
  229. if (!this.animationTimer && !this.isLRAnimation) {
  230. this.animationTimer = setTimeout(() => {
  231. this.isLRAnimation = true;
  232. }, 10000);
  233. }
  234. }
  235. /* 风门动画 */
  236. render() {
  237. if (!this.model) {
  238. return;
  239. }
  240. if (this.isLRAnimation && this.group) {
  241. // 左右摇摆动画
  242. if (Math.abs(this.group.rotation.y) >= 0.2) {
  243. this.direction = -this.direction;
  244. this.group.rotation.y += 0.00002 * 30 * this.direction;
  245. } else {
  246. this.group.rotation.y += 0.00002 * 30 * this.direction;
  247. }
  248. }
  249. }
  250. mountedThree(playerDom) {
  251. return new Promise((resolve) => {
  252. this.model.setGLTFModel('sdFc-d').then((gltf) => {
  253. const fcModal = gltf[0];
  254. fcModal.name = 'sdFc';
  255. this.group?.add(fcModal);
  256. this.setModalPosition();
  257. this.initAnimation();
  258. this.addLight();
  259. resolve(null);
  260. });
  261. });
  262. }
  263. destroy() {
  264. this.model.clearGroup(this.group);
  265. this.windowsActionArr.frontWindow = undefined;
  266. this.model = null;
  267. this.group = null;
  268. }
  269. }
  270. export default doubleWindow;