我正在使用SceneKit来显示文本节点。这是我的代码:
// foo.swift
extension SCNNode {
func getNodeSize() -> (width: Float, height: Float) {
let (min, max) = boundingBox
return (width: max.x - min.x, height: max.y - min.y)
}
func setPivotCenter() {
let (min, max) = boundingBox
let x = min.x + 0.5 * (max.x - min.x)
let y = min.y + 0.5 * (max.y - min.y)
let z = min.z + 0.5 * (max.z - min.z)
self.pivot = SCNMatrix4MakeTranslation(x, y, z)
}
}
// bar.swift
func foo() {
let textGeometry = SCNText(string: "foo", extrusionDepth: 0.01)
let textNode = SCNNode(geometry: textGeometry)
// set pivot
textNode.setPivotCenter()
// get size
let (width, height) = textNode.getNodeSize()
// some other codes
}
但是有时(偶然性)在getNodeSize()函数中崩溃了:
symbol stub for: (extension in SceneKit):__C.SCNBoundingVolume.boundingBox.getter : (min: __C.SCNVector3, max: __C.SCNVector3)
EXC_BAD_ACCESS (code=1, address=0xxx)
or
malloc: *** error for object 0x283811360: pointer being freed was not allocated
我将两个扩展功能代码更改为一个,然后一切似乎都正常:
// bar.swift
func foo() {
let textGeometry = SCNText(string: "foo", extrusionDepth: 0.01)
let textNode = SCNNode(geometry: textGeometry)
let (min, max) = textNode.boundingBox
// set pivot
let x = min.x + 0.5 * (max.x - min.x)
let y = min.y + 0.5 * (max.y - min.y)
let z = min.z + 0.5 * (max.z - min.z)
textNode.pivot = SCNMatrix4MakeTranslation(dx, dy, dz)
// get size
let (width, height) = (width: max.x - min.x, height: max.y - min.y)
// some other codes
}
我的环境:
macOS:10.14 Beta(18A293u)
Xcode:10.0 beta(10L176w)
斯威夫特:4.2提前谢谢大家。