Showing posts with label create. Show all posts
Showing posts with label create. Show all posts

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

Tuesday, May 26, 2015

Using CocoaPods and setting up Facebook SDK 4.1 in an xcode workspace in iOS8

Cocoapods are the new external framework. They are easy to setup, easy to update and easy to incorporate... one you have done it a few times. The most common external framework people use is the FacebookSDK. I will walk you through the basics of setting up a xcode workspace that incorporates the new facebookSDK which are now three parts; 'FBSDKCoreKit', 'FBSDKLoginKit', and 'FBSDKShareKit'.

I will probably go through some basic facebook usage in another post... maybe.

First you need to setup cocoapods. If you have not done so already, go into a terminal from your launchpad/utilities and type;
 $ sudo gem install cocoapods
This should use your integrated Ruby and setup cocoapods so you can use them whenever you want. It will most likely ask for your computer password and give you a warning but that is normal. Your mac should then  install a bunch of files and you are off to the races.

Next you will need to setup a work location. To be clean, create a new folder for your new app.
$ mkdir NewApp
$ cd NewApp
$ touch Podfile

You should be in your new app folder and in that folder should be an empty file called Podfile. This file is the instructions for cocoapods on what pods you want installed for this particular app. Since we want the app to have the new facebook pods installed, open up the Podfile in whichever prefered editor you want and copy this in;

source 'https://github.com/CocoaPods/Specs.git'
xcodeproj 'NewApp'
platform :ios, '7.0'
#Facebook Stuff
pod 'FBSDKCoreKit', '~> 4.1'
pod 'FBSDKLoginKit', '~> 4.1'
pod 'FBSDKShareKit', '~> 4.1'

What this does; The first line is to tell Cocoapods where on the internet you wish to look for the pods. The second line says what project you want your pods to work with. The third line says what target you want to setup. The lines with a # at the beginning are just comments and the last three lines are the actual facebook pods you wish to install into your workspace. Close this up and open up xcode.

In xcode, create a new project called NewApp (or whatever you decided to call it in the other areas) and put it in the same directory as the Podfile. Now go back to your terminal session and type;
$pod install

If everything was done correctly, you should see some green command lines installing the facebook pods and then the folder should also have a new xcworkspace file. Open up that file and you should see both your project and and 'Pods' in your workspace directory in xcode.

And you are done!

Why do this? Well, not only can you copy and paste this into all future projects but you can also update the pods at any point by just typing 'pod install' or 'pod update'. You can also create your own local pods and do the same thing. The way to do that is just specify a local directory for the pod you wish to incorporate;


#pod 'D2TSounds', :path => '/Users/yourname/Documents/CocoaPods/D2TSounds'

You might need to do a little setup for the local files but if you have several items that you use a lot, this will save you a lot of time in the long run.

Helpful links;
Cocoapods initial setup
Cocoapod pod link page
Facebook Login
Facebook Sharing
Facebook App Links
Facebook Graph API