问题描述
我正在尝试通过 SceneKit & 在 .obj 3d 模型上应用 .mtl 文件纹理.模型 I/0.
当我尝试在其上应用 .jpg 纹理时,下面的代码工作正常:
let url = NSBundle.mainBundle().URLForResource("chair", withExtension: "obj")让资产 = MDLAsset(URL: NSURL(string:url)!)守卫让对象 = asset.objectAtIndex(0) as?MDLMesh 其他 {//fatalError("无法从资产中获取网格.")返回}如果 shouldApplyTexture == true {var textureFileName = "chair.mtl"//从各种纹理创建一个材质让散射函数 = MDLScatteringFunction()让材料 = MDLMaterial(名称:baseMaterial",scatteringFunction:scatteringFunction)material.setTextureProperties(纹理:[.BaseColor:textureFileName])//将纹理应用于资产的每个子网格用于 object.submeshes 中的子网格!{如果让 submesh = submesh as?MDLSubmesh {submesh.material = 材料}}}//将 ModelIO 对象包装在 SceneKit 对象中让节点 = SCNNode(MDLObject: object)如果(scene.rootNode.childNodes.count > 0){scene.rootNode.enumerateChildNodesUsingBlock {(节点,停止)->作废node.removeFromParentNode()}}场景.rootNode.addChildNode(节点)
我正在为 setTextureProperties 使用以下 MDMaterial 扩展:
extension MDLMaterial {func setTextureProperties([MDLMaterialSemantic:String]) ->空白 {对于(键,值)在纹理{var finalURL = NSBundle.mainBundle().URLForResource(value, withExtension: "")守卫让 url = finalURL else {//致命错误(无法找到资源 (值)的 URL.")返回}让属性 = MDLMaterialProperty(名称:文件名!,语义:键,网址:网址)self.setProperty(属性)}}}
我应该如何加载 .mtl 文件并将其应用到我的模型上以在其上添加纹理?为了从 .mtl 文件中获取纹理数据,我应该声明 SCNMaterial 的哪些属性?
可能有点晚了,但我面临着同样的问题,我可以加载 .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 文件模型输入/输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!