您的 SCNVector3 topLeft 使用与世界坐标系无关的屏幕宽度作为 z 坐标。
坐标系
SceneKit 使用右手坐标系,其中(默认情况下)视图方向沿负 z 轴,如下图所示。
https://developer.apple.com/documentation/scenekit/organizing_a_scene_with_nodes
从世界坐标到二维视图的投影点
projectPoint(_:)
将一个点从场景的 3D 世界坐标系投影到
渲染器的二维像素坐标系。
https://developer.apple.com/documentation/scenekit/scnscenerenderer/1524089-projectpoint
插图
这里我们看到一个维度为 2 的立方体。它位于世界坐标系的原点(x: 0, y: 0, z: 0)。相机在 x: 0, y: 0, z: 10。
现在要将立方体的位置 (0/0/0) 投影到 2D 视图,可以在 Objective-C 中编写:
SCNVector3 projectedPoint = [self.sceneView projectPoint:SCNVector3Make(0, 0, 0)];
在 iPhone 8 Plus 上,我们知道屏幕尺寸为 414 x 736。所以我们预计结果为 x:207 y:368。
如果我们记录输出,那么使用以下代码:
NSLog(@"projectedPoint: %f %f %f", projectedPoint.x, projectedPoint.y, projectedPoint.z);
一个人会得到类似的东西:
projectedPoint: 207.000000 368.000000 0.909091
所以现在使用这个projectedPoint 并取消投影它,我们应该再次获得原点:
SCNVector3 unprojectedPoint = [self.sceneView unprojectPoint:projectedPoint];
NSLog(@"unprojectedPoint: %f %f %f", unprojectedPoint.x, unprojectedPoint.y, unprojectedPoint.z);
确实输出是
unprojectedPoint: 0.000000 0.000000 0.000000
完整示例
这里是上述场景的完整代码:
@interface GameViewController ()
@property (nonatomic, weak) SCNView *scnView;
@property (nonatomic, strong) SCNNode *cameraNode;
@property (nonatomic, strong) SCNScene *scnScene;
@property (nonatomic, strong) SCNNode *boxNode;
@end
@implementation GameViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self _setupView];
[self _setupScene];
[self _setupCamera];
[self _setupTapping];
[self _addCube];
}
-(void)_setupTapping {
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_tapHandler:)];
self.scnView.gestureRecognizers = @[tapGestureRecognizer];
}
-(void)_tapHandler:(UIGestureRecognizer *)gestureRecognizer {
NSLog(@"--------------");
SCNVector3 projectedPoint = [self.scnView projectPoint:SCNVector3Make(0, 0, 0)];
NSLog(@"projectedPoint: %f %f %f", projectedPoint.x, projectedPoint.y, projectedPoint.z);
SCNVector3 unprojectedPoint = [self.scnView unprojectPoint:projectedPoint];
NSLog(@"unprojectedPoint: %f %f %f", unprojectedPoint.x, unprojectedPoint.y, unprojectedPoint.z);
CGPoint point = [gestureRecognizer locationInView:self.scnView];
SCNVector3 scenePoint = [self.scnView unprojectPoint:SCNVector3Make(point.x, point.y, projectedPoint.z)];
NSLog(@"point %f %f -> scenePoint: %f %f %f", point.x, point.y, scenePoint.x, scenePoint.y, scenePoint.z);
}
-(void)_setupView {
self.scnView = (SCNView *)self.view;
self.scnView.autoenablesDefaultLighting = YES;
}
-(void)_setupScene {
self.scnScene = [SCNScene new];
self.scnView.scene = self.scnScene;
}
- (void)_setupCamera {
self.cameraNode = [SCNNode new];
self.cameraNode.camera = [SCNCamera camera];
self.cameraNode.position = SCNVector3Make(0, 0, 10);
[self.scnScene.rootNode addChildNode:self.cameraNode];
}
- (void)_addCube {
SCNBox *box = [SCNBox boxWithWidth:2 height:2 length:2 chamferRadius:0];
SCNMaterial *mat = [SCNMaterial new];
mat.diffuse.contents = @"art.scnassets/crosshair.png";
box.materials = @[mat];
self.boxNode = [SCNNode new];
self.boxNode.geometry = box;
self.boxNode.position = SCNVector3Make(0, 0, 0);
[self.scnScene.rootNode addChildNode:self.boxNode];
}
- (BOOL)prefersStatusBarHidden {
return YES;
}
@end
点击魔方的左上角
示例中的立方体具有十字准线纹理。在 iPhone 上,这看起来像这样:
所以现在您可以点按视图上的某个位置。
用下面的代码
CGPoint point = [gestureRecognizer locationInView:self.scnView];
SCNVector3 scenePoint = [self.scnView unprojectPoint:SCNVector3Make(point.x, point.y, projectedPoint.z)];
NSLog(@"point %f %f -> scenePoint: %f %f %f", point.x, point.y, scenePoint.x, scenePoint.y, scenePoint.z);
我们在上图中显示为网格的区域上输出未投影点(世界坐标系)。
由于立方体的维度为 2,并且位于原点,因此确切的世界坐标点将是 x:-1,y:-1,z:0。
例如,当我在模拟器中点击立方体的左上角时,我会得到如下信息:
point 141.000000 299.000000 -> scenePoint: -1.035465 1.082531 0.000000