ios - 垂直和水平向ARKit场景添加平面节点

标签 ios swift arkit

我希望我的应用程序将节点放置在表面上,可以是垂直的也可以是水平的。但是,节点始终是垂直的。这是一张照片,这些节点放置不正确。

enter image description here

@objc func didTapAddButton() {
    let screenCentre = CGPoint(x: self.sceneView.bounds.midX, y: self.sceneView.bounds.midY)
    let arHitTestResults: [ARHitTestResult] = sceneView.hitTest(screenCentre, types: [.featurePoint]) // Alternatively, we could use '.existingPlaneUsingExtent' for more grounded hit-test-points.
    if let closestResult = arHitTestResults.first {
        let transform: matrix_float4x4 = closestResult.worldTransform
        let worldCoord: SCNVector3 = SCNVector3Make(transform.columns.3.x, transform.columns.3.y, transform.columns.3.z)
        if let node = createNode() {
            sceneView.scene.rootNode.addChildNode(node)
            node.position = worldCoord
        }
    }
}

func createNode() -> SCNNode? {
    guard let theView = myView else {
        print("Failed to load view")
        return nil
    }

    let plane = SCNPlane(width: 0.06, height: 0.06)
    let imageMaterial = SCNMaterial()
    imageMaterial.isDoubleSided = true
    imageMaterial.diffuse.contents = theView.asImage()
    plane.materials = [imageMaterial]
    let node = SCNNode(geometry: plane)
    return node
}

应用程序能够看到地面,但节点仍然与我们平行。我该如何解决这个问题?

编辑:我想我可以使用 node.eulerAngles.x = -.pi/2,这可以确保平面水平放置,但它在垂直表面上仍然是水平的。

最佳答案

解决了!以下是使 View 始终与相机“平行”的方法:

let yourNode = SCNNode()

let billboardConstraint = SCNBillboardConstraint()
billboardConstraint.freeAxes = [.X, .Y, .Z]
yourNode.constraints = [billboardConstraint]

或者

guard let currentFrame = sceneView.session.currentFrame else {return nil}
let camera = currentFrame.camera
let transform = camera.transform
var translationMatrix = matrix_identity_float4x4
translationMatrix.columns.3.z = -0.1
let modifiedMatrix = simd_mul(transform, translationMatrix)
let node = SCNNode(geometry: plane)
node.simdTransform = modifiedMatrix

关于ios - 垂直和水平向ARKit场景添加平面节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53804913/

相关文章:

ios - 如何在 Swift 中正确计算日期时差

ios - TesseractOCR BAD_ACCESS

ios - SceneKit——在 SCNNode 面对的方向上施加力

ios - 是否可以在模拟器中运行 ARKit?

ios - 如何获得 NSString 的正确大小或检查它是否适合 UILabel?

iphone - iOS - 当 UIView 出现在屏幕上时,它调用什么方法?

ios - 尽管有标签和按钮,可重用的 UITableViewCells 显示为 null

ios - 具有自定义转换的 presentViewController

ios - 使用用户默认值保存数据

ios - Xcode 更新到 7.3 后断言失败