【发布时间】:2019-09-05 15:03:39
【问题描述】:
【问题讨论】:
【问题讨论】:
您创建一个父节点,然后将所有节点添加为子节点。
即:
var nodes: [SCNNode] = getMyNodes()
var parentNode = SCNNode()
parentNode.name = "chair"
for node in nodes {
parentNode.addChildNodes(node)
}
当你对一个节点应用更新时,更新也会下推给子节点,例如:
parentNode.transform = SCNMatrix4MakeTranslation(0, 0.5, 1.2)
将对所有附加到parentNode 的孩子应用相同的翻译转换。
您可以使用以下方式访问特定的孩子:
parentNode.childNode(withName: nameOfChildNode, recursively: true)
并使用以下方法获取任何孩子的父母:
myChildNode.parent
编辑:
如果您从场景文件中导入,您仍然可以轻松地以编程方式访问您的节点:
let scene = SCNScene(named: "myScene.scn")
func getMyNodes() -> [SCNNode] {
var nodes: [SCNNode] = [SCNNode]()
for node in scene.rootNode.childNodes {
nodes.append(node)
}
return nodes
}
顺便说一句,这意味着您可以忽略上述大部分内容并使用myScene.rootNode 作为parent 节点。
【讨论】:
我的建议是:
我多次使用第二个来使用 ARKit 构建动态场景。像魅力一样工作。
基本上,我将 .dae 文件的所有子项添加到 SCNNode :
var node = SCNNode()
let scene = SCNScene(named: "myScene.dae")
var nodeArray = scene.rootNode.childNodes
for childNode in nodeArray {
node.addChildNode(childNode as SCNNode)
}
您应该使用相同的 .addChildNode 方法尝试一下您的文件:)
【讨论】:
以防万一,如果有人正在寻找像这种黄黑棒这样的解决方案
class VirtualObject: SCNNode {
enum Style {
case anchor
case fence
}
let style: Style
var value: String?
init(_ style: Style) {
self.style = style
super.init()
switch style {
case .anchor:
let material = SCNMaterial()
material.diffuse.contents = UIColor.magenta
material.lightingModel = .physicallyBased
let object = SCNCone(topRadius: 0.02, bottomRadius: 0, height: 0.1) // SCNSphere(radius: 0.02)
object.materials = [material]
self.geometry = object
case .fence:
for index in 0 ... 6 {
let material = SCNMaterial()
material.diffuse.contents = index % 2 == 0 ? UIColor.yellow : UIColor.black
material.lightingModel = .physicallyBased
let node = SCNNode(geometry: SCNCylinder(radius: 0.01, height: 0.05))
node.geometry?.materials = [material]
node.transform = SCNMatrix4MakeTranslation(0, 0.05 * Float(index), 0)
addChildNode(node)
}
}
}
【讨论】: