unity vuforia AR模型多卡切換后脫卡效果


找unity同事要的一個腳本直接加上去

 

 

這是脫卡的管理類

TrackManager.cs

using UnityEngine;
using System.Collections;

public class TrackManager : MonoBehaviour
{

    #region 單例
    private static TrackManager _instance;
    public static TrackManager Instance { get { return _instance; } }
    void Awake()
    {
        _instance = this;
    }
    #endregion

    private Transform _camera;
    private Transform _target;
    private Transform _model;

    void Start()
    {
        _camera = Camera.main.transform;
    }

    /// <summary>
    /// 識別到圖片時
    /// </summary>
    /// <param name="target"></param>
    public void OnTrackingFound(Transform target)
    {
        if (_target == null)
        {
            _target = target;
            _model = _target.GetChild(0);
            InitModel();
        }
        else if (_target == target) //如果再次識別到的圖片還是上次的識別圖
        {
            InitModel();
        }
        else    //當前的識別圖不是上次的識別圖
        {
            ResetModel();   //重置上次的模型

            _target = target;
            _model = _target.GetChild(0);
            InitModel();
        }
    }

    /// <summary>
    /// 脫離圖片時
    /// </summary>
    /// <param name="model"></param>
    public void OnTrackingLost(Transform target)
    {
        if (_target == null) //初始化時調用TrackLost則全部將模型隱藏
            HideModel(target.GetChild(0).gameObject);
        else
            ShowInCamera(); //否則設為相機子對象顯示在屏幕中
    }

    /// <summary>
    /// 在識別圖上重置
    /// </summary>
    void InitModel()
    {
        if (_model == null || _target == null)
            return;
        if (_model.parent == _target)   //判斷當前模型的父對象是否是識別圖
        {
            _model.localPosition = Vector3.zero;
            _model.localEulerAngles = Vector3.zero;
            _model.localScale = Vector3.one;
        }
        else
        {
            _model.parent = null;
            _model.localScale = Vector3.one;
            _model.parent = _target;
            _model.localPosition = Vector3.zero;
            _model.localEulerAngles = Vector3.zero;
        }

        ShowModel();
    }

    /// <summary>
    /// 脫離識別圖時重置模型
    /// </summary>
    void ResetModel()
    {
        if (_model == null || _target == null)
            return;
        _model.parent = null;
        _model.localScale = Vector3.one;
        _model.parent = _target;
        _model.position = Vector3.zero;
        _model.eulerAngles = Vector3.zero;

        HideModel();
    }

    /// <summary>
    /// 脫離識別圖后的模型顯示在屏幕中央
    /// </summary>
    void ShowInCamera()
    {
        if (_model == null || _camera == null)
            return;
        if (_model.parent == _camera)
        {
            _model.localPosition = new Vector3(0f, -0.25f, 1.5f);
			_model.localEulerAngles = new Vector3(0f,90f,0f);
            _model.localScale = Vector3.one;
        }
        else
        {
            _model.parent = null;
            _model.localScale = Vector3.one;
            _model.parent = _camera;
			_model.localPosition = new Vector3(0f, -0.25f, 1.5f);
			_model.localEulerAngles = new Vector3(0f,90f,0f);
        }

        ShowModel();
    }

    /// <summary>
    /// 隱藏模型
    /// </summary>
    void HideModel()
    {
        if (_model != null)
            _model.gameObject.SetActive(false);
    }

    /// <summary>
    /// 初始化時隱藏模型
    /// </summary>
    /// <param name="model"></param>
    void HideModel(GameObject model)
    {
        model.SetActive(false);
    }

    /// <summary>
    /// 顯示模型
    /// </summary>
    void ShowModel()
    {
        if (_model != null)
            _model.gameObject.SetActive(true);
    }
}

 同時在vuforia的DefaultTrackableEventHandler類中引用這個類

TrackableEventHandler.cs

 

/*==============================================================================
Copyright (c) 2010-2014 Qualcomm Connected Experiences, Inc.
All Rights Reserved.
Confidential and Proprietary - Protected under copyright and other laws.
==============================================================================*/

using UnityEngine;

namespace Vuforia
{
    /// <summary>
    /// A custom handler that implements the ITrackableEventHandler interface.
    /// </summary>
    public class TrackableEventHandler : MonoBehaviour,
                                                ITrackableEventHandler
    {
        #region PRIVATE_MEMBER_VARIABLES
 
        private TrackableBehaviour mTrackableBehaviour;
    
        #endregion // PRIVATE_MEMBER_VARIABLES



        #region UNTIY_MONOBEHAVIOUR_METHODS
    
        void Start()
        {
            mTrackableBehaviour = GetComponent<TrackableBehaviour>();
            if (mTrackableBehaviour)
            {
                mTrackableBehaviour.RegisterTrackableEventHandler(this);
            }
        }

        #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)
        {

            if (newStatus == TrackableBehaviour.Status.DETECTED ||
                newStatus == TrackableBehaviour.Status.TRACKED ||
                newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
            {
                OnTrackingFound();
            }
            else
            {
                OnTrackingLost();
            }
        }

        #endregion // PUBLIC_METHODS



        #region PRIVATE_METHODS


        private void OnTrackingFound()
        {
			

//            Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
//            Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
//
//            // Enable rendering:
//            foreach (Renderer component in rendererComponents)
//            {
//                component.enabled = true;
//            }
//
//            // Enable colliders:
//            foreach (Collider component in colliderComponents)
//            {
//                component.enabled = true;
//            }

            Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
			TrackManager.Instance.OnTrackingFound(mTrackableBehaviour.transform);
        }


        private void OnTrackingLost()
        {
			

//            Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
//            Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
//
//            // Disable rendering:
//            foreach (Renderer component in rendererComponents)
//            {
//                component.enabled = false;
//            }
//
//            // Disable colliders:
//            foreach (Collider component in colliderComponents)
//            {
//                component.enabled = false;
//            }

            Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
			TrackManager.Instance.OnTrackingLost(mTrackableBehaviour.transform);
        }

        #endregion // PRIVATE_METHODS
    }
}

 

 最后在每個imageTarget上添加這個腳本,並把原先的vuforia自帶的那個刪掉

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM