- 添加对有线连接的支持,特别是 Pico 4 的有线连接 - 改进 ALVR 服务器检测和连接逻辑 - 优化 VR 帧获取和处理流程 - 增加备用帧获取功能(主摄像机视图) - 修复 VR 眼部纹理相关问题 - 优化 VR 任务管理,确保正确渲染和提交帧
197 lines
6.6 KiB
Python
197 lines
6.6 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
"""
|
||
直接VR串流测试脚本
|
||
测试不通过ALVR,直接使用OpenVR API进行串流的功能
|
||
"""
|
||
|
||
import sys
|
||
import os
|
||
import time
|
||
from main import MyWorld
|
||
|
||
|
||
def test_direct_vr_streaming():
|
||
"""测试直接VR串流功能"""
|
||
print("=== 直接VR串流功能测试 ===")
|
||
|
||
# 创建世界实例
|
||
world = MyWorld()
|
||
|
||
# 初始化VR系统
|
||
print("🥽 初始化VR系统...")
|
||
if not world.initializeVR():
|
||
print("✗ VR系统初始化失败")
|
||
return False
|
||
|
||
print("✓ VR系统初始化成功")
|
||
|
||
# 获取VR信息
|
||
vr_info = world.getVRInfo()
|
||
print(f"\n📊 VR系统信息:")
|
||
print(f" - 模式: {vr_info['mode']}")
|
||
print(f" - 启用状态: {vr_info['enabled']}")
|
||
print(f" - 模拟模式: {vr_info['simulation_mode']}")
|
||
print(f" - 渲染尺寸: {vr_info['render_size']}")
|
||
|
||
# 检查是否为真实VR模式
|
||
if not vr_info['enabled'] or vr_info['simulation_mode']:
|
||
print("⚠ 未检测到真实VR设备或处于模拟模式")
|
||
print(" 直接串流需要真实的VR设备支持")
|
||
return False
|
||
|
||
# 检查VR纹理是否正确创建
|
||
print("\n🔍 检查VR纹理...")
|
||
if hasattr(world.vr_manager, 'left_eye_texture') and world.vr_manager.left_eye_texture:
|
||
print(" ✓ 左眼纹理已创建")
|
||
print(f" 纹理尺寸: {world.vr_manager.left_eye_texture.getXSize()}x{world.vr_manager.left_eye_texture.getYSize()}")
|
||
else:
|
||
print(" ✗ 左眼纹理未创建")
|
||
return False
|
||
|
||
if hasattr(world.vr_manager, 'right_eye_texture') and world.vr_manager.right_eye_texture:
|
||
print(" ✓ 右眼纹理已创建")
|
||
print(f" 纹理尺寸: {world.vr_manager.right_eye_texture.getXSize()}x{world.vr_manager.right_eye_texture.getYSize()}")
|
||
else:
|
||
print(" ✗ 右眼纹理未创建")
|
||
return False
|
||
|
||
# 检查OpenVR系统是否正常
|
||
print("\n🔍 检查OpenVR系统...")
|
||
if hasattr(world.vr_manager, 'vr_system') and world.vr_manager.vr_system:
|
||
print(" ✓ OpenVR系统已初始化")
|
||
else:
|
||
print(" ✗ OpenVR系统未初始化")
|
||
return False
|
||
|
||
if hasattr(world.vr_manager, 'vr_compositor') and world.vr_manager.vr_compositor:
|
||
print(" ✓ OpenVR合成器已初始化")
|
||
else:
|
||
print(" ✗ OpenVR合成器未初始化")
|
||
return False
|
||
|
||
# 创建简单的测试场景
|
||
print("\n🎨 创建测试场景...")
|
||
try:
|
||
from panda3d.core import CardMaker, Vec4, AmbientLight, DirectionalLight
|
||
|
||
# 添加光照
|
||
ambient_light = AmbientLight('ambient')
|
||
ambient_light.setColor(Vec4(0.3, 0.3, 0.3, 1))
|
||
ambient_light_np = world.render.attachNewNode(ambient_light)
|
||
world.render.setLight(ambient_light_np)
|
||
|
||
directional_light = DirectionalLight('directional')
|
||
directional_light.setColor(Vec4(0.8, 0.8, 0.8, 1))
|
||
directional_light.setDirection(Vec4(-1, -1, -1, 0))
|
||
directional_light_np = world.render.attachNewNode(directional_light)
|
||
world.render.setLight(directional_light_np)
|
||
|
||
# 创建地面网格
|
||
for i in range(-5, 6):
|
||
for j in range(-5, 6):
|
||
cm = CardMaker(f"grid_{i}_{j}")
|
||
cm.setFrame(-0.4, 0.4, -0.4, 0.4)
|
||
grid = world.render.attachNewNode(cm.generate())
|
||
grid.setPos(i * 1.0, j * 1.0, -1)
|
||
grid.setHpr(0, -90, 0)
|
||
|
||
# 棋盘格颜色
|
||
if (i + j) % 2 == 0:
|
||
grid.setColor(0.8, 0.8, 0.8, 1.0)
|
||
else:
|
||
grid.setColor(0.6, 0.6, 0.6, 1.0)
|
||
|
||
# 创建一些彩色立方体
|
||
for i in range(8):
|
||
cm = CardMaker(f"cube_{i}")
|
||
cm.setFrame(-0.3, 0.3, -0.3, 0.3)
|
||
cube = world.render.attachNewNode(cm.generate())
|
||
|
||
import math
|
||
angle = i * math.pi * 2 / 8
|
||
radius = 3
|
||
cube.setPos(math.cos(angle) * radius, math.sin(angle) * radius, 0.5)
|
||
cube.setHpr(i * 45, 0, 0)
|
||
cube.setScale(1, 1, 2)
|
||
|
||
# 彩虹颜色
|
||
hue = i / 8.0
|
||
r = abs(math.sin(hue * math.pi * 2))
|
||
g = abs(math.sin((hue + 0.33) * math.pi * 2))
|
||
b = abs(math.sin((hue + 0.66) * math.pi * 2))
|
||
cube.setColor(r, g, b, 1.0)
|
||
|
||
print("✓ 测试场景创建完成")
|
||
|
||
except Exception as e:
|
||
print(f"场景创建错误: {str(e)}")
|
||
|
||
# 测试直接串流
|
||
print("\n🎮 开始直接VR串流测试...")
|
||
print(" 测试将持续15秒,请观察VR头显中的画面")
|
||
|
||
start_time = time.time()
|
||
frame_count = 0
|
||
|
||
try:
|
||
while time.time() - start_time < 15:
|
||
# 处理引擎更新
|
||
if hasattr(world, 'taskMgr'):
|
||
world.taskMgr.step()
|
||
|
||
frame_count += 1
|
||
|
||
# 每秒显示一次状态
|
||
if frame_count % 60 == 0: # 约每秒
|
||
elapsed = time.time() - start_time
|
||
fps = frame_count / elapsed
|
||
print(f" [{elapsed:.1f}s] FPS: {fps:.1f}")
|
||
|
||
time.sleep(0.016) # ~60 FPS
|
||
|
||
except KeyboardInterrupt:
|
||
print("\n⏹️ 用户中断测试")
|
||
|
||
# 关闭系统
|
||
world.shutdownVR()
|
||
print("\n✓ VR系统已关闭")
|
||
|
||
final_fps = frame_count / (time.time() - start_time)
|
||
print(f"✅ 直接VR串流测试完成,平均FPS: {final_fps:.1f}")
|
||
|
||
return True
|
||
|
||
|
||
def main():
|
||
"""主函数"""
|
||
print("🎮 直接VR串流测试脚本")
|
||
print("="*50)
|
||
print("此脚本将测试不通过ALVR,直接使用OpenVR API进行串流的功能")
|
||
print("请确保:")
|
||
print(" 1. VR设备已连接(如Pico 4通过USB-C连接)")
|
||
print(" 2. SteamVR正在运行")
|
||
print(" 3. VR设备已被SteamVR识别")
|
||
|
||
try:
|
||
input("\n按回车键开始测试,或按Ctrl+C退出...")
|
||
success = test_direct_vr_streaming()
|
||
|
||
print("\n" + "="*50)
|
||
if success:
|
||
print("✓ 直接VR串流测试成功完成")
|
||
print("🎉 您现在已经可以不通过ALVR直接串流到VR设备!")
|
||
else:
|
||
print("✗ 直接VR串流测试遇到问题")
|
||
print("💡 请检查VR设备连接和SteamVR状态")
|
||
|
||
except KeyboardInterrupt:
|
||
print("\n用户中断测试")
|
||
except Exception as e:
|
||
print(f"测试过程中发生错误: {str(e)}")
|
||
import traceback
|
||
traceback.print_exc()
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main() |