如何计算SCNText的中心与其左,右角之间的距离

时间:2019-12-28 17:36:43

标签: ios swift scnnode scntext

如何计算SCNText中心与其左上角和右上角之间的距离。

例如,使用下面的代码,如果单词为“ texas”,则字母“ x”的中间为pivot point,因为它位于单词的中间。我如何获得字母“ t”左侧和字母“ x”之间的距离。如何获得字母“ s”右侧和字母“ x”之间的距离

let material = SCNMaterial()
material.diffuse.contents = UIColor.red

let geometry = SCNText(string: "texas", extrusionDepth: 0.1)
geometry.flatness = 0
geometry.font = UIFont.systemFont(ofSize: 6.3)
geometry.materials = [material]

let textNode = SCNNode()
textNode.geometry = geometry
textNode.geometry?.firstMaterial?.isDoubleSided = true

// set pivot point 
let min = textNode.boundingBox.min
let max = textNode.boundingBox.max
textNode.pivot = SCNMatrix4MakeTranslation(
    min.x + (max.x - min.x)/2,
    min.y + (max.y - min.y)/2,
    min.z + (max.z - min.z)/2
)

1 个答案:

答案 0 :(得分:0)

let totalWidth: Float = textNode.boundingBox.max.x - textNode.boundingBox.min.x

let halfWidth: Float = totalWidth / 2

print("total width:", totalWidth)

print("half width:", halfWidth)