ARFoundation road -Raycasting

(A) ray detector concept

  Raycasting, literally translated as ray-casting, we usually based on its effect is called radiation detection, radiation detection by selecting a specific object commonly used technique in 3D digital world, such as the detection of bullets hit the enemy situation in 3D, VR games or from the ground to pick up a gun, to be used in radiation detection.

  In AR, when we detect and visualize a plane, we object to be placed on a flat surface, but what position we are placed in the plane of the object it? We want to know the three-dimensional plane is detected, and our phone screen is two-dimensional, three-dimensional how to choose the placement point in two-dimensional plane? The usual practice is for radiation detection.

  The basic idea of ​​radiation detected in the three-dimensional world from a point in one direction emit an infinite rays, in the direction of the radiation, upon collision with added model impactor occurs, generates a detected object collision, we can use radiation to detect the bullet hit the target to achieve, you can also use radiation to detect the location of a collision, for example, we can from the screen the user clicks the point to construct a use (that is, in our eyes AR) of the camera position rays with the plane of collision detection in a scene, if the collision position of the collision is returned, so that we can place the virtual objects on the plane detected.

In the foregoing, we use the following code to place a virtual object.

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;

[RequireComponent(typeof(ARRaycastManager))]
public class AppControler : MonoBehaviour
{
    public  GameObject spawnPrefab;
    static List<ARRaycastHit> Hits;
    private ARRaycastManager mRaycastManager;
    private GameObject spawnedObject = null;
    private void Start()
    {
        Hits = new List<ARRaycastHit>();
        mRaycastManager = GetComponent<ARRaycastManager>();
    }

    void Update()
    {
        if (Input.touchCount == 0)
            return;
        var touch = Input.GetTouch(0);
        if (mRaycastManager.Raycast(touch.position, Hits, TrackableType.PlaneWithinPolygon | TrackableType.PlaneWithinBounds))
        {
            var hitPose = Hits[0].pose;
            if (spawnedObject == null)
            {
                spawnedObject = Instantiate(spawnPrefab, hitPose.position, hitPose.rotation);
            }
            else
            {
                spawnedObject.transform.position = hitPose.position;
            }
        }
    }
}

  In the above code, we use mRaycastManager.Raycast () method does make-ray detector to the virtual object placement. In fact, in the AR, unless the position is not required automatically placed (e.g., grass on a plane detected), precise placement of the virtual object at the time, usually do ray detector to determine the position.

(B) Detailed ray detector

  In ARFoundation, the current-ray detector for detecting only a plane and the point clouds. ARFoundation the radiation detecting ray detector Unity in Physics module similar, but provides a separate interface, using ray detector requires ARRaycastManager assembly ARFoundation, since the radiation detecting object to be detected needs to be in the same coordinate space so that components need to be mounted on ARSessionOrigin object.

  ARFoundation ray detector can use one of two methods, which function prototype in the following table.

Ray detection method Explanation
public bool Raycast(Vector2 screenPoint, List hitResults, TrackableType trackableTypeMask = TrackableType.All) Reference 1 is a screen coordinate point, a list of all the parameter 2 rays collide with the target, the parameter type mask 3 Trackable (i.e. only the category or categories may be tracked object radiographed), the method returns a value of Bool type, true representation of a collision, false representation collision occurred.
public bool Raycast(Ray ray, List hitResults, TrackableType trackableTypeMask = TrackableType.All) Ray 1 is a type of reference ray (including location and orientation), a list of all the parameter 2 collide with the object rays, parameter 3 Trackable type mask (i.e., only the class or classes of objects can be tracked ray detector), this method returns a value of type Bool, true indicates collision, false indicates not collide.

  trackableTypeMask collision detection is required to filter object types, the type of trackable values ​​may be one of the following attribute values, or may be several, if a few, may be employed bitwise or, as TrackableType.PlaneWithinPolygon | TrackableType.FeaturePoint.

trackableType property Explanation
All The values ​​for all the objects placed in collision detection. If we fill this value, in ARFoundation, the radiation emitted we will work with all planes of the scene surrounding the polygon, the normal of the feature point with collision detection.
FeaturePoint Collision detection of all feature points in the current frame in the point cloud.
None This value is used to indicate no return trackableHit collision, if this value is passed to raycast, will not give any result of the collision.
PlaneWithinPolygon Convex polygon boundary has been detected and the collision detection plane.
PlaneWithinBounds It has been detected in the current frame in a plane bounding box for collision detection.
PlaneWithinInfinity And collision detection plane has been detected, but not limited to the bounding box detection or polygonal, but may be extended with collision detection plane detected plane.
FeaturePointWithSurfaceNormal Ray and the evaluation plane collision detection, evaluation can form a flat plane necessarily.
Planes Ray with all the above types of collision detection plane.
Image 2D image radiographed.
Face Radiographic detection and face.

  ARRaycastHit class is saved collision detected collision body information, its main attributes shown in the following table.

ARRaycastHit property Explanation
Distance type float, the distance from the radiation source to obtain hit point.
trackableId Collisions can be tracked object ID.
Pose Pose type, a light hitting the projection attitude Unity object world coordinates.
hitType Trackable type, a hit can be tracked object, i.e. seven previously described type may be one of the track (in fact, currently ARFoundation plane can only do the feature point detection radiation).
sessionRelativeDistance The distance from the ray origin to the point of impact in Session space.
sessionRelativePose Session posture in space collision point.

  With more than after the foundation, we can easily understand the code in the previous section, we first initialize a List ARRaycastHit type of array used to save all tracked objects collide with radiation detection, the List must be initialized, not to null value, which is to avoid garbage collection mechanism (garbage Collection, GC) to allocate memory back and forth. Then call the specified ray detection method can track objects collision detection, if a collision occurs, the Hits [0] (This is the first qualifying trackable objects collide with rays from the radiation source is a recent, usually this is the result we need, we usually choose the first object collides with the rays of) the location of the placement of virtual objects.

references

About AR Foundation About AR Foundation

Guess you like

Origin blog.csdn.net/yolon3000/article/details/94458183