Scene Kit is a 3D rendering framework that Apple provides to OS X developers for Cocoa.
Scene Kit is built on OpenGL and includes advanced engine features such as lighting, models, materials, cameras, and so on, which are object-oriented and you can write code in a familiar objective-c or Swift language. If you have used the earliest version of OpenGL, there is no shader at that time, only to use a variety of low-level limited API development. Scene Kit is a lot better, and for most requirements (even advanced features like dynamic shadows), it's enough to use the upper API that it provides.
Create a project:
In the demo more than the average file a scnasssets folder, this is the folder to store the 3d model, open can see the. scn suffix file, this is the model file suffix Xcode identified
//Create a new scene//Create a sceneScnscene *scene = [Scnscene scenenamed:@"ART.SCNASSETS/SHIP.SCN"]; //instantiating Scncene, the scene itself is not visible and needs to be added to the Sceneview scene//Create and add an album to scene//This code is used to create and configure the camera. The position the camera is in is where the view is viewed. Note that creating a camera here creates a scnnode that assigns the properties of node. Here's how node works. In Scenekit, node is a very critical part. Node itself is also invisible, and its role is to separate parts. For example, a car, body and steering wheel are models, you can add the steering wheel node on the body model node, so when the car moved, the model of the child node will also move together. The relative position between the parts of the body is constant. This can greatly save effort. When rendering the scene, Scenekit will traverse all the sub-node,cameranode to set the property camera and add itself to the scene's rootnode to function when the scene is displayed.//Create and add a camera to the sceneScnnode *cameranode =[Scnnode node]; Cameranode.camera=[Scncamera camera]; [Scene.rootnode Addchildnode:cameranode]; //set up camera//Place the cameraCameranode.position = Scnvector3make (0,0, the); //The code is used to create and configure lighting effects. In 3d imaging, lighting is an important part of the process. Lights and shadows can make objects more textured. There are four types of light, which you can try. //Create light to scene//Create and add a light to the sceneScnnode *lightnode =[Scnnode node]; Lightnode.light=[Scnlight Light]; LightNode.light.type=Scnlighttypeomni; Lightnode.position= Scnvector3make (0,Ten,Ten); [Scene.rootnode Addchildnode:lightnode]; //Create and add an ambient light to the sceneScnnode *ambientlightnode =[Scnnode node]; Ambientlightnode.light=[Scnlight Light]; AmbientLightNode.light.type=scnlighttypeambient; AmbientLightNode.light.color=[Uicolor Darkgraycolor]; [Scene.rootnode Addchildnode:ambientlightnode]; //get the airplane model. Note that this method recursively: (BOOL) means whether to query in child node. node is a tree structure that returns the first encountered @ "Ship" node. //retrieve the ship nodeScnnode *ship = [Scene.rootnode childnodewithname:@" Ship"Recursively:yes]; //let the plane fly around the y axis. The animations here are scnaction, and the encapsulation and use methods are similar to UIView's two-dimensional animations and are quite handy. //Animate the 3d object[Ship Runaction:[scnaction repeatactionforever:[scnaction Rotatebyx:0Y:2Z:0Duration1]]]; //get the Scnview for display and configure the scene for Scnview. //Retrieve the ScnviewScnview *scnview = (Scnview *) Self.view; //set the scene to the viewScnview.scene =scene; //settings allow the user to control the camera, display state, (FPS, etc.) are generally used as debugging in development//allows the user to manipulate the cameraScnview.allowscameracontrol =YES; //show statistics such as FPS and timing informationScnview.showsstatistics =YES; //Configure the ViewScnview.backgroundcolor =[Uicolor Blackcolor]; //Add gestures//Add a tap gesture recognizerUITapGestureRecognizer *tapgesture =[[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (Handletap:)]; Nsmutablearray*gesturerecognizers =[Nsmutablearray array]; [Gesturerecognizers Addobject:tapgesture]; [Gesturerecognizers AddObjectsFromArray:scnView.gestureRecognizers]; Scnview.gesturerecognizers=gesturerecognizers; //This is the basic content of the official demo. Implements the display and animation of a model. It tells us that the model wants to become visible, requires a camera analog view, lights and surrounding lights to be added to the root node.
- (void) Handletap: (uigesturerecognizer*) gesturerecognize{//go to Scnview.//Retrieve the ScnviewScnview *scnview = (Scnview *) Self.view; //find the corresponding node//check what nodes is tappedCgpoint p =[Gesturerecognize Locationinview:scnview]; Nsarray*hitresults =[Scnview hittest:p Options:nil]; //check that we clicked on at least one object if([Hitresults Count] >0){ //retrieved the first clicked objectScnhittestresult *result = [Hitresults objectatindex:0]; //get its materialScnmaterial *material =result.node.geometry.firstMaterial; //Highlight It[scntransaction begin]; [Scntransaction setanimationduration:0.5]; //On completion-unhighlight[Scntransaction setcompletionblock:^{[scntransaction begin]; [Scntransaction setanimationduration:0.5]; Material.emission.contents=[Uicolor Blackcolor]; [Scntransaction commit]; }]; Material.emission.contents=[Uicolor Redcolor]; [Scntransaction commit]; }}
IOS Scenekit Framework Demo Analysis