首页 文章

Unity Cardboard Orientation Landscape Right Up

提问于
浏览
3

嗨,我有一个Unity应用程序,它使用谷歌Cardboard SDK启用立体视图,所以我将有一个支持VR的应用程序 . 我的应用程序运行完美 .

但是如果我将播放器设置方向设置为自动方向,只有横向左侧和允许横向右侧,则会出现问题 . 当它处于横向左侧时,一切都按照正常情况工作,但是当它处于横向右侧时,纸板视图将转动180度(设置按钮移动到屏幕的底部)但是我的统一对象没有 . 因此,我有一个颠倒的对象 .

有什么办法解决这个问题?

谢谢 .

5 回答

  • 2

    看起来SDK用于读取陀螺仪的本机代码仅针对Landscape Left方向进行了硬编码 . 这可以通过编辑BaseCardboardDevice.cs并使用以下代码替换UpdateState()的定义来解决:

    private Quaternion fixOrientation;
    
    public override void UpdateState() {
      GetHeadPose(headData, Time.smoothDeltaTime);
      ExtractMatrix(ref headView, headData);
      headPose.SetRightHanded(headView.inverse);
    
      // Fix head pose based on device orientation (since native code assumes Landscape Left).
      switch (Input.deviceOrientation) {
        case DeviceOrientation.LandscapeLeft:
          fixOrientation = Quaternion.identity;
          return;
        case DeviceOrientation.LandscapeRight:
          fixOrientation = Quaternion.Euler(0, 0, 180);
          break;
      }
      headPose.Set(headPose.Position, headPose.Orientation * fixOrientation);
    }
    

    我建议在Cardboard设置中关闭Neck Model Scale(将其设置为0),因为它不会出现在此代码中 .

  • 1

    我已经解决了这个问题,这是解决方案(适用于Cardboard v0.5.2)

    首先,根据smd的建议,您需要在BaseCardboardDevice / UpdateState()中应用方向调整 . 但是,您应该检查ScreenOrientation而不是DeviceOrientation . 代码如下:

    public override void UpdateState() {
    
        ProcessEvents();
        GetHeadPose(headData);
        ExtractMatrix(ref headView, headData);
        headPose.SetRightHanded(headView.inverse);
    
        if (Screen.orientation == ScreenOrientation.LandscapeRight) {
            headPose.Set(headPose.Position, headPose.Orientation * Quaternion.Euler(0, 0, 180));                           // Workaround
    }
    

    这将修复方向,但这还不够,因为在Cardboard Recenter()之后你将面临错误的方向 . 要解决此问题,您还需要在CardboardHead / UpdateHead()方法中应用旋转,代码如下:

    private void UpdateHead() {
    
        if (updated) {  // Only one update per frame, please.
          return;
        }
        updated = true;
        Cardboard.SDK.UpdateState();
    
        if (trackRotation) {
          var rot = Cardboard.SDK.HeadPose.Orientation;
    
          if (Screen.orientation == ScreenOrientation.LandscapeRight) {
            rot = Quaternion.Euler(0, 180, 0) * rot; //           << Workaround
          }
    
          if (target == null) {
            transform.localRotation = rot;
          } else {
            transform.rotation = target.rotation * rot;
          }
        }
    
        if (trackPosition) {
          Vector3 pos = Cardboard.SDK.HeadPose.Position;
          if (target == null) {
            transform.localPosition = pos;
          } else {
            transform.position = target.position + target.rotation * pos;
          }
        }
    
        if (OnHeadUpdated != null) {
          OnHeadUpdated(gameObject);
        }
      }
    
  • 1

    你可以添加:

    headPose.Set(headPose.Position, headPose.Orientation * Quaternion.Euler(0,0,180));
    

    在BaseCardboardDevice类的UpdateState()和 . 无论我如何转动我试过的设备,它都为我解决了这个问题 . Unity 5纸板SDK v0.5.1

  • 3
    var fixOrientation = Quaternion.identity;
            // Fix head pose based on device orientation (since native code assumes Landscape Left).
            switch (Input.deviceOrientation) {
            case DeviceOrientation.LandscapeLeft:
                fixOrientation = Quaternion.identity;
                break;
            case DeviceOrientation.LandscapeRight:
                fixOrientation = Quaternion.Euler(0, 0, 180);
                break;
            default:
                Debug.Log ("Error No Orientation" + Input.deviceOrientation.ToString ());
                break;
            }
            headPose.Set(headPose.Position, headPose.Orientation * fixOrientation);
    

    现在,Cardboard SDK成为了GoogleVR,所以我在GvrDevice.cs上这样写了

  • 0

    让您的应用程序同时出现在LandscapeLeft和LandscapeRight中的一种方法是,在Start方法中按以下顺序启用屏幕设置:

    void Start () {
         Screen.orientation = ScreenOrientation.Landscape;
         Screen.orientation = ScreenOrientation.AutoRotation;
         Screen.autorotateToPortrait = false;
         Screen.autorotateToPortraitUpsideDown = false;
    }
    

    基本上你强制一侧,然后启用自动旋转,然后防止旋转成为肖像 .

    请注意,这样您的应用程序可以在两个横向上工作,因此您只需要向左或向右旋转屏幕 .

    另请注意,这也适用于一个带有Cardboard的VR应用程序 .

相关问题