shuangdaoFc.threejs.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. if (this.isLRAnimation && this.group) {
  144. // 左右摇摆动画
  145. if (Math.abs(this.group.rotation.y) >= 0.2) {
  146. this.direction = -this.direction;
  147. this.group.rotation.y += 0.00002 * 30 * this.direction;
  148. } else {
  149. this.group.rotation.y += 0.00002 * 30 * this.direction;
  150. }
  151. }
  152. }
  153. /* 提取风门序列帧,初始化前后门动画 */
  154. initAnimation() {
  155. const meshArr01: THREE.Object3D[] = [];
  156. const meshArr02: THREE.Object3D[] = [];
  157. const windowGroup = new THREE.Group();
  158. windowGroup.name = 'hiddenGroup';
  159. this.group.getObjectByName('sdFc')?.children.forEach((obj) => {
  160. if (obj.type === 'Mesh' && obj.name && (obj.name.startsWith('shanye') || obj.name.startsWith('FCshanye'))) {
  161. if (obj.name.startsWith('FCshanye')) {
  162. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  163. meshArr01.push(obj);
  164. } else if (obj.name.startsWith('shanye')) {
  165. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  166. meshArr02.push(obj);
  167. }
  168. }
  169. });
  170. this.windowsActionArr.frontWindow = meshArr01;
  171. this.windowsActionArr.backWindow = meshArr02;
  172. this.group?.add(windowGroup);
  173. }
  174. /* 点击风窗,风窗全屏 */
  175. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  176. this.isLRAnimation = false;
  177. if (this.animationTimer) {
  178. clearTimeout(this.animationTimer);
  179. this.animationTimer = null;
  180. }
  181. // 判断是否点击到视频
  182. intersects.find((intersect) => {
  183. const mesh = intersect.object;
  184. return false;
  185. });
  186. }
  187. mouseUpModel() {
  188. // 10s后开始摆动
  189. if (!this.animationTimer && !this.isLRAnimation) {
  190. this.animationTimer = setTimeout(() => {
  191. this.isLRAnimation = true;
  192. }, 10000);
  193. }
  194. }
  195. play(rotationParam, flag) {
  196. if (this.windowsActionArr.frontWindow.length <= 0 || this.windowsActionArr.backWindow.length <= 0) {
  197. return;
  198. }
  199. if (flag === 1) {
  200. // 前风窗动画
  201. this.windowsActionArr.frontWindow.forEach((mesh) => {
  202. gsap.to(mesh.rotation, {
  203. y: THREE.MathUtils.degToRad(rotationParam.frontDeg1),
  204. duration: (1 / 9) * Math.abs(rotationParam.frontDeg1 - mesh.rotation.y),
  205. overwrite: true,
  206. });
  207. });
  208. } else if (flag === 2) {
  209. // 后风窗动画
  210. this.windowsActionArr.backWindow.forEach((mesh) => {
  211. gsap.to(mesh.rotation, {
  212. y: THREE.MathUtils.degToRad(rotationParam.backDeg1),
  213. duration: (1 / 9) * Math.abs(rotationParam.backDeg1 - mesh.rotation.y),
  214. overwrite: true,
  215. });
  216. });
  217. } else if (flag === 0) {
  218. ([...this.windowsActionArr.frontWindow, ...this.windowsActionArr.backWindow] as THREE.Mesh[]).forEach((mesh) => {
  219. gsap.to(mesh.rotation, {
  220. y: 0,
  221. overwrite: true,
  222. });
  223. });
  224. }
  225. }
  226. async initCamera(dom1?, dom2?) {
  227. const videoPlayer1 = dom1;
  228. const videoPlayer2 = dom2;
  229. let monitorPlane: THREE.Mesh | null = null;
  230. if (!videoPlayer1 || !videoPlayer2) {
  231. const textArr = [
  232. {
  233. text: `无信号输入`,
  234. font: 'normal 40px Arial',
  235. color: '#009900',
  236. strokeStyle: '#002200',
  237. x: 170,
  238. y: 40,
  239. },
  240. ];
  241. const canvas = await getTextCanvas(320, 180, '', 'noSinge.png');
  242. let textMaterial: THREE.MeshBasicMaterial | null = null;
  243. if (canvas) {
  244. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  245. textMaterial = new THREE.MeshBasicMaterial({
  246. map: textMap, // 设置纹理贴图
  247. transparent: true,
  248. side: THREE.DoubleSide, // 这里是双面渲染的意思
  249. });
  250. textMaterial.blending = THREE.CustomBlending;
  251. const planeGeometry = new THREE.PlaneGeometry(100, 100); // 平面3维几何体PlaneGeometry
  252. monitorPlane = new THREE.Mesh(planeGeometry, textMaterial);
  253. textMaterial.dispose();
  254. planeGeometry.dispose();
  255. textMap.dispose();
  256. }
  257. }
  258. const player1 = this.group.getObjectByName('player1');
  259. if (player1) {
  260. this.model.clearMesh(player1);
  261. this.group.remove(player1);
  262. }
  263. const noPlayer1 = this.group.getObjectByName('noPlayer1');
  264. if (noPlayer1) {
  265. this.model.clearMesh(noPlayer1);
  266. this.group.remove(noPlayer1);
  267. }
  268. if (!videoPlayer1 && videoPlayer1 === null) {
  269. if (monitorPlane && !this.group.getObjectByName('noPlayer1')) {
  270. const planeMesh = monitorPlane.clone();
  271. planeMesh.name = 'noPlayer1';
  272. planeMesh.scale.set(0.011, 0.0053, 0.012);
  273. planeMesh.position.set(-4.3, 0.13, -0.23);
  274. this.group?.add(planeMesh.clone());
  275. }
  276. } else if (videoPlayer1) {
  277. try {
  278. const mesh = renderVideo(this.group, videoPlayer1, 'player1');
  279. if (mesh) {
  280. mesh?.scale.set(-0.034, 0.024, 1);
  281. mesh?.position.set(-3.332, 0.11, -0.23);
  282. mesh.rotation.y = -Math.PI;
  283. this.group.add(mesh);
  284. }
  285. } catch (error) {
  286. console.log('视频信号异常');
  287. }
  288. }
  289. const player2 = this.group.getObjectByName('player2');
  290. if (player2) {
  291. this.model.clearMesh(player2);
  292. this.group.remove(player2);
  293. }
  294. const noPlayer2 = this.group.getObjectByName('noPlayer2');
  295. if (noPlayer2) {
  296. this.model.clearMesh(noPlayer2);
  297. this.group.remove(noPlayer2);
  298. }
  299. if (!videoPlayer2 && videoPlayer2 === null) {
  300. if (monitorPlane && !this.group.getObjectByName('noPlayer2')) {
  301. const planeMesh = monitorPlane.clone();
  302. planeMesh.name = 'noPlayer2';
  303. planeMesh.scale.set(0.0085, 0.0056, 0.012);
  304. planeMesh.position.set(4.29, 0.02, -0.41);
  305. this.group?.add(planeMesh.clone());
  306. }
  307. } else if (videoPlayer2) {
  308. try {
  309. const mesh = renderVideo(this.group, videoPlayer2, 'player2');
  310. if (mesh) {
  311. mesh?.scale.set(-0.034, 0.024, 1);
  312. mesh?.position.set(-3.332, 0.11, -0.23);
  313. mesh.rotation.y = -Math.PI;
  314. this.group.add(mesh);
  315. }
  316. } catch (error) {
  317. console.log('视频信号异常');
  318. }
  319. }
  320. }
  321. mountedThree(playerDom) {
  322. return new Promise((resolve) => {
  323. this.model.setGLTFModel(['sdFc'], this.group).then(() => {
  324. this.setModalPosition();
  325. this.initAnimation();
  326. resolve(null);
  327. // this.initCamera(playerDom);
  328. });
  329. });
  330. }
  331. destroy() {
  332. this.model.clearGroup(this.group);
  333. this.windowsActionArr.frontWindow = undefined;
  334. this.windowsActionArr.backWindow = undefined;
  335. this.model = null;
  336. this.group = null;
  337. }
  338. }
  339. export default doubleWindow;