【问题标题】:SceneKit detect touch in given areaSceneKit 检测给定区域的触摸
【发布时间】:2016-01-12 16:27:35
【问题描述】:

我正在尝试使用 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 数组,我如何检测其他点是否落在它们中间?

【问题讨论】:

    标签: objective-c 3d scenekit


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2017-03-21
      • 1970-01-01
      • 1970-01-01
      • 2012-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-03
      • 1970-01-01
      相关资源
      最近更新 更多