ARKit : how to get a SCNNode hit by hitTest?

我的梦境 提交于 2019-12-14 02:07:34

问题


I have several planes detected by ARKit and made visible to the user.

I want the user to be able to click on the screen in order to keep only the plane he selected, change its color and delete the other ones.

My planes are added in the didAddNode delegate method like this:

 Plane *plane = [[Plane alloc] initWithAnchor: (ARPlaneAnchor *)anchor];
    [self.planes setObject:plane forKey:anchor.identifier];

In another method, I catch the click of the user and want to be able to change the color of the plane he selected and delete the other ones.

Here is what I wrote:

 CGPoint holdPoint = [recognizer locationInView:self.sceneView];

    NSArray<SCNHitTestResult *> *result = [self.sceneView hitTest:holdPoint
                                                              options:@{SCNHitTestBoundingBoxOnlyKey: @YES, SCNHitTestFirstFoundOnlyKey: @YES}]; 

        if (result.count == 0) {
                    return;
        }

        SCNHitTestResult * hitResult = [result firstObject];

 SCNNode *node = hitResult.node;

 SCNNode *parentNode = node.parentNode; // the geometry being child of my Plane object

The problem is that I don't get a reliable result. The result array always contains one single object when I click on a plane but it is not always a Plane object (neither its parent), but just a SCNNode.

What is happening ? How to solve this problem ?

If you have any code working to detect a click on a plane among other planes, I am interested !


回答1:


The options you use seem too permissive if you want perfect results

SCNHitTestBoundingBoxOnlyKey The default value is NO, specifying that hit-testing searches should test against node geometry. Specifying YES for this option increases search performance at the expense of geometric accuracy.

SCNHitTestFirstFoundOnlyKey The default value is NO, specifying that hit-testing should return all objects found. If you specify YES, the array of hit-test results contains only the first object found (which is not necessarily the nearest).



来源:https://stackoverflow.com/questions/46203065/arkit-how-to-get-a-scnnode-hit-by-hittest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!