SceneKit在给定区域检测触摸

问题描述:

我试图使用SceneKit检测给定区域内的触摸。使用一个几何体完成此操作相当简单(只需对场景视图执行命中测试),但是,我有一个由SCNNode s(SCNVector3 s)数组定义的自定义区域。SceneKit在给定区域检测触摸

创建我的自定义区域,像这样:

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 
{ 
    if (!self.isMakingLine) { 
     [super touchesBegan:touches withEvent:event]; 
    } else { 
     self.vectors = [[NSMutableArray alloc] init]; 
     NSArray <SCNHitTestResult *> *res = [self.sceneView hitTest:[[touches anyObject] locationInView:self.sceneView] options:@{SCNHitTestFirstFoundOnlyKey : @YES}]; 
     if (res.count) { 
      SCNHitTestResult *result = res.lastObject; 
      if (result.node == self.sphereNode) { 
       SCNNode *n = [SCNNode nodeWithGeometry:[SCNBox boxWithWidth:0.01 height:0.01 length:0.01 chamferRadius:0]]; 
       n.geometry.firstMaterial.diffuse.contents = [UIColor greenColor]; 
       n.position = result.localCoordinates; 
       [self.sphereNode addChildNode:n]; 
       [self.vectors addObject:n]; 
      } 
     } 
    } 
} 

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 
{ 
    if (!self.isMakingLine) { 
     [super touchesMoved:touches withEvent:event]; 
    } else { 
     NSArray <SCNHitTestResult *> *res = [self.sceneView hitTest:[[touches anyObject] locationInView:self.sceneView] options:@{SCNHitTestFirstFoundOnlyKey : @YES}]; 
     if (res.count) { 
      SCNHitTestResult *result = res.lastObject; 
      if (result.node == self.sphereNode) { 
       SCNNode *n = [SCNNode nodeWithGeometry:[SCNBox boxWithWidth:0.01 height:0.01 length:0.01 chamferRadius:0]]; 
       n.geometry.firstMaterial.diffuse.contents = [UIColor greenColor]; 
       n.position = result.localCoordinates; 
       [self.sphereNode addChildNode:n]; 
       [self.vectors addObject:n]; 
      } 
     } 
    } 
} 

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 
{ 
    if (!self.isMakingLine) { 
     [super touchesEnded:touches withEvent:event]; 
    } else { 
     NSArray <SCNHitTestResult *> *res = [self.sceneView hitTest:[[touches anyObject] locationInView:self.sceneView] options:@{SCNHitTestFirstFoundOnlyKey : @YES}]; 
     if (res.count) { 
      SCNHitTestResult *result = res.lastObject; 
      if (result.node == self.sphereNode) { 
       SCNNode *n = [SCNNode nodeWithGeometry:[SCNBox boxWithWidth:0.01 height:0.01 length:0.01 chamferRadius:0]]; 
       n.geometry.firstMaterial.diffuse.contents = [UIColor greenColor]; 
       n.position = result.localCoordinates; 
       [self.sphereNode addChildNode:n]; 
       [self.vectors addObject:n]; 
       self.isMakingLine = NO; 
      } 
     } 
    } 
} 

所以给我的阵列的SCNBox ES我怎样才能检测是否有其他点落在他们的中间?

SCNView符合SCNSceneRenderer协议,这提供了一种方法projectPoint:(SCNVector3)point,它会在3D场景中取一个点并将其投影到2D视图坐标。

我试着将你的盒子节点的位置投影到2D视图坐标中,然后检查你的2D触摸坐标是否在这个2D形状内。 There's another SO question这将有助于此。

+0

谢谢,这是一个非常好的提示。 –