VR開發(fā)--CardboardSDK解析

虛擬成像原理


Paste_Image.png

1、首先SDK里面有什么

Paste_Image.png
來源游戲蠻牛gmq517

Cardboard腳本解析

1-Cardboard.cs

這是提供訪問底層方法的類.

Paste_Image.png
Paste_Image.png
Paste_Image.png
Paste_Image.png
Paste_Image.png
Paste_Image.png
2-CardboardEye.cs
using UnityEngine;

/// in order to reset its cache.
[RequireComponent(typeof(Camera))]
[AddComponentMenu("Cardboard/CardboardEye")]
public class CardboardEye : MonoBehaviour {
  /// Whether this is the left eye or the right eye.
  /// Determines which stereo eye to render, that is, which `EyeOffset` and
  /// `Projection` matrix to use and which half of the screen to render to.
  /// 確定要渲染的立體眼睛,就是用EyeOffset和projecttion 矩陣并且去渲染到屏幕的哪一半
  public Cardboard.Eye eye;

  /// Allows you to flip on or off specific culling mask layers for just this
  /// eye.  The mask is a toggle:  The eye's culling mask is first copied from
  /// the parent mono camera, and then the layers specified here are flipped.
  /// Each eye has its own toggle mask.
  /// 允許你為這個眼睛打開或關閉特定的剔除層.是一個觸發(fā)器,這個層首先從父相機復制,然后在此制定那些層被啟用,每只眼睛有它自己的剔除層
  [Tooltip("Culling mask layers that this eye should toggle relative to the parent camera.")]
  public LayerMask toggleCullingMask = 0;

    /// The StereoController in charge of this eye (and whose mono camera
    /// we will copy settings from).
    /// 返回StereoController 控制這個眼睛
    public StereoController Controller {
    // This property is set up to work both in editor and in player.
    get {
      if (transform.parent == null) { // Should not happen.
        return null;
      }
      if ((Application.isEditor && !Application.isPlaying) || controller == null) {
        // Go find our controller.
        return transform.parent.GetComponentInParent<StereoController>();
      }
      return controller;
    }
  }

    /// Returns the closest ancestor CardboardHead.
    /// @note Uses GetComponentInParent(), so the result will be null if no active ancestor is found.
    /// 返回父類CardboardHead
    public CardboardHead Head {
    get {
      return GetComponentInParent<CardboardHead>();
    }
  }
3-CardboardHead.cs

將此腳本附加到任何與用戶頭部運動相匹配的游戲對象上。也就是說Head模擬了現(xiàn)實中用戶的頭,Head下的部分都是頭的一部分.
里面有:
Main Camera與GazePointer
Main Camera就是我們的視野,左右眼睛.
GazePointer其實就是視野注視點的實體,將這個注視點作為頭部的一部分,跟隨頭部旋轉.

1
2
4-GazeInputModule.cs

提供了一個Unity的BaseInputModule類的實現(xiàn),使得UGUI可以通過觸發(fā)與觸摸的方式來選擇和使用

Paste_Image.png

Cardboard 的UI 系統(tǒng)是基于UGUI制作的.
我們在上一篇項目http://www.itdecent.cn/p/3696bc837551
里面應該已經(jīng)看到,我們修改并將GazeInputModule.cs腳本添加到EventSystem中

GazeInputModule:

這個腳本控制人眼視角發(fā)射的射線并觸發(fā)相應的事件。

 public override void Process() {
    // Save the previous Game Object
    GameObject gazeObjectPrevious = GetCurrentGameObject();

    CastRayFromGaze();   // 控制射線發(fā)射,先將3D坐標轉換為2D UI坐標系,發(fā)出射線
    UpdateCurrentObject(); // 更新選中物體的狀態(tài),比如按鈕會設置選中狀態(tài)等
    UpdateReticle(gazeObjectPrevious); // 

    // Get the camera
    Camera camera = pointerData.enterEventCamera;

    // Handle input
    if (!Cardboard.SDK.TapIsTrigger && !Input.GetMouseButtonDown(0) && Input.GetMouseButton(0)) {
      // Drag is only supported if TapIsTrigger is false.
      HandleDrag();           // 拖動狀態(tài)
    } else if (Time.unscaledTime - pointerData.clickTime < clickTime) {
      // Delay new events until clickTime has passed.
    } else if (!pointerData.eligibleForClick &&
               (Cardboard.SDK.Triggered || !Cardboard.SDK.TapIsTrigger && Input.GetMouseButtonDown(0))) {
     
      HandleTrigger();           //觸發(fā)事件
      if (cardboardPointer != null) {
        cardboardPointer.OnGazeTriggerStart(camera);
      }
    } else if (!Cardboard.SDK.Triggered && !Input.GetMouseButton(0)) {
      HandlePendingClick(); // 就是光標選中,啥也沒干
    }
  }
5-StereoController.cs
1
2
3
4
5
6
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容