VRTK学习记录

  1. 1. 配置环境
  2. 2. 键鼠模拟基本操作
  3. 3. 基本配置
  4. 4. Controller相关
    1. 4.1. 控制器基本组件
    2. 4.2. 射线与场景互动

配置环境

unity 2018.4.14
VRTK 3.3.0
steamVR 1.2.3

键鼠模拟基本操作

  • wasd控制人物移动
  • 鼠标默认控制人物方向
  • alt切换控制模式
    • 鼠标:控制人物方向 —>控制左右Controller的水平位置
    • ctrl:按住ctrl控制Controller的垂直位置
  • tab切换左右Controller
  • ctrl 切换为抓取物体模式
    • 鼠标右键为右Controller抓取
    • 鼠标左键为左Controller抓取
  • shift 奔跑
  • q 发出射线
    • 鼠标右键代表Controller的扳机

基本配置

  1. 新建场景,删除原本的Camera。

  2. simulator为键鼠模拟;steamVr为设备操作;[VRSimulator_CameraRig]是VRTK的prefab,可直接使用;

    RightController 为右控制器; LeftController 为左控制器; PlayArea 为需要传送时添加。

    基本结构

  1. VRTK SDK 需要添加VRTK_SDKManager 组件,并将子物体simulator和steamVR添加到组件中Setups的列表中(因为只需要键鼠模拟,所以只添加了simulator),然后将左右手控制器分别添加到Scripts Aliases中。

    Manager

  1. Simulator和SteamVR需要添加VRTK_SDK Setup 组件,并将Quick Select设置为相应的模式

    Setup

Controller相关

控制器基本组件

Controller基本组件

Controller抓取物体所需组件

Controller射线组件

  • ​ 射线类型可以为直线(VRTK_Straight Pointer Renderer)或贝塞尔曲线(VRTK_Bezier Pointer Renderer)

Controller_UI

  • Heighlighter …. ; VRTK_UI Pointer为ui互动所必须组件

射线与场景互动

控制器上可以添加自定义脚本,使之可以与场景中的物体进行互动,脚本参考VRTK_ControllerPointerEvents_ListenerExample

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public class VRTK_ControllerPointerEvents_ListenerExample : MonoBehaviour
{
public bool showHoverState = false;

private void Start()
{
if (GetComponent<VRTK_DestinationMarker>() == null)
{
VRTK_Logger.Error(VRTK_Logger.GetCommonMessage(VRTK_Logger.CommonMessageKeys.REQUIRED_COMPONENT_MISSING_FROM_GAMEOBJECT, "VRTK_ControllerPointerEvents_ListenerExample", "VRTK_DestinationMarker", "the Controller Alias"));
return;
}

//Setup controller event listeners
GetComponent<VRTK_DestinationMarker>().DestinationMarkerEnter += new DestinationMarkerEventHandler(DoPointerIn);
if (showHoverState)
{
GetComponent<VRTK_DestinationMarker>().DestinationMarkerHover += new DestinationMarkerEventHandler(DoPointerHover);
}
GetComponent<VRTK_DestinationMarker>().DestinationMarkerExit += new DestinationMarkerEventHandler(DoPointerOut);
GetComponent<VRTK_DestinationMarker>().DestinationMarkerSet += new DestinationMarkerEventHandler(DoPointerDestinationSet);
}

private void DebugLogger(uint index, string action, Transform target, RaycastHit raycastHit, float distance, Vector3 tipPosition)
{
string targetName = (target ? target.name : "<NO VALID TARGET>");
string colliderName = (raycastHit.collider ? raycastHit.collider.name : "<NO VALID COLLIDER>");
VRTK_Logger.Info("Controller on index '" + index + "' is " + action + " at a distance of " + distance + " on object named [" + targetName + "] on the collider named [" + colliderName + "] - the pointer tip position is/was: " + tipPosition);
}

private void DoPointerIn(object sender, DestinationMarkerEventArgs e)
{
DebugLogger(VRTK_ControllerReference.GetRealIndex(e.controllerReference), "POINTER IN", e.target, e.raycastHit, e.distance, e.destinationPosition);
}

private void DoPointerOut(object sender, DestinationMarkerEventArgs e)
{
DebugLogger(VRTK_ControllerReference.GetRealIndex(e.controllerReference), "POINTER OUT", e.target, e.raycastHit, e.distance, e.destinationPosition);
}

private void DoPointerHover(object sender, DestinationMarkerEventArgs e)
{
DebugLogger(VRTK_ControllerReference.GetRealIndex(e.controllerReference), "POINTER HOVER", e.target, e.raycastHit, e.distance, e.destinationPosition);
}

private void DoPointerDestinationSet(object sender, DestinationMarkerEventArgs e)
{
DebugLogger(VRTK_ControllerReference.GetRealIndex(e.controllerReference), "POINTER DESTINATION", e.target, e.raycastHit, e.distance, e.destinationPosition);
}
}

//