現(xiàn)在我們已經(jīng)有了開發(fā)環(huán)境,還沒安裝環(huán)境的小伙伴可以看上一篇:
(一)Unity3D進行Android開發(fā)的環(huán)境搭建(虛擬機調(diào)試)
今天主要介紹的是谷歌為自己的Cardboard平臺提供的開發(fā)工具Cardboard SDK插件解析。Cardboard是谷歌公司在14年發(fā)布的一款極具創(chuàng)意的產(chǎn)品,由手機內(nèi)部的傳感器支持,它僅需硬紙板和兩片透鏡就能打造移動平臺上的VR體驗。Cardboard這里不多做介紹,網(wǎng)上可以買到原裝正版,價格在50以內(nèi)很便宜,有條件的人可以去體驗一下。
在正式開始之前,先說明一下本節(jié)需要對Unity3D引擎有一些基礎(chǔ)才能較流暢的看完,若是中間有疑問可以評論也可私信,疑問較多也可以再補充一章專門介紹基礎(chǔ)的東西。
上一章忘記說了,如果是VR應(yīng)用的話,就不要在虛擬機里跑了(虛擬機根本沒有傳感器去定位你的頭部,也就不存在VR一說了,不過簡單的小游戲還是可以在虛擬機上跑的,所以如果是跑大型應(yīng)用的話,還是選擇真機吧),虛擬機的存在其實純粹是為了學(xué)習(xí)示例用的。
沒有真機的同學(xué),這個demo在Unity里也可以跑,具體按住Alt移動鼠標(biāo)就相當(dāng)于轉(zhuǎn)動頭部,按住Ctrl移動鼠標(biāo)相當(dāng)于歪脖子看。
/***************************************************************分割線*******************************************************************/
首先要為Unity安裝這個插件,對于本插件,目前網(wǎng)上有些資源是不帶demo的,正好我這邊有一個帶demo的,附上下載地址:
http://download.csdn.net/detail/mao_xiao_feng/9577849
導(dǎo)入以后,工程目錄下多了以下兩個文件夾,雙擊打開DemoScene下的場景
場景大概就是以下這個樣子了,這個demo是插件中提供的官方示例,一定要把里面所有東西都研究透,才能很好理解SDK當(dāng)中的每一個腳本的功能。
直接切入主題,看到CardboardMain這個物體已經(jīng)被設(shè)為了Prefab,那么它一定是獲得VR效果的關(guān)鍵性物體,事實上所有VR的實現(xiàn)都在這個物體極其子物體下,我們把它一層層的剝離開來。
CardboardMain物體上只綁定了這一個腳本,開始看源碼
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
// The Cardboard object communicates with the head-mounted display in order to:
// - Query the device for viewing parameters
// - Retrieve the latest head tracking data
// - Provide the rendered scene to the device for distortion correction
public class Cardboard : MonoBehaviour {
// The singleton instance of the Cardboard class.
public static Cardboard SDK {
get {
if (sdk == null) {
sdk = UnityEngine.Object.FindObjectOfType<Cardboard>();
}
if (sdk == null) {
Debug.Log("Creating Cardboard object");
var go = new GameObject("Cardboard");
sdk = go.AddComponent<Cardboard>();
go.transform.localPosition = Vector3.zero;
}
return sdk;
}
}
private static Cardboard sdk = null;
public bool DistortionCorrection {
get {
return distortionCorrection;
}
set {
if (value != distortionCorrection && device != null) {
device.SetDistortionCorrectionEnabled(value && NativeDistortionCorrectionSupported);
}
distortionCorrection = value;
}
}
[SerializeField]
private bool distortionCorrection = true;
public bool VRModeEnabled {
get {
return vrModeEnabled;
}
set {
if (value != vrModeEnabled && device != null) {
device.SetVRModeEnabled(value);
}
vrModeEnabled = value;
}
}
[SerializeField]
private bool vrModeEnabled = true;
public bool EnableAlignmentMarker {
get {
return enableAlignmentMarker;
}
set {
if (value != enableAlignmentMarker && device != null) {
device.SetAlignmentMarkerEnabled(value && NativeUILayerSupported);
}
enableAlignmentMarker = value;
}
}
[SerializeField]
private bool enableAlignmentMarker = true;
public bool EnableSettingsButton {
get {
return enableSettingsButton;
}
set {
if (value != enableSettingsButton && device != null) {
device.SetSettingsButtonEnabled(value && NativeUILayerSupported);
}
enableSettingsButton = value;
}
}
[SerializeField]
private bool enableSettingsButton = true;
public bool TapIsTrigger = true;
public float NeckModelScale {
get {
return neckModelScale;
}
set {
value = Mathf.Clamp01(value);
if (!Mathf.Approximately(value, neckModelScale) && device != null) {
device.SetNeckModelScale(value);
}
neckModelScale = value;
}
}
[SerializeField]
private float neckModelScale = 0.0f;
public bool AutoDriftCorrection {
get {
return autoDriftCorrection;
}
set {
if (value != autoDriftCorrection && device != null) {
device.SetAutoDriftCorrectionEnabled(value);
}
autoDriftCorrection = value;
}
}
[SerializeField]
private bool autoDriftCorrection = true;
#if UNITY_IOS
public bool SyncWithCardboardApp {
get {
return syncWithCardboardApp;
}
set {
if (value && value != syncWithCardboardApp) {
Debug.LogWarning("Remember to enable iCloud capability in Xcode, "
+ "and set the 'iCloud Documents' checkbox. "
+ "Not doing this may cause the app to crash if the user tries to sync.");
}
syncWithCardboardApp = value;
}
}
[SerializeField]
private bool syncWithCardboardApp = false;
#endif
#if UNITY_EDITOR
// Mock settings for in-editor emulation of Cardboard while playing.
public bool autoUntiltHead = true;
// Whether to perform distortion correction in the editor.
public bool simulateDistortionCorrection = true;
// Use unity remote as the input source.
[HideInInspector]
public bool UseUnityRemoteInput = false;
public CardboardProfile.ScreenSizes ScreenSize {
get {
return screenSize;
}
set {
if (value != screenSize) {
screenSize = value;
device.UpdateScreenData();
}
}
}
[SerializeField]
private CardboardProfile.ScreenSizes screenSize = CardboardProfile.ScreenSizes.Nexus5;
public CardboardProfile.DeviceTypes DeviceType {
get {
return deviceType;
}
set {
if (value != deviceType) {
deviceType = value;
device.UpdateScreenData();
}
}
}
[SerializeField]
public CardboardProfile.DeviceTypes deviceType = CardboardProfile.DeviceTypes.CardboardJun2014;
#endif
// The VR device that will be providing input data.
private static BaseVRDevice device;
public bool NativeDistortionCorrectionSupported { get; private set; }
public bool NativeUILayerSupported { get; private set; }
// The texture that Unity renders the scene to. This is sent to the VR device,
// which renders it to screen, correcting for lens distortion.
public RenderTexture StereoScreen {
get {
// Don't need it except for distortion correction.
if (!distortionCorrection || !vrModeEnabled) {
return null;
}
if (stereoScreen == null && NativeDistortionCorrectionSupported) {
StereoScreen = CreateStereoScreen(); // Note: use set{}
}
return stereoScreen;
}
set {
if (value == stereoScreen) {
return;
}
if (!NativeDistortionCorrectionSupported && value != null) {
Debug.LogError("Can't set StereoScreen: native distortion correction is not supported.");
return;
}
if (stereoScreen != null) {
stereoScreen.Release();
}
stereoScreen = value;
if (stereoScreen != null && !stereoScreen.IsCreated()) {
stereoScreen.Create();
}
if (device != null) {
device.SetStereoScreen(stereoScreen);
}
}
}
private static RenderTexture stereoScreen = null;
public bool UseDistortionEffect {
get {
return !NativeDistortionCorrectionSupported && distortionCorrection && vrModeEnabled
&& SystemInfo.supportsRenderTextures;
}
}
// Describes the current device, including phone screen.
public CardboardProfile Profile {
get {
return device.Profile;
}
}
// Distinguish the stereo eyes.
public enum Eye {
Left,
Right,
Center
}
// When asking for project, viewport, etc, whether to assume viewing through
// the lenses.
public enum Distortion {
Distorted, // Viewing through the lenses
Undistorted // No lenses
}
// The transformation of head from origin in the tracking system.
public Pose3D HeadPose {
get {
return device.GetHeadPose();
}
}
// The transformation from head to eye.
public Pose3D EyePose(Eye eye) {
return device.GetEyePose(eye);
}
// The projection matrix for a given eye.
public Matrix4x4 Projection(Eye eye, Distortion distortion = Distortion.Distorted) {
return device.GetProjection(eye, distortion);
}
// The screen-space rectangle each eye should render into.
public Rect Viewport(Eye eye, Distortion distortion = Distortion.Distorted) {
return device.GetViewport(eye, distortion);
}
// The distance range from the viewer in user-space meters where objects
// may be viewed comfortably in stereo. If the center of interest falls
// outside this range, the stereo eye separation should be adjusted to
// keep the onscreen disparity within the limits set by this range.
public Vector2 ComfortableViewingRange {
get {
return defaultComfortableViewingRange;
}
}
private readonly Vector2 defaultComfortableViewingRange = new Vector2(1.0f, 100000.0f);
private void InitDevice() {
if (device != null) {
device.Destroy();
}
device = BaseVRDevice.GetDevice();
device.Init();
List<string> diagnostics = new List<string>();
NativeDistortionCorrectionSupported = device.SupportsNativeDistortionCorrection(diagnostics);
if (diagnostics.Count > 0) {
Debug.LogWarning("Built-in distortion correction disabled. Causes: ["
+ String.Join("; ", diagnostics.ToArray()) + "]");
}
diagnostics.Clear();
NativeUILayerSupported = device.SupportsNativeUILayer(diagnostics);
if (diagnostics.Count > 0) {
Debug.LogWarning("Built-in UI layer disabled. Causes: ["
+ String.Join("; ", diagnostics.ToArray()) + "]");
}
device.SetVRModeEnabled(vrModeEnabled);
device.SetDistortionCorrectionEnabled(distortionCorrection
&& NativeDistortionCorrectionSupported);
device.SetAlignmentMarkerEnabled(enableAlignmentMarker
&& NativeUILayerSupported);
device.SetSettingsButtonEnabled(enableSettingsButton
&& NativeUILayerSupported);
device.SetNeckModelScale(neckModelScale);
device.SetAutoDriftCorrectionEnabled(autoDriftCorrection);
device.UpdateScreenData();
}
// NOTE: Each scene load causes an OnDestroy of the current SDK, followed
// by and Awake of a new one. That should not cause the underlying native
// code to hiccup. Exception: developer may call Application.DontDestroyOnLoad
// on the SDK if they want it to survive across scene loads.
void Awake() {
if (sdk == null) {
sdk = this;
}
if (sdk != this) {
Debug.LogWarning("Cardboard SDK object should be a singleton.");
enabled = false;
return;
}
#if UNITY_IOS
Application.targetFrameRate = 60;
#endif
InitDevice();
AddDummyCamera();
StereoScreen = null;
}
public event Action OnTrigger;
public event Action OnTilt;
public bool Triggered { get; private set; }
public bool Tilted { get; private set; }
private bool updated = false;
private CardboardUILayer uiLayer = null;
public void UpdateState() {
if (!updated) {
device.UpdateState();
if (TapIsTrigger) {
if (Input.GetMouseButtonUp(0)) {
device.triggered = true;
}
if (Input.GetKeyUp(KeyCode.Escape)) {
device.tilted = true;
}
}
updated = true;
}
}
private void DispatchEvents() {
Triggered = device.triggered;
Tilted = device.tilted;
device.triggered = false;
device.tilted = false;
if (Tilted) {
if (OnTilt != null) {
OnTilt();
}
}
if (Triggered) {
if (OnTrigger != null) {
OnTrigger();
}
}
}
private void AddDummyCamera() {
var go = gameObject;
if (go.GetComponent<Camera>()) {
go = new GameObject("CardboardDummy");
go.transform.parent = gameObject.transform;
}
var cam = go.AddComponent<Camera>();
cam.clearFlags = CameraClearFlags.SolidColor;
cam.backgroundColor = Color.black;
cam.cullingMask = 0;
cam.useOcclusionCulling = false;
cam.depth = -100;
}
IEnumerator EndOfFrame() {
while (true) {
yield return new WaitForEndOfFrame();
UpdateState();
device.PostRender(vrModeEnabled);
if (vrModeEnabled && !NativeUILayerSupported) {
if (uiLayer == null) {
uiLayer = new CardboardUILayer();
}
uiLayer.Draw();
}
DispatchEvents();
updated = false;
}
}
// Return a StereoScreen with sensible default values.
public RenderTexture CreateStereoScreen() {
return device.CreateStereoScreen();
}
// Reset the tracker so that the user's current direction becomes forward.
public void Recenter() {
device.Recenter();
}
// Set the screen coordinates of the mouse/touch event.
public void SetTouchCoordinates(int x, int y) {
device.SetTouchCoordinates(x, y);
}
void OnEnable() {
device.OnPause(false);
StartCoroutine("EndOfFrame");
}
void OnDisable() {
StopCoroutine("EndOfFrame");
device.OnPause(true);
}
void OnApplicationPause(bool pause) {
device.OnPause(pause);
}
void OnApplicationFocus(bool focus) {
device.OnFocus(focus);
}
void OnLevelWasLoaded(int level) {
device.Reset();
}
void OnDestroy() {
if (device != null) {
device.Destroy();
}
if (sdk == this) {
sdk = null;
}
}
void OnApplicationQuit() {
device.OnApplicationQuit();
}
//********* OBSOLETE ACCESSORS *********
[System.Obsolete("Use DistortionCorrection instead.")]
public bool nativeDistortionCorrection {
get { return DistortionCorrection; }
set { DistortionCorrection = value; }
}
[System.Obsolete("InCardboard is deprecated.")]
public bool InCardboard { get { return true; } }
[System.Obsolete("Use Triggered instead.")]
public bool CardboardTriggered { get { return Triggered; } }
[System.Obsolete("Use HeadPose instead.")]
public Matrix4x4 HeadView { get { return HeadPose.Matrix; } }
[System.Obsolete("Use HeadPose instead.")]
public Quaternion HeadRotation { get { return HeadPose.Orientation; } }
[System.Obsolete("Use HeadPose instead.")]
public Vector3 HeadPosition { get { return HeadPose.Position; } }
[System.Obsolete("Use EyePose() instead.")]
public Matrix4x4 EyeView(Eye eye) {
return EyePose(eye).Matrix;
}
[System.Obsolete("Use EyePose() instead.")]
public Vector3 EyeOffset(Eye eye) {
return EyePose(eye).Position;
}
[System.Obsolete("Use Projection() instead.")]
public Matrix4x4 UndistortedProjection(Eye eye) {
return Projection(eye, Distortion.Undistorted);
}
[System.Obsolete("Use Viewport() instead.")]
public Rect EyeRect(Eye eye) {
return Viewport(eye, Distortion.Distorted);
}
[System.Obsolete("Use ComfortableViewingRange instead.")]
public float MinimumComfortDistance { get { return ComfortableViewingRange.x; } }
[System.Obsolete("Use ComfortableViewingRange instead.")]
public float MaximumComfortDistance { get { return ComfortableViewingRange.y; } }
}
來自CODE的代碼片Cardboard.cs
上面的源代碼理解起來比較麻煩,我解析的時候會把變量定義等一些順序調(diào)換一下注釋部分
The Cardboard object communicates with the head-mounted display in order to:Query the device for viewing parameters Retrieve the latest head tracking data Provide the rendered scene to the device for distortion correction 翻譯/Cardboard物體與頭盔顯示器進行交互以獲得設(shè)備上的一些參數(shù),返回最近頭部跟蹤數(shù)據(jù),為被渲染的場景提供失真校正/
定義了一個Cardboard類型的靜態(tài)公有變量SDK,主要用來獲取游戲中的Cardboard.cs腳本,可以看到它是只讀的,即SDK提供了一個共有的訪問接口,在任何時候它都只有一個實例。 // The singleton instance of the Cardboard class. 翻譯/Cardboard類的單件實例/---關(guān)于單件模式,編程用的還是挺多的。
BaseVRDevice類型的私有靜態(tài)變量device,BaseVRDevice也是插件當(dāng)中定義的一個類,注意!!它與Cardboard類不同,它不是腳本類!!這個變量用的比較多 // The VR device that will be providing input data. 翻譯/VR設(shè)備(在這里直接理解為手機吧?。┨峁┹斎氲臄?shù)據(jù)/
公有bool類型的變量DistortionCorrection,默認為true。DistortionCorrection我把它翻譯為失真校正(暫且這樣翻譯吧),讀屬性不說了,寫屬性加了個if語句,想說明假如設(shè)備接入了并且支持失真校正功能,才能修改它的值,不然一直為true。(這個變量是控制扭曲的,開或者關(guān)的效果如下,差距應(yīng)該一目了然)
[圖片上傳中。。。(5)]
同上還有VRModeEnabled(VR模式的使能,開了就是分屏),EnableAlignmentMarker(就是下圖黃圈中間那根線,關(guān)了,線就沒了),EnableSettingsButton(就是下圖綠圈內(nèi)的設(shè)置按鈕,同樣也是關(guān)了就沒了),AutoDriftCorrection(這個屬性還沒搞懂先放著),NeckModelScale(這個是專業(yè)術(shù)語,應(yīng)該是頸部的微調(diào)吧...猜的,值在0-1之間,調(diào)整的話視角會有微小的變化,但可以忽略不計)
[圖片上傳中。。。(6)]
后面從#if UNITY_IOS到#endif中間的部分是預(yù)編譯部分,根據(jù)iOS,unity_editor,Android選擇性的編譯,可以根據(jù)自己的平臺選擇性的看。我們現(xiàn)在使用unity編輯器在運行,所以他編譯的是#if UNITY_EDITOR到#endif中間的這部分。
RenderTexture類型的公有變量StereoScreen,渲染紋理是一種即時更新的紋理。//The texture that Unity renders the scene to. This is sent to the VR device,which renders it to screen, correcting for lens distortion. 翻譯/Unity渲染場景產(chǎn)生的紋理,它被傳輸?shù)絍R設(shè)備上,在屏幕上被繪制產(chǎn)生正確的透鏡彎曲效果/這個變量的讀屬性當(dāng)vr模式或者distortionCorrection關(guān)閉時,為null,寫屬性也只有在支持distortionCorrection的時候可以使用,總的來說,可以把它看作產(chǎn)生彎曲效果的一個工具或者一個中間量。
bool類型只讀屬性的UseDistortionEffect,這個量是用來判斷透鏡效果是否開啟的量,后面會用到。
CardboardProfile類型的公有變量Profile,也是只讀的。 // Describes the current device, including phone screen. 翻譯/描述當(dāng)前設(shè)備,包括手機屏幕/
公有枚舉類型Eye。 // Distinguish the stereo eyes. 翻譯/看立體效果時用來區(qū)分左右眼/
公有枚舉類型Distortion。這個量主要控制在某些特殊情況下是否需要透鏡預(yù)覽,在下面會用到
Pose3D類型的公有量HeadPose。 // The transformation of head from origin in the tracking system. 翻譯/在跟蹤在系統(tǒng)中,頭部距離起始點的信息/由于這里Pose3D也是插件自定義的類,后續(xù)需要分析Pose3D這個類才能知道信息是如何傳遞的。同理還有變量EyePose。
返回Matrix4x4類型的方法Projection(Eye eye, Distortion distortion = Distortion.Distorted)返回結(jié)果是device.GetProjection(eye, distortion);從兩個參數(shù)推斷這個方法是要根據(jù)眼睛計算返回一個投影矩陣,在Unity圣典中闡述過Matrix4x4類型的投影矩陣,關(guān)于投影矩陣,涉及到計算機圖形學(xué)的東西,我目前也正在學(xué)習(xí),給一個鏈接大家可以學(xué)習(xí)一下。
http://blog.csdn.net/yanwei2016/article/details/7326180
Viewport(Eye eye, Distortion distortion = Distortion.Distorted)方法返回視口矩形,應(yīng)該是處理在屏幕上的位置的一個方法
Vector2類型的變量ComfortableViewingRange,只讀,控制我們?nèi)タ纯臻g中物體的一個最舒服的距離范圍,這個量不需要去配置,理解就行。
私有方法InitDevice()用于初始化設(shè)備,因為涉及到BaseVRDevice類,我們以后再去解析其細節(jié)。
私有方法AddDummyCamera()用來添加一個黑色背景,當(dāng)然背景也可以自己更換。
一些瑣碎的定義先到這里
/********************************************************分割線********************************************************************/
NOTE: Each scene load causes an OnDestroy of the current SDK, followed by and Awake of a new one. That should not cause the underlying native code to hiccup. Exception: developer may call Application.DontDestroyOnLoad on the SDK if they want it to survive across scene loads.
翻譯/說明:每一個新場景被加載會導(dǎo)致現(xiàn)在sdk的destroy,然后產(chǎn)生新的sdk,這會發(fā)生一些問題(這句話實在不會翻譯了),所以開發(fā)者們在切換場景的時候如果希望sdk依然存活,可以使用Application.DontDestroyOnLoad/。這里其實可以間接的說明SDK是采用的單件模式,因為我們無法用new()去創(chuàng)建,而且自始而終最多只有一個實例。
比較關(guān)鍵的awake函數(shù)來了
void Awake() {
if (sdk == null) {
sdk = this; }
if (sdk != this) {
Debug.LogWarning("Cardboard SDK object should be a singleton."); enabled = false; return;
}
#if UNITY_IOS
Application.targetFrameRate = 60;
#endif
InitDevice();
AddDummyCamera();
StereoScreen = null; }
相信上面的語句根據(jù)我之前的解釋大家都能看懂了
這個類中沒有Update()所以我認為更新可能放在BaseVRDevice類型的device里面了。
我先碼到這里(太累了),后面接著此篇,另外會解析BaseVRDevice這個類(很重要?。。€有CardboardProfile這個類。
總結(jié):這一章還只是把每個類的功能和大致框架整理了一下,并沒有涉及到很多細節(jié)的東西,也沒有涉及到原理,注意我們現(xiàn)在還在頂端的父物體Cardboard物體上分析,后面當(dāng)涉及到原理的的時候,就要開始看下層的Head,和它的子物體幾個camera了。