0

我是 SceneKit 的新手,我正在尝试将 dae 文件加载到 SCNScene,将此 SCNScene 设置为 SCNView,启用用户交互,然后我可以通过手势旋转 3D 模型。到目前为止一切顺利,当我滑动或放大/缩小时,3D 模型按应有的方式工作。但是,我真正需要的是,当手势(向右或向左滑动)发生时,3D 模型仅水平旋转,没有放大/缩小,我该怎么做才能让它发生?这是我的代码:

// retrieve the SCNView
SCNView *myView = (SCNView *)self.view;

// load dae file and set the scene to the view
myView.scene = [SCNScene sceneNamed:@"model.dae"];

myView.userInteractionEnabled = YES;
myView.allowsCameraControl = YES;
myView.autoenablesDefaultLighting = YES;
myView.backgroundColor = [UIColor lightGrayColor];

谢谢你的帮助!

4

1 回答 1

3

我不确定您是否可以做到这一点allowsCameraControl-这似乎是与模型交互的非常基本的规定。

如果您向场景添加平移手势,您可以随意操作模型中的任何节点:

- (void)viewDidLoad {
    // Add the scene etc....

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
    [_sceneView addGestureRecognizer:panRecognizer];
}

- (void)panGesture:(UIPanGestureRecognizer *)sender {
    CGPoint translation = [sender translationInView:sender.view];

    if (sender.state == UIGestureRecognizerStateChanged) {
        [self doPanWithPoint:translation];
    }
}

- (void)doPanWithPoint:(CGPoint)translation {
    CGFloat x = (CGFloat)(translation.y) * (CGFloat)(M_PI)/180.0;
    CGFloat y = (CGFloat)(translation.x) * (CGFloat)(M_PI)/180.0;

    // Manuipulate the required (root?) node as you see fit
    _geometryNode.transform = SCNMatrix4MakeRotation(x, 0, 1, 0);
    _geometryNode.transform = SCNMatrix4Mult(_geometryNode.transform, SCNMatrix4MakeRotation(y, 1, 0, 0));
}

您显然可以省略第二个旋转步骤(或设置 y=0)只水平旋转。

于 2017-02-02T07:36:25.070 回答