shuangdaoFc.threejs.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. playerStartClickTime1 = new Date().getTime();
  16. playerStartClickTime2 = new Date().getTime();
  17. constructor(model) {
  18. this.model = model;
  19. // this.group.name = this.modelName;
  20. }
  21. // 设置模型位置
  22. setModalPosition() {
  23. // this.group.getObjectByName(this.modelName)?.scale.set(9, 9, 9);
  24. this.group?.scale.set(22, 22, 22);
  25. this.group?.position.set(-15, 25, 15);
  26. }
  27. addMonitorText(selectData) {
  28. if (!this.group) {
  29. return;
  30. }
  31. const screenDownText = VENT_PARAM['modalText']
  32. ? VENT_PARAM['modalText']
  33. : History_Type['type'] == 'remote'
  34. ? `国能神东煤炭集团监制`
  35. : '煤炭科学技术研究院有限公司研制';
  36. const screenDownTextX = 120 - (screenDownText.length - 10) * 6;
  37. const textArr = [
  38. {
  39. text: `远程定量调节自动风窗`,
  40. font: 'normal 30px Arial',
  41. color: '#009900',
  42. strokeStyle: '#002200',
  43. x: 95,
  44. y: 97,
  45. },
  46. {
  47. text: `${selectData.OpenDegree ? '开度值(°)' : selectData.forntArea ? '过风面积(㎡)' : '过风面积(㎡)'}:`,
  48. font: 'normal 28px Arial',
  49. color: '#009900',
  50. strokeStyle: '#002200',
  51. x: 5,
  52. y: 150,
  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 28px Arial',
  61. color: '#009900',
  62. strokeStyle: '#002200',
  63. x: 330,
  64. y: 150,
  65. },
  66. {
  67. text: `${selectData.frontRearDP ? '风窗压差(Pa)' : selectData.windSpeed ? '风速' : '通信状态'}:`,
  68. font: 'normal 28px Arial',
  69. color: '#009900',
  70. strokeStyle: '#002200',
  71. x: 5,
  72. y: 210,
  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 28px Arial',
  85. color: '#009900',
  86. strokeStyle: '#002200',
  87. x: 325,
  88. y: 210,
  89. },
  90. {
  91. text: `${selectData.fWindowM3 ? '过风量(m³/min)' : '风窗道数'}: `,
  92. font: 'normal 28px Arial',
  93. color: '#009900',
  94. strokeStyle: '#002200',
  95. x: 5,
  96. y: 266,
  97. },
  98. {
  99. text: `${selectData.fWindowM3 ? selectData.fWindowM3 : selectData.nwindownum}`,
  100. font: 'normal 28px Arial',
  101. color: '#009900',
  102. strokeStyle: '#002200',
  103. x: 330,
  104. y: 266,
  105. },
  106. {
  107. text: screenDownText,
  108. font: 'normal 28px Arial',
  109. color: '#009900',
  110. strokeStyle: '#002200',
  111. x: screenDownTextX,
  112. y: 322,
  113. },
  114. ];
  115. getTextCanvas(570, 346, 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. textMaterial.blending = THREE.CustomBlending;
  124. const monitorPlane = this.group?.getObjectByName('monitorText');
  125. if (monitorPlane) {
  126. monitorPlane.material = textMaterial;
  127. } else {
  128. const planeGeometry = new THREE.PlaneGeometry(570, 346); // 平面3维几何体PlaneGeometry
  129. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  130. planeMesh.name = 'monitorText';
  131. planeMesh.scale.set(0.0018, 0.0018, 0.0018);
  132. planeMesh.position.set(4.04, 0.178, -0.2);
  133. this.group?.add(planeMesh);
  134. }
  135. textMap.dispose();
  136. });
  137. }
  138. /* 风门动画 */
  139. render() {
  140. if (!this.model) {
  141. return;
  142. }
  143. }
  144. /* 提取风门序列帧,初始化前后门动画 */
  145. initAnimation() {
  146. const meshArr01: THREE.Object3D[] = [];
  147. const meshArr02: THREE.Object3D[] = [];
  148. const windowGroup = new THREE.Group();
  149. windowGroup.name = 'hiddenGroup';
  150. this.group.getObjectByName('sdFc')?.children.forEach((obj) => {
  151. if (obj.type === 'Mesh' && obj.name && (obj.name.startsWith('shanye') || obj.name.startsWith('FCshanye'))) {
  152. if (obj.name.startsWith('FCshanye')) {
  153. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  154. meshArr01.push(obj);
  155. } else if (obj.name.startsWith('shanye')) {
  156. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  157. meshArr02.push(obj);
  158. }
  159. }
  160. });
  161. this.windowsActionArr.frontWindow = meshArr01;
  162. this.windowsActionArr.backWindow = meshArr02;
  163. this.group?.add(windowGroup);
  164. }
  165. /* 点击风窗,风窗全屏 */
  166. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  167. if (this.animationTimer) {
  168. clearTimeout(this.animationTimer);
  169. this.animationTimer = null;
  170. }
  171. // 判断是否点击到视频
  172. intersects.find((intersect) => {
  173. const mesh = intersect.object;
  174. return false;
  175. });
  176. }
  177. mouseUpModel() {}
  178. play(rotationParam, flag) {
  179. if (this.windowsActionArr.frontWindow.length <= 0 || this.windowsActionArr.backWindow.length <= 0) {
  180. return;
  181. }
  182. if (flag === 1) {
  183. // 前风窗动画
  184. this.windowsActionArr.frontWindow.forEach((mesh) => {
  185. gsap.to(mesh.rotation, {
  186. y: THREE.MathUtils.degToRad(rotationParam.frontDeg1),
  187. duration: (1 / 9) * Math.abs(rotationParam.frontDeg1 - mesh.rotation.y),
  188. overwrite: true,
  189. });
  190. });
  191. } else if (flag === 2) {
  192. // 后风窗动画
  193. this.windowsActionArr.backWindow.forEach((mesh) => {
  194. gsap.to(mesh.rotation, {
  195. y: THREE.MathUtils.degToRad(rotationParam.backDeg1),
  196. duration: (1 / 9) * Math.abs(rotationParam.backDeg1 - mesh.rotation.y),
  197. overwrite: true,
  198. });
  199. });
  200. } else if (flag === 0) {
  201. ([...this.windowsActionArr.frontWindow, ...this.windowsActionArr.backWindow] as THREE.Mesh[]).forEach((mesh) => {
  202. gsap.to(mesh.rotation, {
  203. y: 0,
  204. overwrite: true,
  205. });
  206. });
  207. }
  208. }
  209. async initCamera(dom1?, dom2?) {
  210. const videoPlayer1 = dom1;
  211. const videoPlayer2 = dom2;
  212. let monitorPlane: THREE.Mesh | null = null;
  213. if (!videoPlayer1 || !videoPlayer2) {
  214. const textArr = [
  215. {
  216. text: `无信号输入`,
  217. font: 'normal 40px Arial',
  218. color: '#009900',
  219. strokeStyle: '#002200',
  220. x: 170,
  221. y: 40,
  222. },
  223. ];
  224. const canvas = await getTextCanvas(320, 180, '', 'noSinge.png');
  225. let textMaterial: THREE.MeshBasicMaterial | null = null;
  226. if (canvas) {
  227. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  228. textMaterial = new THREE.MeshBasicMaterial({
  229. map: textMap, // 设置纹理贴图
  230. transparent: true,
  231. side: THREE.DoubleSide, // 这里是双面渲染的意思
  232. });
  233. textMaterial.blending = THREE.CustomBlending;
  234. const planeGeometry = new THREE.PlaneGeometry(100, 100); // 平面3维几何体PlaneGeometry
  235. monitorPlane = new THREE.Mesh(planeGeometry, textMaterial);
  236. textMaterial.dispose();
  237. planeGeometry.dispose();
  238. textMap.dispose();
  239. }
  240. }
  241. const player1 = this.group.getObjectByName('player1');
  242. if (player1) {
  243. this.model.clearMesh(player1);
  244. this.group.remove(player1);
  245. }
  246. const noPlayer1 = this.group.getObjectByName('noPlayer1');
  247. if (noPlayer1) {
  248. this.model.clearMesh(noPlayer1);
  249. this.group.remove(noPlayer1);
  250. }
  251. if (!videoPlayer1 && videoPlayer1 === null) {
  252. if (monitorPlane && !this.group.getObjectByName('noPlayer1')) {
  253. const planeMesh = monitorPlane.clone();
  254. planeMesh.name = 'noPlayer1';
  255. planeMesh.scale.set(0.011, 0.0053, 0.012);
  256. planeMesh.position.set(-4.3, 0.13, -0.23);
  257. this.group?.add(planeMesh.clone());
  258. }
  259. } else if (videoPlayer1) {
  260. try {
  261. const mesh = renderVideo(this.group, videoPlayer1, 'player1');
  262. if (mesh) {
  263. mesh?.scale.set(-0.034, 0.024, 1);
  264. mesh?.position.set(-3.332, 0.11, -0.23);
  265. mesh.rotation.y = -Math.PI;
  266. this.group.add(mesh);
  267. }
  268. } catch (error) {
  269. console.log('视频信号异常');
  270. }
  271. }
  272. const player2 = this.group.getObjectByName('player2');
  273. if (player2) {
  274. this.model.clearMesh(player2);
  275. this.group.remove(player2);
  276. }
  277. const noPlayer2 = this.group.getObjectByName('noPlayer2');
  278. if (noPlayer2) {
  279. this.model.clearMesh(noPlayer2);
  280. this.group.remove(noPlayer2);
  281. }
  282. if (!videoPlayer2 && videoPlayer2 === null) {
  283. if (monitorPlane && !this.group.getObjectByName('noPlayer2')) {
  284. const planeMesh = monitorPlane.clone();
  285. planeMesh.name = 'noPlayer2';
  286. planeMesh.scale.set(0.0085, 0.0056, 0.012);
  287. planeMesh.position.set(4.29, 0.02, -0.41);
  288. this.group?.add(planeMesh.clone());
  289. }
  290. } else if (videoPlayer2) {
  291. try {
  292. const mesh = renderVideo(this.group, videoPlayer2, 'player2');
  293. if (mesh) {
  294. mesh?.scale.set(-0.034, 0.024, 1);
  295. mesh?.position.set(-3.332, 0.11, -0.23);
  296. mesh.rotation.y = -Math.PI;
  297. this.group.add(mesh);
  298. }
  299. } catch (error) {
  300. console.log('视频信号异常');
  301. }
  302. }
  303. }
  304. mountedThree(playerDom) {
  305. return new Promise((resolve) => {
  306. this.model.setGLTFModel(['sdFc'], this.group).then(() => {
  307. this.setModalPosition();
  308. this.initAnimation();
  309. resolve(null);
  310. // this.initCamera(playerDom);
  311. });
  312. });
  313. }
  314. destroy() {
  315. this.model.clearGroup(this.group);
  316. this.windowsActionArr.frontWindow = undefined;
  317. this.windowsActionArr.backWindow = undefined;
  318. this.model = null;
  319. this.group = null;
  320. }
  321. }
  322. export default doubleWindow;