【问题标题】:SCNBox make the front side rotated to the camera in ARKitSCNBox 使 ARKit 中的正面旋转到相机
【发布时间】:2018-03-26 11:41:34
【问题描述】:

我有一个简单的 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)
}

当我第一次添加它时,我看到了正面和右侧。如果我绕过另一侧的立方体并添加另一个立方体,我会看到立方体的背面。那么,无论用户如何走动,我能否做到让用户只看到正面。

谢谢。

【问题讨论】:

    标签: swift scenekit arkit scnnode


    【解决方案1】:

    您可以使用SCNBillboardConstraint 使给定节点面向相机。只需在将其添加到 rootNode 之前添加 boxNode.constraints = [SCNBillboardConstraint()]

    【讨论】:

    • 这是正确答案,但作为替代方案,您可以将节点的 .orientation 属性设置为渲染委托中 SCNView(活动相机)的 pointOfView 的 .orientation。
    猜你喜欢
    • 2018-01-30
    • 2016-12-14
    • 2021-01-01
    • 2018-06-21
    • 2018-07-21
    • 1970-01-01
    • 2021-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多