其他分享
首页 > 其他分享> > android studio Vuforia_4

android studio Vuforia_4

作者:互联网

Unity2019 Vuforia AR教程
晋中职业技术学院 智祥明 QQ 1064270685

第四章 目标图片组件介绍
在Vuforia提供的SDK中,最简单、也是最常见的AR功能就是Image Target-图像识别。你只需提供一张可识别的图片,将图片移到设备的摄像头下,设备上就能出现之前已经集成进去的虚拟场景,而且可以根据ARCamera的不同模式,让图片和虚拟场景的叠加现象有所不同。
ImageTarget对象有Transform、Image Target Behaviour、Advanced、Default TrackbleEvent Handler、Turn Off Behaviour、Materials、Lighting、Probes、Additional Settings、Image Target Mash 13702、Shder等组件。
一、 Image Target Behaviour组件
Type:类型。Predefined 重新定义;User Defined 用户定义;Cloud Reco 云记录。
Database:默认为VuforiaMars_Images。如(图一)
在这里插入图片描述

(图一)
Image Target:目标图片。默认选择的是默认数据库中的第一个图片。如(图二)
在这里插入图片描述

(图二)
二、 Advanced组件
高级选项设置。Width默认为0.06985;Height默认为0.12065.
Preserve Child Size 保持子对象的尺寸大小。
For extended tracking capabilities please use the DeviceTracker.
对于扩展跟踪功能,请使用DeviceTracker。
三、 Default Trackable Event Handler组件
默认可跟踪事件处理程序。可修改。
/==============================================================================
Copyright © 2019 PTC Inc. All Rights Reserved.
版权所有(c)2019 PTC公司。保留所有权利。美国参数技术公司(PTC公司)1985年成立,
公司总部位于美国马萨诸塞州。
Copyright © 2010-2014 Qualcomm Connected Experiences, Inc.
All Rights Reserved.
版权所有(c)2010-2014 Qualcomm Connected Experiences,Inc。版权所有。
美国高通互联体验公司(Qualcomm Connected Experiences, Inc.)
Inc.的原形是Incorporation. 意思是指:组成公司/注册/合并
Confidential and Proprietary - Protected under copyright and other laws.
机密和专有-受版权和其他法律保护。
==============================================================================
/

using UnityEngine;
using Vuforia;

///
/// A custom handler that implements the ITrackableEventHandler interface.
///实现ITrackableEventHandler接口的自定义处理程序。
/// Changes made to this file could be overwritten when upgrading the Vuforia version.
/// 升级Vuforia版本时,对此文件所做的更改可能会被覆盖。
/// When implementing custom event handler behavior, consider inheriting from this class instead.
/// 在实现自定义事件处理程序行为时,请考虑从此类继承。
///
public class DefaultTrackableEventHandler : MonoBehaviour, ITrackableEventHandler
{
#region PROTECTED_MEMBER_VARIABLES
//受保护的成员变量
protected TrackableBehaviour mTrackableBehaviour;
protected TrackableBehaviour.Status m_PreviousStatus;
protected TrackableBehaviour.Status m_NewStatus;

#endregion // PROTECTED_MEMBER_VARIABLES

#region UNITY_MONOBEHAVIOUR_METHODS

protected virtual void Start()
{
    mTrackableBehaviour = GetComponent<TrackableBehaviour>();
    if (mTrackableBehaviour)
        mTrackableBehaviour.RegisterTrackableEventHandler(this);
}

protected virtual void OnDestroy()
{
    if (mTrackableBehaviour)
        mTrackableBehaviour.UnregisterTrackableEventHandler(this);
}

#endregion // UNITY_MONOBEHAVIOUR_METHODS

#region PUBLIC_METHODS

/// <summary>
///     Implementation of the ITrackableEventHandler function called when the
///     tracking state changes.
/// </summary>
public void OnTrackableStateChanged(
    TrackableBehaviour.Status previousStatus,
    TrackableBehaviour.Status newStatus)
{
    m_PreviousStatus = previousStatus;
    m_NewStatus = newStatus;
    
    Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + 
              " " + mTrackableBehaviour.CurrentStatus +
              " -- " + mTrackableBehaviour.CurrentStatusInfo);

    if (newStatus == TrackableBehaviour.Status.DETECTED ||
        newStatus == TrackableBehaviour.Status.TRACKED ||
        newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
    {
        OnTrackingFound();
    }
    else if (previousStatus == TrackableBehaviour.Status.TRACKED &&
             newStatus == TrackableBehaviour.Status.NO_POSE)
    {
        OnTrackingLost();
    }
    else
    {
        // For combo of previousStatus=UNKNOWN + newStatus=UNKNOWN|NOT_FOUND
        // Vuforia is starting, but tracking has not been lost or found yet
        // Call OnTrackingLost() to hide the augmentations
        OnTrackingLost();
    }
}

#endregion // PUBLIC_METHODS

#region PROTECTED_METHODS

protected virtual void OnTrackingFound()
{
    if (mTrackableBehaviour)
    {
        var rendererComponents = mTrackableBehaviour.GetComponentsInChildren<Renderer>(true);
        var colliderComponents = mTrackableBehaviour.GetComponentsInChildren<Collider>(true);
        var canvasComponents = mTrackableBehaviour.GetComponentsInChildren<Canvas>(true);

        // Enable rendering:
        foreach (var component in rendererComponents)
            component.enabled = true;

        // Enable colliders:
        foreach (var component in colliderComponents)
            component.enabled = true;

        // Enable canvas':
        foreach (var component in canvasComponents)
            component.enabled = true;
    }
}


protected virtual void OnTrackingLost()
{
    if (mTrackableBehaviour)
    {
        var rendererComponents = mTrackableBehaviour.GetComponentsInChildren<Renderer>(true);
        var colliderComponents = mTrackableBehaviour.GetComponentsInChildren<Collider>(true);
        var canvasComponents = mTrackableBehaviour.GetComponentsInChildren<Canvas>(true);

        // Disable rendering:
        foreach (var component in rendererComponents)
            component.enabled = false;

        // Disable colliders:
        foreach (var component in colliderComponents)
            component.enabled = false;

        // Disable canvas':
        foreach (var component in canvasComponents)
            component.enabled = false;
    }
}

#endregion // PROTECTED_METHODS

}

四、 Turn Off Befaviour 组件
关闭Befaviour
五、 Probes组件
灯光探头。
六、 Additional Settings组件
其他设置

和Image Target选中对应的名字。
将想要展现的3D模型拖入到场景中,并将其作为ImageTarget的子物体。这样,已经完成了最简单的AR图像识别,如果你现在编译工程在设备上运行,将识别图放到摄像头取景范围内,模型就能出来了,并随着识别图的位置改变而改变。但是如果想要加入更多的交互功能,就要用代码来控制。修改Default Trackable Event Handler。

标签:Status,mTrackableBehaviour,component,TrackableBehaviour,studio,var,android,true,
来源: https://blog.csdn.net/weixin_47908682/article/details/114408267