ARFoundation之路-ARSession & ARSessionOrigin

  Before the project, we first add the Hierarchy window in the ARSession and ARSessionOrigin two objects, these two objects built up a framework for the most basic of AR applications, all other staff were deployed this basis, then this What is the role of two objects from throughout the AR application?

Here Insert Picture Description

(A) ARSession

  ARSession objects including two components, one is ARSession, the Session Manager, the other AR Input Manager, the management information input, as shown in FIG. AR in Session (the Chinese translation for the session) to the AR application state management, application lifecycle process AR, AR API's main entrance, AR enabled or disabled on the target platform by its control. In the life-cycle approach to deal with the corresponding Unity Session of the life cycle, such AR applications will be based on need to start or pause capture camera frame, initialization or release associated resources. AR Scene ARSession assembly must include, but ARSession can be hung on any scene objects, typically for administrative purposes, we will mount on ARSession object.

  AR Input Manager component is to enable the world to track the components necessary, this component is not enabled, Tracked Pose Driver (tracking pose driver) can not obtain attitude of the equipment.
Here Insert Picture Description
  If you disable ARSession during the application running, the system will no longer track the feature point in their environment and related functions, but if re-enable it later, the system will attempt to recover the feature point information previously detected. Select "Attempt Update", the device will try where possible to install AR software that supports this feature depends on the platform, on the Andriod platform, will try to install the latest version of ARCore, on the iOS platform, will try to install the latest version ARKit.

  Of particular note is that in any one of the AR application, there is one and only allowed a Session, Unity will therefore Session set to a global components, so if there are multiple ARSession scene, ARSession that these components will try to manage with a Session. Similarly, AR Input Manager component can only be one and only one.

  Many mobile phone platform equipment of all kinds, not every platform supports ARFoundation, so we need some Session status indicates the availability of the platform, so that step operation or when the device does not support the AR application may provide an alternative scheme in the state according to Session.

  To determine the current Session state (for example, if the device supports, and whether the software is being installed AR Session is working), you can use ARSessionState enumerated type, you can also change the subscription ARSession.stateChanged event in Session state. ARSessionState enumeration includes the following types:

ARSessionState Explanation
CheckingAvailability Applications are testing equipment availability.
Installing AR software is installed (in this case ARCore end of the phone or ARKit).
NeedsInstall The device supports AR, but you need to install the appropriate software (referred to herein as ARCore end of the phone or ARKit).
None Application has not yet completed initialization, device availability is unknown.
Ready AR available and ready.
SessionInitializing AR Session is initializing, usually AR available on the device, but the AR application has not yet gathered enough environmental information.
SessionTracking Session in normal operation and normal tracking state.
Unsupported Device does not support AR.

  Therefore, we can detect the current state of the AR application with the following code:

        if (ARSession.state == ARSessionState.None || ARSession.state == ARSessionState.CheckingAvailability)
        {
            //设备状态未知
        }
        if (ARSession.state == ARSessionState.Unsupported)
        {
            // 设备不支持AR
        }
        else
        {
            // 设备支持AR
        }

(二)ARSessionOrigin

  AR Session Origin objects have a default Transfrom AR Session Origin assembly and a component, as shown below.

Here Insert Picture Description
  AR Session Origin action assembly is to be tracked object (e.g., plane and the feature point) information into the pose Unity scene end position, orientation and scale. Because the AR device by the Session Manager, thus acquires posture information "session space (session space)", which is a relative non-scale space AR Session start, so AR Session Origin performs a coordinate space transformation, which Unity to transform coordinate space, this is similar to the model from local space to world space transformation, for example, when we make the model, the model we usually have their own coordinate system, all parts of the model are relatively construct its own coordinate system the model we own coordinate system called "model space" or "local space", when these models into Unity, all vertex position information model to be converted to Unity origin of coordinates referenced in world space. Also, the tracking device can pose generated by the AR device in the "Session Space", and therefore do a coordinate transformation is to transform into Unity in the world coordinate space.

  AR Session Origin also accepts a camera parameters, the camera is AR camera, AR camera with user equipment cameras can be aligned by Tracked Pose Driver components while aligned with AR session space, so that it can correct perspective rendering of a virtual object at a particular location .

  More convenient for rendering a virtual object, AR Session Origin also allows scaling the virtual objects and the relative offset of the camera. If you are using the zoom function or offset, the camera must AR Session Origin if the child object (the default is the sub-object), the camera is driven by the Session, so disposed and scaling the AR Session Origin offset, the camera may be detected and tracked scaling is also shifted together with the object.

  要将缩放应用于AR Session Origin,只需设置其Transform 中的Scale值,将偏移应用于AR Session Origin,只需设置其Transform 中的Position值,这里设置的缩放与偏移会影响所有虚拟物体的渲染,较大的Scale值将会使AR虚拟内容显示得更小,例如,10倍的Scale将使虚拟对象显示小10倍,而0.1将使虚拟对象显示大10倍。在需要整体缩放虚拟物体时可以考虑调整该值。

  AR Session Origin对象还有一个子对象AR Camera,AR Camera默认挂载Tracked Pose Driver 、AR Camera Manager、AR Camera Background,如下图所示。
Here Insert Picture Description
  Tracked Pose Driver 组件主要作用是将Unity中的场景摄像机与设备的真实摄像机对齐,即是根据设备真实摄像机的位置与方向来调整Unity中的场景摄像机姿态,通过与真实摄像机对齐匹配,Unity中场景摄像机与设备真实摄像机所有参数均一致(即拥有相同的投影矩阵),这样设置后真实摄像机与AR Session Space也是匹配的,由此来保证从Unity中渲染的虚拟物体在真实世界中的位置与姿态正确。反而言之,如果没有该组件,Unity渲染的虚拟物体将会在真实世界中杂乱无章。Tracked Pose Driver 组件还有一些控制参数,可以根据工程需要进行选择,甚至可以自己写姿态控制脚本。

  AR Camera Manage组件负责处理控制摄像机的一些细节参数,如表示纹理和控制光照估计模式。其有两个参数Focus Mode 和 Light Estimation Mode,相关意义如下表所示:

参数 选项
Focus Mode 摄像机对焦模式,可以为“Auto”或“Fixed”,Auto即允许摄像机自动对焦,一般我们会选择此模式,Fixed即为固定焦距,固定焦距不会改变设备摄像机焦距,因此,与被拍摄物体距离不合适时就是出现模糊的现象。
Light Estimation Mode 光照估计模式,可以为“Disable”或“Ambient Intesity”,选择Ambient Intesity将启用光照估计功能,ARFoundation将根据真实世界中的光照信息来评估当前环境中的光照强度、光照颜色、光照方向等信息,这个评估每一帧都要进行,所以如果不需要光照估计功能时选择Disable可以节约资源。

  在AR中,我们以真实环境做为背景,因此我们需要将摄像机捕捉到的图像渲染成背景,使用AR Camera Background组件就可以轻松实现这个功能。AR Camera Background组件还有两个参数Use Custom Material和Use Custom Render Asset ,这两个参数均为可选项。Use Custom Material、Use Custom Material一般情况下我们都不用勾选,这个默认会由Unity根据平台来进行背景渲染,但如果勾选,我们就要提供背景渲染的材质、Shader,利用这两个选项可以实现一些高级功能,如背景模糊、描边等。

  如果在场景中只有一个AR Session Origin,我们可以简单的为其添加一个AR Camera Background就可以将摄像机的图像渲染成背景,但如果有多个AR Session Origin,有多个摄像机(例如在不同的缩放尺度上渲染场景),那么这时我们就要为每一个AR Session Origin、每一个摄像机指定一个AR Camera Background组件并进行相应设置。

  ARSession and ARSessionOrigin responsible for the entire application life cycle management AR, AR camera head and processing background rendering-related work, which every AR applications are needed, but these efforts need to interact with the hardware devices and usually will be very complicated. The two function modules have conducted good encapsulation process, users no longer need to worry about the specific details, which greatly simplifies the difficulty of AR application development.

references

  About AR Foundation About AR Foundation

Guess you like

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