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?

== DATE:

    Tuesday, 12 November 2024 at 01:12:12 Moscow Standard Time
    
    2024-11-11T22:12:12Z



== PREVIEW UPDATE ERROR:

    CrashReportError: Dice crashed
    
    Dice crashed. Check ~/Library/Logs/DiagnosticReports for crash logs from your application.
    
    Process:             Dice [736]
    Path:                
    
    Date/Time:           2024-11-11 22:11:45 +0000
    
    Application Specific Information:
        dyld:
            dyld config: DYLD_LIBRARY_PATH=/Users/prosto/Library/Developer/Xcode/DerivedData/Dice-aeivbknqrhwewebbagbzvnruaplj/Build/Products/Debug DYLD_INSERT_LIBRARIES=@executable_path/__preview.dylib:/Applications/Xcode.app/Contents/Developer/usr/lib/libLogRedirect.dylib:/System/Library/PrivateFrameworks/LiveExecutionResultsProbe.framework/LiveExecutionResultsProbe:/System/Library/PrivateFrameworks/PreviewsInjection.framework/PreviewsInjection DYLD_FRAMEWORK_PATH=/Users/prosto/Library/Developer/Xcode/DerivedData/Dice-aeivbknqrhwewebbagbzvnruaplj/Build/Products/Debug
    
    Crashing Thread:
    0   RealityKit                0x000012f04 ARView.ray(through:) + 424
    1   RealityKit                0x0000131ec ARView.hitTest(_:requireInputTarget:query:mask:) + 564
    2   _RealityKit_SwiftUI       0x00001b1ac RealityViewCameraContent.hoverEffect(_:) + 672
    3   _RealityKit_SwiftUI       0x00001aed8 closure #3 in closure #1 in _RealityViewImpl.body.getter + 196
    4   SwiftUI                   0x0006ea994 closure #1 in HoverResponder.updatePhase(_:) + 112
    5   SwiftUICore               0x0001fb0c4 partial apply for thunk for @escaping @callee_guaranteed () -> () + 28
    6   SwiftUICore               0x0004afba0 static Update.dispatchActions() + 1236
    7   SwiftUICore               0x0004af084 static Update.end() + 212
    8   SwiftUICore               0x0009b69e4 specialized closure #1 in static NSRunLoop.addObserver(_:) + 176
    9   CoreFoundation            0x00007d078 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 36
    10  CoreFoundation            0x00007cf64 __CFRunLoopDoObservers + 552
    11  CoreFoundation            0x00007bc10 CFRunLoopRunSpecific + 664
    12  HIToolbox                 0x00010bf64 RunCurrentEventLoopInMode + 292
    13  HIToolbox                 0x000111d54 ReceiveNextEventCommon + 636
    14  HIToolbox                 0x000111eb8 _BlockUntilNextEventMatchingListInModeWithFilter + 76
    15  AppKit                    0x00003aa08 _DPSNextEvent + 660
    16  AppKit                    0x00097ae0c -[NSApplication(NSEventRouting) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 688
    17  AppKit                    0x00002dae0 -[NSApplication run] + 480
    18  AppKit                    0x000004364 NSApplicationMain + 888
    19  SwiftUI                   0x00004c340 specialized runApp(_:) + 160
    20  SwiftUI                   0x0004bcee4 runApp<a>(_:) + 84
    21  SwiftUI                   0x000835f24 static App.main() + 224
    22  ???                       0x34000c57c _$s4Dice0A3AppV5$mainyyFZ$8$body
    23  ???                       0x34000c62c _main$8$body
    24  Dice                      0x000002238 __debug_blank_executor_run_user_entry_point + 144
    25  PreviewsInjection         0x0000375c8 ???
    26  PreviewsInjection         0x0000382e0 ???
    27  PreviewsInjection         0x0000381a4 __previews_injection_run_user_entrypoint + 16
    28  XOJITExecutor             0x000007adc __xojit_executor_run_program_wrapper + 1832
    29  XOJITExecutor             0x0000037cc ???
    30  PreviewsInjection         0x0000380d8 ???
    31  Dice                      0x000001978 __debug_blank_executor_main + 1056
    32  dyld                      0x000006274 start + 2840
    
    Binary Images:
           0x102fb8000 Dice <16af2326-4cb6-3bc0-b1b1-cc5a58619bf6> /Users/USER/Library/Developer/Xcode/DerivedData/Dice-aeivbknqrhwewebbagbzvnruaplj/Build/Products/Debug/Dice.app/Contents/MacOS/Dice
           0x102fdc000 __preview.dylib <0548e097-d2f9-3cd6-943d-7460f0056b12> /Users/USER/Library/Developer/Xcode/DerivedData/Dice-aeivbknqrhwewebbagbzvnruaplj/Build/Products/Debug/Dice.app/Contents/MacOS/__preview.dylib
           0x103024000 libLogRedirect.dylib <4bce1470-c91f-329c-9855-dac458846145> /Applications/Xcode.app/Contents/Developer/usr/lib/libLogRedirect.dylib
           0x1052ac000 libobjc-trampolines.dylib <f1242604-dcb7-30a9-a19a-182991e6e645> /usr/lib/libobjc-trampolines.dylib
           0x109978000 AGXMetal13_3 <25ed3810-fd45-3e81-8e80-5161e3d9d8a9> /System/Library/Extensions/AGXMetal13_3.bundle/Contents/MacOS/AGXMetal13_3
           0x10d008000 libSTS-N.dylib <d635f5de-7b55-35e0-ac5b-2eace0a11acd> /System/Library/Frameworks/MediaToolbox.framework/Support/libSTS-N.dylib
           0x2360a9000 RealityKit <e5d71368-6601-304f-b195-aca70019e4e3> /System/Library/Frameworks/RealityKit.framework/Versions/A/RealityKit
           0x238462000 _RealityKit_SwiftUI <461aa31a-1b6a-3e4a-b70b-6f4b4ab97018> /System/Library/Frameworks/_RealityKit_SwiftUI.framework/Versions/A/_RealityKit_SwiftUI
           0x1c8e70000 SwiftUI <766fb524-278b-3c0c-9c63-2386ec6b2bfd> /System/Library/Frameworks/SwiftUI.framework/Versions/A/SwiftUI
           0x2368c1000 SwiftUICore <40cc5e58-7201-3013-9820-9e0d999ebc83> /System/Library/Frameworks/SwiftUICore.framework/Versions/A/SwiftUICore
           0x19ac9c000 CoreFoundation <ae4610f8-7c5c-3484-858e-cae7457d206e> /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation
           0x1a607c000 HIToolbox <738685cb-aca1-3dfe-91c5-192bab97a042> /System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/HIToolbox
           0x19e809000 AppKit <49d8b1b6-0ea8-36ad-89fd-a41acf00a742> /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit
           0x000000000 ??? <00000000-0000-0000-0000-000000000000> ???
           0x25ac0b000 PreviewsInjection <efb21227-2510-3ec9-a8b5-9b0dabc71778> /System/Library/PrivateFrameworks/PreviewsInjection.framework/Versions/A/PreviewsInjection
           0x26f5dc000 XOJITExecutor <8c3583b1-5d79-32e8-88f9-6a155fba91b2> /System/Library/PrivateFrameworks/XOJITExecutor.framework/Versions/A/XOJITExecutor
           0x19a8aa000 dyld <6beafde4-b011-3e47-8aae-4d7b6e4bb7e8> /usr/lib/dyld
           0x1ab88e000 libswiftCore.dylib <53497bdd-8c93-3dc1-9ae9-82f27e47fb0a> /usr/lib/swift/libswiftCore.dylib
           0x19ac2c000 libsystem_pthread.dylib <3b8268be-4e02-3b4a-8b41-baed2bbaacff> /usr/lib/system/libsystem_pthread.dylib
           0x19abf1000 libsystem_kernel.dylib <9fea25a4-e8ca-3f3d-901c-a53ff2bc7217> /usr/lib/system/libsystem_kernel.dylib
           0x19aa7c000 libdispatch.dylib <9ea577db-73c2-39d8-9fc0-544fa595a142> /usr/lib/system/libdispatch.dylib
           0x19ab48000 libc++.1.dylib <601f843f-71f1-30bb-88fb-55b131097d1d> /usr/lib/libc++.1.dylib
           0x241757000 CoreRE <daf7b7bc-3700-3659-aa25-4082a31170bb> /System/Library/PrivateFrameworks/CoreRE.framework/Versions/A/CoreRE
           0x1a5c85000 CoreMedia <fdaf6508-0e93-3e60-8454-8d2c56d2a07f> /System/Library/Frameworks/CoreMedia.framework/Versions/A/CoreMedia
           0x1a3fa8000 CoreVideo <c07f30c3-7eed-3339-b7a8-585bc22f6867> /System/Library/Frameworks/CoreVideo.framework/Versions/A/CoreVideo
    
    EOF
    
    ==================================
    
    |  [Remote] NSError.BSServiceConnectionErrorDomain.3: Не удалось завершить операцию. XPC error received on message reply handler
    |  
    |  BSServiceConnectionErrorDomain (3):
    |  ==BSErrorCodeDescription: OperationFailed
    |  ==NSLocalizedFailureReason: XPC error received on message reply handler



== VERSION INFO:

    Tools: 16B40
    OS:    24B83
    PID:   630
    Model: Mac mini
    Arch:  arm64e



== ENVIRONMENT:

    openFiles = [
        /Users/prosto/Desktop/Dice/Dice/Untitled.swift
    ]
    wantsNewBuildSystem = true
    newBuildSystemAvailable = true
    activeScheme = Dice
    activeRunDestination = My Mac variant macos arm64
    workspaceArena = [x]
    buildArena = [x]
    buildableEntries = [
        Dice.app
    ]
    runMode = JIT Executor



== SELECTED RUN DESTINATION:

    macOS 15.1 | macos | arm64 | Mac mini | no proxy



== EXECUTION MODE OVERRIDES:

    Workspace JIT mode user setting: true
    Falling back to Dynamic Replacement: false



== PACKAGE RESOLUTION ERRORS:

    



== REFERENCED SOURCE PACKAGES:

    



== JIT LINKAGE:

    Run Destination: 00008103-000955313AA3001E-macosx15.1-arm64-macos
    JIT Link Description {
        3:Dice.app
    }
    



== SESSION GROUP 8242:

    workspace identifier: WorkspaceIdentifier(identifier: E1D5D52E-EE4A-4831-BE4C-E21DEF6F68FD)
    providers: [
        Preview Provider | Registry-Untitled.swift#1[preview] [Editor(8206)]
    ]
    translation units: [
        /Users/prosto/Desktop/Dice/Dice/Untitled.swift
    ]
    attributes: [
        Editor(8206):     []
    ]
    session: 8247
    request sessions: [
        Registry[Registry-Untitled.swift#1[preview] (line 22)]: not completed
    ]



== UPDATE SESSION 8247:

    Start Date: Tuesday, 12 November 2024 at 01:11:42 Moscow Standard Time
    Preview Preflight {
        UpdaterStore {
            updaterLimit: none
            expectedAbandonedIdentifiers: [8267]
        }
        My Mac {
            dvtDevice: My Mac
            buildNumber: 15.1 (24B83)
            arch: arm64e
        }
        pid: 736
        host bundle: 
        com.company.Dice {
            url: file:///Users/prosto/Library/Developer/Xcode/DerivedData/Dice-aeivbknqrhwewebbagbzvnruaplj/Build/Products/Debug/Dice.app
            version: 8254
            signingInformation: 
            Code Signing {
                identifier: com.company.Dice
                hasGetTaskAllow: false
                isSandboxed: true
            }
            attributes: [
                AppExtensionIdentifierPreviewAttributeKey: nil,
                LaunchConfigArgumentsPreviewAttributesKey: ["-NSDocumentRevisionsDebugMode", "YES"],
                LaunchConfigEnvironmentVariablesPreviewAttributesKey: ["OS_ACTIVITY_TOOLS_PRIVACY": "YES", "__XPC_DYLD_LIBRARY_PATH": "/Users/prosto/Library/Developer/Xcode/DerivedData/Dice-aeivbknqrhwewebbagbzvnruaplj/Build/Products/Debug", "OS_ACTIVITY_TOOLS_OVERSIZE": "YES", "DYLD_LIBRARY_PATH": "/Users/prosto/Library/Developer/Xcode/DerivedData/Dice-aeivbknqrhwewebbagbzvnruaplj/Build/Products/Debug", "IDE_DISABLED_OS_ACTIVITY_DT_MODE": "1", "DYLD_INSERT_LIBRARIES": "/Applications/Xcode.app/Contents/Developer/usr/lib/libLogRedirect.dylib", "PACKAGE_RESOURCE_BUNDLE_PATH": "/Users/prosto/Library/Developer/Xcode/DerivedData/Dice-aeivbknqrhwewebbagbzvnruaplj/Build/Products/Debug", "__XCODE_BUILT_PRODUCTS_DIR_PATHS": "/Users/prosto/Library/Developer/Xcode/DerivedData/Dice-aeivbknqrhwewebbagbzvnruaplj/Build/Products/Debug", "SQLITE_ENABLE_THREAD_ASSERTIONS": "1", "TERM": "dumb", "DYLD_FRAMEWORK_PATH": "/Users/prosto/Library/Developer/Xcode/DerivedData/Dice-aeivbknqrhwewebbagbzvnruaplj/Build/Products/Debug", "CFLOG_FORCE_DISABLE_STDERR": "1", "OS_LOG_DT_HOOK_MODE": "0x07", "OS_LOG_TRANSLATE_PRINT_MODE": "0x80", "OS_LOG_DT_HOOK_PREFIX": "OSLOG-E1D5E507-A99B-4CE8-AFA0-98E9C1FDBF69", "__XPC_DYLD_FRAMEWORK_PATH": "/Users/prosto/Library/Developer/Xcode/DerivedData/Dice-aeivbknqrhwewebbagbzvnruaplj/Build/Products/Debug"],
                AgentRoleKey: Previews,
                AgentCompanionAppBundleIdentifierKey: nil,
                AgentRunModeKey: JIT Executor,
            ]
        }
        builtTargetDescriptions: Build Logs/ResolvedBuiltTargetDescriptions-request-1-com.company.Dice.txt
    }
    Preview Provider {
        UpdaterStore {
            updaterLimit: none
            expectedAbandonedIdentifiers: [8267]
        }
        My Mac {
            dvtDevice: My Mac
            buildNumber: 15.1 (24B83)
            arch: arm64e
        }
        pid: 736
        host bundle: 
        com.company.Dice {
            url: file:///Users/prosto/Library/Developer/Xcode/DerivedData/Dice-aeivbknqrhwewebbagbzvnruaplj/Build/Products/Debug/Dice.app
            version: 8254
            signingInformation: 
            Code Signing {
                identifier: com.company.Dice
                hasGetTaskAllow: false
                isSandboxed: true
            }
            attributes: [
                AppExtensionIdentifierPreviewAttributeKey: nil,
                LaunchConfigArgumentsPreviewAttributesKey: ["-NSDocumentRevisionsDebugMode", "YES"],
                LaunchConfigEnvironmentVariablesPreviewAttributesKey: ["OS_ACTIVITY_TOOLS_PRIVACY": "YES", "__XPC_DYLD_LIBRARY_PATH": "/Users/prosto/Library/Developer/Xcode/DerivedData/Dice-aeivbknqrhwewebbagbzvnruaplj/Build/Products/Debug", "OS_ACTIVITY_TOOLS_OVERSIZE": "YES", "DYLD_LIBRARY_PATH": "/Users/prosto/Library/Developer/Xcode/DerivedData/Dice-aeivbknqrhwewebbagbzvnruaplj/Build/Products/Debug", "IDE_DISABLED_OS_ACTIVITY_DT_MODE": "1", "DYLD_INSERT_LIBRARIES": "/Applications/Xcode.app/Contents/Developer/usr/lib/libLogRedirect.dylib", "PACKAGE_RESOURCE_BUNDLE_PATH": "/Users/prosto/Library/Developer/Xcode/DerivedData/Dice-aeivbknqrhwewebbagbzvnruaplj/Build/Products/Debug", "__XCODE_BUILT_PRODUCTS_DIR_PATHS": "/Users/prosto/Library/Developer/Xcode/DerivedData/Dice-aeivbknqrhwewebbagbzvnruaplj/Build/Products/Debug", "SQLITE_ENABLE_THREAD_ASSERTIONS": "1", "TERM": "dumb", "DYLD_FRAMEWORK_PATH": "/Users/prosto/Library/Developer/Xcode/DerivedData/Dice-aeivbknqrhwewebbagbzvnruaplj/Build/Products/Debug", "CFLOG_FORCE_DISABLE_STDERR": "1", "OS_LOG_DT_HOOK_MODE": "0x07", "OS_LOG_TRANSLATE_PRINT_MODE": "0x80", "OS_LOG_DT_HOOK_PREFIX": "OSLOG-E1D5E507-A99B-4CE8-AFA0-98E9C1FDBF69", "__XPC_DYLD_FRAMEWORK_PATH": "/Users/prosto/Library/Developer/Xcode/DerivedData/Dice-aeivbknqrhwewebbagbzvnruaplj/Build/Products/Debug"],
                AgentRoleKey: Previews,
                AgentCompanionAppBundleIdentifierKey: nil,
                AgentRunModeKey: JIT Executor,
            ]
        }
        builtTargetDescriptions: Build Logs/ResolvedBuiltTargetDescriptions-request-2-com.company.Dice.txt
    }
    Build Graph {
        Dice.app (#3)
           sourceFile(file:///Users/prosto/Desktop/Dice/Dice/Untitled.swift -> Untitled.swift) (#1)
           Untitled.swift (#2)
    }
    Update Plan {
        macOS [arm64 macosx15.1 macos] (Mac mini, 00008103-000955313AA3001E-macosx15.1-arm64-macos), [], thinning enabled, thunking enabled) {
            Destination: My Mac dvtdevice-local-computer:localhost | default device for macos [
                Dice app - Previews {
                    execution point packs [
                        [source: Untitled.swift, role: Previews] (in Dice)
                    ]
                    translation units [
                        Untitled.swift (in Dice.app)
                    ]
                    modules [
                        Dice.app
                    ]
                    jit link description [
                        Dice.app
                    ]
                }
            ]
        }
    }



== POWER STATE LOGS:

    12.11.2024, 1:11 Received power source state: Externally Powered
    12.11.2024, 1:11 No device power state user override user default value.Current power state: Full Power


</a>

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