Canvatorium Visio Lab 5019
A Scene Scaled to Fit.
Scaling the scene to fit a volume by has some quirks. I had to set the scale on the root entity of the scene, and then add the .scaleEffect()
modifier. Using one of these without the other gives us the wrong scale.
struct Lab5019: View {
var body: some View {
RealityView { content in
if let root = try? await Entity(named: "5019ScaleVolume", in: realityKitContentBundle) {
guard let resource = try? await TextureResource(named: "Starfield") else {
fatalError("Unable to load starfield texture.")
}
var material = UnlitMaterial()
material.color = .init(texture: .init(resource))
let skybox = Entity()
skybox.components.set(ModelComponent(
mesh: .generateSphere(radius: 5),
materials: [material]
))
skybox.scale *= .init(x: -1, y: 1, z: 1)
root.addChild(skybox, preservingWorldTransform: true)
// 1. Scale the root to also scale any children
root.scale = [0.2, 0.2, 0.2]
root.position = [0, -1, 0]
content.add(root)
}
}
// 2. Use the scale modifier to reduce the overall scale of the RealityView
.scaleEffect(x: 0.2, y: 0.2, z: 0.2)
}
}