我读到要在AR场景中使“虚拟对象似乎相对于现实世界保持原状”,应该使用ARAnchors。
答案 0 :(得分: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
}