【问题标题】:SceneKit – HitTest with DAE modelsSceneKit – 带有 DAE 模型的 HitTest
【发布时间】:2014-09-07 02:24:43
【问题描述】:

我想知道哪个节点被击中,但我的方法仅适用于具有 SCNBox 和 SCNFloor 等几何形状的节点,但不适用于 DAE 模型:

- (void) handleTap:(UIGestureRecognizer*)gestureRecognize {

    // retrieve the SCNView
    SCNView *scnView = (SCNView *)self.view;

    // check what nodes are tapped
    CGPoint p = [gestureRecognize locationInView:scnView];
    NSArray *hitResults = [scnView hitTest:p options:nil];

    // check that we clicked on at least one object
    if([hitResults count] > 0) {
        SCNNode *hitNode = ((SCNHitTestResult*)[hitResults objectAtIndex:0]).node;

        if(hitNode == boxNode) {
            NSLog(@"box hit"); //works
        }

        if(hitNode == floorNode) {
            NSLog(@"floor hit"); //works
        }

        if(hitNode == heroNode) {
            NSLog(@"heroNode from .dae hit"); //doesn't work
        }
    } 
}

这就是我制作 .dae 节点 (heroNode) 的方式:

SCNScene *heroScene = [SCNScene sceneNamed:@"hero" inDirectory:nil options:nil];
heroNode = [heroScene.rootNode childNodeWithName:@"root" recursively:YES];
[scene.rootNode addChildNode:heroNode];

问题出在哪里?

【问题讨论】:

    标签: ios objective-c scenekit


    【解决方案1】:

    英雄节点没有附加几何图形,但它的子节点确实具有几何图形。因此英雄节点不会出现在命中测试结果中。

    检查 hero 节点是否是 hitNode 的父节点有效吗?

    【讨论】:

    • 圣洁@* 非常感谢我被阻止了一个小时
    【解决方案2】:

    我遵循了@mnuages 的建议并得出了这个结论,我正在使用 Apples WWDC 2014 中的 boss.dae 文件 SceneKit 中的新功能

    - (void) handleTap:(UIGestureRecognizer*)gestureRecognize {
    // retrieve the SCNView
    SCNView *scnView = (SCNView *)self.view;
    
    // check what nodes are tapped
    CGPoint p = [gestureRecognize locationInView:scnView];
    NSArray *hitResults = [scnView hitTest:p options:nil];
    
    // check that we clicked on at least one object
    if([hitResults count] > 0){
    
        // retrieved the first clicked object
        SCNHitTestResult *result = [hitResults objectAtIndex:0];
    
        //search in the node tree with a specified name.
        SCNNode *tempNode = [self.monsterCharacter childNodeWithName:@"Box03" recursively:YES];
    
        // Search for the node named "name"
        if (tempNode == result.node.parentNode) {
            NSLog(@"FOUND IT");
        }
    }
    

    }

    在 viewDidLoad 我这样创建角色:

    //add Monster to scene
    SCNNode *heroNodeRoot = [SMLGameView loadNodeWithName:nil fromSceneNamed:@"art.scnassets/characters/boss/boss.dae"];
    self.monsterCharacter = [[SMLMonster alloc] initWithNode:heroNodeRoot withSkeleton:@"skeleton"];
    self.monsterCharacter.position = SCNVector3Make(0, 0, 0);
    [scene.rootNode addChildNode:self.monsterCharacter];
    

    【讨论】:

    • 我用更好的方法添加了一个答案:)
    【解决方案3】:
        if([daeNode childNodeWithName:hitTestResultNode.name recursively:YES])
        {
            NSLog(@"hit!");
        }
    

    daeNode - 来自 .dae 的节点

    hitTestResultNode - 来自 SCNHitTestResult 的节点:

    CGPoint p = [gestureRecognize locationInView:scnView];
    NSArray *hitResults = [scnView hitTest:p options:nil];
    
    // check that we clicked on at least one object
    if([hitResults count] > 0)
    {
        SCNHitTestResult *hitResult = (SCNHitTestResult*)[hitResults objectAtIndex:0];
        SCNNode *hitTestResultNode = hitResult.node;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-02-22
      • 2016-02-15
      • 1970-01-01
      • 2019-03-17
      • 2017-12-24
      • 2015-12-25
      • 2016-03-27
      • 2018-03-03
      • 2018-05-28
      相关资源
      最近更新 更多