我試圖在下面的代碼中將一個文本字符串(SCNText)放入一個框(SCNBox)中。框的大小看起來是正確的,但文本不在框的正中。任何想法或解決方案?由於SceneKit - SCNText居中不正確
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)
編輯:我添加了一個debugOptions showBoundingBoxes字符串(不使用SCNBox節點)的新形象,看看它的邊框
解決方案1:
從vdugnist的回答3210,我創造的人誰願意操場代碼來測試:
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),而不是移動文本和周圍的框,因此我繼續更改解決方案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)
謝謝,但看起來它不是一個解決方案(我已經嘗試過,但它更糟糕 - 也許我不明白你的想法?)。邏輯上,在我的問題圖像中,中心點對於x軸和y軸都不在正確的位置。你的答案只是試圖修復x,顯然這是不夠的。這是最好的,如果你在這裏發佈完整的修復代碼。 – Tony
您是否可以嘗試使用我的第二個解決方案中的座標將盒子節點添加爲文本節點的子節點而不是根節點? 我在代碼中看到的問題是,您將從textNode獲取boundingBox,該文本將在textNode座標系中返回,並在rootNode座標系中使用它們。 – vdugnist
明白了,它現在運作良好! – Tony