DayDream的controller主要集中在图中箭头标示的两个地方。
他们实现的效果如下图:
图中的箭头标明了它们实现的模块。
GvrControllerMain
里面很重要的一个模块,它包含两个脚本组件,GvrController和GvrArmModel。
其中GvrController部分代码如下
/// If true, the user is currently touching the controller's touchpad.
public static bool IsTouching {
get {
return instance != null ? instance.controllerState.isTouching : false;
}
}
/// If true, the user just started touching the touchpad. This is an event flag (it is true
/// for only one frame after the event happens, then reverts to false).
public static bool TouchDown {
get {
return instance != null ? instance.controllerState.touchDown : false;
}
}
/// If true, the user just stopped touching the touchpad. This is an event flag (it is true
/// for only one frame after the event happens, then reverts to false).
public static bool TouchUp {
get {
return instance != null ? instance.controllerState.touchUp : false;
}
}
public static Vector2 TouchPos {
get {
return instance != null ? instance.controllerState.touchPos : Vector2.zero;
}
}
以TouchPos 举例说明。
TouchPos 获取用户触摸触摸盘时候的位置。它是从controllerState中获取的,而且controllerState中的信息,google注释说是从C api中获取的。
总之你可以从GvrController中获取触摸手柄上的任何触摸信息。
GvrArmModel
引用google的解释
/// The GvrArmModel is a standard interface to interact with a scene with the controller.
/// It is responsible for:
/// - Determining the orientation and location of the controller.
/// - Predict the location of the shoulder, elbow, wrist, and pointer.
///
/// There should only be one instance in the scene, and it should be attached
/// to the GvrController
它的主要作用应该就是控制虚拟手柄的方向和位置,以及预测肩膀,肘部,手腕和指针的位置。你可以根据它预测的位置画出一个人。
它会把这些位置都计算出来,最后会调用方法
OnArmModelUpdate
这个方法最终会调用到GvrArmModelOffsets中的OnArmModelUpdate方法,在GvrArmModelOffsets的Start中把OnArmModelUpdate方法的实现暴露给了GvrArmModel。
如下:
void Start() {
if (GvrArmModel.Instance != null) {
GvrArmModel.Instance.OnArmModelUpdate += OnArmModelUpdate;
} else {
Debug.LogError("Unable to find GvrArmModel.");
}
}
google对GvrArmModelOffsets的解释
/// This script positions and rotates the transform that it is attached to
/// according to a joint in the arm model. See GvrArmModel.cs for details.
它的作用主要是移动和旋转它所在object的transform。
GvrArmModelOffsets脚本被两个object拥有:
GvrControllerPointer下的Laser和Controller。
Controller主要就是实现了控制虚拟器
Laser实现了激光射线。
因为他们都要随着控制器的动作,而进行相应的动作,所以他们的位置都会被GvrArmModelOffsets指定。
GvrArmModelOffsets是怎么指定它所在模块的位置的?
private void OnArmModelUpdate() {
Vector3 jointPosition;
Quaternion jointRotation;
switch (joint) {
case Joint.Pointer:
jointPosition = GvrArmModel.Instance.pointerPosition;
jointRotation = GvrArmModel.Instance.pointerRotation;
break;
case Joint.Wrist:
jointPosition = GvrArmModel.Instance.wristPosition;
jointRotation = GvrArmModel.Instance.wristRotation;
break;
case Joint.Elbow:
jointPosition = GvrArmModel.Instance.elbowPosition;
jointRotation = GvrArmModel.Instance.elbowRotation;
break;
case Joint.Shoulder:
jointPosition = GvrArmModel.Instance.shoulderPosition;
jointRotation = GvrArmModel.Instance.shoulderRotation;
break;
default:
throw new System.Exception("Invalid FromJoint.");
}
transform.localPosition = jointPosition;
transform.localRotation = jointRotation;
Debug.Log("jointPosition " + jointPosition + " jointRotation " + jointRotation);
DrawDebugLine();
}
主要是
transform.localPosition = jointPosition;
transform.localRotation = jointRotation;
把从GvrArmModel计算出的位置给了GvrArmModelOffsets所在的object的transform。
另外GvrArmModelOffsets模块有个public的joint,他有四个模式:
Pointer,Wrist,Elbow,Shoulder。你可以试试看他们都有什么效果。
当选中相应选项的时候,会获取GvrArmModel预测的相应位置。
ddcontroller实现了具体的虚拟手柄,里面有虚拟手柄的模型。
其中的GvrControllerVisual脚本实现了当点击真实手柄的时候,虚拟手柄进行一些点击效果的显示,主要在OnVisualUpdate方法中。
所以你可以看它的代码实现手柄点击事件的响应。
它里面还有一个ddtouch,ddtouch里面包含一个脚本组件GvrTouchPoint,它里面的方法OnVisualUpdate实现了触摸真实手柄的触摸盘的时候,虚拟手柄进行相应的触摸效果显示。所以你可以根据它里面的代码,实现触摸盘的响应事件。
Laser
模块中包含两个脚本GvrArmModelOffsets和GvrLaserPointer。
GvrArmModelOffsets主要是把手柄的位置赋值给它的GameObject的transform。
GvrLaserPointer主要是根据它的GameObject的transfrom画出来一条激光线,移动。
可以看出ddcontroller和Laser主要是显示出来虚拟的手柄和激光射线的。而GvrControllerMain主要是获取真实手柄的动作,然后把位置给了ddcontroller和Laser,最终实现了效果。