简体   繁体   English

将.jpg和.mtl文件应用于SceneKit中的.obj文件

[英]Apply .jpg and .mtl file to .obj file in SceneKit

The designer has delivered three files: 设计师提供了三个文件:

  • image. 图片。 jpg JPG
  • something. 一些东西。 mtl MTL
  • whatever. 随你。 obj OBJ

I can successfully load the .obj file into my scene like such: 我可以成功将.obj文件加载到我的场景中,如下所示:

  SCNView * sceneView = [SCNView new];
  sceneView.frame = view.bounds;
  [view addSubview:sceneView];

  SCNScene * scene = [SCNScene sceneNamed:@"models.scnassets/whatever.obj"];
  [sceneView setScene:scene];

What I'm struggling with is applying the .jpg and .mtl files to the .obj file. 我正在努力将.jpg和.mtl文件应用于.obj文件。 I've tried applying the image with the following code, but no love: 我尝试使用以下代码应用图像,但没有爱:

SCNMaterial * material = [SCNMaterial material];
material.diffuse.contents = [UIImage imageNamed:@"image.jpg"];

SCNNode * materialNode = [SCNNode node];
materialNode.geometry.firstMaterial = material;
[scene.rootNode addChildNode:materialNode];

Hi @Johnny There are multiple ways to apply texture files on .obj file. 您好@Johnny有多种方法可以在.obj文件上应用纹理文件。

  1. If you want to load some .obj files with awesome customizations you can open .obj file in Xcode and Apply Textures(Diffuse Map, Normal Map etc, Environments and Lights Camera also) then .obj file will convert .scn file to use in SCNView. 如果你想加载一些具有很棒自定义功能的.obj文件,你可以在Xcode中打开.obj文件并应用纹理(漫反射贴图,法线贴图等,环境和灯光摄像头),然后.obj文件将转换.scn文件以在SCNView中使用。

  2. If you want to apply textures/change textures on specific node of .obj or .scn file dynamically you may Use Model I/O Framework which provides Import, export, and manipulate 3D models using a common infrastructure that integrates MetalKit, GLKit, and SceneKit. 如果要动态地在.obj或.scn文件的特定节点上应用纹理/更改纹理,可以使用模型I / O框架,该框架使用集成了MetalKit,GLKit和SceneKit的通用基础结构提供导入,导出和操作3D模型。 SceneKit provides classes for ModelIO assets ex..+ (instancetype)sceneWithMDLAsset:(MDLAsset *)mdlAsset;(as @Zeeshan answered). SceneKit为ModelIO资产提供了类.. +(instancetype)sceneWithMDLAsset:(MDLAsset *)mdlAsset;(如@Zeeshan回答)。

  3. Your designer may exports 3D model files in .dae formate. 您的设计师可以在.dae formate中导出3D模型文件。 In .dae file a single file contains texture, camera, light reference. 在.dae文件中,单个文件包含纹理,相机,光引用。 No .mtl or others file needed as you use in .obj. 在.obj中使用时,不需要.mtl或其他文件。 .dae also supported in SCNKit .dae也在SCNKit中得到支持

  4. If you uses metal rendered you can load textures directly from NSURL. 如果使用渲染的金属,则可以直接从NSURL加载纹理。

This has gotten me some of the way there, albeit without using the .mtl file: 虽然没有使用.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

I run into the same problem and only solution i found is in this link Click here . 我遇到了同样的问题,我找到的唯一解决方案是在这个链接点击这里 I could load the .mtl information was to create the object through scene 我可以加载.mtl信息是通过场景创建对象

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

Make sure to have the .mtl and the jpg with the textures in your bundle. 确保包含纹理的.mtl和jpg。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM