在vuforia中找到跟踪器后保持对象统一

时间:2016-08-01 09:15:26

标签: c# android unity3d vuforia

请帮我解决我的问题 我用unity3d和vuforia写了一个应用程序,在检测到跟踪器后播放视频,我想在摄像头检测后修复视频并停止跟踪,我在TrackableEventHandler.cs中编辑onTrackingFound和onTrackingLost方法好几次但是我无法解决它,请帮忙我找到了解决方案。非常感谢我的朋友。 onTrackingFound方法是:

private void OnTrackingFound()
{
    Renderer[] rendererComponents = GetComponentsInChildren<Renderer>();
    Collider[] colliderComponents = GetComponentsInChildren<Collider>();

    // 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");

    // Optionally play the video automatically when the target is found

    VideoPlaybackBehaviour video = GetComponentInChildren<VideoPlaybackBehaviour>();
    if (video != null && video.AutoPlay)
    {
        if (video.VideoPlayer.IsPlayableOnTexture())
        {
            VideoPlayerHelper.MediaState state = video.VideoPlayer.GetStatus();
            if (state == VideoPlayerHelper.MediaState.PAUSED ||
                state == VideoPlayerHelper.MediaState.READY ||
                state == VideoPlayerHelper.MediaState.STOPPED)
            {
                // Pause other videos before playing this one
                PauseOtherVideos(video);

                // Play this video on texture where it left off
                video.VideoPlayer.Play(false, video.VideoPlayer.GetCurrentPosition());
            }
            else if (state == VideoPlayerHelper.MediaState.REACHED_END)
            {
                // Pause other videos before playing this one
                PauseOtherVideos(video);

                // Play this video from the beginning
                video.VideoPlayer.Play(false, 0);
            }
        }
    }

    mHasBeenFound = true;
    mLostTracking = false;
}

和onTrackingLost方法是:

private void OnTrackingLost()
{
    Renderer[] rendererComponents = GetComponentsInChildren<Renderer>();
    Collider[] colliderComponents = GetComponentsInChildren<Collider>();

    /*
    // 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");

    mLostTracking = false;
    mSecondsSinceLost = 0;
}

1 个答案:

答案 0 :(得分:0)

全屏

如果我理解正确,在检测到可跟踪后,您希望跟踪和AR摄像机视图停止,并播放全屏视频吗?如果是这样,最简单的方法是在检测到可跟踪时,即在OnTrackingFound中禁用Vuforia。然后,您还有一个全屏Canvas对象,该对象在作为孩子时已被禁用RawImageGameObject > UI > Raw Image)。

  • AR相机
  • ImageTarget
  • 帆布
    • RawImage (包含电影资料,最初已停用)

RawImage将分配一个材质,其中材质的主反照率纹理是一个电影文件(所以Movie Texture),比如一个Ogg Theora .ogv文件,我们称之为电影素材。然后,当您检测到可跟踪时,只需启用RawImagePlay()电影素材即可。如下所示:

using UnityEngine.UI;       // for RawImage

public RawImage rawImage;   // public, so remember to drag-assign in editor

private void OnTrackingFound()
{
    VuforiaBehaviour.Instance.enabled = false;             // disable Vuforia
    rawImage.gameObject.SetActive(true);                   // enable the RawImage
    ((MovieTexture)rawImage.material.mainTexture).Play();  // play the movie
}

在3D空间

相反,如果您不想播放全屏视频,而是在通过相机看到可跟踪位于3D空间中的位置播放视频,则只需创建Plane对象即可(GameObject > 3D Object > Plane)作为你可追踪的孩子,我们给它起名叫电影。让飞机面向AR摄像机,为其分配电影素材,然后在编辑器中禁用其网格渲染器:

  • AR相机
  • ImageTarget
    • 电影(包含电影素材,并且最初禁用了网格渲染器)

OnTrackingFound方法中,您可以启用其渲染器,并将其材料引用为MovieTexture,只需执行Play()。在trackable的事件处理程序中有类似的东西:

private void OnTrackingFound()
{
    Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
    foreach (Renderer component in rendererComponents)
        component.enabled = true;
    MovieTexture mov = (MovieTexture)transform.GetChild(0).GetComponent<Renderer>().material.mainTexture;
    if (!mov.isPlaying)
        mov.Play();
}