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

标签 ios swift scenekit arkit

我正在开发一个 ARKit 应用程序,您可以在其中走过门户,然后到达其他地方。我购买了一个拱门的 3D 模型作为入口。我将模型放入 Blender 中,将其压平为一 block ,但纹理消失了,每当我应用新纹理时,它仅显示在模型的一部分上。

这是原始模型,仅修改为.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.

我需要模型是一体的,但整个模型仍然具有相同的石制品 Material 。

最佳答案

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

第一步(并不总是必需的,但很有用)是将 dae 文件转换为 .scn 文件:

enter image description here

完成此操作后,您需要创建一个空节点作为 Portal Door RootNode 并使 Portal 的每个元素成为该节点的子元素,例如:

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
 }

希望这有帮助...

关于ios - 如何一次加载同一 3D 模型的多个节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49176279/

相关文章:

ios - 我怎样才能防止用户点击按钮两次,这样当用户关闭应用程序并打开他仍然无法点击

ios - Swift 2.0、SpriteKit - 不使用页面的 ScrollView 。

ios - 跟踪 Alamofire 请求的进度

ios - 如何使闭包作为函数参数可选

swift - swift 中的 `.floatValue` 是什么

iphone - SceneKit – AVPlayer Material 崩溃

javascript - 解析 CloudCode 的执行顺序

swift - SceneKit:使用骨骼动画渲染两个 SCNNode 时发生错误

swift - 具有透明度的闪烁 SCNTube

ios - 删除 NSString 中所有出现的地方