在使用统一的ARKIT中,如何仅在一个特定方向上放置模型?

时间:2018-06-22 06:11:28

标签: c# unity3d arkit

Unity和ARKIT的新手。我使用Focussquare尝试了我的项目。它检测到一个平面,当我触摸屏幕设备时,将模型放置在该位置。问题是我每次启动应用程序时触摸模型方向可能会不同。有时候模型会面向我,否则模型会转向不同的位置。我不是在谈论翻转,而是面对不同的位置。我需要这样一种方式,模型应该面向我所有时间或其他任何方向,但只能在一个方向上进行。

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

namespace UnityEngine.XR.iOS
{
public class UnityARHitTestExample : MonoBehaviour
{
    public GameObject hidesquare;

    public GameObject hidetoast;



    public Camera playerCamera;

    private float distance = 3.0f;

    public Transform m_HitTransform;





    public float maxRayDistance = 30.0f;
    public GameObject lockobject;
    public LayerMask collisionLayer = 1 << 10;  //ARKitPlane layer



    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!");
                hidesquare.SetActive(false);
                hidetoast.SetActive(false);

                lockobject.GetComponent<UnityARHitTestExample>().enabled = false;
                m_HitTransform.GetChild(0).gameObject.SetActive(true);


                m_HitTransform.position = UnityARMatrixOps.GetPosition(hitResult.localTransform);
                m_HitTransform.rotation = UnityARMatrixOps.GetRotation(hitResult.localTransform);


                m_HitTransform.LookAt(Camera.main.transform.position);
                m_HitTransform.eulerAngles = new Vector3(0, m_HitTransform.eulerAngles.y, 0);


                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;
    }


    //private bool IsPointerOverUIObject()
    //{
    //    PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
    //    eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
    //    List<RaycastResult> results = new List<RaycastResult>();
    //    EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
    //    return results.Count > 0;
    //}


    //bool IsPointerOverGameObject(int fingerId)
    //{
    //    EventSystem eventSystem = EventSystem.current;
    //    return (eventSystem.IsPointerOverGameObject(fingerId)
    //        && eventSystem.currentSelectedGameObject != null);
    //}

    // Update is called once per frame
    void Update()
    {
#if UNITY_EDITOR   //we will only use this script on the editor side, though there is nothing that would prevent it from working on device
        if (Input.GetMouseButtonDown(0))
        {

            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            //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))
            {
                //we're going to get the position from the contact point
                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));

                //and the rotation from the transform of the plane collider
                m_HitTransform.rotation = hit.transform.rotation;
            }
        }
#else
        if (Input.touchCount == 1 && m_HitTransform != null)
        {

            var touch = Input.GetTouch(0);
            if ((touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved) && !EventSystem.current.IsPointerOverGameObject(touch.fingerId))

            {
                //hidebtn.SetActive(false);
               // this.transform.gameObject.SetActive(true);
                var screenPosition = Camera.main.ScreenToViewportPoint(touch.position);
                ARPoint point = new ARPoint
                {
                    x = screenPosition.x,
                    y = screenPosition.y
                };

                // prioritize reults types
                ARHitTestResultType[] resultTypes = 
                {
                    ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent, 
                    // if you want to use infinite planes use this:
                    //ARHitTestResultType.ARHitTestResultTypeExistingPlane,
                    ARHitTestResultType.ARHitTestResultTypeHorizontalPlane, 
                    ARHitTestResultType.ARHitTestResultTypeFeaturePoint
                }; 

                foreach (ARHitTestResultType resultType in resultTypes)
                {
                    if (HitTestWithResultType (point, resultType))
                    {
                        return;
                    }
                }
            }
        }

#endif
    }
}

}

我尝试使用foundSquare.transform.position和foundSquare.transform.rotation将其位置和旋转更改为摄影机变换,但是没有用。

Hierarachy using both Hitcube and focussquare

0 个答案:

没有答案