Unity Cardboard Orientation Landscape Right Upside Down

前端 未结 5 628
挽巷
挽巷 2021-01-21 10:25

Hi I have a Unity app which uses google Cardboard SDK to enable stereoscopic view so I will have a VR enabled app. My app runs perfectly fine.

But there is a problem if

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-21 10:58

    I’ve solved this issue, here’s the solution (valid for Cardboard v0.5.2)

    First, as suggested by smd, you need to apply orientation adjust in BaseCardboardDevice / UpdateState(). However, you should check ScreenOrientation rather than DeviceOrientation. Code is as follows:

    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
    }
    

    This will fix the orientation, but that’s not enough, because you will be facing wrong direction after Cardboard Recenter(). To workaround this, you also need to apply the rotation in CardboardHead / UpdateHead() method, the code is like this:

    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);
        }
      }
    

提交回复
热议问题