如何将3D AR对象移动/拖动到场景视图?

时间:2017-08-21 10:53:49

标签: ios objective-c arkit

我在场景视图中移动/拖动AR对象时遇到了一些问题。

//这是我需要移动的对象

@property (nonatomic, strong) SCNNode *movedObject;

//这是我在场景视图周围移动对象的方法

-(void)handleMoveObjectFrom:(UIPanGestureRecognizer *)recognizer {

if (recognizer.state == UIGestureRecognizerStateBegan) {
    NSLog(@"Pan state began");
    CGPoint tapPoint = [recognizer locationInView:self.sceneView];
    NSArray <SCNHitTestResult *> *result = [self.sceneView hitTest:tapPoint options:nil];

    if ([result count] == 0) {
        return;
    }
    SCNHitTestResult *hitResult = [result firstObject];
    self.movedObject = [hitResult.node parentNode];
}
if (recognizer.state == UIGestureRecognizerStateChanged) {
    NSLog(@"Pan State Changed");
    if (self.movedObject) {

        CGPoint tapPoint = [recognizer locationInView:self.sceneView];
        NSArray <ARHitTestResult *> *hitResults = [self.sceneView hitTest:tapPoint types:ARHitTestResultTypeFeaturePoint];
        ARHitTestResult *result = [hitResults lastObject];

        //Attempt 1
        SCNVector3 vector = SCNVector3Make(result.worldTransform.columns[3].x,
                                           result.worldTransform.columns[3].y,
                                           result.worldTransform.columns[3].z
                                           );
        self.movedObject.position = vector;

        //Attempt 2
        simd_float4x4 cameraTransform = self.sceneView.session.currentFrame.camera.transform;
        simd_float3 cameraWorldPosition = simd_make_float3(cameraTransform.columns[3]);
        NSLog(@"vector: %f, %f, %f", cameraWorldPosition.x, cameraWorldPosition.y, cameraWorldPosition.z);
        [self.movedObject setSimdPosition:cameraWorldPosition];

        //Attempt3
        SCNMatrix4 matrix = SCNMatrix4FromMat4(result.worldTransform);
        SCNVector3 vector = SCNVector3Make(matrix.m41, matrix.m42, matrix.m43);
        NSLog(@"vector: %f, %f, %f", vector.x, vector.y, vector.z);
        self.movedObject.position = vector;
        NSLog(@"Moving object position");
    }
}
if (recognizer.state == UIGestureRecognizerStateEnded) {
    NSLog(@"Done moving object");
    self.movedObject = nil;
}
}

任何帮助都将受到高度赞赏。

感谢。

0 个答案:

没有答案