SceneKit基本场景黑屏(目标C)

时间:2016-05-06 16:16:51

标签: objective-c 3d scenekit

我是SceneKit的新手。我正在尝试创建一个基本场景。我有模特,灯光和相机。但出于某种原因,我无法看到我的模特。我一直在寻找发生这种情况的原因,但我找不到任何东西。

任何方向赞赏。

我的设置如下:

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

0 个答案:

没有答案