2016-09-07 4 views
0

SCNCylinder을 사용하여 SceneKit에서 플랫 실린더를 만들려고합니다. 사용자가 스크린을 두드리는 위치에서 실린더를 장면에두기를 원합니다.터치 위치에서 SceneKit의 SCNCylinder를 생성하는 것이 정확하지 않습니다.

현재 접근 방식이 작동하지만 어떤 이유로 원통이 터치 위치에 정확하게 놓이지 않습니다. 화면의 일부에 따라 실린더가 가끔 터치 위치의 중간에 있고 때로는 상당한 양만큼 떨어져 있습니다. 스크린 샷이 문제를 충분히 잘 나타내기를 바랍니다.

Offset when creating SCNCylinder at screenpos

나는 현재 카메라가 위치하는 SCNSphere 있습니다. 구체로 화면 터치 점을 히트 테스트함으로써 히트 테스트 방향으로 광선을 검색합니다. 그런 다음 광선의 법선 벡터를 취하고 벡터를 따라 6을 곱한 벡터의 위치를 ​​결정합니다.

이 방법의 문제점은 누구나 알 수 있습니다. 왜 이러한 오프셋 동작이 발생합니까?

이 어떻게 현재 SCNCylinder 만들 :

- (IBAction)longPressGesture:(UILongPressGestureRecognizer *)sender { 
    if (sender.state == UIGestureRecognizerStateBegan) { 
     CGPoint location = [sender locationInView:self.sceneView]; 
     NSArray *hitTestResult = [self.sceneView hitTest:location options:nil]; 

     if (hitTestResult.count == 1) { 
      SCNHitTestResult *sphereHit = hitTestResult.firstObject; 
      // Get ray coordinates from local camera position 
      SCNVector3 localCoordinates = sphereHit.worldNormal; 
      localCoordinates = SCNVector3Make(localCoordinates.x * 6, localCoordinates.y * 6, localCoordinates.z * 6); 
      [self addCylinder:SCNVector3Make(localCoordinates.x, localCoordinates.y, localCoordinates.z)]; 
     } 
    } 
} 

- (void)addCylinder:(SCNVector3)position { 
    SCNCylinder *cylinder = [SCNCylinder cylinderWithRadius:0.5 height:0.01]; 
    SCNNode *cylinderNode = [SCNNode nodeWithGeometry:cylinder]; 

    // Create LookAt Contstraint 
    NSMutableArray *constraints = [NSMutableArray new]; 
    SCNLookAtConstraint *lookAtCameraConstraint = [SCNLookAtConstraint lookAtConstraintWithTarget:cameraNode]; 
    lookAtCameraConstraint.gimbalLockEnabled = YES; 
    [constraints addObject:lookAtCameraConstraint]; 

    // Turn 90° Constraint 
    SCNTransformConstraint *turnConstraint = [SCNTransformConstraint transformConstraintInWorldSpace:NO withBlock:^SCNMatrix4(SCNNode * _Nonnull node, SCNMatrix4 transform) { 
     transform = SCNMatrix4Mult(SCNMatrix4MakeRotation(M_PI_2, 1, 0, 0), transform); 
     return transform; 
    }]; 
    [constraints addObject:turnConstraint]; 

    cylinderNode.constraints = constraints; 

    cylinderNode.position = position; 

    SCNNode *rootNode = self.sceneView.scene.rootNode; 
    [rootNode addChildNode:cylinderNode]; 
} 

답변

0

SceneKit의 모든 SCNSphere은 다각형에서 생성됩니다. 다각형의 수와 SCNSphere 메시의 세분성은 segmentCount 속성에 의해 결정됩니다.

Documentation of SCNSphere

은 기본적으로 segmentCount 값은 매우 미세 그레인되지 않은 48로 설정되어 있습니다. 낮은 segmentCount를 사용하여 SCNSpherehitTest:을 수행하면 실제 터치 포인트와 비교하여 오프셋이있는 다각형이 검색됩니다. segmentCount를 늘리면 (예 : 96) 수평 및 수직 방향으로 세그먼트가 증가하고 오프셋이 줄어 듭니다.

segmentCount을 늘리면 성능에 영향을 미칩니다.