DGArt Scenekit Playgrounds mini Game Tutorial 3
Create Moving Stars : iPadOS
The example Star was modeled using DGArt and used in the Playground app. And the 3D scene contains a "Star" node.
Import the .scn file into the Playgrounds app using the following steps:
- Obtain the Star.scn file here for practicing the exercise mentioned below. Download Star.zip
- In the Playgrounds app, tap on "Insert from..." in the side menu.
- Navigate to File Locations, and select Star.scn.
- You will see Star.scn appear under Resources.
Edit the ContentView code as shown below:
// Sample provide from www.nitrio.com for DGArt iPad
import SwiftUI
import SceneKit
struct ContentView: View {
var body: some View {
VStack {
GameViewControllerRepresentable()
VStack {
Text("DGArt Mini Game | Star")
}.padding(10)
}
}
}
struct GameViewControllerRepresentable: UIViewControllerRepresentable {
func makeUIViewController (context: Context) -> GameViewController {
GameViewController ()
}
func updateUIViewController(_ uiViewController: GameViewController, context: Context) {
}
}
Edit the GameViewController code as shown below:
// Sample provide from www.nitrio.com for DGArt iPad
import UIKit
import SceneKit
public class GameViewController: UIViewController {
private var scene: SCNScene!
private var sceneView = SCNView()
private var screenSize = CGSize(width: 500, height: 1200)
public override func viewDidLoad() {
super.viewDidLoad ()
setup()
}
func setup () {
sceneView.frame = CGRect (origin: .zero, size: screenSize)
scene = SCNScene(named: "Star.scn")
// set scene background color
scene?.background.contents = UIColor.init(red: 0.1, green: 0.1, blue: 0.2, alpha: 1.0)
// load star
let star = (scene?.rootNode.childNode(withName: "Star", recursively: true))!
star.position = SCNVector3(x: 0, y: -10, z: -5)
star.scale = SCNVector3(x: 0.4, y: 0.4, z: 0.4)
// clone star
let star1 = star.clone()
scene?.rootNode.addChildNode(star1)
let star2 = star.clone()
scene?.rootNode.addChildNode(star2)
let star3 = star.clone()
scene?.rootNode.addChildNode(star3)
let star4 = star.clone()
scene?.rootNode.addChildNode(star4)
// gen and animate Stars
scene?.rootNode.childNodes.filter({ $0.name!.contains("Star") }).forEach({
self.genStarLocation($0)
})
// Add Ground
let box = SCNBox(width: 50, height: 0.1, length: 50, chamferRadius: 0)
box.materials[0].diffuse.contents = UIColor.black
let boxNode = SCNNode(geometry: box)
boxNode.position = SCNVector3(0, -3, 0)
scene?.rootNode.addChildNode(boxNode)
// Add Light
let myLight = SCNNode()
myLight.light = SCNLight()
myLight.position = SCNVector3(x: -5, y: 10, z: 10)
myLight.look(at: SCNVector3(x: 0, y: 0, z: 0))
myLight.light?.intensity = 800
myLight.light?.type = SCNLight.LightType.directional
myLight.light?.color = UIColor.white
myLight.light?.castsShadow = true
scene?.rootNode.addChildNode(myLight)
// Add Camera
let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
cameraNode.camera?.usesOrthographicProjection = true
cameraNode.camera?.orthographicScale = 8
cameraNode.position = SCNVector3(x: 0, y: 10, z: 15)
cameraNode.look(at: SCNVector3(x: 0, y: 0, z: 0))
sceneView.scene = scene
sceneView.pointOfView = cameraNode
sceneView.autoenablesDefaultLighting = true
//sceneView.showsStatistics = true
//sceneView.allowsCameraControl = true
//sceneView.debugOptions = [.showWireframe, .showBoundingBoxes, .showPhysicsShapes]
view.addSubview(sceneView)
}
// animate and reposition stars after move from -z to z
func genStarLocation(_ snode: SCNNode) {
let rand1 = randomNum(min: -3, max: 3)
let rand2 = randomNum(min: -5, max: 5)
let randTime = randomNum(min: 0, max: 5)
let moveAct = SCNAction.sequence([
SCNAction.move(to: SCNVector3(rand1, 0.0, -25.0+rand2), duration: 0.2),
SCNAction.move(to: SCNVector3(rand1, 0.0, 20.0+rand2), duration: 6.0),
SCNAction.move(to: SCNVector3(rand1, -10.0, 20.0+rand2), duration: 0.2),
SCNAction.move(to: SCNVector3(rand1, -10.0, -25.0+rand2), duration: 0.2),
SCNAction.move(to: SCNVector3(rand1, 0.0, -25.0+rand2), duration: 0.2),
SCNAction.wait(duration: randTime),
])
snode.runAction(moveAct, completionHandler: {
self.genStarLocation(snode)
})
}
func randomNum(min:Int, max:Int) -> CGFloat {
let randomPosition = Int.random(in: min...max)
return CGFloat(randomPosition)
}
}
Finally, you will be able to see the star moving animation appear beside.
Alternatively, you can obtain the playground file here for exercise mentioned above. Download miniGameStar.swiftpm.zip