【问题标题】:SceneKit – How to apply MTL texture to OBJ file?SceneKit – 如何将 MTL 纹理应用于 OBJ 文件?
【发布时间】:2021-07-20 07:34:34
【问题描述】:
【问题讨论】:
标签:
objective-c
scenekit
mtl-file
【解决方案1】:
要在 Xcode 中使用带纹理的 OBJ 场景,您需要包含三个文件:.obj、.mtl 和 .jpg。要加载具有相应纹理的.obj 场景,所有三个捆绑文件必须位于相同的Documents 目录中。以下是使用 Objective-C 应用程序时代码的外观:
#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
如您所知,.mtl 文件是.obj 文件的基于 ASCII 的材料定义。
它是这样的:
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