UnityVuforia中脱卡和多目标追踪的实现

更新时间:2023-05-31 13:27:23 阅读: 评论:0

UnityVuforia中脱卡和多⽬标追踪的实现
脱卡功能:【当丢失追踪⽬标时,追踪⽬标显⽰在屏幕中央】
实现思路:默认的追踪⽅式是实现ITrackableEventHandler接⼝,当追踪到识别图,调⽤OnTrackingFound()函数,当丢失时,调⽤OnTrackingLost()函数。脱卡功能主要是修改当丢失追踪⽬标时的函数。
脱卡功能实现:⼀张是改变ARCamera的位置,⼀种是改变追踪模型的位置。本⽂主要是通过第⼀种⽅式,先保存好ARCamera的位置,当丢失时,将之前的保存好的ARCamera位置给ARCamera.localPosition;
using UnityEngine;
using Vuforia;
public class ChangeTargetEventHandler : MonoBehaviour, ITrackableEventHandler
{
private GameObject ARCamera;
private bool isFirstFound = fal;
private Vector3 LostARCameraPosition = new Vector3(0.013f,0.111f,-1f);//ARCamera位于屏幕中⼼位置
public void Awake()
{
ARCamera = GameObject.Find("ARCamera");
}
protected TrackableBehaviour mTrackableBehaviour;
protected virtual void Start()
{
mTrackableBehaviour = GetComponent<TrackableBehaviour>();
if (mTrackableBehaviour)
mTrackableBehaviour.RegisterTrackableEventHandler(this);
}
西藏英文protected virtual void OnDestroy()
{
if (mTrackableBehaviour)
mTrackableBehaviour.UnregisterTrackableEventHandler(this);
}
public void OnTrackableStateChanged(
TrackableBehaviour.Status previousStatus,
TrackableBehaviour.Status newStatus)
{
if (newStatus == TrackableBehaviour.Status.DETECTED ||
newStatus == TrackableBehaviour.Status.TRACKED ||
newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
{
Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
OnTrackingFound();
}
el if (previousStatus == TrackableBehaviour.Status.TRACKED &&
newStatus == TrackableBehaviour.Status.NOT_FOUND)
{
Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
OnTrackingLost();
}
el
{
OnTrackingLost();
}
}
/// <summary>
/// 当追踪到时遍历ImageTarget下的⼦物体,显⽰为可见状态
/// </summary>汽车培训学校
public void OnTrackingFound()
{
var rendererComponents = GetComponentsInChildren<Renderer>(true);
var colliderComponents = GetComponentsInChildren<Collider>(true);
var canvasComponents = GetComponentsInChildren<Canvas>(true);
// Enable rendering:
foreach (var component in rendererComponents)
// Enable colliders:
foreach (var component in colliderComponents)
// Enable canvas':新年快乐英文怎么写
foreach (var component in canvasComponents)
宾果什么意思
isFirstFound = true;
}
/// <summary>
/// 当追踪识别,相机移到屏幕中⼼位置(与默认的追踪⽅式,是将ImageTarget下的⼦物体隐藏起来)    /// </summary>
public void OnTrackingLost()
{
if (isFirstFound)
{
}
}
}
实现结果
追踪到时:
丢失时:
多个ImageTarget追踪的实现
实现思路:新建⼀个脚本管理ImageTarget,追踪之前,所有模型隐藏,当追踪到指定图⽚时,显⽰指定模型
using UnityEngine;
using Vuforia;
public class ChangeTrackableEventHandler : MonoBehaviour, ITrackableEventHandler
{
private bool IsFirstFound = fal;
protected TrackableBehaviour mTrackableBehaviour;
protected void Awake()
{
MoreTargetManger.Instance.TotalImageTarget.ansform);//将所有含有该脚本的Transform添加到列表当中
}
protected virtual void Start()
{
mTrackableBehaviour = GetComponent<TrackableBehaviour>();
if (mTrackableBehaviour)
mTrackableBehaviour.RegisterTrackableEventHandler(this);
}
protected virtual void OnDestroy()
{
if (mTrackableBehaviour)
mTrackableBehaviour.UnregisterTrackableEventHandler(this);
}
public void OnTrackableStateChanged(
TrackableBehaviour.Status previousStatus,
TrackableBehaviour.Status newStatus)
{fromtimetotime
if (newStatus == TrackableBehaviour.Status.DETECTED ||
newStatus == TrackableBehaviour.Status.TRACKED ||
newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
IsFirstFound = true;
if (MoreTargetManger.Instance.LastTrackingName == mTrackableBehaviour.TrackableName)//当前追踪的图⽚和丢失的图⽚是相同的名字时,返回空
{
return;
}
el
{
tahiti组合
MoreTargetManger.Instance.HideImageTarget();//隐藏掉所有模型
Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
OnTrackingFound();//显⽰指定模型
}
}
el if (previousStatus == TrackableBehaviour.Status.TRACKED &&
newStatus == TrackableBehaviour.Status.NOT_FOUND)
{
if (IsFirstFound)
{
MoreTargetManger.Instance.LastTrackingName = mTrackableBehaviour.TrackableName;//找到丢失ImageTarge的名字
MoreTargetManger.Instance.Lost = true;//开启脱卡
}
}
el
{
OnTrackingLost();
}
}
protected virtual void OnTrackingFound()
{
var rendererComponents = GetComponentsInChildren<Renderer>(true);
var colliderComponents = GetComponentsInChildren<Collider>(true);
var canvasComponents = GetComponentsInChildren<Canvas>(true);
// Enable rendering:
foreach (var component in rendererComponents)
// Enable colliders:
foreach (var component in colliderComponents)
// Enable canvas':
foreach (var component in canvasComponents)
}
public virtual void OnTrackingLost()
{
var rendererComponents = GetComponentsInChildren<Renderer>(true);
var colliderComponents = GetComponentsInChildren<Collider>(true);
var canvasComponents = GetComponentsInChildren<Canvas>(true);
// Disable rendering:
foreach (var component in rendererComponents)
// Disable colliders:
foreach (var component in colliderComponents)
// Disable canvas':
foreach (var component in canvasComponents)
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoreTargetManger : MonoBehaviour {
private GameObject ARCamera;
takethelead
private Vector3 LostARCameraPosition = new Vector3(0.004f, 0.158f, -0.413f);
public static MoreTargetManger Instance;
public List<Transform> TotalImageTarget = new List<Transform>();
public string LastTrackingName = "";
public bool Lost = fal;
public static MoreTargetManger instance
{
get
{
return Instance;
}
}
private void Awake()
{
Instance = this;
ARCamera = GameObject.Find("ARCamera");
}
// U this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Lost)
{
Lost = fal;
}
}
public void HideImageTarget()
世界杯主题曲we will rock you{
foreach (Transform ImageTarget in TotalImageTarget)
zai xian fan yi
{
ImageTarget.GetComponent<ChangeTrackableEventHandler>().OnTrackingLost();        }
}
}
实现结果然而英语

本文发布于:2023-05-31 13:27:23,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/129328.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:追踪   位置   丢失   实现   模型   脚本
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图