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:
Obtain the .usdz file here for practicing the exercise mentioned below.
In the Playgrounds app, tap on "Insert from..." in the side menu.
If you export the PolyPlane from the DGArt App, follow the steps below.
Navigate to Locations \ On My iPad \ DGArt \ export \ PolyPlane, and select PolyPlane.usdz.
You will see PolyPlane.usdz appear under Resources.
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.