如何显示(添加)两点之间的平面文本节点?

时间:2019-07-05 08:08:51

标签: ios swift scenekit augmented-reality arkit

如何在测量ARKit / SceneKit中显示两点之间的距离值平面?与图片相同。

enter image description here

1 个答案:

答案 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附加到它的方法。

enter image description here