'SceneKit Black Screen for Basic Scene (Objective C)
I am new to SceneKit. I am trying to create a basic scene. I have a model, lights, and camera. But for some reason I can't see my model. I've been searching for why this is happening, but I can't find anything.
Any direction appreciated.
My Setup is below:
art.scnassets - _models: Cube.scn - _textures: cubeFaces01.png, cubeFaces02.png, ...
GameViewController.h
#import <UIKit/UIKit.h>
#import <SceneKit/SceneKit.h>
@interface GameViewController : UIViewController
@end
GameViewController.m
//
// GameViewController.m
// PuzzleCube
//
// Created by Robert Lee on 2016-04-30.
// Copyright (c) 2016 Robert Lee. All rights reserved.
//
#import "GameViewController.h"
@implementation GameViewController{
@private
// Scene
SCNScene *_scene;
// Manipulation Nodes
SCNNode *_floorNode;
SCNNode *_backdropNode;
SCNNode *_cubeHandle;
SCNNode *_cameraHandle;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self setup];
}
#pragma mark - Setup
- (void)setup
{
SCNView *sceneView = (SCNView *)self.view;
// Redraw forever
sceneView.playing = YES;
sceneView.loops = YES;
sceneView.showsStatistics = YES;
sceneView.backgroundColor = [UIColor blackColor];
// Setup the scene
[self setupScene];
// Initial Point of View
sceneView.pointOfView = _cameraHandle;
// Present the scene
sceneView.scene = _scene;
}
- (void)setupScene
{
_scene = [SCNScene scene];
[self setupEnvironment];
[self setupMainSceneElements];
}
- (void)setupEnvironment
{
// Setup Main Floor Node
SCNFloor *mainFloor = [SCNFloor floor];
_floorNode = [SCNNode node];
_floorNode.geometry = mainFloor;
// Add to Main Scene
[_scene.rootNode addChildNode:_floorNode];
}
# pragma mark - Setup Main Scene
- (void)setupMainSceneElements
{
//
// Scene Environment Elements
//
// Setup Main Backdrop Node
SCNPlane *mainBackdrop = [SCNPlane planeWithWidth:20 height:20];
_backdropNode = [SCNNode node];
_backdropNode.geometry = mainBackdrop;
_backdropNode.position = SCNVector3Make(200, 100, -200);
//
// Character Elements
//
// Cube Node
[self setupCubeNodeCharacter];
//
// Static Elements
//
//
// Lighting Elements
//
// Spot Light Node
[self setupBasicLightingNodeLight];
//
// Camera Elements
//
// Main Camera Node
[self setupCameraNodeCamera];
//
// Add to Main Scene
//
[_scene.rootNode addChildNode:_backdropNode];
}
# pragma mark -Character Elements
- (void)setupCubeNodeCharacter
{
/* Element Hierarchy:
cubeHandle
|_ cubeRotationY
|_ cubePivot
|_ cubeNode
*/
SCNScene *modelScene = [SCNScene sceneNamed:@"Cube.scn" inDirectory:@"assets.scnassets/_models" options:nil];
SCNNode *_cubeNode = [modelScene.rootNode childNodeWithName:@"CubeGeometry" recursively:YES];
SCNNode *cubeMesh = _cubeNode.childNodes[0];
cubeMesh.hidden = YES;
SCNNode *_cubePivot = [SCNNode node];
SCNNode *_cubeRotationY = [SCNNode node];
_cubeHandle = [SCNNode node];
_cubeHandle.position = SCNVector3Make(0, 0, 0);
_cubeNode.position = SCNVector3Make(0, 0, 0);
// Configure Element Hierarchy
[_cubePivot addChildNode:_cubeNode];
[_cubeRotationY addChildNode:_cubePivot];
[_cubeHandle addChildNode:_cubeRotationY];
// Offset Cube Handle position
_cubeHandle.position = SCNVector3Make(_cubeHandle.position.x, _cubeHandle.position.y, _cubeHandle.position.z-50);
//
// Add to Main Scene
//
[_scene.rootNode addChildNode:_cubeHandle];
}
# pragma mark -Lighting Elements
-(void)setupBasicLightingNodeLight
{
/* Element Hierarchy:
lightGroupNode
|_ spotLight01Node
|_ spotLight02Node
|_ spotLight03Node
|_ spotLight04Node
*/
// Model file with Lights
SCNScene *modelScene = [SCNScene sceneNamed:@"Cube.scn" inDirectory:@"assets.scnassets/_models" options:nil];
// Light Group Container
SCNNode *_lightGroupNode = [SCNNode node];
_lightGroupNode.position = SCNVector3Make(0, 300, 0);
// SpotLight Node 01
SCNNode *_spotLight01Node = [modelScene.rootNode childNodeWithName:@"cubeSpotLight01" recursively:YES];
// SpotLight Node 02
SCNNode *_spotLight02Node = [modelScene.rootNode childNodeWithName:@"cubeSpotLight02" recursively:YES];
// SpotLight Node 03
SCNNode *_spotLight03Node = [modelScene.rootNode childNodeWithName:@"cubeSpotLight03" recursively:YES];
// SpotLight Node 04
SCNNode *_spotLight04Node = [modelScene.rootNode childNodeWithName:@"cubeSpotLight04" recursively:YES];
// Configure Element Hierarchy
[_lightGroupNode addChildNode:_spotLight01Node];
[_lightGroupNode addChildNode:_spotLight02Node];
[_lightGroupNode addChildNode:_spotLight03Node];
[_lightGroupNode addChildNode:_spotLight04Node];
//
// Add to Main Camera
//
[_cameraHandle addChildNode:_lightGroupNode];
}
# pragma mark -Camera Elements
-(void)setupCameraNodeCamera
{
/* Element Hierarchy:
cameraHandle
|_ cameraOrientation
|_ cameraNode */
// Attach camera from cube model file
SCNScene *cubeModelScene = [SCNScene sceneNamed:@"Cube.scn" inDirectory:@"assets.scnassets/_models" options:nil];
SCNNode *_cameraNode = [cubeModelScene.rootNode childNodeWithName:@"cubeCamera01" recursively:YES];
SCNNode *_cameraOrientation = [SCNNode node];
_cameraHandle = [SCNNode node];
// Configure Element Hierarchy
[_cameraOrientation addChildNode:_cameraNode];
[_cameraHandle addChildNode:_cameraOrientation];
//
// Add to Main Scene
//
[_scene.rootNode addChildNode:_cameraHandle];
}
@end
Solution 1:[1]
In some cases, a light beam from the spotlight may not hit the model (due to its position, orientation, narrow light angle or soft edge). Therefore, use the default lighting to quickly turn the light on for the entire scene.
SCNView *sceneView = (SCNView *)self.view;
sceneView.autoenablesDefaultLighting = YES;
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 |
