shuangdaoFc.threejs.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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?.scale.set(22, 22, 22);
  24. this.group?.position.set(-15, 25, 15);
  25. }
  26. addMonitorText(selectData) {
  27. if (!this.group) {
  28. return;
  29. }
  30. const textArr = [
  31. {
  32. text: `远程定量调节自动风窗`,
  33. font: 'normal 30px Arial',
  34. color: '#009900',
  35. strokeStyle: '#002200',
  36. x: 95,
  37. y: 95,
  38. },
  39. {
  40. text: `过风量(m3/min):`,
  41. font: 'normal 28px Arial',
  42. color: '#009900',
  43. strokeStyle: '#002200',
  44. x: 5,
  45. y: 150,
  46. },
  47. {
  48. text: selectData.forntm3 ? Number(`${selectData.forntm3}`).toFixed(0) : '-',
  49. font: 'normal 28px Arial',
  50. color: '#009900',
  51. strokeStyle: '#002200',
  52. x: 215,
  53. y: 150,
  54. },
  55. {
  56. text: `过风面积(m2): `,
  57. font: 'normal 28px Arial',
  58. color: '#009900',
  59. strokeStyle: '#002200',
  60. x: 5,
  61. y: 205,
  62. },
  63. {
  64. text: `${selectData.forntArea ? selectData.forntArea : '-'}`,
  65. font: 'normal 28px Arial',
  66. color: '#009900',
  67. strokeStyle: '#002200',
  68. x: 200,
  69. y: 205,
  70. },
  71. {
  72. text: `风窗压差(Pa):`,
  73. font: 'normal 28px Arial',
  74. color: '#009900',
  75. strokeStyle: '#002200',
  76. x: 5,
  77. y: 256,
  78. },
  79. {
  80. text: `${selectData.frontRearDP ? selectData.frontRearDP : '-'}`,
  81. font: 'normal 28px Arial',
  82. color: '#009900',
  83. strokeStyle: '#002200',
  84. x: 200,
  85. y: 256,
  86. },
  87. {
  88. text: `调节精度:`,
  89. font: 'normal 28px Arial',
  90. color: '#009900',
  91. strokeStyle: '#002200',
  92. x: 330,
  93. y: 150,
  94. },
  95. {
  96. // text: `1% FS`,
  97. text: `-`,
  98. font: 'normal 28px Arial',
  99. color: '#009900',
  100. strokeStyle: '#002200',
  101. x: 460,
  102. y: 150,
  103. },
  104. {
  105. text: `调节范围(㎡):`,
  106. font: 'normal 28px Arial',
  107. color: '#009900',
  108. strokeStyle: '#002200',
  109. x: 330,
  110. y: 205,
  111. },
  112. {
  113. text: `0~${selectData.maxarea ? selectData.maxarea : '-'}`,
  114. font: 'normal 28px Arial',
  115. color: '#009900',
  116. strokeStyle: '#002200',
  117. x: 500,
  118. y: 205,
  119. },
  120. {
  121. text: History_Type['type'] == 'remote' ? `国能神东煤炭集团监制` : '煤炭科学技术研究院有限公司研制',
  122. font: 'normal 28px Arial',
  123. color: '#009900',
  124. strokeStyle: '#002200',
  125. x: History_Type['type'] == 'remote' ? 120 : 60,
  126. y: 302,
  127. },
  128. ];
  129. getTextCanvas(570, 346, textArr, '').then((canvas: HTMLCanvasElement) => {
  130. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  131. const textMaterial = new THREE.MeshBasicMaterial({
  132. // 关于材质并未讲解 实操即可熟悉 这里是漫反射类似纸张的材质,对应的就有高光类似金属的材质.
  133. map: textMap, // 设置纹理贴图
  134. transparent: true,
  135. side: THREE.DoubleSide, // 这里是双面渲染的意思
  136. });
  137. textMaterial.blending = THREE.CustomBlending;
  138. const monitorPlane = this.group?.getObjectByName('monitorText');
  139. if (monitorPlane) {
  140. monitorPlane.material = textMaterial;
  141. } else {
  142. const planeGeometry = new THREE.PlaneGeometry(570, 346); // 平面3维几何体PlaneGeometry
  143. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  144. planeMesh.name = 'monitorText';
  145. planeMesh.scale.set(0.0019, 0.002, 0.002);
  146. planeMesh.position.set(2.66, 0.158, -0.23);
  147. this.group?.add(planeMesh);
  148. }
  149. textMap.dispose();
  150. });
  151. }
  152. /* 风门动画 */
  153. render() {
  154. if (!this.model) {
  155. return;
  156. }
  157. if (this.isLRAnimation && this.group) {
  158. // 左右摇摆动画
  159. if (Math.abs(this.group.rotation.y) >= 0.2) {
  160. this.direction = -this.direction;
  161. this.group.rotation.y += 0.00002 * 30 * this.direction;
  162. } else {
  163. this.group.rotation.y += 0.00002 * 30 * this.direction;
  164. }
  165. }
  166. }
  167. /* 提取风门序列帧,初始化前后门动画 */
  168. initAnimation() {
  169. const meshArr01: THREE.Object3D[] = [];
  170. const meshArr02: THREE.Object3D[] = [];
  171. const windowGroup = new THREE.Group();
  172. windowGroup.name = 'hiddenGroup';
  173. this.group?.children.forEach((obj) => {
  174. if (obj.type === 'Mesh' && obj.name && (obj.name.startsWith('shanye') || obj.name.startsWith('FCshanye'))) {
  175. if (obj.name.startsWith('FCshanye')) {
  176. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  177. meshArr01.push(obj);
  178. } else if (obj.name.startsWith('shanye')) {
  179. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  180. meshArr02.push(obj);
  181. }
  182. }
  183. });
  184. this.windowsActionArr.frontWindow = meshArr01;
  185. this.windowsActionArr.backWindow = meshArr02;
  186. this.group?.add(windowGroup);
  187. }
  188. /* 点击风窗,风窗全屏 */
  189. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  190. this.isLRAnimation = false;
  191. if (this.animationTimer) {
  192. clearTimeout(this.animationTimer);
  193. this.animationTimer = null;
  194. }
  195. // 判断是否点击到视频
  196. intersects.find((intersect) => {
  197. const mesh = intersect.object;
  198. return false;
  199. });
  200. }
  201. mouseUpModel() {
  202. // 10s后开始摆动
  203. if (!this.animationTimer && !this.isLRAnimation) {
  204. this.animationTimer = setTimeout(() => {
  205. this.isLRAnimation = true;
  206. }, 10000);
  207. }
  208. }
  209. play(rotationParam, flag) {
  210. if (!this.windowsActionArr.frontWindow || !this.windowsActionArr.backWindow) {
  211. return;
  212. }
  213. if (flag === 1) {
  214. // 前风窗动画
  215. this.windowsActionArr.frontWindow.forEach((mesh) => {
  216. gsap.to(mesh.rotation, {
  217. y: THREE.MathUtils.degToRad(rotationParam.frontDeg1),
  218. duration: (1 / 9) * Math.abs(rotationParam.frontDeg1 - mesh.rotation.y),
  219. overwrite: true,
  220. });
  221. });
  222. } else if (flag === 2) {
  223. // 后风窗动画
  224. this.windowsActionArr.backWindow.forEach((mesh) => {
  225. gsap.to(mesh.rotation, {
  226. y: THREE.MathUtils.degToRad(rotationParam.backDeg1),
  227. duration: (1 / 9) * Math.abs(rotationParam.backDeg1 - mesh.rotation.y),
  228. overwrite: true,
  229. });
  230. });
  231. } else if (flag === 0) {
  232. ([...this.windowsActionArr.frontWindow, ...this.windowsActionArr.backWindow] as THREE.Mesh[]).forEach((mesh) => {
  233. gsap.to(mesh.rotation, {
  234. y: 0,
  235. overwrite: true,
  236. });
  237. });
  238. }
  239. }
  240. async initCamera(dom1?, dom2?) {
  241. const videoPlayer1 = dom1;
  242. const videoPlayer2 = dom2;
  243. let monitorPlane: THREE.Mesh | null = null;
  244. if (!videoPlayer1 || !videoPlayer2) {
  245. const textArr = [
  246. {
  247. text: `无信号输入`,
  248. font: 'normal 40px Arial',
  249. color: '#009900',
  250. strokeStyle: '#002200',
  251. x: 170,
  252. y: 40,
  253. },
  254. ];
  255. const canvas = await getTextCanvas(320, 180, '', 'noSinge.png');
  256. let textMaterial: THREE.MeshBasicMaterial | null = null;
  257. if (canvas) {
  258. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  259. textMaterial = new THREE.MeshBasicMaterial({
  260. map: textMap, // 设置纹理贴图
  261. transparent: true,
  262. side: THREE.DoubleSide, // 这里是双面渲染的意思
  263. });
  264. textMaterial.blending = THREE.CustomBlending;
  265. const planeGeometry = new THREE.PlaneGeometry(100, 100); // 平面3维几何体PlaneGeometry
  266. monitorPlane = new THREE.Mesh(planeGeometry, textMaterial);
  267. textMaterial.dispose();
  268. planeGeometry.dispose();
  269. textMap.dispose();
  270. }
  271. }
  272. const player1 = this.group.getObjectByName('player1');
  273. if (player1) {
  274. this.model.clearMesh(player1);
  275. this.group.remove(player1);
  276. }
  277. const noPlayer1 = this.group.getObjectByName('noPlayer1');
  278. if (noPlayer1) {
  279. this.model.clearMesh(noPlayer1);
  280. this.group.remove(noPlayer1);
  281. }
  282. if (!videoPlayer1 && videoPlayer1 === null) {
  283. if (monitorPlane && !this.group.getObjectByName('noPlayer1')) {
  284. const planeMesh = monitorPlane.clone();
  285. planeMesh.name = 'noPlayer1';
  286. planeMesh.scale.set(0.011, 0.0053, 0.012);
  287. planeMesh.position.set(-4.3, 0.13, -0.23);
  288. this.group?.add(planeMesh.clone());
  289. }
  290. } else if (videoPlayer1) {
  291. try {
  292. const mesh = renderVideo(this.group, videoPlayer1, 'player1');
  293. if (mesh) {
  294. mesh?.scale.set(-0.038, 0.029, 1);
  295. mesh?.position.set(-4.302, 0.15, -0.23);
  296. mesh.rotation.y = -Math.PI;
  297. this.group.add(mesh);
  298. }
  299. } catch (error) {
  300. console.log('视频信号异常');
  301. }
  302. }
  303. const player2 = this.group.getObjectByName('player2');
  304. if (player2) {
  305. this.model.clearMesh(player2);
  306. this.group.remove(player2);
  307. }
  308. const noPlayer2 = this.group.getObjectByName('noPlayer2');
  309. if (noPlayer2) {
  310. this.model.clearMesh(noPlayer2);
  311. this.group.remove(noPlayer2);
  312. }
  313. if (!videoPlayer2 && videoPlayer2 === null) {
  314. if (monitorPlane && !this.group.getObjectByName('noPlayer2')) {
  315. const planeMesh = monitorPlane.clone();
  316. planeMesh.name = 'noPlayer2';
  317. planeMesh.scale.set(0.0085, 0.0056, 0.012);
  318. planeMesh.position.set(4.29, 0.02, -0.41);
  319. this.group?.add(planeMesh.clone());
  320. }
  321. } else if (videoPlayer2) {
  322. try {
  323. const mesh = renderVideo(this.group, videoPlayer2, 'player2');
  324. if (mesh) {
  325. mesh?.scale.set(-0.028, 0.0285, 1);
  326. mesh?.position.set(4.298, 0.02, -0.4);
  327. mesh.rotation.y = -Math.PI;
  328. this.group.add(mesh);
  329. }
  330. } catch (error) {
  331. console.log('视频信号异常');
  332. }
  333. }
  334. }
  335. mountedThree(playerDom) {
  336. return new Promise((resolve) => {
  337. this.model.setGLTFModel(['sdFc']).then((gltf) => {
  338. this.group = gltf[0];
  339. this.setModalPosition();
  340. this.initAnimation();
  341. resolve(null);
  342. this.initCamera(playerDom);
  343. });
  344. });
  345. }
  346. destroy() {
  347. this.model.clearGroup(this.group);
  348. this.windowsActionArr.frontWindow = undefined;
  349. this.windowsActionArr.backWindow = undefined;
  350. this.model = null;
  351. this.group = null;
  352. }
  353. }
  354. export default doubleWindow;