ARCore Unity-跟踪的对象(视频)无法正确缩放到图像目标

时间:2018-10-31 04:41:27

标签: c# unity3d arcore

因此,我一直在思考如何链接内容,无论是3D,2D还是只是在使用ARCore检测到图像时触发声音,都想在此添加一些空间选项。 Atm不同的视频在检测到不同的图像时会播放,但是它们与所检测到的图像的比例过小或超出范围都不一致。

此代码看起来有问题...

将两个C#文件制成一个叫做: AugmentedImageController

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleARCore;
using System;
using UnityEngine.UI;
using System.Runtime.InteropServices;

public class AugmentedImageController : MonoBehaviour {
   [SerializeField] private AugmentedimageVisualizer _augmentedimageVisualizer;

private readonly Dictionary<int, AugmentedimageVisualizer> _visualizers = new Dictionary<int, AugmentedimageVisualizer>();

private readonly List<AugmentedImage> _images = new List<AugmentedImage>();

private void Update()
{
    if (Session.Status != SessionStatus.Tracking)
    {
        return;
    }

    Session.GetTrackables(_images, TrackableQueryFilter.Updated);
    VisualizeTrackables();
}
private void VisualizeTrackables()
{
    foreach (var image in _images)
    {
        var visualizer = GetVisualizer(image);

        if (image.TrackingState == TrackingState.Tracking && visualizer == null)
        {
            AddVisualizer(image);
        }
        else if (image.TrackingState == TrackingState.Stopped && visualizer != null)
        {
            RemoveVisualizer(image, visualizer);
        }
    }
}

private void RemoveVisualizer(AugmentedImage image, AugmentedimageVisualizer visualizer)
{
    _visualizers.Remove(image.DatabaseIndex);
    Destroy(visualizer.gameObject);
}

private void AddVisualizer(AugmentedImage image)
{
    var anchor = image.CreateAnchor(image.CenterPose);
    var visualizer = Instantiate(_augmentedimageVisualizer, anchor.transform);
    visualizer.Image = image;
    _visualizers.Add(image.DatabaseIndex, visualizer);
}

private AugmentedimageVisualizer GetVisualizer(AugmentedImage image)
{
    AugmentedimageVisualizer visualizer;
    _visualizers.TryGetValue(image.DatabaseIndex, out visualizer);
    return visualizer;
}
}

第二个AugmentedimageVisualizer

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using GoogleARCore;
using GoogleARCoreInternal;
using System.Runtime.InteropServices;


public class AugmentedimageVisualizer : MonoBehaviour {
[SerializeField] private VideoClip[] _videoClips;
  public AugmentedImage Image;
  private VideoPlayer _videoPlayer;

 void Start()
{
    _videoPlayer = GetComponent<VideoPlayer>();
    _videoPlayer.loopPointReached += OnStop;
}

private void OnStop(VideoPlayer source)
{
    gameObject.SetActive(false);
}

void Update()
{
    if (Image == null || Image.TrackingState != TrackingState.Tracking)
    {
        return;
    }

    if (!_videoPlayer.isPlaying)
    {
        _videoPlayer.clip = _videoClips[Image.DatabaseIndex];
        _videoPlayer.Play();
    }

    transform.localScale = new Vector3(Image.ExtentX, Image.ExtentZ, 1);
}
}

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

您可能已经有了答案,但是如果其他人对此有所了解,我会抛出某种解决方案。

U应该将所有样本垃圾扔掉。当您只剩下基本的东西时,image.extentx吗?我相信给你以米为单位的近似图像宽度。然后将该值与某种标量一起使用,以便获得正确的宽度。