Has anyone had success with MeshInstancesComponent
? I tried to follow the sample code from What's New in RealityKit but it wouldn't compile. I was able to use one of the init overloads to get it to compile, but using it crashes both my device and the simulator. Even with one instance.
MeshInstancesComponent VisionOS 26 Beta 2
Hi @jkdufair,
This code is working for me:
let scene = ModelEntity(mesh: MeshResource.generateSphere(radius: 0.2), materials: [SimpleMaterial(color: .orange, isMetallic: false)])
var meshInstancesComponent = MeshInstancesComponent()
let count = 20
do {
let instances = try LowLevelInstanceData(instanceCount: 20)
meshInstancesComponent[partIndex: 0] = .init(data: instances)
instances.withMutableTransforms { transforms in
for i in 0..<count {
let scale: Float = .random(in:0.018...0.025)
let angle: Float = .random(in:0..<2) * .pi
let range = Float(-0.1)...Float(0.1)
let positionX = Float.random(in: range)
let positionY = Float.random(in: range)
let positionZ = Float.random(in: range)
let position = SIMD3<Float>(x: positionX, y: positionY, z: positionZ)
let transform = Transform(scale: .init(repeating: scale),
rotation: .init(angle: angle,axis: [0, 1, 0]),
translation: position)
transforms[i] = transform.matrix
}
}
scene.components.set(meshInstancesComponent)
} catch {
print("error = \(error)")
}
content.add(scene)