Objective-C Scenekit: import an OBJ file from the outside and paint it

I am new to this. And my first problem comes.

I am creating an object loader using SceneKit. I got the path, the object is available, but I do not know how I can color the shown object.

ScnScene *testScene = [SCNScene sceneWithURL:url options:nil error:NULL];
testScene.background.contents = [UIImage imageWithName:@"color.png"];

[self.mainView.scene.rootnode addChildNode:testScene.rootNode];

This did not work. I also tried:

SCNMaterialProperty *testColor = [SCNMaterialProperty materialPropertyWithContents [UIImage imageNamed:@"color.png"]];
testScene.rootnode.geometry.materials = testcolor;

Or:

SCNMaterial *testColor = [SCNMaterial material];
testColor.diffuse.contesnts = [UIColor redColor];
testScene.rootnode.geometry.firstMaterial = testColor;

Nothing works. When I launch the application, each object is displayed. So far, OBJ-Loader works very well. But everything is still gray. I absolutely do not understand how to color the displayed objects. Does anyone have a hint / idea / solution for me?

Btw. I want to avoid the fact that I need to build geometry from OBJ information manually. Therefore, I am trying to solve this problem using SCNScene.

+4
2

, SCNScene . obj.file SCNNode, SCNMaterial ( ) SCNNode SCNNode SCNScene. obj.file, Model IO Framework.

, .

#import <SceneKit/SceneKit.h>
#import <ModelIO/ModelIO.h>
#import <SceneKit/ModelIO.h>

...

@property (nonatomic) SCNView* mainView;

....

MDLAsset *asset = [[MDLAsset alloc] initWithURL:url];     
SCNScene *scene = [SCNScene scene];
SCNNode *node = [SCNNode nodeWithMDLObject:[asset objectAtIndex:0]];

SCNMaterial *material = [SCNMaterial material];
material.diffuse.contents = [UIColor colorWithHue:0 saturation:0.1 brightness:0.5 alpha:1];
node.geometry.firstMaterial = material;

[scene.rootNode addChildNode:node];
[self.mainView.scene.rootNode addChildNode:scene.rootNode];

:

material.diffuse.contents = [UIImage imageNamed:@"farbe.png"];

obj.file ( ) .

SGlindemann, cashmash , .


(29.1.2017)

- . , . , 3D ( mainBundle, ). SCNNode, ViewController.m. SCNScene ViewController. , SCNNode.

, .obj .mtl( ) Xcode. .

#import <ModelIO/ModelIO.h>
#import <SceneKit/ModelIO.h>

...

@property (nonatomic) SCNNode *objectNode;

...

NSString* path = [[NSBundle mainBundle]
            pathForResource:[NSString stringWithFormat:@"name of the obj.file"]
            ofType:@"obj"];
NSURL *url = [NSURL fileURLWithPath:path];
MDLAsset *asset = [[MDLAsset alloc]initWithURL:url];

// Create the Block
self.objectNode = [SCNNode nodeWithMDLObject:[asset objectAtIndex:0]];


[self addChildNode: self.objectNode];

return self;

self .

[self.view.scene.rootNode addChildNode:returnedObj];

MDLAsset .obj .mtl png . MagicaVoxel ( obj + mtl + png ). .

SCNMaterial. , , . .

+6

, .

. SCNMaterialProperty testScene.rootnode.geometry.materials, SCNMaterial ( SCNMaterialProperty). ?

: contents contesnts. , .

, MDLAsset OBJ SCNNode. . OBJ Wavefront SCNNode - . , , SCNScene ( ) , OBJ.

+2

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


All Articles