简体   繁体   English

ARKit-放置物体而不触摸屏幕

[英]ARKit - place object without touching the screen

Using ARKit I can tap on the surface to place 3D object here. 使用ARKit,我可以点击表面将3D对象放置在此处。 I also can move my finger and thus move the object along the surface. 我也可以移动手指,从而使物体沿表面移动。

How can I make an object to automatically appear and stick to the surface in front of the camera, with no need in touching the screen? 如何使物体自动出现并粘在相机前面的表面上,而无需触摸屏幕?

Here is the example script for placing 3D objects by finger tap: 这是通过手指点击放置3D对象的示例脚本:

using System;
using System.Collections.Generic;

namespace UnityEngine.XR.iOS
{
    public class UnityARHitTestExample : MonoBehaviour
    {
        public Transform m_HitTransform;

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

        // Update is called once per frame
        void Update () {
            if (Input.touchCount > 0 && m_HitTransform != null)
            {
                var touch = Input.GetTouch(0);
                if (touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved)
                {
                    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;
                        }
                    }
                }
            }
        }


    }
}

I've done something similar in Objective-C, so hopefully I can help with your unity example. 我在Objective-C中做了类似的事情,因此希望可以为您提供统一的示例。

My logic was basically that placing the objects using a tap is based on the hitTest function with a point provided to it retrieved from the touch location. 我的逻辑基本上是,使用轻敲放置对象是基于hitTest函数的,从触摸位置检索到的是提供给它的点。 So I just created a CGPoint programmatically in the center of the screen and ran the hitTest function with this point. 因此,我只是在屏幕中央以编程方式创建了CGPoint,并在此点运行了hitTest函数。 Had it hooked to a timer, and when a hit returned objects were added there. 它是否已挂接到计时器,并且当命中返回对象时,已在其中添加了对象。

CGPoint point = CGPointMake(self.sceneView.frame.size.width / 2, self.sceneView.frame.size.height / 2); //Get a point at the middle of the screen
NSArray <ARHitTestResult *> *hitResults = [_sceneView hitTest:point types:ARHitTestResultTypeFeaturePoint]; //Try to do a AR Hit Test on this point
if ([hitResults count] != 0) { //If have any results
    //Perform desired operation
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM