2017-10-14 84 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) 
} 

我添加了一個立方體

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的.orientation。 – Xartec