Vuforia----自定义目标识别

2025-04-05 22:45:04

1、侑喏嵋甲插件导入访问https://developer.vuforia.com/downloads/sdk,进入如下界面,并下载vufor足毂忍珩iaSDK的Unity版本。2、创建工程新建一个Unity工程并将下载好的插件导入到Unity中。3、场景搭建删除场景中原有的MainCamera,接着从Vuforia->Prefabs路径下将AR Camera,ImageTarget以及UserDefinedTargetBuilder拖入场景。接着在AR Camera的Inspector面板下添加AppLicenseKey。随后在ImageTarge的Inspector面版下将ImageTarget类型设置为UserDefined,如下图:

Vuforia----自定义目标识别

2、接下来我们需要添加模型了,这里就用Cube代钣伊帝藻替了,我们在ImageTarget下添加一个Cube。最后创建Button按钮,也就是我们创建自定义识别图最重要的一步。3、编写脚本在Project下新建一个C#脚本,脚本主框架如下:usingUnityEngine;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Linq;usingVuforia;publicclassUDTTest: MonoBehaviour,IUserDefinedTargetEventHandler{voidStart (){}publicvoidOnFrameQualityChanged(ImageTargetBuilder.FrameQualityframeQuality) { }publicvoidOnInitialized() { }publicvoidOnNewTrackableSource(TrackableSourcetrackableSource) { }publicvoidBuildNewTarget(){}}这个类当中比较关键的就是IUserDefinedTargetEventHandler这个接口,这个接口中定义了OnFrameQualityChanged,OnInitialized,OnNewTrackableSource这三个方法,我们实现这个接口中的三个方法,并将脚本附加到UserDefinedTargetBuilder上。首先我们需要声明一个UserDefinedTargetBuildingBehaviour类型的变量,接着在Start函数里将当前类注册为该类型事件。UserDefinedTargetBuildingBehaviourmTargetBuildingBehaviour;voidStart (){mTargetBuildingBehaviour = GetComponent<UserDefinedTargetBuildingBehaviour>();if(mTargetBuildingBehaviour){mTargetBuildingBehaviour.RegisterEventHandler(this);Debug.Log("Registering User Defined Target event handler.");}}接下来要实现接口中的三个函数,先是OnInitialized,用来初始化一些数据,我们在这个函数里写入下面的代码:publicvoidOnInitialized(){mObjectTracker = TrackerManager.Instance.GetTracker<ObjectTracker>();if(mObjectTracker != null){mBuiltDataSet = mObjectTracker.CreateDataSet();mObjectTracker.ActivateDataSet(mBuiltDataSet);}}这里用到两个新的变量需要提前声明,如下:ObjectTrackermObjectTracker;// 新定义的数据集添加到DataSet里DataSetmBuiltDataSet;接着是OnFrameQualityChanged这个函数,用来更新当前帧质量。我们在里面写入下面的代码:publicvoidOnFrameQualityChanged(ImageTargetBuilder.FrameQualityframeQuality){mFrameQuality = frameQuality;if(mFrameQuality == ImageTargetBuilder.FrameQuality.FRAME_QUALITY_LOW){Debug.Log("Low camera image quality");}}这里的mFrameQuality 是一个用来记录当前帧图像质量的枚举类型,声明如下:ImageTargetBuilder.FrameQualitymFrameQuality = ImageTargetBuilder.FrameQuality.FRAME_QUALITY_NONE;最后也是最重要的一个方法OnNewTrackableSource,代码如下:publicvoidOnNewTrackableSource(TrackableSourcetrackableSource){mTargetCounter++;// Deactivates the dataset firstmObjectTracker.DeactivateDataSet(mBuiltDataSet);// Destroy the oldest target if the dataset is full or the dataset// already contains five user-defined targets.if(mBuiltDataSet.HasReachedTrackableLimit() || mBuiltDataSet.GetTrackables().Count() >= 5){IEnumerable<Trackable> trackables = mBuiltDataSet.GetTrackables();Trackableoldest = null;foreach(Trackabletrackable intrackables){if(oldest == null|| trackable.ID < oldest.ID)oldest = trackable;}if(oldest != null){Debug.Log("Destroying oldest trackable in UDT dataset: "+ oldest.Name);mBuiltDataSet.Destroy(oldest, true);}}// Get predefined trackable and instantiate itImageTargetBehaviourimageTargetCopy = (ImageTargetBehaviour)Instantiate(ImageTargetTemplate);imageTargetCopy.gameObject.name = "UserDefinedTarget-"+ mTargetCounter;// Add the duplicated trackable to the data set and activate itmBuiltDataSet.CreateTrackable(trackableSource, imageTargetCopy.gameObject);// Activate the dataset againmObjectTracker.ActivateDataSet(mBuiltDataSet);}这个函数里需要提前声明的变量有两个,如下:intmTargetCounter;//声明一个公开的ImageTargetBehaviour然后在Unity中赋值publicImageTargetBehaviourImageTargetTemplate;最后一个就是我们自定义的一个函数BuildNewTarget,代码如下:publicvoidBuildNewTarget(){mTargetBuildingBehaviour.BuildNewTarget("test", 50);}然后还需要返回到Unity中进行一些细微的设置调整,将我们的脚本拖到UserDefinedTargetBuilder下,并给ImageTargetTemplate进行赋值,如下图:

Vuforia----自定义目标识别

3、然后我们在Button上添加点击事件,步骤如下:

Vuforia----自定义目标识别
Vuforia----自定义目标识别

4、然后在UserDefinedTargetBuilder上勾选Start scanning automatically,表示开启自动扫描。5、运行工程,首先去拍摄一张图片,界面如下:

Vuforia----自定义目标识别

5、将我们需要自定义的识别图置于取景框内,点击Button,便可识别出Cube,将识别图移除摄像头取景范围,模型消失,再次扫描刚才的识别图,依然出现模型。如下:

Vuforia----自定义目标识别

6、至此,Vuforia的自定义目标识别,介绍完毕,大家可以根据自己的需求,发挥想象力,制作出属于自己的炫酷AR产品。

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢