nitrio DGArt

DGArt PolyPlane to Playground RealityKit

Using RealityKit in Swift Playground Demo 1 : iPadOS

Create your 3D model using DGArt and import it into the Playground App for your own app project. The example PolyPlane was modeled using DGArt, exported into a USDZ file, and used in the Playground app. The 3D file contains a "PolyPlane."

This demo loads the Polyplane.usd file and animates 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 {
    struct ARViewContainer: UIViewRepresentable {        
        let arView = ARView(frame: .zero)
        
        func makeUIView(context: Context) -> ARView {            
            
            // an anchor is a point in the AR world that can be used as a reference for placing other entities
            // Create an Entity to be positioned at world zero
            let anchor = AnchorEntity(world: .zero)
            
            // set background color
            arView.environment.background = .color(.init(red: 0.1, green: 0.1, blue: 0.3, alpha: 1))
            
            // create ModelEntity
            var plane = ModelEntity()
            do {
                //loads a 3D model  
                let path = Bundle.main.path(forResource: "PolyPlane", ofType: "usdz")!
                let url = URL(fileURLWithPath: path)
                plane = try Entity.loadModel(contentsOf: url)
                //scaled down to 25% of its original size
                plane.scale = SIMD3(x: 0.25, y: 0.25, z: 0.25)
                //sets different materials for each part of the loaded model
                plane.model?.materials[0] = SimpleMaterial(color: .white, roughness: 1, isMetallic: true)
                plane.model?.materials[1] = SimpleMaterial(color: .lightGray, roughness: 1, isMetallic: true)
                plane.model?.materials[2] = SimpleMaterial(color: .gray, roughness: 1, isMetallic: true)
                //entity is added as a child
                anchor.addChild(plane)
            } catch {
                print(error)
            }
            
            // Add the anchor to the scene
            arView.scene.anchors.append(anchor)
            
            // animate the plane
            let transform = Transform(pitch: -0.6, yaw: .pi, roll: 0.9)
            plane.move(to: transform,
                       relativeTo: plane,
                       duration: 15.0,
                       timingFunction: .easeInOut)
            
            // 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, 5], relativeTo: nil)
            anchor.addChild(posCam)
            
            return arView
        }
        
        func updateUIView(_ view: ARView, context: Context) { }
    }
    
    var body: some View {
        ARViewContainer().ignoresSafeArea()
    }
}

Finally, you will be able to see the PolyPlane animation appear alongside.

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