RealityKit Crash with Orthographic Camera

In macOS project with RealityKit and SwiftUI, adding OrthographicCameraComponent causes crashes in both Xcode Preview and at runtime.

import SwiftUI
import RealityKit

struct ContentView: View {

    var body: some View {
        RealityView { content in
            var camera = Entity()
            var component = OrthographicCameraComponent()
            
            component.scale = 5
            camera.position = [0, 0, 5]
            
            camera.components.set(component)
            
            content.add(camera)
            content.add(ModelEntity(mesh: .generateSphere(radius: 1)))
        }
    }
}

#Preview {
    ContentView()
}

Has anyone faced this issue or knows a fix?

Hi, can you share a crash report?

Thank you, can you see if this issue is fixed in .2 beta releases (macOS 15.2, iOS 18.2, visionOS 2.2)? I believe this should be fixed in those updates.

Hello! I had a similar issue. Adding OrthographicCameraComponent does not crash anymore, but it crashed for me when computing a projection from the world coordinates to screen coordinates or another way around.

import SwiftUI
import RealityKit

struct ContentView: View {

    var body: some View {
        RealityView { content in
            var camera = Entity()
            var component = OrthographicCameraComponent()
            
            component.scale = 5
            camera.position = [0, 0, 5]
            
            camera.components.set(component)
            
            content.add(camera)
            content.add(ModelEntity(mesh: .generateSphere(radius: 1)))
        }  update: { content in
           if let p = content.project(point: [0, 0, 0], to: .local) {
                print(p)
           }
        }
    }
}

#Preview {
    ContentView()
}

Updated: In Xcode 16.3 beta, the unprojection of a point from screen to world is already better. However, the values are still wrong (very large values). Because of this, interaction with entities using InputTargetComponent is not possible.

import SwiftUI
import RealityKit

struct ContentView: View {
  @State var tapPoint: CGPoint? = nil
  
  var body: some View {
    RealityView { content in
      var camera = Entity()
      var component = OrthographicCameraComponent()
      
      component.scale = 5
      camera.position = [0, 0, 5]
      
      camera.components.set(component)
      
      content.add(camera)
      
      let model = ModelEntity(mesh: .generateSphere(radius: 1))
      model.name = "Model"
      model.components.set(InputTargetComponent())
      model.generateCollisionShapes(recursive: false)
      content.add(model)
    }  update: { content in
      if let p = tapPoint {
        let planeMatrix = simd_float4x4(
          SIMD4<Float>( 1,  0,  0, 0),
          SIMD4<Float>( 0,  0,  1, 0),
          SIMD4<Float>( 0, -1,  0, 0),
          SIMD4<Float>( 0,  0,  0, 1)
        )
        
        if let u = content.unproject(p, from: .global, to: .scene, ontoPlane: planeMatrix) {
          print("Tapped at \(u)")
        }
      }
    }
    .simultaneousGesture(
      SpatialTapGesture()
        .onEnded({value in
          tapPoint = value.location
        })
    )
    .simultaneousGesture(
      SpatialTapGesture()
        .targetedToAnyEntity()
        .onEnded({value in
          print(value.entity.name)
        })
    )
  }
}
RealityKit Crash with Orthographic Camera
 
 
Q