初始化模型,位置为 SCNVector3(0, -1, -2)
, 对于大的模型, 初始化后不容易被用户找到, 所以初始化时让模型全貌的显示在屏幕前方
lazy var node: AudioinARKitLocationNode = {
let modelScene = SCNScene(named: scnName)!
let cup = modelScene.rootNode.childNodes[0]
let node = AudioinARKitLocationNode(location: location, node: cup)
node.name = scnName
let boundingBox = node.boundingBox
let distance = boundingBox.max.distance(to: boundingBox.min)
let base: Float = 0.8
if distance > base {
// 缩放模型, 初始化时,不超出屏幕大小
let scale = base / distance
node.scale = SCNVector3(scale, scale, scale)
}
return node
}()
extension SCNVector3 {
func distance(to anotherVector: SCNVector3) -> Float {
return sqrt(pow(anotherVector.x - x, 2) + pow(anotherVector.z - z, 2))
}
}