dandaoFc.threejs.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. import * as THREE from 'three';
  2. import { getTextCanvas, renderVideo } from '/@/utils/threejs/util';
  3. import gsap from 'gsap';
  4. class singleWindow {
  5. model;
  6. modelName = 'ddFc';
  7. group: THREE.Object3D = new THREE.Object3D();
  8. animationTimer;
  9. isLRAnimation = true;
  10. direction = 1;
  11. windowsActionArr = {
  12. frontWindow: [],
  13. };
  14. player1;
  15. player2;
  16. playerStartClickTime1 = new Date().getTime();
  17. constructor(model) {
  18. this.model = model;
  19. // this.group.name = 'ddFc';
  20. }
  21. // // 重置摄像头
  22. // const resetCamera = () => {
  23. // this.model.camera.position.set(30.328, 58.993, 148.315);
  24. // this.model.camera.rotation.set(-27.88, 14.35, 7.47);
  25. // this.model.orbitControls?.update();
  26. // this.model.camera.updateProjectionMatrix();
  27. // };
  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 textArr = [
  38. {
  39. text: `远程定量调节自动风窗`,
  40. font: 'normal 30px Arial',
  41. color: '#009900',
  42. strokeStyle: '#002200',
  43. x: 110,
  44. y: 90,
  45. },
  46. {
  47. text: `${selectData.OpenDegree ? '开度值(%)' : selectData.forntArea ? '过风面积(㎡)' : '过风面积(㎡)'}:`,
  48. font: 'normal 30px Arial',
  49. color: '#009900',
  50. strokeStyle: '#002200',
  51. x: 5,
  52. y: 145,
  53. },
  54. {
  55. text: selectData.OpenDegree
  56. ? Number(`${selectData.OpenDegree}`).toFixed(2)
  57. : selectData.forntArea
  58. ? Number(`${selectData.forntArea}`).toFixed(2)
  59. : '-',
  60. font: 'normal 30px Arial',
  61. color: '#009900',
  62. strokeStyle: '#002200',
  63. x: 330,
  64. y: 145,
  65. },
  66. {
  67. text: `${selectData.frontRearDP ? '风窗压差(Pa)' : selectData.windSpeed ? '风速(m/s)' : '通信状态'}:`,
  68. font: 'normal 30px Arial',
  69. color: '#009900',
  70. strokeStyle: '#002200',
  71. x: 5,
  72. y: 200,
  73. },
  74. {
  75. text: `${
  76. selectData.frontRearDP
  77. ? selectData.frontRearDP
  78. : selectData.windSpeed
  79. ? selectData.windSpeed
  80. : selectData.netStatus == '0'
  81. ? '断开'
  82. : '连接'
  83. }`,
  84. font: 'normal 30px Arial',
  85. color: '#009900',
  86. strokeStyle: '#002200',
  87. x: 330,
  88. y: 200,
  89. },
  90. {
  91. text: `${selectData.fWindowM3 ? '过风量(m³/min)' : '风窗道数'}: `,
  92. font: 'normal 30px Arial',
  93. color: '#009900',
  94. strokeStyle: '#002200',
  95. x: 5,
  96. y: 256,
  97. },
  98. {
  99. text: `${selectData.fWindowM3 ? selectData.fWindowM3 : selectData.nwindownum}`,
  100. font: 'normal 30px Arial',
  101. color: '#009900',
  102. strokeStyle: '#002200',
  103. x: 330,
  104. y: 256,
  105. },
  106. {
  107. text: History_Type['type'] == 'remote' ? `国能神东煤炭集团监制` : '煤炭科学技术研究院有限公司研制',
  108. font: 'normal 28px Arial',
  109. color: '#009900',
  110. strokeStyle: '#002200',
  111. x: History_Type['type'] == 'remote' ? 120 : 60,
  112. y: 302,
  113. },
  114. ];
  115. getTextCanvas(726, 546, textArr, '').then((canvas: HTMLCanvasElement) => {
  116. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  117. const textMaterial = new THREE.MeshBasicMaterial({
  118. // 关于材质并未讲解 实操即可熟悉 这里是漫反射类似纸张的材质,对应的就有高光类似金属的材质.
  119. map: textMap, // 设置纹理贴图
  120. transparent: true,
  121. side: THREE.DoubleSide, // 这里是双面渲染的意思
  122. });
  123. textMap.dispose();
  124. textMaterial.blending = THREE.CustomBlending;
  125. const monitorPlane = this.group?.getObjectByName('monitorText');
  126. if (monitorPlane) {
  127. monitorPlane.material = textMaterial;
  128. } else {
  129. const planeGeometry = new THREE.PlaneGeometry(570, 346); // 平面3维几何体PlaneGeometry
  130. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  131. planeMesh.name = 'monitorText';
  132. planeMesh.scale.set(0.0025, 0.003, 0.002);
  133. planeMesh.position.set(3.71, -0.042, -0.23);
  134. this.group?.add(planeMesh);
  135. }
  136. });
  137. }
  138. /* 提取风门序列帧,初始化前后门动画 */
  139. initAnimation() {
  140. const meshArr01: THREE.Object3D[] = [];
  141. if (this.group) {
  142. this.group.getObjectByName('ddFc')?.children.forEach((obj) => {
  143. if (obj.type === 'Mesh' && ((obj.name && obj.name.startsWith('shanye')) || obj.name.startsWith('FCshanye'))) {
  144. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  145. meshArr01.push(obj);
  146. }
  147. });
  148. }
  149. this.windowsActionArr.frontWindow = meshArr01;
  150. }
  151. play(rotationParam, flag) {
  152. if (this.windowsActionArr.frontWindow.length <= 0) {
  153. return;
  154. }
  155. if (flag === 1) {
  156. // 前风窗动画
  157. this.windowsActionArr.frontWindow.forEach((mesh: THREE.Mesh) => {
  158. gsap.to(mesh.rotation, {
  159. y: THREE.MathUtils.degToRad(rotationParam.frontDeg1),
  160. duration: (1 / 9) * Math.abs(rotationParam.frontDeg1 - mesh.rotation.y),
  161. overwrite: true,
  162. });
  163. });
  164. } else if (flag === 0) {
  165. ([...this.windowsActionArr.frontWindow] as THREE.Mesh[]).forEach((mesh) => {
  166. gsap.to(mesh.rotation, {
  167. y: 0,
  168. overwrite: true,
  169. });
  170. });
  171. }
  172. }
  173. /* 点击风窗,风窗全屏 */
  174. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  175. this.isLRAnimation = false;
  176. if (this.animationTimer) {
  177. clearTimeout(this.animationTimer);
  178. this.animationTimer = null;
  179. }
  180. // 判断是否点击到视频
  181. intersects.find((intersect) => {
  182. const mesh = intersect.object;
  183. if (mesh.name === 'player1') {
  184. if (new Date().getTime() - this.playerStartClickTime1 < 400) {
  185. // 双击,视频放大
  186. if (this.player1) {
  187. this.player1.requestFullscreen();
  188. }
  189. }
  190. this.playerStartClickTime1 = new Date().getTime();
  191. return true;
  192. }
  193. return false;
  194. });
  195. }
  196. mouseUpModel() {
  197. // 10s后开始摆动
  198. if (!this.animationTimer && !this.isLRAnimation) {
  199. this.animationTimer = setTimeout(() => {
  200. this.isLRAnimation = true;
  201. }, 10000);
  202. }
  203. }
  204. /* 风门动画 */
  205. render() {
  206. if (!this.model) {
  207. return;
  208. }
  209. if (this.isLRAnimation && this.group) {
  210. // 左右摇摆动画
  211. if (Math.abs(this.group.rotation.y) >= 0.2) {
  212. this.direction = -this.direction;
  213. this.group.rotation.y += 0.00002 * 30 * this.direction;
  214. } else {
  215. this.group.rotation.y += 0.00002 * 30 * this.direction;
  216. }
  217. }
  218. }
  219. async initCamera(dom1) {
  220. const videoPlayer1 = dom1;
  221. let monitorPlane: THREE.Mesh | null = null;
  222. const textArr = [
  223. {
  224. text: `无信号输入`,
  225. font: 'normal 40px Arial',
  226. color: '#009900',
  227. strokeStyle: '#002200',
  228. x: 170,
  229. y: 40,
  230. },
  231. ];
  232. const canvas = await getTextCanvas(320, 180, textArr, null);
  233. if (canvas) {
  234. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  235. const textMaterial = new THREE.MeshBasicMaterial({
  236. map: textMap, // 设置纹理贴图
  237. transparent: true,
  238. side: THREE.DoubleSide, // 这里是双面渲染的意思
  239. });
  240. textMaterial.blending = THREE.CustomBlending;
  241. monitorPlane = this.group?.getObjectByName('noPlayer');
  242. if (monitorPlane) {
  243. monitorPlane.material = textMaterial;
  244. } else {
  245. const planeGeometry = new THREE.PlaneGeometry(100, 100); // 平面3维几何体PlaneGeometry
  246. monitorPlane = new THREE.Mesh(planeGeometry, textMaterial);
  247. textMaterial.dispose();
  248. planeGeometry.dispose();
  249. }
  250. }
  251. const videoPlayer = this.group.getObjectByName('player1');
  252. if (videoPlayer) {
  253. this.model.clearMesh(videoPlayer);
  254. this.group.remove(videoPlayer);
  255. }
  256. const noPlayer1 = this.group.getObjectByName('noPlayer1');
  257. if (noPlayer1) {
  258. this.model.clearMesh(noPlayer1);
  259. this.group.remove(noPlayer1);
  260. }
  261. if (!videoPlayer1 && videoPlayer1 === null) {
  262. if (monitorPlane && !this.group.getObjectByName('noPlayer1')) {
  263. const planeMesh = monitorPlane.clone();
  264. planeMesh.name = 'noPlayer1';
  265. planeMesh.scale.set(0.011, 0.0053, 0.012);
  266. planeMesh.position.set(-4.3, 0.13, -0.23);
  267. this.group?.add(planeMesh.clone());
  268. }
  269. } else if (videoPlayer1) {
  270. try {
  271. const mesh = renderVideo(this.group, videoPlayer1, 'player1');
  272. if (mesh) {
  273. mesh?.scale.set(0.0382, 0.028, 0.022);
  274. mesh?.position.set(-3.008, 0.148, -0.22);
  275. this.group.add(mesh);
  276. }
  277. } catch (error) {
  278. console.log('视频信号异常');
  279. }
  280. }
  281. }
  282. mountedThree(playerDom) {
  283. return new Promise((resolve) => {
  284. this.model.setGLTFModel('ddFc').then((gltf) => {
  285. const fcModal = gltf[0];
  286. fcModal.name = 'ddFc';
  287. this.group?.add(fcModal);
  288. this.setModalPosition();
  289. this.initAnimation();
  290. resolve(null);
  291. });
  292. });
  293. }
  294. destroy() {
  295. this.model.clearGroup(this.group);
  296. this.windowsActionArr.frontWindow = undefined;
  297. this.model = null;
  298. this.group = null;
  299. }
  300. }
  301. export default singleWindow;