2017-11-10 13 views
1

ARKitを使用すると、3Dオブジェクトを配置するサーフェスをタップできます。また、私は指を動かすことができるので、表面に沿ってオブジェクトを動かすことができます。ARKit - 画面に触れることなくオブジェクトを置く

画面に触れる必要はなく、オブジェクトを自動的にカメラの前面に表示したり貼り付けることができますか?ここで

は指タップで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; 
         } 
        } 
       } 
      } 
     } 


    } 
} 

答えて

0

私はObjective-Cで似た何かをやったので、うまくいけば、私はあなたの団結の例を支援することができます。

私のロジックは基本的に、タップを使ってオブジェクトを配置することは、タッチ位置から取得したポイントを持つhitTest関数に基づいていることです。だから私はスクリーンの中央にプログラムでCGPointを作成し、この時点でhitTest関数を実行しました。タイマーに繋がっていて、ヒットしたオブジェクトがそこに追加されたとき。

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 
} 
関連する問題