Game Development Reference
In-Depth Information
Listing 7-1 shows how to use sceneWithOptions:statusHandler: to load a scene and get updates inside a block.
Listing 7-1. Using sceneWithOptions:statusHandler: to Load a Scene and Get Updates Inside a Block
//our test scene is the famous COLLADA duck that we added as a
//resource to the Xcode project
NSURL* sceneURL;
sceneURL = [[NSBundle mainBundle] URLForResource:@"duck" withExtension:@"dae"];
//create the scene source
SCNSceneSource* sceneSource = [SCNSceneSource sceneSourceWithURL:sceneURL options:nil];
//create the scene by loading the scene source
SCNScene* scene = [sceneSource sceneWithOptions:nil statusHandler: ^(float totalProgress,
SCNSceneSourceStatus status, NSError* error, BOOL* stopLoading) {
if (status != SCNSceneSourceStatusError) {
//at this point progress could be used to update a progress bar
NSLog(@" totalProgress:%f",totalProgress);
} else {
NSLog(@"Error loading scene %@",error.localizedDescription);
}
}
];
So far, the method sceneWithOptions: was called with nil for options. But the options available at loading time
can be very useful. An efficient COLLADA viewer can easily be built up by passing the following set of options to
the scene source:
SCNSceneSourceCreateNormalsIfAbsentKey automatically computes normals for a
given model.
SCNSceneSourceFlattenSceneKey merges together all the geometries contained in the scene
graph. When the asset has animations related to nodes containing geometry, the merge is not
performed and the scene is left unchanged.
Listing 7-2 shows how to build the dictionary to be passed to the sceneWithOptions: method.
Listing 7-2. Building the Dictionary to be Passed to the sceneWithOptions: Method
NSDictionary *options;
options = [NSDictionary dictionaryWithObjectsAndKeys:
SCNSceneSourceFlattenSceneKey, @YES, SCNSceneSourceCreateNormalsIfAbsentKey,@YES, nil];
Additional details to complete a simple viewer setup are provided in the rendering section.
Extraction of COLLADA Library Entries
Beyond its typical usage in a viewer, a COLLADA asset can be considered a library of 3D entities (geometries, lights,
etc.) to be organized at runtime by the application. Therefore, the SCNSceneSource class provides methods to extract
these entities by simply referring to their unique COLLADA ID.
The first method retrieves all the identifiers for the entries of a given class.
- (NSArray *)identifiersOfEntriesWithClass:(Class)entryClass;
 
Search WWH ::




Custom Search