DGArt Smiley Box to Playground
Your browser does not support the video.
Using SceneKit in Swift Playground Demo 3 : iPadOS
Create your 3D model using DGArt and import it into the Playground App for your own app project. The example SmileyBox was modeled using DGArt and used in the Playground app. And the 3D scene contains a "Smiley" node and "Floor" node.
Make the smiley bounce while rotating using the SCNAction.group.
Import the .scn file into the Playgrounds app using the following steps:
Obtain the .scn file here for practicing the exercise mentioned below.
Download SmileyBox.zip
In the Playgrounds app, tap on "Insert from..." in the side menu.
Navigate to Locations \ On My iPad \ DGArt \ projects \ SmileyBox, and select SmileyBox.scn.
You will see SmileyBox.scn appear under Resources.
Edit the ContentView code as shown below:
import SwiftUI
import SceneKit
struct ContentView: View {
var scene: SCNScene? {
let scene = SCNScene(named: "SmileyBox.scn")
// set scene background color
scene?.background.contents = UIColor.init(red: 0.6, green: 0.1, blue: 0.6, alpha: 1.0)
let Smiley = scene?.rootNode.childNode(withName: "Smiley", recursively: true)
let moveUp = SCNAction.move(by: SCNVector3(0.0, 2.0, 0.0), duration: 0.8)
moveUp.timingMode = .easeOut
let moveDown = SCNAction.move(by: SCNVector3(0.0, -2.0, 0.0), duration: 0.8)
moveDown.timingMode = .easeIn
let moveMotion = SCNAction.sequence([moveUp, moveDown])
let rotation = SCNAction.rotateBy(x: 0, y: 1, z: 0, duration: 1.6)
let motion = SCNAction.group([moveMotion, rotation])
Smiley?.runAction(SCNAction.repeatForever(motion))
return scene
}
var cameraNode: SCNNode? {
let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
cameraNode.position = SCNVector3(x: 5, y: 5.5, z: 5)
cameraNode.look(at: SCNVector3(x: 0, y: 1, z: 0))
return cameraNode
}
var body: some View {
SceneView(
scene: scene,
pointOfView: cameraNode,
options: [
.allowsCameraControl,
.autoenablesDefaultLighting,
.temporalAntialiasingEnabled
]
)
}
}
Finally, you will be able to see the Smiley Box animation appear beside.
Alternatively, you can obtain the playground file here for exercise mentioned above. Download SceneKitSmileyBox.swiftpm.zip