IOS 3D SceneKit framework Demo analysis, iosscenekit

Source: Internet
Author: User

IOS 3D SceneKit framework Demo analysis, iosscenekit

Scene Kit is a 3D rendering framework provided by Apple to OS X developers in Cocoa.

Scene Kit is built on OpenGL and contains advanced engine features such as illumination, model, material, and camera. These components are object-oriented, you can use the familiar Objective-C or Swift language to write code. If you have used the earliest version of OpenGL, and there is no shader at that time, you can only use various underlying restricted API development. The Scene Kit is much better. For most of the requirements (even advanced features such as dynamic shadow), it is enough to use the upper-layer API provided by it for configuration.

Create a project:

 

 

 

In the demo, A scnasssets folder is added to a file that stores 3d models. open the file with the. scn suffix. This is a type of model file suffix recognized by xcode.

 

 

// Create a new scene // create a scenario scene SCNScene * scene = [SCNScene sceneNamed: @ "art. scnassets/ship. scn "]; // instantiate SCNcene. The scenario itself is invisible, the code to create and add an album to SceneView is used to create and configure a camera. The camera is in the position of the angle of view. Note that the camera is created with a SCNNode assigned the node attribute. Here you need to understand the role of node. In SceneKit, node is a key part. Node itself is also invisible, and its role is to node all components. For example, if a car, the car body and the steering wheel are both models, you can add the node of the steering wheel to the node of the car body model, so that when the car moves, the child node of the car model will also move together. The relative positions of the parts of the vehicle body remain unchanged. This greatly reduces the workload. In the Rendering scenario, sceneKit traverses all sub-nodes, and cameraNode sets the attribute camera, and add yourself to the rootNode of scene to work when scene is displayed. // create and add a camera to the scene SCNNode * cameraNode = [SCNNode node]; cameraNode. camera = [SCNCamera camera]; [scene. rootNode addChildNode: cameraNode]; // set camera // place the camera cameraNode. position = SCNVector3Make (0, 0, 15); // The code is used to create and configure the lighting effect. In 3d imaging, lighting is an important part. Lighting and shadows make objects more texture. There are four types of light. You can try it. // Create light to scene // create and add a light to the scene SCNNode * lightNode = [SCNNode node]; lightNode. light = [SCNLight light]; lightNode. light. type = SCNLightTypeOmni; lightNode. position = SCNVector3Make (0, 10, 10); [scene. rootNode addChildNode: lightNode]; // create and add an ambient light to the scene SCNNode * ambientLightNode = [SCNNode node]; ambientLightNode. light = [SCNLight light]; ambient LightNode. light. type = SCNLightTypeAmbient; ambientLightNode. light. color = [UIColor darkGrayColor]; [scene. rootNode addChildNode: ambientLightNode]; // obtain the airplane model. Note that this method recursively :( BOOL) indicates whether to query in the subnode. Node is a tree structure and returns the First @ "ship" node. // Retrieve the ship node SCNNode * ship = [scene. rootNode childNodeWithName: @ "ship" recursively: YES]; // let the plane fly around the Y axis. The animation here is SCNAction. encapsulation and usage are similar to the two-dimensional animation of UIVIew, which is quite convenient. // Animate the 3d object [ship runAction: [SCNAction repeatActionForever: [SCNAction rotateByX: 0 y: 2 z: 0 duration: 1]; // obtain the SCNView used for display, configure scene for scnView. // Retrieve the SCNView * scnView = (SCNView *) self. view; // set the scene to the view scnView. scene = scene; // the settings allow the user to control the camera and display the status. (fps, etc.) are generally used for debugging in development. // allows the user to manipulate the camera scnView. allowsCameraControl = YES; // show statistics such as fps and timing information scnView. showsStatistics = YES; // configure the view scnView. backgroundColor = [UIColor blackColor]; // Add a gesture // Dd a tap gesture recognizer runner * tapGesture = [[using alloc] initWithTarget: self action: @ selector (handleTap :)]; NSMutableArray * handle = [NSMutableArray array]; [using addObject: tapGesture]; [gestureRecognizers addObjectsFromArray: scnView. gestureRecognizers]; scnView. gestureRecognizers = gestureRecognizers; // This is the basic content of the official demo. A model is displayed and animated. It tells us that to make the model visible, the camera needs to simulate the angle of view, light and surrounding light display, and add it to the root node.

 

-(Void) handleTap :( UIGestureRecognizer *) gestureRecognize {// go to SCNView // retrieve the SCNView * scnView = (SCNView *) self. view; // find the corresponding node // check what nodes are tapped CGPoint 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 object SCNHitTestResult * result = [hitResults objectAtIndex: 0]; // get its material SCNMaterial * material = result. node. geometry. firstMaterial; // highlight it [SCNTransaction begin]; [SCNTransaction beg: 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] ;}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.