首页 文章

SceneKit - SCNText 居中不正确

提问于
浏览
4

我试图在下面的代码中使文本字符串(SCNText)适合框(SCNBox)。框的大小看起来正确,但文本不在框的右侧中心。任何想法或解决方案?谢谢

let geoText = SCNText(string: "Hello", extrusionDepth: 1.0)
geoText.font = UIFont (name: "Arial", size: 8)
geoText.firstMaterial!.diffuse.contents = UIColor.red
let textNode = SCNNode(geometry: geoText)

let (minVec, maxVec) = textNode.boundingBox
scnScene.rootNode.addChildNode(textNode)

let w = CGFloat(maxVec.x - minVec.x)
let h = CGFloat(maxVec.y - minVec.y)
let d = CGFloat(maxVec.z - minVec.z)

let geoBox = SCNBox(width: w, height: h, length: d, chamferRadius: 0)
geoBox.firstMaterial!.diffuse.contents = UIColor.green.withAlphaComponent(0.5)
scnScene.rootNode.addChildNode(boxNode)

文本不是框的中心

**编辑:**我添加了一个新的字符串图像(没有 SCNBox 节点)与 debugOptions showBoundingBoxes 看到它的边界框

在此输入图像描述

解决方案 1:

从 vdugnist 的回答中,我为任何想要测试的人创建了一个游乐场代码:

import UIKit
import SceneKit
import PlaygroundSupport

var sceneView = SCNView(frame: CGRect(x: 0, y: 0, width: 600, height: 600))
var scene = SCNScene()
sceneView.scene = scene
PlaygroundPage.current.liveView = sceneView

let geoText = SCNText(string: "Hello", extrusionDepth: 1.0)
geoText.font = UIFont (name: "Arial", size: 12)
geoText.firstMaterial!.diffuse.contents = UIColor.red
let textNode = SCNNode(geometry: geoText)

let (minVec, maxVec) = textNode.boundingBox
textNode.position = SCNVector3(x: (minVec.x - maxVec.x) / 2, y: minVec.y - maxVec.y, z: 0)
textNode.pivot = SCNMatrix4MakeTranslation((maxVec.x - minVec.x) / 2, 0, 0)
scene.rootNode.addChildNode(textNode)

let w = CGFloat(maxVec.x - minVec.x)
let h = CGFloat(maxVec.y - minVec.y)
let d = CGFloat(maxVec.z - minVec.z)

let geoBox = SCNBox(width: w, height: h, length: d, chamferRadius: 0)
geoBox.firstMaterial!.diffuse.contents =   UIColor.green.withAlphaComponent(0.5)
let boxNode = SCNNode(geometry: geoBox)
boxNode.position = SCNVector3Make((maxVec.x - minVec.x) / 2 + minVec.x, (maxVec.y - minVec.y) / 2 + minVec.y, 0);
textNode.addChildNode(boxNode)

在此输入图像描述

解决方案 2:

我需要将文本移动到零(0,0,0)而不是移动文本和 around 框,因此我继续从解决方案 1 更改文本的轴。现在代码如下:

import UIKit
import SceneKit
import PlaygroundSupport

var sceneView = SCNView(frame: CGRect(x: 0, y: 0, width: 600, height: 600))
var scene = SCNScene()
sceneView.scene = scene
PlaygroundPage.current.liveView = sceneView

let geoText = SCNText(string: "Hello", extrusionDepth: 1.0)
geoText.font = UIFont (name: "Arial", size: 12)
geoText.firstMaterial!.diffuse.contents = UIColor.red
let textNode = SCNNode(geometry: geoText)

let (minVec, maxVec) = textNode.boundingBox
textNode.pivot = SCNMatrix4MakeTranslation((maxVec.x - minVec.x) / 2 + minVec.x, (maxVec.y - minVec.y) / 2 + minVec.y, 0)
scene.rootNode.addChildNode(textNode)

let w = CGFloat(maxVec.x - minVec.x)
let h = CGFloat(maxVec.y - minVec.y)
let d = CGFloat(maxVec.z - minVec.z)

let geoBox = SCNBox(width: w, height: h, length: d, chamferRadius: 0)
geoBox.firstMaterial!.diffuse.contents = UIColor.green.withAlphaComponent(0.6)
let boxNode = SCNNode(geometry: geoBox)
scene.rootNode.addChildNode(boxNode)

4 回答

  • 8

    SCNText 与其他几何的区别在于 SCNText 原点位于左下角。在其他几何形状中,它是一个底部中心。

    要修复父节点中的文本位置,可以将其 pivotPoint.x 设置为宽度的一半:

    SCNVector3 min, max;
    [textNode getBoundingBoxMin:&min max:&max];
    textNode.pivot = SCNMatrix4MakeTranslation((max.x - min.x) / 2, 0, 0);
    

    要修复子节点位置,您应将其位置设置为宽度加上 min 的一半:

    SCNVector3 min, max;
    [textNode getBoundingBoxMin:&min max:&max];
    subnode.position = SCNVector3Make((max.x - min.x) / 2 + min.x, (max.y - min.y) / 2 + min.y, 0);
    
  • 4

    Swift:正确_ _中心 text-node

    let (min, max) = textNode.boundingBox
    
    let dx = min.x + 0.5 * (max.x - min.x)
    let dy = min.y + 0.5 * (max.y - min.y)
    let dz = min.z + 0.5 * (max.z - min.z)
    textNode.pivot = SCNMatrix4MakeTranslation(dx, dy, dz)
    
    let textNodeParent = SCNNode()
    textNodeParent.addChildNode(textNode)
    
  • 0

    要找到边界框的中心,您需要在除以 2 之前添加(而不是减去)最小值和最大值。

    textNode.position = SCNVector3(x: (minVec.x + maxVec.x) / 2, y: minVec.y + maxVec.y, z: 0)
    
  • 0

    我想你可能会被 SCNText 对原产地的异常处理所困扰。 IIRC Y 的零线位于弦线的基线,而不是在下降线的底部。

    打开showBoundingBoxesdebugOptions中,看看是否有帮助。

相关问题