ARKit--HITTEST模式

作者: 以霏之名 | 来源:发表于2017-06-19 20:30 被阅读974次

HITTEST介绍

点击测试(hittest),找到与相机图像中的某个点所对应的真实世界面。如果您在 Session (会话) 配置当中启用了 planeDetection 配置的话,那么 ARKit 就会去检测相机图像当中的水平面,并报告其位置和大小。您可以使用点击测试所生成的结果,或者使用所检测到的水平面,从而就可以在场景当中放置虚拟内容,或者与之进行交互。
HITTEST可以想象成,发射一个射线到平面上,碰到的平面的点都做成ARHitTestResult返回。

API

ARSCNView类里定义了hittest的API.

/* 
@param point A point in the view's coordinate system.
                屏幕坐标系里的点
 @param types The types of results to search for.
                 搜索类型
*/
- (NSArray<ARHitTestResult *> *)hitTest:(CGPoint)point 
types:(ARHitTestResultType)types;

一个手指触碰点HITEST的例子

手指触摸的时候,做一次hittest,返回3d坐标系里触摸的点,放一个小物品。

//定义点击手势
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] 
initWithTarget:self 
action:@selector(handleTapFrom:)];

- (void)handleTapFrom: (UITapGestureRecognizer *)recognizer {
  // Take the screen space tap coordinates and pass them to the hitTest method on the ARSCNView instance
  CGPoint tapPoint = [recognizer locationInView:self.sceneView];
  NSArray<ARHitTestResult *> *result = [self.sceneView hitTest:tapPoint types:ARHitTestResultTypeExistingPlaneUsingExtent];
  
  // If the intersection ray passes through any plane geometry they will be returned, with the planes
  // ordered by distance from the camera
  if (result.count == 0) {
    return;
  }
  
  // If there are multiple hits, just pick the closest plane
  ARHitTestResult * hitResult = [result firstObject];
  [self insertGeometry:hitResult];
}

hitResult里面worldTransform,直接返回在3D世界的坐标,下面的代码是插入一个小小的cube做一个演示。

- (void)insertGeometry:(ARHitTestResult *)hitResult {
  // Right now we just insert a simple cube, later we will improve these to be more
  // interesting and have better texture and shading
  
  float dimension = 0.1;
  SCNBox *cube = [SCNBox boxWithWidth:dimension height:dimension length:dimension chamferRadius:0];
  SCNNode *node = [SCNNode nodeWithGeometry:cube];
  
  // The physicsBody tells SceneKit this geometry should be manipulated by the physics engine
  node.physicsBody = [SCNPhysicsBody bodyWithType:SCNPhysicsBodyTypeDynamic shape:nil];
  node.physicsBody.mass = 2.0;
  node.physicsBody.categoryBitMask = CollisionCategoryCube;
  
  // We insert the geometry slightly above the point the user tapped, so that it drops onto the plane
  // using the physics engine
  float insertionYOffset = 0.5;
  node.position = SCNVector3Make(
                                 hitResult.worldTransform.columns[3].x,
                                 hitResult.worldTransform.columns[3].y + insertionYOffset,
                                 hitResult.worldTransform.columns[3].z
                                 );
  [self.sceneView.scene.rootNode addChildNode:node];
  [self.boxes addObject:node];
}

具体代码参看例子
demo

相关文章

  • ARKit--HITTEST模式

    HITTEST介绍 点击测试(hittest),找到与相机图像中的某个点所对应的真实世界面。如果您在 Sessio...

  • JS 设计模式

    工厂模式 单体模式 模块模式 代理模式 职责链模式 命令模式 模板方法模式 策略模式 发布-订阅模式 中介者模式 ...

  • iOS设计模式

    设计模式:MVC模式、单例模式、观察者模式、工厂模式、代理模式、策略模式、适配器模式、模版模式、外观模式、创建模式...

  • iOS知识点总结

    一、设计模式:MVC模式、单例模式、观察者模式、MVVM模式、工厂模式、代理模式、策略模式、适配器模式、模板模式、...

  • 常用设计模式 2018-09-15

    目录 代理模式 单例模式 命令模式 工厂模式 桥接模式 策略模式 装饰模式 观察者模式 门面模式 代理模式 静态代...

  • 计算机等级考试三级数据库复习(五)

    1.数据库系统 三级模式——》内模式,模式,外模式 二级映像模式(外模式/模式,模式/内模式) 模式/内模式提供数...

  • Retrofit源码解析

    设计模式:建造者模式、工厂方法模式、外观模式、代理模式、单例模式、策略模式、装饰模式、适配器模式、代理模式 一、R...

  • GOF23(java设计模式)--行为型模式

    职责链模式、迭代器模式、中介者模式、命令模式、解释器模式、访问者模式、策略模式、模版方法模式、状态模式、观察者模式...

  • vim学习 02——模式

    vim学习 02——模式 基本模式普通模式插入模式可视模式选择模式命令行模式Ex 模式 派生模式操作符等待模式插入...

  • 互联网营销与促销模式

    互联网营销与促销模式:众筹模式 团购模式 秒杀模式 满赠模式 积分模式 软文模式 会员模式 抽奖模式 体验模式 拍...

网友评论

  • ee642675d7fc:您好,您知道通过Hit-test检测到模型之后如何用手势引导模型平移或旋转吗
    ee642675d7fc:@以霏之名 谢谢您的回复,可以加您请教关于ARKit和SceneKit的问题吗?谢谢
    以霏之名:@比哈哈更哈哈 SCNNode里面有scale,eulerAngles可以实现缩放和旋转,平移的话需要用到unpointproject和project进行3d到2d的投影
  • DeerRun:点击返回数组为空是什么情况呢
    脸狐:ARHitTestResultTypeExistingPlane:手机晃动导致无法确定此平面与重力相垂直
    ARHitTestResultTypeExistingPlaneUsingExtent:触碰到点在平面之外,则不返回值。
    ARHitTestResultTypeEstimatedHorizontalPlane:正常情况一定会返回值。
    以霏之名:应该是没hit到吧

本文标题:ARKit--HITTEST模式

本文链接:https://www.haomeiwen.com/subject/rndnqxtx.html