2017-10-14 17 views
1

저는 간단한 ARKit 앱이 있습니다. 사용자가 화면을SCNBox는 ARKit에서 카메라 앞면을 회전시킵니다.

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    guard let touch = touches.first else { return } 
    let result = sceneView.hitTest(touch.location(in: sceneView), types: [ARHitTestResult.ResultType.featurePoint]) 
    guard let hitResult = result.last else { return } 
    let hitTrasform = SCNMatrix4(hitResult.worldTransform) 
    let hitVector = SCNVector3Make(hitTrasform.m41, hitTrasform.m42, hitTrasform.m43) 

    createBox(position: hitVector) 
} 

닿을 때 제가 처음 추가하면, I는 전방 우측의 비트를 볼 큐브

func createBox(position: SCNVector3) { 
    let box = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 1) 
    box.firstMaterial?.diffuse.contents = UIColor.black 

    let sides = [ 
     UIColor.red,   // Front 
     UIColor.black,  // Right 
     UIColor.black,  // Back 
     UIColor.black,  // Left 
     UIColor.black,  // Top 
     UIColor.black   // Bottom 
    ] 

    let materials = sides.map { (side) -> SCNMaterial in 
     let material = SCNMaterial() 
     material.diffuse.contents = side 
     material.locksAmbientWithDiffuse = true 
     return material 
    } 

    box.materials = materials 

    let boxNode = SCNNode(geometry: box) 
    boxNode.position = position 
    sceneView.scene.rootNode.addChildNode(boxNode) 
} 

추가. 내가 다른 쪽 큐브를 둘러보고 다른 큐브를 추가하면 큐브의 뒷면이 보입니다. 따라서 사용자가 어떻게 돌아가는지에 관계없이 전면 측면 만 볼 수 있도록 할 수 있습니까?

감사합니다.

답변

1

SCNBillboardConstraint을 사용하면 지정된 노드를 카메라와 마주 보게 할 수 있습니다. rootNode에 추가하기 전에 boxNode.constraints = [SCNBillboardConstraint()]을 추가하십시오.

+0

이것은 올바른 대답이지만 노드의 .orientation 속성을 렌더링 위임자의 SCNView (활성 카메라)의 pointOfView로 설정할 수 있습니다. – Xartec