Created
March 26, 2019 20:58
-
-
Save hcwiley/ec2dbf384585a389037dfdebfa2d5e8a to your computer and use it in GitHub Desktop.
Load a USDZ, OBJ, STL, or PLY into a SCNNode using ModelIO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <SceneKit/ModelIO.h> | |
- (SCNNode *) loadFileIntoSCNNode:(NSString *)path { | |
// Get the path as a file url | |
NSURL *url = [NSURL fileURLWithPath:path]; | |
// Load the file using Model IO. | |
// OBJ, STL, PLY, and USDZ are supported. Although binary PLY is not, so watch out. | |
MDLAsset *asset = [[MDLAsset alloc] initWithURL:url]; | |
// Load the textures, not sure this is needed but what the heck! | |
[asset loadTextures]; | |
// Make a new node for us to put everything in | |
SCNNode *node = [SCNNode node]; | |
// Add all the objects from the MDLAsset | |
for (int i = 0; i < asset.count; i++) { | |
MDLObject* child = [asset objectAtIndex:i]; | |
// As a child node to our node | |
[node addChildNode:[SCNNode nodeWithMDLObject:child]]; | |
} | |
// Return the node | |
return node; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment