![](/img/trans.png)
[英]How To Apply .MTL File on .OBJ 3d Model via SceneKit & Model I/O
[英]Apply .jpg and .mtl file to .obj file in SceneKit
設計師提供了三個文件:
我可以成功將.obj文件加載到我的場景中,如下所示:
SCNView * sceneView = [SCNView new];
sceneView.frame = view.bounds;
[view addSubview:sceneView];
SCNScene * scene = [SCNScene sceneNamed:@"models.scnassets/whatever.obj"];
[sceneView setScene:scene];
我正在努力將.jpg和.mtl文件應用於.obj文件。 我嘗試使用以下代碼應用圖像,但沒有愛:
SCNMaterial * material = [SCNMaterial material];
material.diffuse.contents = [UIImage imageNamed:@"image.jpg"];
SCNNode * materialNode = [SCNNode node];
materialNode.geometry.firstMaterial = material;
[scene.rootNode addChildNode:materialNode];
您好@Johnny有多種方法可以在.obj文件上應用紋理文件。
如果你想加載一些具有很棒自定義功能的.obj文件,你可以在Xcode中打開.obj文件並應用紋理(漫反射貼圖,法線貼圖等,環境和燈光攝像頭),然后.obj文件將轉換.scn文件以在SCNView中使用。
如果要動態地在.obj或.scn文件的特定節點上應用紋理/更改紋理,可以使用模型I / O框架,該框架使用集成了MetalKit,GLKit和SceneKit的通用基礎結構提供導入,導出和操作3D模型。 SceneKit為ModelIO資產提供了類.. +(instancetype)sceneWithMDLAsset:(MDLAsset *)mdlAsset;(如@Zeeshan回答)。
您的設計師可以在.dae formate中導出3D模型文件。 在.dae文件中,單個文件包含紋理,相機,光引用。 在.obj中使用時,不需要.mtl或其他文件。 .dae也在SCNKit中得到支持
如果使用渲染的金屬,則可以直接從NSURL加載紋理。
雖然沒有使用.mtl文件,但這讓我有了一些方法:
//START 3D
NSURL * url = [[NSBundle mainBundle] URLForResource:@"playerModel" withExtension:@"obj"];
MDLAsset * asset = [[MDLAsset alloc] initWithURL:url];
MDLMesh * object = (MDLMesh *)[asset objectAtIndex:0];
MDLScatteringFunction * scatFunction = [MDLScatteringFunction new];
MDLMaterial * material = [[MDLMaterial alloc] initWithName:@"playerMaterial" scatteringFunction:scatFunction];
NSURL * materialURL = [[NSBundle mainBundle] URLForResource:skinFilename withExtension:@"jpg"];
MDLMaterialProperty * baseColour = [MDLMaterialProperty new];
[baseColour setType:MDLMaterialPropertyTypeTexture];
[baseColour setSemantic:MDLMaterialSemanticBaseColor];
[baseColour setURLValue:materialURL];
[material setProperty:baseColour];
for (MDLSubmesh * sub in object.submeshes){
sub.material = material;
}
SCNScene * scene = [SCNScene new];
SCNNode * node = [SCNNode nodeWithMDLObject:object];
SCNVector3 v = SCNVector3Make(100, 200, 0);
[node setPosition:v];
[scene.rootNode addChildNode:node];
SCNView * view = [SCNView new];
view.frame = transferInView.bounds;
view.autoenablesDefaultLighting = true;
view.allowsCameraControl = false;
view.scene = scene;
view.backgroundColor = [UIColor clearColor];
[transferInView addSubview:view];
SCNAction * rotate = [SCNAction rotateByX:0 y:1 z:0 duration:1.0f];
[node runAction:[SCNAction repeatActionForever:rotate]];
SCNVector3 min = SCNVector3Zero;
SCNVector3 max = SCNVector3Zero;
[node getBoundingBoxMin:&min max:&max];
node.pivot = SCNMatrix4MakeTranslation((max.x - min.x) / 2 + min.x, (max.y - min.y) / 2 + min.y, 0);
//END 3D
我遇到了同樣的問題,我找到的唯一解決方案是在這個鏈接點擊這里 。 我可以加載.mtl信息是通過場景創建對象
let scene = SCNScene(named: "rose.obj")
確保包含紋理的.mtl和jpg。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.