Aravind Mantravadi
Aravind Mantravadi

Reputation: 145

Can I remove an object from my scene?

Relatively new to ARKit, I was wondering if there's a way to remove a 3D object after it has been placed in a scene.

Upvotes: 0

Views: 1743

Answers (3)

To remove an object(SCNNode) from your Scene view, you can use Long Press Gesture. Just add the below code in your viewDidLoad.

UILongPressGestureRecognizer *longPressGestureRecognizer = 
[[UILongPressGestureRecognizer alloc] initWithTarget:self 
action:@selector(handleRemoveObjectFrom:)];
longPressGestureRecognizer.minimumPressDuration = 0.5;
[self.sceneView addGestureRecognizer:longPressGestureRecognizer];

Then handle your gesture recognizer method like below,

- (void)handleRemoveObjectFrom: (UILongPressGestureRecognizer *)recognizer {

if (recognizer.state != UIGestureRecognizerStateBegan) {
    return;
}
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];
[[hitResult.node parentNode] removeFromParentNode];

}

Hope this will help you to solve your problem.

Thanks

Upvotes: 0

Alan
Alan

Reputation: 1142

Just use this function:

Swift:

node.removeFromParentNode()

Objective-C

[node removeFromParentNode];

Upvotes: 3

jlsiewert
jlsiewert

Reputation: 3574

I suggest reading the documentation of ARKit, SceneKit and their basic classes.

You need to remove a node from the scene graph if you don't want it to appear on screen. You need to remove it from its parent node. Read more about it in SCNNode - Managing the Node Hierachy in Apples documentation.

Upvotes: 0

Related Questions