如何一次加载同一个3d模型的多个节点?

时间:2018-03-08 15:06:05

标签: ios swift scenekit arkit

我正在开发一个ARKit应用程序,您可以在其中浏览门户网站并在其他地方工作。我购买了一个拱门的3d模型作为门户。我将模型放入Blender将其压平为一块,但纹理消失了,每当我应用新纹理时,它只显示在模型的一部分上。

这是原始模型,只能通过将其更改为.dae格式进行修改。但是,我无法反复加载arch1,arch2,arch3等,而无需反复编写相同的行

使用这个模型,我一遍又一遍地编写相同的代码

private func addPortalArch(hitResult: ARHitTestResult) {

            let portalScene = SCNScene(named: "art.scnassets/Medieval_portal.dae")
            let portalNode = portalScene?.rootNode.childNode(withName: "arch1", recursively: true) 

//我正在为每个拱门反复写这个portalNode = portalScene行。

//据我所知,我还必须获得每件作品的位置,我不知道我是如何根据用户点击放置门户的位置开始这样做的

            portalNode?.position = SCNVector3(hitResult.worldTransform.columns.3.x, hitResult.worldTransform.columns.3.y, hitResult.worldTransform.columns.3.z)

            self.sceneView.scene.rootNode.addChildNode(portalNode!)

        }

Now, if I've taken the model in to Blender, and I join all the pieces as one, it turns into a basic grayish brownish model and whenever I add textures, it just puts it on the one spot.

我需要模型一次性完成但仍然在整个模型中使用相同的石雕材料。

1 个答案:

答案 0 :(得分:0)

查看您发送的文件,最好的办法是使用包含拱门各个方面的模型(第1张图片)。

第一步(并非总是必要但有用)是将dae文件转换为.scn文件:

enter image description here

完成此操作后,您需要创建一个Empty Node作为门户门RootNode,并使门户网站的每个元素成为这样的孩子:

enter image description here

您还需要将每个ChildNode的(不是PortalRoot)欧拉角X旋转-90(当您加载模型时门户门就在它旁边):

enter image description here

现在,由于您只有一个RootNode,因此无需遍历所有节点,以便显示在屏幕上。你可以简单地使用这样的函数:

  /// Places The Portal Door 5m Aeay From The Camera
func setupPortalDoor(){

    //1. Get The Portal Scene
    guard let portalScene = SCNScene(named: "portalDoor.scn"),
        //2. Get The Portal Node
        let portalModel = portalScene.rootNode.childNode(withName: "PortalRoot", recursively: false) else { return }

    //3. Add To The ARSCNView Root
    augmentedRealityView?.scene.rootNode.addChildNode(portalModel)

    //4. Position It 5m Away From The Camera
    portalModel.position = SCNVector3(0, 0, -5)

    /*
     The Portal Is Large So I Suggest Scaling It ^__________*
    */
}

如果您以后想要将门户拱门更改为大理石,您可以轻松地执行此操作:

 for node in portalModel.childNodes{

        node.geometry?.firstMaterial?.diffuse.contents = UIColor.cyan
 }

希望这会有所帮助......

相关问题