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