nitrio DGArt

DGArt PolyPlane to Playground RealityKit AR View

Using RealityKit AR View in Swift Playground Demo 2 : iPadOS

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

This demo loads the Polyplane.usd file and animates it in AR View.

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
            let anchor = AnchorEntity(plane: .any)
            
            // 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.1, y: 0.1, z: 0.1)
                //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)
            
            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 beside, with the camera enabled.

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