shuangdaoFc.threejs.ts 11 KB

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