UEpix.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <script setup>
  2. // npm i @epicgames-ps/lib-pixelstreamingfrontend-ue5.2 @epicgames-ps/lib-pixelstreamingfrontend-ui-ue5.2
  3. import {
  4. Config,
  5. PixelStreaming,
  6. } from "@epicgames-ps/lib-pixelstreamingfrontend-ue5.2";
  7. import {
  8. Application,
  9. PixelStreamingApplicationStyle,
  10. } from "@epicgames-ps/lib-pixelstreamingfrontend-ui-ue5.2";
  11. import { onMounted } from "vue";
  12. import { ref } from "vue";
  13. const props = defineProps({
  14. ss: {
  15. type: String,
  16. default: "ws://127.0.0.1/",
  17. },
  18. });
  19. const loading = ref(true);
  20. const emitter = defineEmits(["receiveMessage", "loaded"]);
  21. let ueWebEmitter = null;
  22. const pixelStreamingContainerRef = ref(null);
  23. const STATE = {
  24. stream: null,
  25. application: null,
  26. };
  27. const initPixelStreaming = () => {
  28. // 控制UI样式(在这里去除了设置按钮)
  29. const PixelStreamingApplicationStyles = new PixelStreamingApplicationStyle({
  30. customStyles: {
  31. "#uiFeatures": {
  32. display: "none",
  33. },
  34. },
  35. });
  36. PixelStreamingApplicationStyles.applyStyleSheet();
  37. // 像素流播放的一些配置
  38. const config = new Config({
  39. useUrlParams: true,
  40. initialSettings: {
  41. ss: props.ss, // 流媒体服务器地址
  42. AutoConnect: true, // 自动连接:如果为 true,应用启动时会自动连接到流媒体服务器。
  43. AutoPlayVideo: true, // 自动播放视频:如果为 true,在连接成功后视频将自动播放。
  44. StartVideoMuted: true, // 启动时静音:如果为 true,视频在开始播放时会处于静音状态。
  45. KeyboardInput: false, // 键盘输入:如果为 true,允许使用键盘进行控制。
  46. MinQP: 15, // 最小量化参数:用于控制视频编码的最小质量水平,数值越低,品质越好,但带宽消耗也越大。
  47. XRControllerInput: false, // XR 控制器输入:如果为 true,允许 XR(虚拟现实)控制器的输入。
  48. GamepadInput: false, // 游戏手柄输入:如果为 true,允许使用游戏手柄进行控制。
  49. TouchInput: false, // 触摸输入:如果为 true,允许在触摸屏设备上进行交互。
  50. HoveringMouse: true, // 鼠标悬停:如果为 true,允许鼠标悬停并与 UI 进行交互。
  51. HoveringMouseMode: true, // 鼠标悬停模式:如果为 true,激活鼠标悬停模式以增强 UI 交互体验。
  52. SuppressBrowserKeys: false, // 抑制浏览器按键:如果为 true,将抑制浏览器的某些键盘输入,以防止干扰应用内的控制。
  53. MatchViewportRes: true, // 匹配视窗分辨率:如果为 true,将调整视频分辨率以匹配视窗分辨率。
  54. },
  55. });
  56. const stream = new PixelStreaming(config);
  57. const application = new Application({
  58. stream,
  59. onColorModeChanged: (isLightMode) =>
  60. PixelStreamingApplicationStyles.setColorMode(isLightMode),
  61. });
  62. // 加载完毕 重写 onPlayStream 添加 loading 状态
  63. const onPlayStream = application.onPlayStream.bind(application);
  64. application.onPlayStream = (...args) => {
  65. onPlayStream(...args);
  66. loading.value = false;
  67. emitter("loaded");
  68. };
  69. // 添加到 dom
  70. pixelStreamingContainerRef.value.appendChild(application.rootElement);
  71. STATE.stream = stream;
  72. STATE.application = application;
  73. // 监听消息
  74. stream.addResponseEventListener("handle_responses", (data) => {
  75. emitter("receiveMessage", data);
  76. ueWebEmitter?.emit("receive-pixel-msg", data);
  77. });
  78. };
  79. onMounted(() => {
  80. initPixelStreaming();
  81. });
  82. // 导出接口
  83. defineExpose({
  84. // 像素流发送消息给 UE
  85. sendMsgToUE: (payload) => {
  86. if (!STATE.stream) return;
  87. STATE.stream.emitUIInteraction(payload);
  88. },
  89. // 设置 WEB - UE - 像素流 通信的 emitter
  90. setUeWebEmitter: (emitter) => {
  91. (ueWebEmitter = emitter)
  92. },
  93. });
  94. </script>
  95. <template>
  96. <div
  97. class="pixel-streaming-container"
  98. ref="pixelStreamingContainerRef"
  99. :class="{
  100. loaded: !loading,
  101. }"
  102. ></div>
  103. </template>
  104. <style scoped>
  105. .pixel-streaming-container {
  106. height: 100%;
  107. width: 100%;
  108. opacity: 0;
  109. transition: opacity 0.5s ease-in-out;
  110. min-height: 100px; /** 最小高度 放置不可见 */
  111. }
  112. .pixel-streaming-container.loaded {
  113. opacity: 1;
  114. }
  115. </style>