nitrio DGArt

DGArt Smiley to Playground RealityKit

Using RealityKit in Swift Playground Demo 3 : iPadOS

Create your 3D model using DGArt and import it into the Playground App for your app project. The example Smiley was modeled using DGArt, exported into a USDZ file, and then incorporated into the Playground app. The 3D file features a "Smiley."

This demo loads the Smiley.usd file and allows users to touch and rotate it.

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

import SwiftUI
import RealityKit
// nitrio.com
// DGArt

struct ContentView: View {
    @State private var rotationY: Float = 0.0
    
    var body: some View {
        ARViewContainer(rotationY: $rotationY)
            .ignoresSafeArea()
    }
}

struct ARViewContainer: UIViewRepresentable {
    @Binding var rotationY: Float
    
    func makeUIView(context: Context) -> ARView {
        let arView = ARView(frame: .zero)
        
        // set background color
        arView.environment.background = .color(.init(red: 0.75, green: 0.1, blue: 0.5, alpha: 1))
        
        //load usdz model
        let modelEntity = try! ModelEntity.loadModel(named: "Smiley.usdz")
        modelEntity.name = "Smiley"
        //scaled down to 25% of its original size
        modelEntity.scale = SIMD3(x: 0.5, y: 0.5, z: 0.5)
        //sets different materials for each part of the loaded model
        modelEntity.model?.materials[0] = SimpleMaterial(color: .black, roughness: 1, isMetallic: true)
        modelEntity.model?.materials[1] = SimpleMaterial(color: .yellow, roughness: 1, isMetallic: true)
        
        let anchorEntity = AnchorEntity()
        //adding modelEntity as a child to the anchor means that its position and orientation are now relative to the anchor.
        anchorEntity.addChild(modelEntity)
        
        //anchorEntity is added to the ARView scene
        arView.scene.addAnchor(anchorEntity)
        
        // Add gesture recognizers for tap and pan gestures
        arView.addGestureRecognizer(UITapGestureRecognizer(target: context.coordinator, action: #selector(context.coordinator.handleTap(_:))))
        
        // ARView is configured to operate in a non-AR mode, meaning it won't rely on augmented reality features but can still be used for 3D graphics and animations.
        arView.cameraMode = .nonAR
        
        // Adding a camera because this is rendered in a non-AR view. This is not necessary, only for framing the example.
        let posCam = PerspectiveCamera()
        posCam.look(at: .zero, from: [0, 0, 3], relativeTo: nil)
        anchorEntity.addChild(posCam)
        
        return arView
    }
    
    func updateUIView(_ uiView: ARView, context: Context) {
        // Find the child entity by name
        if let rotatingBox = uiView.scene.findEntity(named: "Smiley") {
            // Apply rotation to the found entity
            rotatingBox.transform.rotation = simd_quatf(angle: rotationY, axis: [0, 1, 0])
        }
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
    
    class Coordinator: NSObject {
        var parent: ARViewContainer
        
        init(_ parent: ARViewContainer) {
            self.parent = parent
        }
        
        @objc func handleTap(_ gesture: UITapGestureRecognizer) {
            if gesture.state == .ended {
                // Handle tap gesture
                // Update the Y-axis rotation based on rotation gesture
                parent.rotationY += Float(0.1)
            }
        }
    }
}

Finally, you will see the Smiley appear. Touch the Smiley, and it will rotate along the y-axis.

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