SceneKit 检测给定区域的触摸

SceneKit detect touch in given area

我正在尝试使用 SceneKit 检测给定区域内的触摸。使用一个几何体执行此操作相当微不足道(您只需在场景视图上执行命中测试)但是,我有一个由 SCNNodes (SCNVector3s) 数组定义的自定义区域。

我这样创建自定义区域:

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

那么给定我的 SCNBoxes 数组,我如何检测另一个点是否落在它们的中间?

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

我会尝试将您的方框节点的位置投影到 2D 视图坐标中,然后检查您的 2D 触摸坐标是否在此 2D 形状内。 There's another SO question 这会有所帮助。