如何将 .dae 文件加载到 iOS SceneKit 中的 SCNNode 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25356301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you load a .dae file into an SCNNode in iOS SceneKit?
提问by M-V
I am having trouble understanding how geometry from .dae
files should be loaded into an iOS SceneKit scene graph.
我无法理解如何将.dae
文件中的几何图形加载到 iOS SceneKit 场景图中。
I know that you can load .dae
files directly into an SCNScene:
我知道您可以将.dae
文件直接加载到 SCNScene 中:
// create a new scene
let scene = SCNScene("test.scnassets/test.dae")
I also know that you can create an empty SCNScene and add built-in 3D objects into it as child nodes:
我也知道您可以创建一个空的 SCNScene 并将内置 3D 对象作为子节点添加到其中:
let scene = SCNScene()
var sphereNode = SCNNode()
sphereNode.geometry = SCNSphere(radius: 1.0)
scene.rootNode.addChildNode(sphereNode)
In the second case, can you load a .dae
file into SCNNode instead of using built in objects? Or is it expected that all .dae
files need to be loaded into the SCNScene first?
在第二种情况下,您可以将.dae
文件加载到 SCNNode 中而不是使用内置对象吗?还是期望所有.dae
文件都需要先加载到 SCNScene 中?
If the latter is true, how then do you place the SCNNode objects into the right place in the scene graph hierarchy under SCNScene?EDIT #1:
如果后者为真,那么如何将 SCNNode 对象放置在 SCNScene 下场景图层次结构中的正确位置? 编辑#1:
This is working for me now, which is modified version of what @FlippinFun suggested.
这现在对我有用,这是@FlippinFun 建议的修改版本。
let url = NSBundle.mainBundle().URLForResource("test.scnassets/test", withExtension: "dae")
let source = SCNSceneSource(URL: url, options: nil)
let block = source.entryWithIdentifier("ID10", withClass: SCNGeometry.self) as SCNGeometry
scene.rootNode.addChildNode(SCNNode(geometry: block))
But perhaps there's a better way? The above involves manually loading each identifier from the .dae tree. In my file, there's a node "SketchUp" with child identifiers "ID2" and "ID10". This method will not let you load the root "SketchUp". (I get a runtime error.)
但也许有更好的方法?以上涉及从 .dae 树手动加载每个标识符。在我的文件中,有一个节点“SketchUp”,其子标识符为“ID2”和“ID10”。此方法不会让您加载根“SketchUp”。(我收到运行时错误。)
采纳答案by Toyos
The usual pattern is to load your scene and retrieve the node you are interested in with its name using
通常的模式是加载您的场景并使用名称检索您感兴趣的节点
[scene.rootNode childNodeWithName:aName recursively:YES];
Then you can reparent this node to your main scene.
然后您可以将此节点重新设置为您的主场景。
回答by Richard Szczerba
I had a similar problem where I had one .dae file and I wanted to load multiple nodes from that file into a node. This worked for me:
我有一个类似的问题,我有一个 .dae 文件,我想将多个节点从该文件加载到一个节点中。这对我有用:
var node = SCNNode()
let scene = SCNScene(named: "helloScene.dae")
var nodeArray = scene.rootNode.childNodes
for childNode in nodeArray {
node.addChildNode(childNode as SCNNode)
}
First, I set a node that will hold it all together so it looks like it does in the original file. Next we import the .dae file we want and copy all the nodes inside of it. Lastly, for each node in the array I added it to the node.
首先,我设置了一个节点,将所有内容放在一起,使其看起来就像在原始文件中一样。接下来我们导入我们想要的 .dae 文件并复制其中的所有节点。最后,对于数组中的每个节点,我将其添加到节点中。
回答by rabie jegham
Thanks for your help!
谢谢你的帮助!
Here is a simple collada2Node class.
这是一个简单的 collada2Node 类。
//collada2SCNNode
class func collada2SCNNode(filepath:String) -> SCNNode {
var node = SCNNode()
let scene = SCNScene(named: filepath)
var nodeArray = scene!.rootNode.childNodes
for childNode in nodeArray {
node.addChildNode(childNode as SCNNode)
}
return node
}
回答by user2252471
func nodeWithFile(path: String) -> SCNNode {
if let scene = SCNScene(named: path) {
let node = scene.rootNode.childNodes[0] as SCNNode
return node
} else {
println("Invalid path supplied")
return SCNNode()
}
}
回答by MB_iOSDeveloper
The following answers didn't work for me.
以下答案对我不起作用。
Here is what worked for me in Xcode 7.0 beta 5 , iOS 9.0 beta / Swift 2.0:
这是在 Xcode 7.0 beta 5 、iOS 9.0 beta / Swift 2.0 中对我有用的内容:
3D model type: Collada 3D model name: "model.dae"
3D 模型类型:Collada 3D 模型名称:“model.dae”
Load the scene:
let lampScene = SCNScene(named: "art.scnassets/model.dae")
Access the child SCNode which is in the SCNScene:
let lampRootNode = lampScene?.rootNode.childNodes[0]
Add the lamp node from the loaded scene into the current loaded and viewed scene:
self.scene.rootNode.addChildNode(lampRootNode!)
加载场景:
let lampScene = SCNScene(named: "art.scnassets/model.dae")
访问 SCNScene 中的子 SCNode:
let lampRootNode = lampScene?.rootNode.childNodes[0]
将加载场景中的灯节点添加到当前加载和查看的场景中:
self.scene.rootNode.addChildNode(lampRootNode!)
NOTE:The self.scene
exists due to:
注:该self.scene
存在的原因是:
private var scene:SCNScene!
Prior of adding the object to the scene I have made some scene setup according to the scene kit example. If you need the code mention it in the comments.
在将对象添加到场景之前,我根据场景套件示例进行了一些场景设置。如果您需要代码,请在评论中提及。
Hope it helps.
希望能帮助到你。
回答by Aayushi
guard let shipScene = SCNScene(named: "ship.dae") else { return }
let shipNode = SCNNode()
let shipSceneChildNodes = shipScene.rootNode.childNodes
for childNode in shipSceneChildNodes {
shipNode.addChildNode(childNode)
}