Apply .jpg and .mtl file to .obj file in SceneKit

The designer provided three files:

  • picture. Jpg
  • something. MTL
  • anything. Obj

I can successfully load the .obj file into my scene, for example:

  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 in the .obj file. I tried applying the image with the following code, but don't like it:

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

SCNNode * materialNode = [SCNNode node];
materialNode.geometry.firstMaterial = material;
[scene.rootNode addChildNode:materialNode];
+4
source share
3 answers

Hi @Johnny There are several ways to apply texture files in a .obj file.

  • .obj , .obj Xcode Apply Textures (Diffuse Map, Normal Map .., Lights Camera), .obj . scn SCNView.

  • / node .obj .scn , Model I/O Framework, , 3D- MetalKit, GLKit SceneKit. SceneKit ModelIO ex.. + (instancetype) sceneWithMDLAsset: (MDLAsset *) mdlAsset; ( @Zeeshan).

  • .dae formate. .dae , , . .mtl , .obj. .dae SCNKit

  • , NSURL.

+1

, . .mtl,

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

, .mtl jpg .

0

, .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
0

Source: https://habr.com/ru/post/1686660/


All Articles