dandaoFc.threejs.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. import * as THREE from 'three';
  2. import { getTextCanvas, renderVideo } from '/@/utils/threejs/util';
  3. import gsap from 'gsap';
  4. class singleWindow {
  5. model;
  6. modelName = 'ddFc';
  7. group: THREE.Object3D = new THREE.Object3D();
  8. animationTimer;
  9. isLRAnimation = true;
  10. direction = 1;
  11. windowsActionArr = {
  12. frontWindow: [],
  13. };
  14. player1;
  15. player2;
  16. playerStartClickTime1 = new Date().getTime();
  17. constructor(model) {
  18. this.model = model;
  19. // this.group.name = 'ddFc';
  20. }
  21. // // 重置摄像头
  22. // const resetCamera = () => {
  23. // this.model.camera.position.set(30.328, 58.993, 148.315);
  24. // this.model.camera.rotation.set(-27.88, 14.35, 7.47);
  25. // this.model.orbitControls?.update();
  26. // this.model.camera.updateProjectionMatrix();
  27. // };
  28. // 设置模型位置
  29. setModalPosition() {
  30. this.group?.scale.set(22, 22, 22);
  31. this.group?.position.set(-35, 25, 15);
  32. }
  33. addMonitorText(selectData) {
  34. if (!this.group) {
  35. return;
  36. }
  37. const screenDownText = VENT_PARAM['modalText']
  38. ? VENT_PARAM['modalText']
  39. : History_Type['type'] == 'remote'
  40. ? `国能神东煤炭集团监制`
  41. : '煤炭科学技术研究院有限公司研制';
  42. const screenDownTextX = 120 - (screenDownText.length - 10) * 6;
  43. const textArr = [
  44. {
  45. text: `远程定量调节自动风窗`,
  46. font: 'normal 30px Arial',
  47. color: '#009900',
  48. strokeStyle: '#002200',
  49. x: 110,
  50. y: 90,
  51. },
  52. {
  53. text: `${selectData.OpenDegree ? '开度值(°)' : selectData.forntArea ? '过风面积(㎡)' : '过风面积(㎡)'}:`,
  54. font: 'normal 30px Arial',
  55. color: '#009900',
  56. strokeStyle: '#002200',
  57. x: 5,
  58. y: 145,
  59. },
  60. {
  61. text: selectData.OpenDegree
  62. ? Number(`${selectData.OpenDegree}`).toFixed(2)
  63. : selectData.forntArea
  64. ? Number(`${selectData.forntArea}`).toFixed(2)
  65. : '-',
  66. font: 'normal 30px Arial',
  67. color: '#009900',
  68. strokeStyle: '#002200',
  69. x: 330,
  70. y: 145,
  71. },
  72. {
  73. text: `${selectData.frontRearDP ? '风窗压差(Pa)' : selectData.windSpeed ? '风速(m/s)' : '通信状态'}:`,
  74. font: 'normal 30px Arial',
  75. color: '#009900',
  76. strokeStyle: '#002200',
  77. x: 5,
  78. y: 200,
  79. },
  80. {
  81. text: `${
  82. selectData.frontRearDP
  83. ? selectData.frontRearDP
  84. : selectData.windSpeed
  85. ? selectData.windSpeed
  86. : selectData.netStatus == '0'
  87. ? '断开'
  88. : '连接'
  89. }`,
  90. font: 'normal 30px Arial',
  91. color: '#009900',
  92. strokeStyle: '#002200',
  93. x: 330,
  94. y: 200,
  95. },
  96. {
  97. text: `${selectData.fWindowM3 ? '过风量(m³/min)' : '风窗道数'}: `,
  98. font: 'normal 30px Arial',
  99. color: '#009900',
  100. strokeStyle: '#002200',
  101. x: 5,
  102. y: 256,
  103. },
  104. {
  105. text: `${selectData.fWindowM3 ? selectData.fWindowM3 : selectData.nwindownum}`,
  106. font: 'normal 30px Arial',
  107. color: '#009900',
  108. strokeStyle: '#002200',
  109. x: 330,
  110. y: 256,
  111. },
  112. {
  113. text: screenDownText,
  114. font: 'normal 28px Arial',
  115. color: '#009900',
  116. strokeStyle: '#002200',
  117. x: screenDownTextX,
  118. y: 302,
  119. },
  120. ];
  121. getTextCanvas(726, 546, textArr, '').then((canvas: HTMLCanvasElement) => {
  122. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  123. const textMaterial = new THREE.MeshBasicMaterial({
  124. // 关于材质并未讲解 实操即可熟悉 这里是漫反射类似纸张的材质,对应的就有高光类似金属的材质.
  125. map: textMap, // 设置纹理贴图
  126. transparent: true,
  127. side: THREE.DoubleSide, // 这里是双面渲染的意思
  128. });
  129. textMap.dispose();
  130. textMaterial.blending = THREE.CustomBlending;
  131. const monitorPlane = this.group?.getObjectByName('monitorText');
  132. if (monitorPlane) {
  133. monitorPlane.material = textMaterial;
  134. } else {
  135. const planeGeometry = new THREE.PlaneGeometry(570, 346); // 平面3维几何体PlaneGeometry
  136. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  137. planeMesh.name = 'monitorText';
  138. planeMesh.scale.set(0.0025, 0.003, 0.002);
  139. planeMesh.position.set(3.71, -0.042, -0.23);
  140. this.group?.add(planeMesh);
  141. }
  142. });
  143. }
  144. /* 提取风门序列帧,初始化前后门动画 */
  145. initAnimation() {
  146. const meshArr01: THREE.Object3D[] = [];
  147. if (this.group) {
  148. this.group.getObjectByName('ddFc')?.children.forEach((obj) => {
  149. if (obj.type === 'Mesh' && ((obj.name && obj.name.startsWith('shanye')) || obj.name.startsWith('FCshanye'))) {
  150. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  151. meshArr01.push(obj);
  152. }
  153. });
  154. }
  155. this.windowsActionArr.frontWindow = meshArr01;
  156. }
  157. play(rotationParam, flag) {
  158. if (this.windowsActionArr.frontWindow.length <= 0) {
  159. return;
  160. }
  161. if (flag === 1) {
  162. // 前风窗动画
  163. this.windowsActionArr.frontWindow.forEach((mesh: THREE.Mesh) => {
  164. gsap.to(mesh.rotation, {
  165. y: THREE.MathUtils.degToRad(rotationParam.frontDeg1),
  166. duration: (1 / 9) * Math.abs(rotationParam.frontDeg1 - mesh.rotation.y),
  167. overwrite: true,
  168. });
  169. });
  170. } else if (flag === 0) {
  171. ([...this.windowsActionArr.frontWindow] as THREE.Mesh[]).forEach((mesh) => {
  172. gsap.to(mesh.rotation, {
  173. y: 0,
  174. overwrite: true,
  175. });
  176. });
  177. }
  178. }
  179. /* 点击风窗,风窗全屏 */
  180. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  181. this.isLRAnimation = false;
  182. if (this.animationTimer) {
  183. clearTimeout(this.animationTimer);
  184. this.animationTimer = null;
  185. }
  186. // 判断是否点击到视频
  187. intersects.find((intersect) => {
  188. const mesh = intersect.object;
  189. if (mesh.name === 'player1') {
  190. if (new Date().getTime() - this.playerStartClickTime1 < 400) {
  191. // 双击,视频放大
  192. if (this.player1) {
  193. this.player1.requestFullscreen();
  194. }
  195. }
  196. this.playerStartClickTime1 = new Date().getTime();
  197. return true;
  198. }
  199. return false;
  200. });
  201. }
  202. mouseUpModel() {
  203. // 10s后开始摆动
  204. if (!this.animationTimer && !this.isLRAnimation) {
  205. this.animationTimer = setTimeout(() => {
  206. this.isLRAnimation = true;
  207. }, 10000);
  208. }
  209. }
  210. /* 风门动画 */
  211. render() {
  212. if (!this.model) {
  213. return;
  214. }
  215. if (this.isLRAnimation && this.group) {
  216. // 左右摇摆动画
  217. if (Math.abs(this.group.rotation.y) >= 0.2) {
  218. this.direction = -this.direction;
  219. this.group.rotation.y += 0.00002 * 30 * this.direction;
  220. } else {
  221. this.group.rotation.y += 0.00002 * 30 * this.direction;
  222. }
  223. }
  224. }
  225. async initCamera(dom1) {
  226. const videoPlayer1 = dom1;
  227. let monitorPlane: THREE.Mesh | null = null;
  228. const textArr = [
  229. {
  230. text: `无信号输入`,
  231. font: 'normal 40px Arial',
  232. color: '#009900',
  233. strokeStyle: '#002200',
  234. x: 170,
  235. y: 40,
  236. },
  237. ];
  238. const canvas = await getTextCanvas(320, 180, textArr, null);
  239. if (canvas) {
  240. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  241. const textMaterial = new THREE.MeshBasicMaterial({
  242. map: textMap, // 设置纹理贴图
  243. transparent: true,
  244. side: THREE.DoubleSide, // 这里是双面渲染的意思
  245. });
  246. textMaterial.blending = THREE.CustomBlending;
  247. monitorPlane = this.group?.getObjectByName('noPlayer');
  248. if (monitorPlane) {
  249. monitorPlane.material = textMaterial;
  250. } else {
  251. const planeGeometry = new THREE.PlaneGeometry(100, 100); // 平面3维几何体PlaneGeometry
  252. monitorPlane = new THREE.Mesh(planeGeometry, textMaterial);
  253. textMaterial.dispose();
  254. planeGeometry.dispose();
  255. }
  256. }
  257. const videoPlayer = this.group.getObjectByName('player1');
  258. if (videoPlayer) {
  259. this.model.clearMesh(videoPlayer);
  260. this.group.remove(videoPlayer);
  261. }
  262. const noPlayer1 = this.group.getObjectByName('noPlayer1');
  263. if (noPlayer1) {
  264. this.model.clearMesh(noPlayer1);
  265. this.group.remove(noPlayer1);
  266. }
  267. if (!videoPlayer1 && videoPlayer1 === null) {
  268. if (monitorPlane && !this.group.getObjectByName('noPlayer1')) {
  269. const planeMesh = monitorPlane.clone();
  270. planeMesh.name = 'noPlayer1';
  271. planeMesh.scale.set(0.011, 0.0053, 0.012);
  272. planeMesh.position.set(-4.3, 0.13, -0.23);
  273. this.group?.add(planeMesh.clone());
  274. }
  275. } else if (videoPlayer1) {
  276. try {
  277. const mesh = renderVideo(this.group, videoPlayer1, 'player1');
  278. if (mesh) {
  279. mesh?.scale.set(0.0382, 0.028, 0.022);
  280. mesh?.position.set(-3.008, 0.148, -0.22);
  281. this.group.add(mesh);
  282. }
  283. } catch (error) {
  284. console.log('视频信号异常');
  285. }
  286. }
  287. }
  288. mountedThree(playerDom) {
  289. return new Promise((resolve) => {
  290. this.model.setGLTFModel(['ddFc'], this.group).then(() => {
  291. this.setModalPosition();
  292. this.initAnimation();
  293. resolve(null);
  294. });
  295. });
  296. }
  297. destroy() {
  298. this.model.clearGroup(this.group);
  299. this.windowsActionArr.frontWindow = undefined;
  300. this.model = null;
  301. this.group = null;
  302. }
  303. }
  304. export default singleWindow;