首页 文章

是否将一个scenekit节点附加到ARAnchor只是将节点的变换设置为锚的变换?

提问于
浏览
0

我读到要在AR场景中使“虚拟物体看起来相对于现实世界保持原位”,你应该使用ARAnchors .

  • 我可以将一个场景包节点附加到我创建的锚点,而不仅仅是将节点的变换设置为锚点变换的值吗?对我来说,它看起来并不像将它附加到锚点,而只是将它们放置在相同的3D位置 .

  • 在这种情况下,我们真的没有看到使用锚点的意义 .

1 回答

  • 0

    ARKit管理锚点的位置,并通过任何必要的变换扩展连接到它们的节点 . 因此,基本相连的节点的相对位置保持不变,但是锚的位置可能会改变 . 您还可以设置约束,以使旋转始终与摄像机或其他节点相同 .

    所以,问题#1:它可以是相对于锚变换的任何变换 . 问题#2:看到上面的解释,节点到锚点的相对变换保持不变但是锚点位置可能会改变 .

    在锚位置添加球体的示例以及在其上方始终面向相机的文本标签:

    func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
    
        guard let featureAnchor = anchor as? FeatureAnchor else {
            return nil
        }
        let feature = featureAnchor.feature
    
        let billboardConstraint = SCNBillboardConstraint()
        billboardConstraint.freeAxes = SCNBillboardAxis.Y
    
        let sphere = SCNSphere(radius: Global.dotRadius)
        sphere.firstMaterial?.transparency = Global.annotationTransparency
        sphere.firstMaterial?.diffuse.contents = UIColor.yellow
        sphere.firstMaterial?.specular.contents = UIColor.white
        let node = SCNNode(geometry: sphere)
        node.constraints = [billboardConstraint]
    
        let text = SCNText(string: feature.description, extrusionDepth: Global.textDepth)
        text.chamferRadius = Global.textDepth
        text.firstMaterial?.transparency = Global.annotationTransparency
        text.firstMaterial?.diffuse.contents = UIColor.green
        text.firstMaterial?.specular.contents = UIColor.white
        text.firstMaterial?.isDoubleSided = true
        text.font = Global.textFont
        text.alignmentMode = kCAAlignmentCenter
    
        let (min, max) = text.boundingBox
        let dx: Float = (max.x - min.x) / 2.0 // x = center
        let dy: Float = min.y // y = bottom
        let dz: Float = Float(Global.textDepth) / 2.0 // z = center
    
        let textNode = SCNNode(geometry: text)
        textNode.pivot = SCNMatrix4MakeTranslation(dx, dy, dz)
        textNode.scale = SCNVector3Make(Global.textScale, Global.textScale, Global.textScale)
    
        node.addChildNode(textNode)
    
        return node
    }
    

相关问题