您的位置:首页 > 移动开发 > Unity3D

gvr sdk for unity 开发笔记 - GazeInputModule解读

2016-10-27 19:52 218 查看
GazeInputModule是在Unity Editor下最容易测试的了,在跟随gvr sdk for unity一起导入的包里,包含了一个GvrReticle这个预设体,这个预设体绑定一个脚本GvrReticle.cs,打开脚本,可以看到它继承有一个IGvrGazePointer的接口,这个就是接收GazeInputModule事件的接口;还有一个在HeadSetDemo里的场景中的一个Cube绑定的Teleport脚本,打开这个脚本则会看到,它继承有另一个叫做IGvrGazeResponder的接口;

IGvrGazePointer 和 IGvrGazeResponder的作用就很明显了,IGvrGazePointer相对于观察者的凝视事件接收器,是主动的,也就是说观察者看到什么东西,可以通过继承实现里面的接口,就可以获取正在凝视着的物体;IGvrGazeResponder是被凝视事件的接收器,当一个物体被凝视,就是触发里面的事件,就像HeadSetDemo里面的Cube,当你把准星对准它的时候它会变色;就是通过继承实现IGvrGazeResponder的事件接口来修改Cube的颜色;

例子说明先到这里,下面翻译一下IGvrGazePointer里面各个事件接口的注释:

1

/// This is called when the 'BaseInputModule' system should be enabled.
void OnGazeEnabled();
上面这个比较好理解就是基础输入模块启用的时候会调用这个函数;无参;

2

/// This is called when the 'BaseInputModule' system should be disabled.
void OnGazeDisabled();
这个跟第一个相反,就是被禁用的使用调用这个函数

3

/// Called when the user is looking on a valid GameObject. This can be a 3D
/// or UI element.
///
/// The camera is the event camera, the target is the object
/// the user is looking at, and the intersectionPosition is the intersection
/// point of the ray sent from the camera on the object.
void OnGazeStart(Camera camera, GameObject targetObject, Vector3 intersectionPosition,
bool isInteractive);
 void OnGazeStay(Camera camera, GameObject targetObject, Vector3 intersectionPosition,
                  bool isInteractive);
上面参数相同的函数,第一个表示开始凝视物体,第二个表示凝视点滞留在物体上面;当凝视点落在3d物体或者UI元素都会调用;camera是指处理事件的摄像机,至于是哪个摄像机,我初步验证是Main Camera;targetObject就是当前凝视点或者正在盯着的游戏物体;intersesionPosition 由摄像机发出射线与当前凝视物体相交点;isInteractive是否可交互的,至于这个参数具体在什么情况下为真,我初步验证当targetObject绑定有Event
Trigger的时候,凝视这个物体,这个参数就是真,当
9cf6
没有Event Trigger的时候就为假;

4

/// Called when the user's look no longer intersects an object previously
/// intersected with a ray projected from the camera.
/// This is also called just before **OnGazeDisabled** and may have have any of
/// the values set as **null**.
///
/// The camera is the event camera and the target is the object the user
/// previously looked at.
void OnGazeExit(Camera camera, GameObject targetObject);
这个就是当视线离开游戏物体的时候调用,参数的意义和上面3的对应;但是这个函数会在OnGazeDisable前调用一次,并且参数会被设置为null,至于那个参数被设置为null,暂时不做验证;

5.

void OnGazeTriggerStart(Camera camera);
void OnGazeTriggerEnd(Camera camera);
上面的这两个意思就很明确,就是在开始凝视和结束结束的时候触发,这两个有点含糊,在例子里面,我发现Editor下鼠标点击一次,这两个函数都会分别调用一次,当然如果在VR设备上,这个点击事件应该是没办法触发的;

6.

/// Return the radius of the gaze pointer. This is used when searching for
/// valid gaze targets. If a radius is 0, the GvrGaze will use a ray
/// to find a valid gaze target. Otherwise it will use a SphereCast.
/// The *innerRadius* is used for finding new targets while the *outerRadius*
/// is used to see if you are still nearby the object currently looked at
/// to avoid a flickering effect when just at the border of the intersection.
void GetPointerRadius(out float innerRadius, out float outerRadius);
最后这个应该是功能最强大的,获取光标或者凝视点的半径,输出内半径和外半径,感情这就是一个环形的的点或者球;这个用于查找凝视目标;如果半径是0,GvrGaze会使用一条射线去查找凝视目标;否则会使用一个球体投射来查找,至于球体投射,我理解的是GvrGaze通过向凝视方向发射一个带碰撞体的球体来模拟射线,这样就可以扩大检测范围,当你需要凝视的物体很小的时候,用射线的话你需要调整很久才能精确找到它,如果用一个大一点的球体,那么你就不用那么精确的去凝视一个物体,哈哈,看来还是想的很周到,没人希望整天用斗鸡眼去看物体;但是这里的函数明显输出两个半径,一个内半径用于查找新目标物体,外半径用于检测你是否还在当前凝视着的物体附近,而且相交点刚好落在边界上的时候,用来避免闪烁效应,这里闪烁效应的理解应该就是一个点是没有半径的,当你输入的半径大于0的时候,使用球体投射来查找物体时,这个球体的中心刚好在边界上,即一半和物体相交,一半露出外面,这时候究竟算不算还盯着这个物体呢,这里就用到了外半径,如果已经超出了外半径,那么肯定就是不盯着物体了;这个在近大远小的3D世界里检测就非常有用了,当然如果远处的物体好几个都在一块,而你只要精确到某一个的时候,就有可能连附近的都盯上了,哈哈哈!在例子里的这个GvrReticle脚本里就有处理内半径和外半径的,这里就不说了,



下面是IGvrGazeResponder这个接口

/// Called when the user is looking on a GameObject with this script,
/// as long as it is set to an appropriate layer (see GvrGaze).
void OnGazeEnter();

/// Called when the user stops looking on the GameObject, after OnGazeEnter
/// was already called.
void OnGazeExit();

/// Called when the trigger is used, between OnGazeEnter and OnGazeExit.
void OnGazeTrigger();
这里有三个无参函数,意思都很明确,凝视进入、退出、触发;但是在例子Teleport.cs里面,明显没有调用这三个函数,起码在Editor下没有调用,至于在VR设备上有没有,我就没有验证过了,例子里面都是直接在EventTrigger里绑定事件函数的,OK,还有很多没有解释明白,不过就先记到此处吧,下次研究明白在更新!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: