'SceneKit – How to apply MTL texture to OBJ file?
As a follow up to:
ios - How to apply mtl texture file to OBJ
I would like to add .MTL texture along with my .OBJ file, when importing my model to SceneKit
using code like this:
let scene = SCNScene(named: "rose.obj")
BUT the texture file I have is stored in DOCUMENTS directory (iOS).
How to call this function in Objective-C?
Solution 1:[1]
To work with the textured OBJ scene in Xcode you need a bundle of three files: .obj, .mtl and .jpg. To load an .obj scene with its corresponding textures, all three bundle-files must be located in the same Documents directory. Here's how your code might look when using Objective-C app:
#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
SCNView *sceneView = (SCNView *)self.view;
NSArray<NSURL *> *urls = [NSFileManager.defaultManager
URLsForDirectory:NSDocumentDirectory
inDomains:NSUserDomainMask];
NSURL *sceneInDocumentsDirectory = [urls[0]
URLByAppendingPathComponent:@"file.obj"];
SCNScene *scene = [SCNScene sceneWithURL:sceneInDocumentsDirectory
options:Nil
error:Nil];
sceneView.scene = scene;
sceneView.allowsCameraControl = YES;
sceneView.autoenablesDefaultLighting = YES;
sceneView.backgroundColor = [UIColor blackColor];
NSLog(@"%@", sceneInDocumentsDirectory);
}
@end
As you know, a .mtl file is an ASCII-based definitions of materials for .obj file.
Here's how it looks like:
newmtl phong1SG
illum 4
Kd 0.00 0.00 0.00
Ka 0.00 0.00 0.00
Tf 1.00 1.00 1.00
map_Kd texture.jpeg
Ni 1.00
Ks 0.50 0.50 0.50
Ns 18.00
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andy Jazz |

