Wednesday, July 22, 2015

How to make a 3D planet in xcode using SceneKit

For some reason I've always wanted to create a spinning 3D globe but never had the time or ability. I decided last weekend to see if I could find someone or some way to make one. I checked all the usual places but the few I found were either overly complicated, way out of date, or no longer worked. So I decided to put my hat in the ring. I thought this might also be a good way to play with scenekit since I have never touched it before.

Wow. It worked better than I could ever imagine. Here is how to make the whole world in a few steps and about 15 lines of code.

First create a new project using scene kit. I like objective-c but you can do whatever you want with the language. You should be able to run it right away and see an airplane that you can move around the screen in 3 dimensions. Don't worry about that.

Here is the quick code;

-(void)worldSetup{
    SCNView *scnView = (SCNView *)self.view;
    
    myScene = [[SCNScene alloc] init];
    
    SCNSphere *planetSphere = [SCNSphere sphereWithRadius:5.0];
    SCNNode *sphereNode = [SCNNode nodeWithGeometry:planetSphere];
    [myScene.rootNode addChildNode:sphereNode];

    
    SCNMaterial *corona = [SCNMaterial material];
    corona.diffuse.contents = [UIImage imageNamed:@"earth"];
    corona.specular.contents = [UIColor colorWithWhite:0.6 alpha:1.0];
    corona.shininess = 0.5;
    [planetSphere removeMaterialAtIndex:0];
    planetSphere.materials = @[corona];
    
    // create and add a light to the scene
    scnView.autoenablesDefaultLighting = YES;
    
    // create and add a camera to the scene
    scnView.allowsCameraControl = true;
    
    mySphere = planetSphere;
    
    scnView.scene = myScene;
}

earth
in celebration of seeing pluto,
I added some love

Here is the alternate slightly more custom code.

-(void)worldSetup{
    SCNView *scnView = (SCNView *)self.view;
    
    myScene = [[SCNScene alloc] init];
    
    SCNSphere *planetSphere = [SCNSphere sphereWithRadius:5.0];
    SCNNode *sphereNode = [SCNNode nodeWithGeometry:planetSphere];
    [myScene.rootNode addChildNode:sphereNode];

    
    SCNMaterial *corona = [SCNMaterial material];
    corona.diffuse.contents = [UIImage imageNamed:@"earth"];
    corona.specular.contents = [UIColor colorWithWhite:0.6 alpha:1.0];
    corona.shininess = 0.5;
    [planetSphere removeMaterialAtIndex:0];
    planetSphere.materials = @[corona];
    
    // create and add a light to the scene
    SCNNode *lightNode = [SCNNode node];
    lightNode.light = [SCNLight light];
    lightNode.light.type = SCNLightTypeAmbient;
    lightNode.light.color = [UIColor colorWithWhite:0.37 alpha:1.0];
    lightNode.position = SCNVector3Make(0, 50, 50);
    [myScene.rootNode addChildNode:lightNode];
    
    SCNNode *omniLightNode = [SCNNode node];
    omniLightNode.light = [SCNLight light];
    omniLightNode.light.type = SCNLightTypeOmni;
    omniLightNode.light.color = [UIColor colorWithWhite:.70 alpha:1.0];
    omniLightNode.position = SCNVector3Make(0, 40, 40);
    [myScene.rootNode addChildNode:omniLightNode];
    
    // create and add a camera to the scene
    scnView.allowsCameraControl = true;
    
    // create and add a camera to the scene
    SCNNode *cameraNode = [SCNNode node];
    cameraNode.camera = [SCNCamera camera];
    cameraNode.position = SCNVector3Make(0, 0, 20);
    [myScene.rootNode addChildNode:cameraNode];
    
    mySphere = planetSphere;
    
    scnView.scene = myScene;
}

You still need to get an 'earth' texture from the internet somewhere, should be easy to find, probably from NASA. And that is it!

Here is a link to the app in github. https://github.com/Darkin/3DPlanet

2 comments:

  1. Hello, would you be willing to share this project? I'm trying to learn SceneKit and I'd love to play with this project. Thanks!

    ReplyDelete
    Replies
    1. Forgot to add the link, here you go https://github.com/Darkin/3DPlanet

      Delete