使用unity+Vuforia实现脱卡识别,多目标情况

更新时间:2023-05-31 13:44:42 阅读: 评论:0

使⽤unity+Vuforia实现脱卡识别,多⽬标情况
Action……………………
当图⽚识别成功后,将当前组件作为模型的⽗物体,当图⽚丢失后,在Camera中新建⼀个空物体(empty),并将模型作为该空物体的⼦物体
替换Default Trackable Event Handler脚本:
///NotFound.cs
using UnityEngine;
using System.Collections;
using Vuforia;
using System;
using UnityEngine.SceneManagement;
public class NotFound : MonoBehaviour , ITrackableEventHandler
{
#region PRIVATE_MEMBER_VARIABLES
private TrackableBehaviour mTrackableBehaviour;
//判断是否是第⼀次识别是否完成,防⽌开启程序未放⼊识别图也在屏幕中央出现模型
bool firstfound = fal;
public GameObject successImage;
public bool imageIsOut = fal;//识别成功的图⽚是否已经出现过
//模型起始位置,值为起始模型组件中Transform.Position
//Vector3 origposition = new Vector3 (0, 0.25f, 0);
public Vector3 originPosition;
//当前imagetarget对应的模型等target
public Transform[] localTargets;
//其他⾮当前imagetarget对应的模型等targets
public Transform[] otherTargets;
//Camera Object
GameObject gObject;
//Camera Object的Creat Empty脚本
CreatEmpty cempty;
//表⽰ImageTarget⽗物体那个组件
public GameObject[] otherImageTargets;
#region ⾃定义的协程延时函数
//定义⼀个延时函数
public static IEnumerator DelayToInvokeDo(Action action, float delaySeconds)
{
yield return new WaitForSeconds(delaySeconds);
action();
}
#endregion //⾃定义的协程延时函数
void Start()
{
//不显⽰两张识别成功失败的图⽚
successImage.SetActive (fal);
//相机⾃动对焦
Vuforia.CameraDevice.Instance.SetFocusMode(Vuforia.CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
mTrackableBehaviour = GetComponent<TrackableBehaviour>();
if (mTrackableBehaviour)
{
mTrackableBehaviour.RegisterTrackableEventHandler(this);
}
//获取Camera中组件的CreatEmpty脚本
gObject = GameObject.FindWithTag("MainCamera");
cempty = gObject.GetComponent<CreatEmpty> ();万圣节英文
上海昂立if(cempty != null)
{
/
/杀死空物体
cempty.destoryempty ();
}
}
void Update()
{
//退出当前程序
if(Input.GetKeyDown(KeyCode.Escape))
{
//Application.Quit();
//Application.LoadLevel("PreschoolEducation-0-MenuScreen");
SceneManager.LoadScene ("PreschoolEducation-0-MenuScreen");
}
}
#endregion // UNTIY_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)
{
//Vector3 orirotation = new Vector3 (270, 0, 0);
if (newStatus == TrackableBehaviour.Status.DETECTED ||
newStatus == TrackableBehaviour.Status.TRACKED ||
newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
{
//如果识别成功图⽚没有出现过,则执⾏下⾯的代码,显⽰并延时0.5秒后消失
if(!imageIsOut)
{
successImage.SetActive (true);
for(int i = 0; i < otherTargets.Length; i++)
{
//但是隐藏其他imagetarget对应的模型,⽬的是防⽌在该imagetarget对应的模型出现在屏幕中央的时候不受其他imagetarget对应的模型的影响//otherTargets[i].gameObject.SetActive (fal);
tShow (otherTargets [i], fal);
}
for(int i = 0; i < localTargets.Length; i++)
{
fifteen//localTargets[i].gameObject.SetActive (fal);
tShow (localTargets [i], fal);
}
//需要延时0.3s,让图⽚多显⽰0.3s后消失(执⾏outImage函数)
//Invoke ("outImage", 0.5f);//如果这样,并不影响下⾯代码的执⾏。
StartCoroutine(DelayToInvokeDo(() =>
{
outImage(newStatus);
//识别成功图⽚已经显⽰过了
imageIsOut = true;
}, 0.4f));
}
el
{
OnTrackingFound ();
}
}
欧盟是什么意思el
{
OnTrackingLost ();
}
}
#endregion // PUBLIC_METHODS
//识别成功图⽚显⽰并消失之后
private void outImage(TrackableBehaviour.Status newStatus)
{
successImage.SetActive (fal);
for(int i = 0; i < otherTargets.Length; i++)
{
//但是隐藏其他imagetarget对应的模型,⽬的是防⽌在该imagetarget对应的模型出现在屏幕中央的时候不受其他imagetarget对应的模型的影响//otherTargets[i].gameObject.SetActive (fal);
tasktShow (otherTargets [i], fal);
}
for(int i = 0; i < localTargets.Length; i++)
{
//localTargets[i].gameObject.SetActive (fal);
tShow (localTargets [i], fal);
}
//显⽰并消失之后在⼀次判断图⽚是否处于追踪状态
if (newStatus == TrackableBehaviour.Status.TRACKED ||
newStatus == TrackableBehaviour.Status.DETECTED ||
OnTrackingFound ();
firstfound = true;
}
el
{
//OnTrackingLost ();
for(int i = 0; i < otherTargets.Length; i++)
{
/
/但是隐藏其他imagetarget对应的模型,⽬的是防⽌在该imagetarget对应的模型出现在屏幕中央的时候不受其他imagetarget对应的模型的影响//otherTargets[i].gameObject.SetActive (fal);
tShow (otherTargets [i], fal);
}
for(int i = 0; i < localTargets.Length; i++)
{
//localTargets[i].gameObject.SetActive (fal);
tShow (localTargets [i], fal);
}
}
}
private void OnTrackingFound()
{
Debug.Log ("it is found");
for(int i = 0; i < otherImageTargets.Length; i++)
{
//让其他的识别图在执⾏⼀次状态识别成功的代码,使得模型归位到imagetarget⼦物体
otherImageTargets[i].GetComponent<NotFound> ().homing ();
//当前图⽚识别成功时,关闭其他图⽚的识别
秋季护肤小常识
//      otherImageTargets[i].SetActive (fal);
//将其他识别图中的识别成功图⽚设置成未显⽰过
otherImageTargets [i].GetComponent<NotFound> ().imageIsOut = fal;
}
for(int i = 0; i < localTargets.Length; i++)
{
//tShow (localTargets [i], true);
//othertargetmodel.gameObject.SetActive (fal);
localTargets[i].parent = ansform;
localTargets[i].localPosition = originPosition;
abncelocalTargets[i].rotation = Quaternion.Euler ();体现英文
//开启当前图⽚对应的模型,因为在识别其他图⽚的时候有可能关闭了这个图⽚对应的模型
//localTargets[i].gameObject.SetActive (true);
tShow (localTargets [i], true);
}
/
/杀死空物体
cempty.destoryempty ();
//因为杀死了空物体,所以⼀旦丢失跟踪,模型就没地⽅放了,所以就加⼀个判断,丢失的话就不显⽰模型
if(mTrackableBehaviour.CurrentStatus ==TrackableBehaviour.Status.NOT_FOUND)
{
for(int i = 0;i < localTargets.Length; i++)
{
}
//ation = Quaternion.Euler(orirotation);
//firstfound = true;
}
private void OnTrackingLost()
{
needDebug.Log ("it is lost");
for(int i = 0; i < localTargets.Length; i++)
{
//localTargets[i].gameObject.SetActive (fal);
tShow (localTargets [i], fal);
}
successImage.SetActive (fal);
if (firstfound == true)
{
/
/创建空物体
for(int i = 0; i < otherTargets.Length; i++)
{
//但是隐藏其他imagetarget对应的模型,⽬的是防⽌在该imagetarget对应的模型出现在屏幕中央的时候不受其他imagetarget对应的模型的影响//otherTargets[i].gameObject.SetActive (fal);
tShow (otherTargets [i], fal);
}
GameObject emptyobject = GameObject.Find ("empty");
pdscTransform cameraPos = ansform;
for(int i = 0; i < localTargets.Length; i++)
{
//将target作为cameraPos的⼦物体,cameraPos就是emptyobject(即空物体)的Transform表现形式
localTargets[i].parent = cameraPos;
localTargets[i].localPosition = originPosition;
//target.localRotation = Quaternion.Euler (orirotation);
localTargets[i].localRotation = Quaternion.Euler ();
//localTargets[i].gameObject.SetActive (true);
tShow (localTargets [i], true);
}
}
el
{
for(int i = 0; i < otherTargets.Length; i++)
{
//但是隐藏其他imagetarget对应的模型,⽬的是防⽌在该imagetarget对应的模型出现在屏幕中央的时候不受其他imagetarget对应的模型的影响//otherTargets[i].gameObject.SetActive (fal);
tShow (otherTargets [i], fal);
}
for(int i = 0; i < localTargets.Length; i++)
{
print ("local Targets t active fal");
//localTargets[i].gameObject.SetActive (fal);

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

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

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

标签:模型   对应   识别   物体
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图