简体   繁体   English

在Scenekit中如何将一个SCNNode拆分为多个节点?

[英]In Scenekit how to split one SCNNode into many nodes?

I trying to load a .obj file.我试图加载一个 .obj 文件。 how ever after loaded, secenekit only generate one node with a huge geometry which has many submeshes.加载后,secenekit 只生成一个具有巨大几何形状的节点,其中包含许多子网格。 Anyone has idea how to split this huge node into many nodes, each node owns one submesh form the original node?任何人都知道如何将这个巨大的节点分成许多节点,每个节点拥有一个来自原始节点的子网格? As I need to apply different physic on them.因为我需要对它们应用不同的物理。 Thanks in advance.提前致谢。

I have tried following approach:我尝试过以下方法:

  1. manually parse the .obj file.手动解析 .obj 文件。 this works but eat up too much memory这有效,但占用了太多内存
  2. after load the file, I get the huge node.加载文件后,我得到了巨大的节点。 then I do然后我做

     let geometry = SCNGeometry(sources: rootNode.geometry!.geometrySources, elements: [rootNode.geometry!.geometryElementAtIndex(count)]) geometry.firstMaterial = rootNode.geometry!.materials[count] let node = SCNNode(geometry: geometry)

for each mesh (get from .obj file) this also work, but take a long time for rendering, I guess is because I copy the whole source for each node.对于每个网格(从 .obj 文件中获取),这也有效,但是渲染需要很长时间,我猜是因为我复制了每个节点的整个源。

It sounds like you have already found a couple of approaches that give you correct results, but they take too long to do at run time.听起来您已经找到了几种可以为您提供正确结果的方法,但是它们在运行时需要很长时间才能完成。 So run with those, but do it in advance.所以和那些一起跑,但要提前做。

Run your manual OBJ parse, or your per-element geometry retrieval, in an auxiliary program.在辅助程序中运行手动 OBJ 解析或每个元素的几何检索。 Archive that result to an SCNScene (or maybe SCNNodes ).将结果存档到SCNScene (或者可能是SCNNodes )。 Embed the archive/archives in your public project.将档案/档案嵌入您的公共项目中。 Unarchiving a .SCN file will be much faster than parsing and instantiating a big OBJ file.解压 .SCN 文件比解析和实例化一个大的 OBJ 文件要快得多。

Here's a function to archive a scene to a file.这是将场景存档到文件的功能。 You can then embed the file in your project and open it the way the space ship is opened in the template code, or use the Xcode Scene Editor to tweak it.然后,您可以将该文件嵌入到您的项目中,并按照在模板代码中打开太空船的方式打开它,或者使用 Xcode 场景编辑器对其进行调整。

func archiveToFile(fileName: String) -> Bool {
    let data = NSKeyedArchiver.archivedDataWithRootObject(gameView!.scene!)

    // Save data to file
    let DocumentDirURL = try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true)

    let fileURL = DocumentDirURL.URLByAppendingPathComponent(fileName).URLByAppendingPathExtension("scn")
    print("FilePath:", fileURL.path)

    if (!data.writeToURL(fileURL, atomically: true)) {
        return false
    }
    return true
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM