C# ARKit-在不接触屏幕的情况下放置对象
使用ARKit,我可以轻触曲面,将3D对象放置在此处。我还可以移动我的手指,从而沿着曲面移动对象 如何使对象自动出现并粘贴到相机前面的表面,而无需触摸屏幕 以下是通过手指轻触放置三维对象的示例脚本:C# ARKit-在不接触屏幕的情况下放置对象,c#,ios,unity3d,arkit,C#,Ios,Unity3d,Arkit,使用ARKit,我可以轻触曲面,将3D对象放置在此处。我还可以移动我的手指,从而沿着曲面移动对象 如何使对象自动出现并粘贴到相机前面的表面,而无需触摸屏幕 以下是通过手指轻触放置三维对象的示例脚本: using System; using System.Collections.Generic; namespace UnityEngine.XR.iOS { public class UnityARHitTestExample : MonoBehaviour { p
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;
}
}
}
}
}
}
}
使用系统;
使用System.Collections.Generic;
名称空间UnityEngine.XR.iOS
{
公共类UnityHitTester示例:MonoBehavior
{
公共转型m_-HitTransform;
bool HitTestWithResultType(ARPoint point,ARHitTestResultType ResultType)
{
List hitResults=UnityAssessionNativeInterface.GetARSessionNativeInterface().HitTest(点,结果类型);
如果(hitResults.Count>0){
foreach(hitResults中的var hitResult){
Log(“被击中了!”);
m_HitTransform.position=UnityARMatrixOps.GetPosition(hitResult.worldTransform);
m_HitTransform.rotation=UnityARMatrixOps.GetRotation(hitResult.worldTransform);
Log(string.Format(“x:{0:0.}y:{1:0.{1:0.{1:0.}z:{2:0.},m#h#h#h#h#h#h#h#h#h#h#h#y:{1:0”,m#u变换.position x,m#u变换.position y,m.hitu变换.position;
返回true;
}
}
返回false;
}
//每帧调用一次更新
无效更新(){
if(Input.touchCount>0&&m_HitTransform!=null)
{
var touch=Input.GetTouch(0);
if(touch.phase==TouchPhase.start | | touch.phase==TouchPhase.Moved)
{
var screenPosition=Camera.main.ScreenToViewportPoint(touch.position);
ARPoint点=新ARPoint点{
x=屏幕位置.x,
y=屏幕位置。y
};
//对reults类型进行优先级排序
ARHitTestResultType[]结果类型={
ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent,
//如果要使用无限平面,请使用以下选项:
//ARHitTestResultType.ARHitTestResultTypeExistingPlane,
ARHitTestResultType.ARHitTestResultTypeHorizontalPlane,
ARHitTestResultType.ARHitTestResultTypeFeaturePoint
};
foreach(ARHitTestResultType resultType in resultTypes)
{
if(HitTestWithResultType(point,resultType))
{
返回;
}
}
}
}
}
}
}
我在Objective-C中也做过类似的工作,希望我能帮助您学习unity示例
我的逻辑基本上是,使用点击放置对象是基于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
}
CGPoint point=CGPointMake(self.sceneView.frame.size.width/2,self.sceneView.frame.size.height/2)//在屏幕中间找到一个点
NSArray*hitResults=[\u sceneView hitTest:点类型:ARHitTestResultTypeFeaturePoint]//尝试在这一点上进行AR命中测试
如果([hitResults count]!=0){//如果有任何结果
//执行所需的操作
}
我在Objective-C中也做过类似的工作,希望我能帮助您学习unity示例
我的逻辑基本上是,使用点击放置对象是基于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
}
CGPoint point=CGPointMake(self.sceneView.frame.size.width/2,self.sceneView.frame.size.height/2)//在屏幕中间找到一个点
NSArray*hitResults=[\u sceneView hitTest:点类型:ARHitTestResultTypeFeaturePoint]//尝试在这一点上进行AR命中测试
如果([hitResults count]!=0){//如果有任何结果
//执行所需的操作
}