答案 0 :(得分:2)
这是一个简单的原理,即如何在两点之间放置带有3D文本的标签。
let p1 = SCNNode() // POINT 1
p1.geometry = SCNSphere(radius: 0.3)
p1.position = SCNVector3(-1, -2, -3)
p1.geometry?.firstMaterial?.diffuse.contents = NSColor.red
scene.rootNode.addChildNode(p1)
let p2 = SCNNode() // POINT 2
p2.geometry = SCNSphere(radius: 0.2)
p2.position = SCNVector3(4, 5, 6)
p2.geometry?.firstMaterial?.diffuse.contents = NSColor.red
scene.rootNode.addChildNode(p2)
let x = (p1.position.x + p2.position.x) / 2
let y = (p1.position.y + p2.position.y) / 2
let z = (p1.position.z + p2.position.z) / 2
let label = SCNNode()
let text = SCNText(string: "label", extrusionDepth: 5)
label.geometry = text
// Shifting text's pivot to its center. Default pivot's position is lower left corner.
label.simdPivot.columns.3.x = Float((text.boundingBox.min.x +
text.boundingBox.max.x) / 2)
label.simdPivot.columns.3.y = Float((text.boundingBox.min.y +
text.boundingBox.max.y) / 2)
label.scale = SCNVector3(0.02, 0.02, 0.02)
label.position = SCNVector3(x, y, z)
label.geometry?.firstMaterial?.diffuse.contents = NSColor.red
scene.rootNode.addChildNode(label)
let constraint = SCNBillboardConstraint()
label.constraints = [constraint]
let plane = SCNNode()
plane.geometry = SCNPlane(width: 2, height: 1)
plane.position = SCNVector3(x, y, z)
plane.geometry?.firstMaterial?.diffuse.contents = NSColor.white
scene.rootNode.addChildNode(plane)
plane.constraints = [constraint]
您可以添加
ARAnchor
并将任何label
附加到它的方法。