Prevent SCNNode from going out of the screen

强颜欢笑 提交于 2020-01-06 03:29:08

问题


I'm playing around with ARKit and SceneKit.

I have a node A - this is the only node that I've added to the rootNode. Node A is allowed to move freely by moving the phone around, except that it's not allowed to leave the screen.

To achieve that (prevent node A from leaving the screen), I can think of the following ways:

  1. Add a transform constraint to node A: I've tried to do it in the following manner:
[SCNTransformConstraint  transformConstraintInWorldSpace:false  withBlock:^SCNMatrix4(SCNNode * _Nonnull node, SCNMatrix4 transform) {
     bool visible = [self.sceneView isNodeInsideFrustum:node withPointOfView:self.cameraNode];
     if (visible) {
         NSLog(@"visible=%d", visible);
     }
     return transform;  
}];

However, I'm stuck as isNodeInsideFrustum always returns false, even when node A is visible.

  1. Change the field of view of the camera itself, to follow the node so that it doesn't go out of view. This however, I have no clue how to get started.

What I'm looking to do is pretty much the same as this SpriteKit question here - Keeping an object within the screen. Swift SpriteKit

And yes, since this is ARKit, I realise that node A will change it's position when it's dragged by the edge of a screen.

How can I prevent the node from leaving the field of view?

来源:https://stackoverflow.com/questions/48724596/prevent-scnnode-from-going-out-of-the-screen

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