小编Rob*_*man的帖子

如何使用ARKit将SCNNode的方向更改为相机

下面的代码将SCNPlane放置到触摸点上,但是平面朝向(旋转)到应用程序启动时手机所处的位置.理想情况下,我希望将节点定位到物理墙或相机的当前方向.如何用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 hitTransform = SCNMatrix4.init(hitResult.worldTransform)

    let hitVector = SCNVector3Make(hitTransform.m41, hitTransform.m42, hitTransform.m43)
    createPlane(position: hitVector)
}

func createPlane(position: SCNVector3) {

    let background = SCNNode()
    background.geometry = SCNPlane.init(width: 0.12, height: 0.10) // better set its size
    background.geometry?.firstMaterial?.diffuse.contents = "odinBW2.jpeg"
    background.position = position

    sceneView.scene.rootNode.addChildNode(background)

}
Run Code Online (Sandbox Code Playgroud)

ios scnnode arkit xcode9

6
推荐指数
2
解决办法
3295
查看次数

标签 统计

arkit ×1

ios ×1

scnnode ×1

xcode9 ×1