本文介绍了如何通过SceneKit和在.OBJ 3d模型上应用.MTL文件型号I / O的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过SceneKit&在3D模型上应用.mtl文件纹理。模型I / 0。



当我尝试在其上应用纹理的.jpg时,下面的代码工作正常:

  let url = NSBundle.mainBundle()。URLForResource( chair,withExtension: obj)
let asset = MDLAsset(URL:NSURL(string:url) !)
保护让对象= asset.objectAtIndex(0)为? MDLMesh其他{
// fatalError(无法从资产获取网格。)
return
}

if shouldApplyTexture == true {
var textureFileName = chair.mtl

//从各种纹理创建材质
let散射Function = MDLScatteringFunction()
let material = MDLMaterial(name: baseMaterial,散射函数:散射函数)

material.setTextureProperties(纹理:[
.BaseColor:textureFileName])

//将纹理应用于资产$ b $的每个子网格b表示object.submeshes中的submesh! {
如果让submesh = submesh为? MDLSubmesh {
submesh.material =材质
}
}

}

//将ModelIO对象包装在SceneKit对象$ b中$ b let node = SCNNode(MDLObject:object)

if(scene.rootNode.childNodes.count> 0){
scene.rootNode.enumerateChildNodesUsingBlock {(节点,停止)-> ;
中的void node.removeFromParentNode()
}
}
scene.rootNode.addChildNode(node)

我对setTextureProperties使用以下MDMaterial扩展名:

 扩展名MDLMaterial {
func setTextureProperties([MDLMaterialSemantic:String])->空隙{

用于纹理中的(键,值){
var finalURL = NSBundle.mainBundle()。URLForResource(value,withExtension:)
保护URL = finalURL else {
// fatalError(未能找到资源\(value)的URL。)
return
}

let property = MDLMaterialProperty(name: fileName !,语义:键,URL:URL)
self.setProperty(property)
}
}
}

如何加载.mtl文件并将其应用到模型上以使其具有纹理?
我应该声明SCNMaterial的哪些属性才能从.mtl文件中获取纹理数据?

解决方案

有点晚了,但是我面临着相同的问题,加载.mtl信息的方式是通过场景创建对象,例如,我正在加载


I am trying to apply an .mtl file texture on .obj 3d model via SceneKit & Model I/0.

My code below works fine when I try to apply .jpg of a texture on it:

       let url = NSBundle.mainBundle().URLForResource("chair", withExtension: "obj")
       let asset = MDLAsset(URL: NSURL(string:url)!)
        guard let object = asset.objectAtIndex(0) as? MDLMesh else {
            //fatalError("Failed to get mesh from asset.")
            return
        }

        if shouldApplyTexture == true {
            var textureFileName = "chair.mtl"

            // Create a material from the various textures
            let scatteringFunction = MDLScatteringFunction()
            let material = MDLMaterial(name: "baseMaterial", scatteringFunction: scatteringFunction)

            material.setTextureProperties(textures: [
                                            .BaseColor:textureFileName])

            // Apply the texture to every submesh of the asset
            for  submesh in object.submeshes!  {
                if let submesh = submesh as? MDLSubmesh {
                    submesh.material = material
                }
            }

        }

        // Wrap the ModelIO object in a SceneKit object
        let node = SCNNode(MDLObject: object)

        if (scene.rootNode.childNodes.count > 0){
            scene.rootNode.enumerateChildNodesUsingBlock { (node, stop) -> Void in
                node.removeFromParentNode()
            }
        }
        scene.rootNode.addChildNode(node)

I am using the following MDMaterial extension for setTextureProperties:

extension MDLMaterial {
func setTextureProperties([MDLMaterialSemantic:String]) -> Void {

    for (key,value) in textures {
        var finalURL = NSBundle.mainBundle().URLForResource(value, withExtension: "")
       guard let url = finalURL else {
           // fatalError("Failed to find URL for resource \(value).")
            return
        }

        let property = MDLMaterialProperty(name:fileName!, semantic: key, URL: url)
        self.setProperty(property)
    }
  }
}

How should I load an .mtl file and apply it on my model to have texture on it?What properties of SCNMaterial should I declare for getting texture data from a .mtl file?

解决方案

It might be a bit late, but I'm facing the same issue and the way in which I could load the .mtl information was to create the object through and scene, for example, I'm loading this model

let scene = SCNScene(named: "rose.obj")

Make sure to have the .mtl and the jpg with the textures in your bundle.

这篇关于如何通过SceneKit和在.OBJ 3d模型上应用.MTL文件型号I / O的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 08:49