gpt4 book ai didi

ios - 将 iOS SceneKit 中的 SCNText 与 SCNBox 与 boundingBox 对齐?

转载 作者:可可西里 更新时间:2023-11-01 01:22:50 26 4
gpt4 key购买 nike

修改后的问题:

我不明白为什么白色节点在它是盒子或球体时居中,但在它是文本时却不居中。您可以注释/取消注释 whiteGeometry 变量以查看每个不同几何图形的显示方式。我原本以为我必须通过确定框和文本的宽度并计算文本的位置来手动将文本居中。我需要这样做吗?为什么文本表现不同?

import SceneKit
import PlaygroundSupport

let scene = SCNScene()
let sceneView = SCNView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))
sceneView.scene = scene
sceneView.backgroundColor = .darkGray
sceneView.autoenablesDefaultLighting = true
sceneView.allowsCameraControl = true
PlaygroundPage.current.liveView = sceneView

// Camera
let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
scene.rootNode.addChildNode(cameraNode)
cameraNode.position = SCNVector3(x: 0, y: 0, z: 25)
sceneView.pointOfView = cameraNode

let blackGeometry = SCNBox(width: 10.0, height: 10.0, length: 10.0, chamferRadius: 0)
blackGeometry.firstMaterial?.diffuse.contents = UIColor.black
print("blackGeometry min=\(blackGeometry.boundingBox.min)")
print("blackGeometry max=\(blackGeometry.boundingBox.max)")
let blackNode = SCNNode(geometry: blackGeometry)
blackNode.position = SCNVector3(x: 0, y: 0, z: 0)
scene.rootNode.addChildNode(blackNode)

// let whiteGeometry = SCNBox(width: 3.0, height: 3.0, length: 3.0, chamferRadius: 0)
let whiteGeometry = SCNText(string: "L", extrusionDepth: 0)
whiteGeometry.alignmentMode = kCAAlignmentLeft
whiteGeometry.font = UIFont.systemFont(ofSize: 8.0)
// let whiteGeometry = SCNSphere(radius: 3.0)
whiteGeometry.firstMaterial?.diffuse.contents = UIColor.white
print("whiteGeometry min=\(whiteGeometry.boundingBox.min)")
print("whiteGeometry max=\(whiteGeometry.boundingBox.max)")
let whiteNode = SCNNode(geometry: whiteGeometry)

let boxWidth = blackGeometry.boundingBox.max.x - blackGeometry.boundingBox.min.x
let boxHeight = blackGeometry.boundingBox.max.y - blackGeometry.boundingBox.min.y
print("boxWidth=\(boxWidth)")
print("boxHeight=\(boxHeight)")

let txtWidth = whiteGeometry.boundingBox.max.x - whiteGeometry.boundingBox.min.x
let txtHeight = whiteGeometry.boundingBox.max.y - whiteGeometry.boundingBox.min.y
print("txtWidth=\(txtWidth)")
print("txtHeight=\(txtHeight)")

whiteNode.position = SCNVector3(x: -5.0, y: -5.0, z: 10)
scene.rootNode.addChildNode(whiteNode)
//blackNode.addChildNode(whiteNode)

print("done")

原始问题(旧):

假设我有两个 SCNBox 节点(我正在对此进行简化以使其清楚,但该解决方案必须适用于其他几何形状)。一个大黑盒子和一个小白盒子。我想将白框居中放在黑框前面。

为此,我需要确定两个节点的宽度和高度。请记住,节点可以不是像球体或文本这样的盒子。据我所知,确定宽度/高度的唯一方法是通过几何上的 boundingBox 属性。它有一个最小值和最大值,在 Apple 的引用手册中没有清楚和完整地描述。为了获得高度,我似乎会根据 boundingBox.max.y 和 boundingBox.min.y 来计算它。所以看看下面这个 10x10x10 盒子的例子,我看不出我怎么能得到 10.0 作为高度,因为 max.y 5.20507812

例如

let box = SCNBox(width: 10.0, height: 10.0, length: 10.0, chamferRadius: 0)
print("box min=\(box.boundingBox.min)")
print("box max=\(box.boundingBox.max)")

产量:

box min=SCNVector3(x: -5.0, y: -5.0, z: -5.0)
box max=SCNVector3(x: 5.0, y: 5.20507812, z: 5.0)

为什么max.y=5.20507812?我应该如何确定高度/宽度?​​

另请参阅:SCNBoundingVolume

最佳答案

我想我现在明白了。

看起来您正试图在对象前面放置一些文本,文本相对于相机以对象为中心。这个问题本身与边界框无关。对吧?

您会看到白色几何体为球体或 SCNText 时的不同行为,因为 SCNText 使用与其他具体 SCNGeometry 类不同的原点约定。其他几何图形将原点放在中心。 SCNText 将它放在文本的左下方。如果我没记错的话,“lower”是指字体的底部(最低的下降),而不是基线。

因此,您需要计算文本的边界框,并在定位文本节点时考虑到这一点,以使其居中。对于其他几何类型,您不需要这样做。如果被覆盖的物体或相机正在移动,您需要在渲染循环中计算从相机到被覆盖的物体的光线,并更新文本的位置。

Swift Scenekit - Centering SCNText - the getBoundingBoxMin:Max issue是相关的。

如果您只想要叠加文本,您可能会发现将文本放在 SKScene 叠加层中会更容易。一个例子是 https://github.com/halmueller/ImmersiveInterfaces/tree/master/Tracking%20Overlay (警告,位腐烂可能已经开始,我已经有一段时间没有尝试过该代码了)。

关于ios - 将 iOS SceneKit 中的 SCNText 与 SCNBox 与 boundingBox 对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43100804/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com