ios draw on 3d object scenekit

Take you ever wanted to make your own video game? Or maybe you lot desire to brand your iOS app stand out past adding beautiful 3D graphics? The world of 3D graphics programming can be intimidating. Between shaders, samplers, mipmaps and tessellation, it's difficult to know where to showtime. Fortunately, on iOS, you can hit the ground running with SceneKit, Apple'due south high-level API for 3D programming!

SceneKit handles a lot of the boring work of 3D programming for y'all, allowing yous to concentrate on the content that makes your game or app unique. In low-level APIs like Metal, y'all're left to grapple with physics and mathematics. SceneKit, on the other hand, abstracts abroad a lot of this complication, making information technology easy to program your scene in lawmaking.

Alternatively, you tin can use Xcode's powerful Scene Editor to lay out scenes, similar to using Interface Builder for storyboards. What'south more than, SceneKit integrates smoothly with everything you might need to create your blockbuster video game: Metallic, GameplayKit, Model I/O and more!

In this tutorial, you'll propel a bland 2D solar system app into an interactive 3D world using SceneKit. Along the way, yous'll learn about:

  • The fundamentals of 3D programming.
  • Creating and modifying scenes in code or in the Scene Editor.
  • Nodes, geometries and materials.
  • How lighting determines the look of your scene.
  • Changing what the user sees with cameras and constraints.

Get gear up to step into the world of 3D graphics!

Getting Started

Download the starter projection by clicking the Download Materials push button at the pinnacle or bottom of the tutorial.

Open the starter project in Xcode, and then build and run.

Solar Scenes starter

Solar Scenes is a cute, informative app designed to teach people facts near the solar organisation they live in. At least, information technology will be when you're washed with information technology.

Right now, it's missing the beautiful part. Information technology's your job to add it. :]

Solar Scenes covers only five planets and so far:

  • Mercury
  • Venus
  • Earth
  • Mars
  • Saturn

When you're finished with this tutorial, y'all'll be ready to add the missing planets — and maybe a few extra creations of your own!

Exploring Solar Scenes

In Xcode, take a look at the construction of the app in the Projection navigator.

Project navigator

Here's what each group does:

  • Information Model: Hosts the different planets and some facts about them.
  • UI: Contains diverse SwiftUI controls that style the current UI.
  • ContentView.swift: With help from ViewModel.swift, this is the trunk of the app., Here, you'll load the SceneKit scene that makes the app come to life.

Creating Your First Scene

To create your scene, you'll apply Xcode'due south Scene Editor to manage a scene file. In Xcode's menu bar, select File ▸ New ▸ File… and select the SceneKit Scene File template.

New file dialog

Click Side by side, and proper noun the file Solar Scene.

New file name

Then, click Create. Xcode's Scene Editor will load your starting time bare scene file.

Scene editor

If you don't see the camera node listed on the left, open the Screen graph by selecting the Scene Graph View button in the lower-left corner of the scene:

Scene graph button in Xcode

SceneKit uses scene graphs to organize scenes as a tree of nodes. The scene graph starts with a root node, and you add whatsoever content for your scene every bit child nodes under that root node.

A node is nothing more a location; information technology has no beliefs or appearance. Despite this, nodes are the heart of your scene. To add annihilation to your scene, you'll need to adhere it to a node.

Your scene graph already comes with ane child node, which has one attachment:

Camera node

The photographic camera node has a camera object attached to it. You lot tin accept every bit many cameras in your scene equally you desire, just y'all'll demand at least one to see anything.

Loading a Scene

To come across your scene in action inside the app, you'll demand to create a SceneView. Open ContentView.swift.

First, import SceneKit at the acme of the file:

import SceneKit        

Next, add this within ContentView:

// 1 static func makeScene() -> SCNScene? {   let scene = SCNScene(named: "Solar Scene.scn")   return scene }  // 2 var scene = makeScene()        

In the code above, y'all:

  1. Create your scene from your scene file.
  2. Call makeScene() to load your scene.

Now, you lot need to get a reference to your camera node. Beneath trunk, just before the endmost brace of ContentView, add this:

func setUpCamera(planet: Planet?) -> SCNNode? {   let cameraNode = scene?.rootNode     .childNode(withName: "camera", recursively: false)   return cameraNode }        

You lot'll expand on this subsequently. For now, setUpCamera(planet:) simply grabs a reference to your camera node.

Finally, you're set up to create your scene view. Well-nigh the start of body, remove ColorPalette.secondary and its view modifier. Then, add this:

SceneView(   // i   scene: scene,   // ii   pointOfView: setUpCamera(planet: viewModel.selectedPlanet),   // iii   options: [.allowsCameraControl] ) // 4 .background(ColorPalette.secondary) .edgesIgnoringSafeArea(.all)        

You've replaced the blank background of the app with a SceneView. Here's what's happening, step past pace:

  1. Cull the scene for the view to display.
  2. The scene view's pointOfView is the photographic camera that volition brandish the scene. Some games bandy between multiple cameras by using this property to change the current bespeak of view.
  3. You can control SceneView's beliefs using a set up of options. Hither, .allowsCameraControl lets the user manipulate the camera.
  4. Set the scene view'south background colour, which you lot'll run across while the scene loads, and stretch the scene beyond the entire window.

At concluding, you lot're set up to see your first scene! Build and run.

Running empty scene

It's not much, but information technology's pretty impressive considering how little code you lot've written so far. Solar Scene.scn provides a default background and camera, and your scene view'southward allowsCameraControl option lets y'all gyre and zoom around the scene freely. All-time of all, you accomplished this without having to write a single line of code.

Right now, yous have a camera, but zilch to see. Next, you'll add a sun to test your scene.

Adding Objects

Open up Solar Scene.scn. At the superlative correct of the Scene Editor, click the plus (+) button to access the Object Library.

Object library

Notice the Sphere object and drag information technology into your scene graph, simply beneath the camera node.

Sphere object

Note: Dragging objects into the scene graph tin be catchy. Endeavour selecting camera before dragging if your new sphere won't stick.

With sphere selected, click it and change its name to dominicus. If yous tin't see it in the scene editor, zoom in or out to to become a good view of your dominicus:

Sun in scene editor

It doesn't look like much. Right now, your dominicus is a simple node with a geometry object fastened.

Recall, nodes take no appearance or behavior by default. SceneKit includes a variety of geometric shapes that you tin can apply to give nodes an appearance, only you're not express. Using Metallic or an external 3D modeling tool, you can create your own custom geometries.

While you lot've fabricated a sphere, it doesn't wait much like a dominicus. And so, to give your dominicus a more fitting appearance, you'll modify its fabric.

Modifying Materials

On the right of the Scene Editor, in the Inspectors panel, select the Materials inspector.

Material inspector

An object'due south material is a set up of properties that, when combined with the material'due south lighting model, determine how to render each pixel of a geometry. Here, you tin change the properties that determine the colour of the sun'due south sphere geometry.

Adjacent, click the Diffuse color to bring up the Colour picker. Navigate to the Sliders tab, so set the drop-down to RGB Sliders. Finally, prepare the Hex Color # to #F2FF2C.

You tin can think of a material's diffuse as the "base colour" of an object.

Adjacent, set the color of Illumination to white: #FFFFFF. A fabric's illumination lets it define how light hits the object. Even if the geometry is obscured from a light source, setting illumination causes the material to color itself every bit if it were receiving low-cal.

Build and run.

Running the scene, but can't see the sun.

If you don't see anything, pan the photographic camera until your sphere comes into view.

The sun comes into view

Behold, the center of our solar system: the dominicus!

Nevertheless, information technology's a little dainty at the moment. Side by side, y'all'll go far bigger to provide some scale for the other planets.

In the Inspectors panel, click the Attributes inspector. Here, you tin control a diversity of backdrop for the node and any of its attachments, like a sphere'southward radius.

The sphere's radius property

Change the radius to ten, which will increase the sphere'southward size tenfold. Build and run.

No sun, again

Wait, where'd it go? Panning the camera won't aid this time. You lot've increased the size of the sun, and it's engulfed the camera. You'll need to motion the camera to a condom altitude.

Setting Upwards the Camera Node

In the scene graph, click the camera node. Then, in the Inspectors panel, click the Node inspector.

The Node inspector

Hither, you can fix the position, shape and orientation of the node. How y'all orient the node will affect anything attached to that node. And so, changing the position of the camera node will alter the position of the photographic camera attached to it.

Every node in your scene has the following properties, which the Node inspector tin edit:

  • Identity: The node's name, used to access the node in code.
  • Position: Where the node is placed in the scene relative to its parent.
  • Euler: The rotation of the node relative to its parent.
  • Scale: Allows you lot to transform the node, transforming its size forth each axis.

To correctly reposition the photographic camera, change Position to ten: -55, y: 65 and z: -68.

Then, change Euler to x: 145, y: -13 and z: -158. This orients the camera to point at your sun. It also creates an empty space that your planets will fill.

Now, your transforms will look like this:

Transforms in Node inspector after update

Zoom out of the Scene Editor until y'all tin can clearly run across the photographic camera and the dominicus.

Camera and sun inside scene editor

Your camera is pointing at your scene, but its viewing depth isn't far plenty to see your sun. In the Inspector panel, switch to the Attributes inspector. Here, you lot'll detect the Z Clipping properties, which let yous increase the viewing depth.

Under Z Clipping, change Far to 300.

Z clipping property

Build and run.

The sun in the running scene

At present, you tin meet the sun, along with plenty of space for more planets!

Creating Planets

Next, you'll add five planets to your solar system:

  1. Mercury
  2. Venus
  3. World
  4. Mars
  5. Saturn

Each is a sphere, like the dominicus. Y'all'll change details like each planet'south color, size and position.

Note: Most of the post-obit steps repeat what you did to create the sun. If you demand a refresher, wait dorsum at the Adding Objects section.

Mercury

First, add together a new sphere geometry from the Objects Library.

Then, in the Node inspector, change the Name to mercury. Change the Position to x: 0, y: 0 and z: 25.

Node inspector for mercury

In the Material inspector, modify the Diffuse to #BBBBBB past using the color picker. So, modify Roughness to ane. By irresolute a material'due south roughness, you can make it more or less shiny. Setting a roughness close to 0 makes it shiny and setting a roughness close to ane makes it less reflective.

Build and run.

Mercury in the running app

SceneKit renders your first planet, Mercury, close to the sun. Next, you'll add the rest of the planets in gild, post-obit the steps you followed for Mercury.

Venus

To create Venus, do the post-obit:

  1. Add a sphere to the scene graph.
  2. In the Node inspector, modify Name to venus. Change Position's z value to 35.
  3. In the Attributes inspector, alter Radius to 2.
  4. In the Fabric inspector, change Diffuse to #59B1D6 and Roughness to 1.

World

For the planet we call home, follow these steps:

  1. Add a sphere to the scene graph.
  2. In the Node inspector, change Proper name to earth. Change Position'southward z value to l.
  3. In the Attributes inspector, change Radius to 2.
  4. In the Material inspector, alter Lengthened to #2F5CD6 and Roughness to ane.

Mars

Next, brand these changes for the ruby-red planet:

  1. Add a sphere to the scene graph.
  2. In the Node inspector, modify Name to mars. Change Position's z to 75.
  3. In the Material inspector, alter Lengthened to #C65B2C and Roughness to ane.

Build and run.

The four planets in the scene editor

Your solar system is taking shape, but it's nonetheless a bit bland. For some variation, yous'll add everyone's favorite gas giant: Saturn!

Adding Saturn'southward Ring

Saturn follows a like pattern to the previous planets, merely you'll take it one pace further: Saturn needs a band.

First, create the body of the planet, but every bit yous did for the previous planets:

  1. Add a sphere to the scene graph.
  2. In the Node inspector, modify Proper noun to saturn. Change Position'due south z to 150.
  3. In the Attributes inspector, change Radius to v.
  4. In the Material inspector, modify Name to saturn, Lengthened to #D69D5F and Roughness to 1.

Notation: Don't forget the prepare Name in both the Node inspector and the Fabric inspector this time. You'll use information technology after.

Pan the Scene Editor to take a closer look at your newest planet.

Saturn in scene editor

Saturn is missing its trademark band. To add the band, you'll utilise another of SceneKit's primitive shapes: a tube.

Adding the Ring

From the Object Library, find Tube and add it to the scene graph. Then, drag the tube then it nests under saturn, similar and then:

The tube as a child of Saturn in the scene graph

While all the planets are child nodes of the scene's root node, you've added the tube as a child node of saturn. Now, you tin position and breathing the tube relative to its parent.

Think of this every bit adding car seats every bit children to a auto node: if the car moves, the kid nodes go along for the ride.

In the Node inspector, change the tube'south Position to x: 0, y: 0 and z: 0. This will middle it on Saturn's position.

Because it'south smaller than Saturn'south sphere, you won't be able to run across the tube. To size the tube appropriately, open the Attributes inspector. Then, make the following changes:

  • Inner radius: seven
  • Outer radius: ix
  • Pinnacle: 0.1

Sizing properties in Saturn's Attributes inspector

Now, your ring is positioned correctly, but it doesn't match the rest of the planet. You could modify the material to match, or you could reuse the material you already styled for Saturn.

Reusing Material

When you lot're making a large-scale scene, the ability to reuse objects is critical. In SceneKit, you lot tin can reuse nodes, geometries and materials. That fashion, if something has to change beyond the scene, yous'll just need to make the change once.

Right now, if you wanted to change Saturn'south color or anything else related to its cloth, you'd have to duplicate the changes in both the body of the planet and its ring. Past sharing a cloth object, the planet and its ring volition share the same appearance.

With the tube selected in the scene graph, open the Textile inspector. At the top, yous'll find a collection of materials.

Materials collection for the tube

For Saturn's ring, you lot only want i fabric that matches that of the planet's body. Then, click the minus (—) button to remove the tube'southward material.

Empty materials collection

Then, click the plus (+) button to bring up the materials picker.

Materials picker

Find and click the saturn material, and then click Done.

The Saturn material in the tube's Material inspector

Now, Saturn's ring shares the same material as the planet'due south body.

Build and run to see all the planets.

All planets in the running app

Your planets wait great, but something's missing. Your scene has calorie-free, but it looks a scrap off — at that place's no light coming from the sun. Next, you'll set up that past calculation a light to the dominicus node.

Attaching a Light

When you create a scene from a scene file, as y'all did with Solar Scene.scn, SceneKit gives yous a running outset past giving y'all a default photographic camera, calorie-free and groundwork. All you demand to do is add together your geometry.

In the scene graph, right-click the sun node.

Right-click context menu on the sun node

In this carte, you can choose between a collection of attachments: lights, cameras, physics bodies and more than. For now, click Add Light.

Light attachment on the sun node

That picayune sun icon next to the sunday node (how appropriate!) indicates the node has a lite attachment. Similarly, the cube icon indicates a geometry attachment.

Click the sun node. Then, in the Inspectors panel, click the Attributes inspector.

Properties for the sun's light in the Attributes inspector

Earlier, you lot merely had properties applicable to the attached geometry, only now y'all as well have a new section for the attached lite.

First, change the light's Blazon to Omni, short for "omnidirectional". This is a light type that shines in all directions. And so, change the Intensity to 20000. It is the sunday, after all! :]

Build and run.

Sunlight in the running app

After that change, your whole solar system is basking in the sun'southward warm glow.

You've gone as far as y'all need to in Solar Scene.scn. To really unlock the power of SceneKit, it's fourth dimension to start adding to your scene in your Swift code.

Adding Textures

Then far, you've colored each planet with a single, uncomplicated color. Earth looks a little bland as a outcome, despite existence known as the "blue planet." To kick things up a notch, you'll utilise a texture to each planet instead of a color.

Textures are images that you wrap around geometries, such as your planet spheres. Remember those various colour properties in the Textile inspector? You lot can apply texture images to each of those backdrop as well.

In Xcode, open up Assets.xcassets and expect at the planets group.

Planet texture maps in the assets catalog

Each planet has an artificial map of its surface. When creating your scene, you'll use these images to employ textures to each planet.

Replacing Colors with Textures

First, open ContentView.swift. Then, add this below makeScene():

static func applyTextures(to scene: SCNScene?) {   // 1   for planet in Planet.allCases {     // 2     permit identifier = planet.rawValue     // 3     permit node = scene?.rootNode       .childNode(withName: identifier, recursively: fake)      // Images courtesy of Solar System Scope https://bit.ly/3fAWUzi     // four     let texture = UIImage(named: identifier)      // v     node?.geometry?.firstMaterial?.lengthened.contents = texture   } }        

Here'southward what's happening, step past step:

  1. The Planet enumeration lists all planets in the app.
  2. Each planet'due south rawValue is the same as the identifiers y'all've been applying to the planets: mercury, venus and and then on.
  3. Using the identifier, grab a reference to the planet's node.
  4. The names of the textures in Avails.xcassets also friction match the planet identifiers. This creates a UIImage for the appropriate planet.
  5. Finally, set the image every bit the planet'south diffuse on the node's textile, thereby replacing the color that served as its base appearance.

To apply your textures, add this to makeScene(), correct before returning the scene:

applyTextures(to: scene)        

This calls applyTextures(to:) while creating your scene.

Build and run. And then, take a closer look at each planet:

The earth texture in the running app

That looks much amend. Textures are a not bad style to add together realism to your scene. Side by side, to actually become that "space" feeling, you'll add one more texture.

Using Skybox Textures

Your planets are in peachy shape, but looking at the rest of your scene, it doesn't look much like infinite. In Xcode, open up Assets.xcassets and look in the skybox grouping.

Skybox images in the assets catalog

In that location's not one, not 2, but half-dozen images of a starry sky. Perfect for the scene's background! But, why half dozen? Well, SceneKit can employ these half-dozen images for a common 3D programming technique known as a skybox. By using these images as the scene's background, SceneKit makes a cube out of the six images and essentially puts the unabridged scene in the cube.

At the lesser of applyTextures(to:), add together this:

// 1 let skyboxImages = (1...6).map { UIImage(named: "skybox\($0)") } // 2 scene?.background.contents = skyboxImages        

Here'southward what'south going on:

  1. Create an array of the six skybox images.
  2. Utilize those images every bit the scene's background contents.

SceneKit will handle the rest from here, as the framework knows to apply an assortment of vi images as a skybox.

Finally, build and run to see what those two lines of lawmaking tin can do.

The skybox applied in the running app

No matter how you scroll or pan the image, you're surrounded by a deep, starry sky. By using a skybox texture, you've shot your scene into outer space!

Your scene is finally taking shape. All that remains are some tweaks in how the app interfaces with SceneKit. For instance, the original app that shows planet info has no real link to the SceneKit scene. It'south fourth dimension to change that.

Working With the Camera

When yous select a different planet in Solar Scenes' planet switcher, it'd be cracking if the scene focused on the selected planet. At the moment, SceneKit controls the camera, but yous can motion it programmatically likewise. To focus the camera on the selected planet, y'all'll use a constraint.

Using Constraints

Similar to Automobile Layout constraints, SceneKit's SCNConstraint and its subclasses let you specify rules to apply to the position and orientation of any node.

For instance, SCNDistanceConstraint tin can force a node to maintain a specified distance from another node. Using SCNBillboardConstraint, you can force a node to face the camera, like a creepy painting that seems to follow you wherever yous stand up.

For the selected planet, you'll use SCNLookAtConstraint to strength the camera to look at the selected planet node. To shine the photographic camera's change in orientation, y'all'll animate it using an action.

Animating With Actions

Deportment are a method of performing elementary animations. By running an action, a node can smoothly modify its position and orientation, scale to a different size and more.

Next, you'll use an action to animate the photographic camera'southward position change when switching planets in Solar Scenes, and you'll utilize a constraint to focus on the planet.

Focusing on the Selected Planet

Starting time, add this to ContentView.swift just before the closing caryatid:

func planetNode(planet: Planet) -> SCNNode? {   scene?.rootNode.childNode(withName: planet.rawValue, recursively: false) }        

This grabs the planet node corresponding to a Planet model object.

Adjacent, notice setUpCamera(planet:). Add this before the return statement at the end:

// 1 if permit planetNode = planet.flatMap(planetNode(planet:)) {   // 2   allow constraint = SCNLookAtConstraint(target: planetNode)   cameraNode?.constraints = [constraint]   // 3   permit globalPosition = planetNode     .convertPosition(SCNVector3(x: 50, y: 10, z: 0), to: nil)   // iv   allow move = SCNAction.move(to: globalPosition, duration: 1.0)   cameraNode?.runAction(move) }        

Here's what'south happening:

  1. ContentView calls setUpCamera(planet:) when setting up the view trunk, passing the currently selected planet from the view model. Here, you're getting a reference to that planet'due south node using planetNode(planet:).
  2. Create an SCNLookAtConstraint, which focuses on planetNode, then employ information technology to cameraNode's constraints.
  3. Nodes define their own coordinate space. So, use convertPosition(_:to:) to convert a position that'southward close to the planet into the same position in the global coordinate space.
  4. Using the global position, create and run an activity to update cameraNode's position.

Finally, when the camera is focusing on a planet, you should disable SceneKit'southward automatic photographic camera control. Go to the initialization of SceneView in body. Observe options and replace it with this:

options: viewModel.selectedPlanet == nix ? [.allowsCameraControl] : []        

Hither, yous're removing automatic control of the camera if you selected a planet.

Build and run. Flip through the planets in the planet switcher and bask that smooth animation as the camera focuses on the selected planet.

The completed running app

Groovy piece of work!

Where to Go From Hither?

You can download the final projection by using the Download Materials push at the pinnacle or lesser of this page.

SceneKit is a powerful, yet accessible, entry point into 3D programming. If you desire to learn more about SceneKit, Apple'due south documentation and WWDC videos volition be your guide.

In this tutorial, you took a simple app and added a fully interactive 3D scene. You made visual representations for planets in the solar system rather than just using a evidently former second interface.

Next, why non add the missing planets to Solar Scenes? Or, you lot could bring the scene into the real world by supporting augmented reality. Await at our book, Apple Augmented Reality by Tutorials, to learn how.

If you lot have questions or comments, or if y'all'd like to share the cool scenes you're making in SceneKit, please join the word beneath.

nicholsonriculd.blogspot.com

Source: https://www.raywenderlich.com/23483920-scenekit-3d-programming-for-ios-getting-started

0 Response to "ios draw on 3d object scenekit"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel