如何在iOS场景中将多个节点加入一个节点 [英] How to join multiple nodes to one node in iOS scene

查看:82
本文介绍了如何在iOS场景中将多个节点加入一个节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我们在下图中看到的,场景图中有多个节点.要求是我想将这些节点添加到一个节点中,如图 2 所示.所以在场景图中,我将有一个包含多个节点的节点(椅子)

As we can see in below pic there all multiple nodes in the scene graph. The requirement is I want to add these nodes in one node as shown in image2. So In scene graph, I will have one node(chair) which will contain multiple nodes

                      Image1

                    Image2

推荐答案

您创建一个父节点,然后将所有节点添加为子节点.

You create a parent node and then add all your nodes as children.

即:

var nodes: [SCNNode] = getMyNodes()

var parentNode = SCNNode()
parentNode.name = "chair"

for node in nodes {
    parentNode.addChildNodes(node)
}

当您对节点应用更新时,更新也会下推到子节点,例如:

When you apply an update to a node, the update is also pushed down to the children, for example:

parentNode.transform = SCNMatrix4MakeTranslation(0, 0.5, 1.2)

将对附加到 parentNode 的所有子节点应用相同的转换转换.

will apply the same translation transformation to all the children attached to parentNode.

您可以使用以下方法访问特定的孩子:

You can access a specific child using:

parentNode.childNode(withName: nameOfChildNode, recursively: true)

并使用以下方法获取任何孩子的父母:

and get the parent of any child using:

myChildNode.parent

如果您从场景文件导入,您仍然可以通过编程轻松访问您的节点:

If you are importing from a scene file, you can still easily access your nodes programmatically:

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 节点.

which, incidentally, means that you can ignore most of the above and use myScene.rootNode as the parent node.

这篇关于如何在iOS场景中将多个节点加入一个节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆