nitrio DGArt

DGArt Trophy to Playground

Using SceneKit in Swift Playground Demo 2 : iPadOS

Create your 3D model using DGArt and import it into the Playground App for your own app project. The example Trophy was modeled using DGArt and used in the Playground app. And the 3D scene contains a "Trophy" node and multiple "Star" node.

Make the Trophy rotate, and have all the stars rotate around the Trophy.

Import the .scn file into the Playgrounds app using the following steps: Edit the ContentView code as shown below:

import SwiftUI
import SceneKit

struct ContentView: View {
    var scene: SCNScene? {
        let scene = SCNScene(named: "Trophy.scn")
        // set scene background color
        scene?.background.contents = UIColor.init(red: 0.1, green: 0.1, blue: 0.2, alpha: 1.0)
        
        let Trophy = scene?.rootNode.childNode(withName: "Trophy", recursively: true)
        // rotate Trophy
        Trophy?.runAction(SCNAction.repeatForever(SCNAction.rotateBy(x: 0, y: 1, z: 0, duration: 2)))
        
        scene?.rootNode.childNodes.filter({ $0.name == "Star" }).forEach({ 
            let randomDouble = Double.random(in: 0.1...1)
            // rotate all Star
            $0.runAction(SCNAction.repeatForever(SCNAction.rotateBy(x: randomDouble, y: randomDouble, z: randomDouble, duration: randomDouble)))
            // add all Star as Trophy child
            Trophy?.addChildNode($0)
        })
        
        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 Trophy animation appear beside.

Alternatively, you can obtain the playground file here for exercise mentioned above. Download SceneKitTrophy.swiftpm.zip