shuangdaoFc.threejs.ts 11 KB

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