Unity中htc vive射线与UI碰撞检测-graphcs.Raycast()

发表于2018-05-07
评论0 6.2k浏览
Unity中htc vive射线与UI碰撞检测其核心思路是用射线+碰撞的方法,不了解的可以看看。

代码如下:
// htc vive CanvasRaycastMethod   
/* 
继承关系:IRaycastMethod 接口<-- BaseRaycastMetod抽象类 <-- CanvasRaycastMethod 
重写Raycast(Ray ray, float distance, List<RaycastResult> raycastResults) 
*/  
public override void Raycast(Ray ray, float distance, List<RaycastResult> raycastResults)  
{  
    var tempCanvases = ListPool<ICanvasRaycastTarget>.Get();  
    tempCanvases.AddRange(canvases);  
    for (int i = tempCanvases.Count - 1; i >= 0; --i)  
    {  
        var target = tempCanvases[i];  
        if (target == null || !target.enabled) { continue; }  
        Raycast(target.canvas, target.ignoreReversedGraphics, ray, distance, raycaster, raycastResults);// raycaster from BaseRaycastMetod  
    }  
    ListPool<ICanvasRaycastTarget>.Release(tempCanvases);  
}  
public static void Raycast(Canvas canvas, bool ignoreReversedGraphics, Ray ray, float distance, Pointer3DRaycaster raycaster, List<RaycastResult> raycastResults)  
{  
    if (canvas == null) { return; }  
    var eventCamera = raycaster.eventCamera;  // 获取相机事件     
    var screenCenterPoint = Pointer3DInputModule.ScreenCenterPoint;  // 获得屏幕的中心点  
    var graphics = GraphicRegistry.GetGraphicsForCanvas(canvas); // 获取canvas中的Graphic列表, UI中图形继承的Graphic  
    // Pointer3DRaycaster should set tje eventCamera to correct position  
    for (int i = 0; i < graphics.Count; ++i)  
    {  
        var graphic = graphics[i];  
        // -1 means it hasn't been processed by the canvas, which means it isn't actually drawn  
        if (graphic.depth == -1 || !graphic.raycastTarget) { continue; }  
        if (!RectTransformUtility.RectangleContainsScreenPoint(graphic.rectTransform, screenCenterPoint, eventCamera)) { continue; }  
// 如果射线方向和graphic法线方向夹角大于90或者大于-90,说明只能看到线或者graphic的背面  
        if (ignoreReversedGraphics && Vector3.Dot(ray.direction, graphic.transform.forward) <= 0f) { continue; }  
// 如果是有效位置返回真,否则返回假  
        if (!graphic.Raycast(screenCenterPoint, eventCamera)) { continue; }  
        //var dist = Vector3.Dot(transForward, trans.position - ray.origin) / Vector3.Dot(transForward, ray.direction);  
        float dist;  
        // 法线加点绘制平面  返回沿射线与平面相交的距离  
        new Plane(graphic.transform.forward, graphic.transform.position).Raycast(ray, out dist);  
        if (dist > distance) { continue; }  
        raycastResults.Add(new RaycastResult  
        {  
            gameObject = graphic.gameObject,  
            module = raycaster,  
            distance = dist,  
            worldPosition = ray.GetPoint(dist),  
            worldNormal = -graphic.transform.forward,  
            screenPosition = screenCenterPoint,  
            index = raycastResults.Count,  
            depth = graphic.depth,  
            sortingLayer = canvas.sortingLayerID,  
            sortingOrder = canvas.sortingOrder  
        });  
    }  
}  

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引