Vuforia_DefinedTarget的使用
程序员文章站
2022-04-28 21:27:46
...
重要的脚本:
1.User DefinedTargetBuilding Behaviour
2.UDT Event Handler(自定义,继承于 IUserDefinedTargetEventHandler)
3.Image Target Behaviour
4.Default Trackable Event Handler(自定义,继承于 ITrackableEventHandler)
案例使用(脚本解读)
UDT Event Handler.cs
实例化一个新的用户定义目标,还负责将回调分派到IUserDefinedTargetEventHandler :: OnNewTrackableSource
public void BuildwNewTarget()
{
if (m_FrameQuality == ImageTargetBuilder.FrameQuality.FRAME_QUALITY_MEDIUM ||
m_FrameQuality == ImageTargetBuilder.FrameQuality.FRAME_QUALITY_HIGH)
{
//创建下一个目标的名称。 原始的链接ImageTargetBehaviour的TrackableName扩展了一个连续的数字,以确保唯一的名称
string targetName = string.Format("{0}-{1}", ImageTargetTemplate.TrackableName, m_TargetCounter);
// 生成 a new target:
m_TargetBuildingBehaviour.BuildNewTarget(targetName, ImageTargetTemplate.GetSize().x);
}
else
{
Debug.Log("Cannot build new target, due to poor camera image quality");
StatusMessage.Instance.Display("Low camera image quality", true);
}
}
成功初始化UserDefinedTargetBuildingBehaviour时调用
public void OnInitialized()
{
m_ObjectTracker = TrackerManager.Instance.GetTracker<ObjectTracker>();
if (m_ObjectTracker != null)
{
// Create a new dataset
m_UDT_DataSet = m_ObjectTracker.CreateDataSet();
m_ObjectTracker.ActivateDataSet(m_UDT_DataSet);
}
}
更新当前帧质量
public void OnFrameQualityChanged(ImageTargetBuilder.FrameQuality frameQuality)
{
Debug.Log("Frame quality changed: " + frameQuality.ToString());
m_FrameQuality = frameQuality;
if (m_FrameQuality == ImageTargetBuilder.FrameQuality.FRAME_QUALITY_LOW)
{
Debug.Log("Low camera image quality");
}
m_FrameQualityMeter.SetQuality(frameQuality);
}
取得一个新的可跟踪源并将其添加到数据集中。一旦您使用UserDefinedTargetBuildingBehaviour BuildNewTarget,就会自动调用该源
public void OnNewTrackableSource(TrackableSource trackableSource)
{
m_TargetCounter++;
// Deactivates the dataset first
m_ObjectTracker.DeactivateDataSet(m_UDT_DataSet);
// Destroy the oldest target if the dataset is full or the dataset
// already contains five user-defined targets.
if (m_UDT_DataSet.HasReachedTrackableLimit() || m_UDT_DataSet.GetTrackables().Count() >= MAX_TARGETS)
{
IEnumerable<Trackable> trackables = m_UDT_DataSet.GetTrackables();
Trackable oldest = null;
foreach (Trackable trackable in trackables)
{
if (oldest == null || trackable.ID < oldest.ID)
oldest = trackable;
}
if (oldest != null)
{
Debug.Log("Destroying oldest trackable in UDT dataset: " + oldest.Name);
m_UDT_DataSet.Destroy(oldest, true);
}
}
// Get predefined trackable and instantiate it
//获取预定义的可跟踪并将其实例化
ImageTargetBehaviour imageTargetCopy = Instantiate(ImageTargetTemplate);
imageTargetCopy.gameObject.name = "UserDefinedTarget-" + m_TargetCounter;
// Add the duplicated trackable to the data set and activate it
m_UDT_DataSet.CreateTrackable(trackableSource, imageTargetCopy.gameObject);
// Activate the dataset again
m_ObjectTracker.ActivateDataSet(m_UDT_DataSet);
// Make sure TargetBuildingBehaviour keeps scanning...
m_TargetBuildingBehaviour.StartScanning();
}