SceneKit - 使用DAE模型的HitTest

问题描述 投票:1回答:3

我想知道哪个节点被击中,但我的方法仅适用于具有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
3个回答
2
投票

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

检查英雄节点是否是你的hitNode工作的父节点?


1
投票

我遵循了@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];

1
投票
    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;
}
© www.soinside.com 2019 - 2024. All rights reserved.