【问题标题】:When you move the camera (phone) object moves with it (ARKit)当您移动相机(手机)时,对象也会随之移动(ARKit)
【发布时间】:2019-03-12 16:57:11
【问题描述】:

我正在使用 ARKit 插件开发 Unity 3D。重新制作标准场景示例。我的任务是把飞机模型放回家(还是挺大的)。但是当我创建一个大对象时,它并没有固定在平面上,而是随着相机移动。我该怎么做才能让他留在原地? 放置小物件时没有问题。

using System;
using System.Collections.Generic;
using UnityEngine.EventSystems;


namespace UnityEngine.XR.iOS
{
public class UnityARHitTestExample : MonoBehaviour
{
    public Transform m_HitTransform;
    public float maxRayDistance = 30.0f;
    public LayerMask collisionLayer = 1 << 10;  //ARKitPlane layer
    public GameObject home;

    private bool isDetecting;

    bool HitTestWithResultType (ARPoint point, ARHitTestResultType 
resultTypes)
    {
        List<ARHitTestResult> hitResults = 
UnityARSessionNativeInterface.GetARSessionNativeInterface 
().HitTest (point, resultTypes);
        if (hitResults.Count > 0) {
            foreach (var hitResult in hitResults) {
                Debug.Log ("Got hit!");
                m_HitTransform.position = 
UnityARMatrixOps.GetPosition (hitResult.worldTransform);
                m_HitTransform.rotation = 
UnityARMatrixOps.GetRotation (hitResult.worldTransform);
                Debug.Log (string.Format ("x:{0:0.######} y: 
{1:0.######} z:{2:0.######}", m_HitTransform.position.x, 
m_HitTransform.position.y, m_HitTransform.position.z));
                return true;
            }
        }
        return false;
    }

    public void Start()
    {
        isDetecting = true;
    }

    // Update is called once per frame
    void Update()
    {
#if UNITY_EDITOR   
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = 
Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            home.SetActive(true);
            //we'll try to hit one of the plane collider 
gameobjects that were generated by the plugin
            //effectively similar to calling HitTest with 
ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent
            if (Physics.Raycast(ray, out hit, maxRayDistance, 
collisionLayer))
            {
                m_HitTransform.position = hit.point;
                Debug.Log(string.Format("x:{0:0.######} y: 
{1:0.######} z:{2:0.######}", m_HitTransform.position.x, 
m_HitTransform.position.y, m_HitTransform.position.z));

                m_HitTransform.rotation = hit.transform.rotation;
            }
        }
#else
        if (Input.touchCount > 0 && m_HitTransform != null)
        {
            var touch = Input.GetTouch(0);
            if ((touch.phase == TouchPhase.Began || touch.phase == 
TouchPhase.Moved) && isDetecting == true)
            {
                var screenPosition = 
Camera.main.ScreenToViewportPoint(touch.position);
                ARPoint point = new ARPoint {
                    x = screenPosition.x,
                    y = screenPosition.y
                };

                home.SetActive(true);

                // prioritize reults types
                ARHitTestResultType[] resultTypes = {

//ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingGeometry,

ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent, 

//ARHitTestResultType.ARHitTestResultTypeExistingPlane,

ARHitTestResultType.ARHitTestResultTypeEstimatedHorizontalPlane, 

//ARHitTestResultType.ARHitTestResultTypeEstimatedVerticalPlane, 

ARHitTestResultType.ARHitTestResultTypeFeaturePoint
                }; 

                foreach (ARHitTestResultType resultType in 
resultTypes)
                {
                    if (HitTestWithResultType (point, resultType))
                    {
                        return;
                    }
                }
                isDetecting = false;
            }
        }
#endif

    }


  }
}

【问题讨论】:

  • 做小物件的时候,是不是也有这个问题?我想知道问题是否只是它找不到足够的边缘或其他点来“修复”底部...
  • 可以,放个小物件就可以了。
  • 欢迎来到 StackOverflow!请您发布代码吗?
  • 谢谢。贴出代码。

标签: c# unity3d arkit


【解决方案1】:

这可能只是因为您的对象实际上位于场景中真实内容(墙壁、地面等)的“背后”。

因为 ARKit(和 ARCore)没有遮挡,如果你把东西放在墙后面,那么它实际上只会出现在它上面。

这样做的结果是您的内容看起来像是在移动!这种情况经常发生,而且很烦人。

要进行测试,请尝试将模型加载到一个非常大的室外空间中。

(请注意,我没有正确阅读您的代码,因此其他人可能会发现错误,但这仍然是它可能导致错误的合理原因。)

【讨论】:

  • 谢谢。我试过你说的。没有帮助(对象仍在相机后面移动。也许有一些方法可以解决这个问题?
猜你喜欢
  • 1970-01-01
  • 2016-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多