On iOS SceneKit 3D engine

Source: Internet
Author: User

Recently, we've been collecting data. 3D finally decided to lock the OC frame, find the learning materials and then slowly organize

SceneKit is a OC frame, before you begin, familiarize yourself with the three-dimensional coordinate system of SceneKit:

It is clear that the coordinate system in SceneKit is the right-hand coordinate system (Cartesian coordinate system), if you need to share data with other 3D frames, first understand whether the frame is a right-handed or left-handed coordinate system. In fact, it is also very good transformation, is the z axis of the positive or negative is not the same.

Before you begin your development, be sure to understand the following very important classes

Class/protocol

Describe

Scnview &Scnscenerenderer

Similar to UIView, used to display the contents of the SceneKit, defines a number of proxy methods that can be added to UIView with the Addsubview method

Scnscene

A container for SceneKit content. You can load a scene from a. Dae file generated by the 3D modeling tool, or create one with code and display it on the view

Scnnode

A basic building block of a scene where you can attach cameras, lights, and geometry to a node

Scngeometry



A three-dimensional object that can be connected to a node. A geometry (sometimes called a model or mesh) defines only the shape of a visible object. To define the surface color pattern of an object, you will need to attach material to the geometry. Then the material is mapped, or colored, and the surface of the geometry is colored, or patterned.

You can load a geometry from a. Dae file generated by the 3D modeling tool, or you can create it in code, SCENEKIT provides several common geometries, which are subclasses of scngeometry such as box, ball, cylindrical ball, etc. We'll write a demo that will give you the official geometry and give you an intuitive feel. Of course, we can also use three-dimensional coordinates, normal vector custom geometry, can also say a 2D pattern into a depth (thickness) of three-dimensional geometry. There should be a special article about using Bezier curves to transform a 2D pattern into a three-dimensional geometry with depth (thickness).

Scnmaterial

The material is also called a material ball because it is spherical in the 3D modeling tool. Color, the map depends on it all.

Scnlight

The light source can be attached to the node, providing shading in the rendered scene

Scncamera

A virtual camera can be attached to a node, providing a view of the scene.

To understand the above, let's start with the development, we can create a new Scenekit project in Xcode:

The results are as follows:

This new Scenekit project will have some sample code, and we'll pick some important notes:

[HTML]View PlainCopy 
    1. Scnscene *scene = [Scnscene scenenamed:@ "ART.SCNASSETS/SHIP.SCN"];


Load a scene from an SCN file, or load from a. Dae file

[HTML]View PlainCopy  
    1. Scnnode * Ship= [scene.rootnode childnodewithname:@ ' ship ' recursively:yes];


Get this scene in the plane this node

[HTML]View PlainCopy 
    1. [Ship runaction:[scnaction repeatactionforever:[scnaction rotatebyx:0 y:2 z:0 Duration:1]];

To keep the plane spinning around his y axis.

[HTML]View PlainCopy 
    1. Scnview.allowscameracontrol = YES;


Allows the user to control the camera, with the following sentence to add a gesture code, the user can zoom in, zoom out, 360 degrees to rotate the entire scene

[HTML]View PlainCopy 
    1. Gesturerecognizers AddObjectsFromArray:scnView.gestureRecognizers];



[HTML]View PlainCopy 
    1. Scnview.showsstatistics = YES; Controls whether the statistics bar below is displayed




In the click Gesture:

[HTML]View PlainCopy 
    1. Check what nodes is tapped
    2. Cgpoint p = [Gesturerecognize Locationinview:scnview];
    3. Nsarray *hitresults = [Scnview hittest:p options:nil];


Similar to the ray detection, the detection of the two-dimensional point deep infinity, the node on the ray, return an array, can be used to click to select, the first data in the array is generally the node we need.

[HTML]View PlainCopy 
  1. [Scntransaction begin];
  2. [Scntransaction setanimationduration:0.5];
  3. On Completion-unhighlight
  4. [Scntransaction setcompletionblock:^{
  5. [Scntransaction begin];
  6. [Scntransaction setanimationduration:0.5];
  7. material.emission.contents = [Uicolor blackcolor];
  8. [Scntransaction commit];
  9. }];
  10. material.emission.contents = [Uicolor Redcolor];
  11. [Scntransaction commit];


An animated chunk in SceneKit, similar to an animated chunk in a UIKit.

On iOS SceneKit 3D engine

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.