Unity对于VR的支持

来源:互联网 发布:mac软件卸载 编辑:程序博客网 时间:2024/05/21 22:24

如何打开Unity对于VR的支持

在edit下拉菜单里,找到Project Setting选项,然后选择Player,在Inspector中找到Other Setting,勾选Virtual Reality Supported复选框,即可获取对于VR设备的支持。

VR支持
点击+号即可添加新的设备。目前,Unity支持的设备有oculus,openVR, Ste reo Display, Split Stereo Display。后面两个是3D显示器。

支持产品

其中None的作用是可以在初始化的过程中,不进行VR头显的渲染,这样可以让同一个程序对多个设置进行支持。
下面我们来介绍下如何使用脚本来进行支持的SDK种类的切换。
- 我们首先把None类型放在最上方,OpenVR放在下方,其余支持的SDK删除,如下图所示:

图1
- 现在我们运行程序,VIVE头盔不会有任何反应,只会在电脑屏幕上出现画面。
- 然后我们新建一个脚本ChangeVRType.cs
新建一个协程

    IEnumerator LoadDevice(string newDevice)    {        yield return new WaitForSeconds(3.0f);        VRSettings.LoadDeviceByName(newDevice);        yield return null;        VRSettings.enabled = true;    }

为了以示区别,我们在更改支持的SDK前先等待3秒,在此时间内,只有电脑屏幕有画面,3s过后,我们可以看到VIVE头盔被激活。此时应注意在enable前,需等待一帧,否则会报错。

相机设置

在添加了VR支持后,我们会发现在camera的Inspector面板中多出如图2所示的几个选项。

图2
- stereo separation指的是瞳距(两个虚拟眼睛之间的距离)
- stereo convergence指的是焦点
- target display指的是渲染的相机
- target eye指的是渲染的左右眼

Unity对手柄的支持

手柄的位置信息

InputTracking类可以用于获取VR节点的位置和旋转角度。其官方的解释如下:

    //    // Summary:    //     ///    //     VR Input tracking data.    //     ///    public static class InputTracking    {        //        // Summary:        //     ///        //     The current position of the requested VRNode.        //     ///        //        // Parameters:        //   node:        //     Node index.        //        // Returns:        //     ///        //     Position of node local to its tracking space.        //     ///        public static Vector3 GetLocalPosition(VRNode node);        //        // Summary:        //     ///        //     The current rotation of the requested VRNode.        //     ///        //        // Parameters:        //   node:        //     Node index.        //        // Returns:        //     ///        //     Rotation of node local to its tracking space.        //     ///        public static Quaternion GetLocalRotation(VRNode node);        //        // Summary:        //     ///        //     Center tracking to the current position and orientation of the HMD.        //     ///        public static void Recenter();    }

以下,我们通过一个简单的示例来演示如何使用InputTracking类来模拟手柄。
- 新建两个cude,一个命名为LeftHand,一个命名为RightHand. Cube的scale设为0.2。
- 然后新建一个empty game object,命名为Virtual man,作为手柄和相机的父物体。
- 把main camera和LeftHand, RightHand拖到Virtual man下面,并将其位置归为0。
新建脚本RightHand.cs和LeftHand.cs,分别关联到LeftHand, RightHand上。
在Update函数中写:

        this.transform.localPosition = InputTracking.GetLocalPosition(VRNode.RightHand);        this.transform.localRotation = InputTracking.GetLocalRotation(VRNode.RightHand);

LeftHand脚本中做出相应的替换即可。

运行程序,我们就可以看到手柄变成了两个cube。

手柄的按键

HTC VIVE手柄的按键可以在Edit->Project Setting->Input中设定。
首先,我们需要找到HTC VIVE手柄对应的ID号,在unity的官网上可以找到其对应的信息:

https://docs.unity3d.com/Manual/OpenVRControllers.html

图3

图4
此时,我们就可以在Input里面设置手柄对应的按键了。我们以手柄上的trigger键为例,介绍如何使用手柄。
- 首先在InputManager中将Axes的数量加1.
- 将其名字命名为LCT.
- 按如图所示的信息设置LCT的信息:

图5
其中positive button对应即是上表中Unity Button ID中对应的值。
- 同理,我们在添加右手柄的trigger控制器。
- 我们在上述建立的脚本Lefthand.cs和Righthand.cs中加入以下控制语句:

      if (Input.GetButton("LCT"))        {            Debug.Log("LCT");        }        if (Input.GetButton("RCT"))        {            Debug.Log("RCT");        }
  • 运行程序,按下左右手的trigger键,即可在控制台打印相应的命令。

如果我们想获得trigger按下去的程度的数值,则可以使用Squeeze类型。
具体做法如下:
- 首先在InputManager中将Axes的数量加1.
- 将其名字命名为LCT。
- 按如图所示的信息设置LCT的信息:

图6
与之前不同的地方是,Axis要改为9th axis,type要改为Joystick Axis类型。
- 在LeftHand.cs添加如下代码:

float f1 = Input.GetAxis("LCT");if (f1 > 0.1f){      Debug.Log(f1);}
  • 运行程序,按下trigger键,即可在控制台上获取trigger键按下程度的大小。
  • 同理,可以设置右手柄的trigger键。

此时,我们可以做一个射线放置到手柄上。
- 首先,我们以RightHand和LeftHand为父物体,各添加一个空物体。
- 然后在空物体上添加Line Renderer组件。
- 新建一个材质,将其shader改为Unlit/Color,并将其添加到Line Renderer上,这样我们就可以更改射线的颜色。
- Line Renderer的设置信息参考如下:

图7
- 我们在场景中新建一个UI组件,比如button。
- 将Canvas设置为World Space,调到合适的位置。
- 新建一个脚本ButtonClick.cs。在其中添加一个Do函数。

    public void Do()    {        Debug.Log("Press the button");    }
  • 将ButtonClick.cs添加到button上,并将Button上的OnClick组件设置为:

图8
- 然后在LeftHand.cs脚本中加入以下语句:

        Ray ray = new Ray(transform.position, transform.forward);        RaycastHit hit;        if (Input.GetButtonDown("LCT"))        {            if (Physics.Raycast(ray, out hit, 10000))            {                Debug.Log(hit.transform.name);                hit.transform.GetComponent<UnityEngine.UI.Button>().OnSubmit(new BaseEventData(EventSystem.current));            }        }

使用这种方法对于UI的通用性比较差,如果我们在添加toggle或是其他UI组件,就要重新的GetComponent,比较麻烦,为此,我们可以采取以下的做法。
- 首先,我们在场景中在新建一个UI toggle。然后在ButtonClick函数中添加如下的函数:

    public void Do2()    {        Debug.Log("Press the toggle");    }}
  • 然后在toggle上的onClick()中添加如下:

图9
- 然后在LeftHand.cs脚本中做如下的修改:

        Ray ray = new Ray(transform.position, transform.forward);        RaycastHit hit;        if (Input.GetButtonDown("LCT"))        {            if (Physics.Raycast(ray, out hit, 10000))            {                Debug.Log(hit.transform.name);                // hit.transform.GetComponent<UnityEngine.UI.Button>().OnSubmit(new BaseEventData(EventSystem.current));                ExecuteEvents.Execute<ISubmitHandler>(hit.transform.gameObject, new BaseEventData(EventSystem.current), ExecuteEvents.submitHandler);            }        }
  • 此时,不论是button还是toggle都可以正常运行。
原创粉丝点击