docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Create the ray tracing context

    The RayTracingContext serves as the initial API entry point, allowing the creation of all essential objects required to execute ray tracing code.

    Follow these steps:

    1. Load the ray tracing resources.
    2. Create the context.

    Load the ray tracing resources

    The RayTracingContext needs a few utility shaders that the RayTracingResources object supplies. You can load these resources in several different ways.

    If your project uses SRP (Scriptable Render Pipeline), load the resources via RayTracingResources.LoadFromRenderPipelineResources. This always works in the Editor.

    var rtResources = new RayTracingResources();
    bool result = rtResources.LoadFromRenderPipelineResources();
    

    You can instruct Unity to also include the resources in Player builds:

    using UnityEngine.Rendering;
    using UnityEngine.Rendering.UnifiedRayTracing;
    
    #if UNITY_EDITOR
    class MyURTStripping: IRenderPipelineGraphicsSettingsStripper<RayTracingRenderPipelineResources>
    {
        public bool active => true;
        public bool CanRemoveSettings(RayTracingRenderPipelineResources settings) => false;
    }
    #endif
    

    You can also load the resources via the Asset Database:

    var rtResources = new RayTracingResources();
    rtResources.Load();
    

    Note: Since the Player doesn't give access to the Asset Database, this method only works in the Editor.

    To load the RayTracingResources in the Player, you can also build the unifiedraytracing AssetBundle. For more information about how to build and load AssetBundles, refer to AssetBundles.

    var rtResources = new RayTracingResources();
    
    // Load the AssetBundle 
    var asssetBundle = AssetBundle.LoadFromFile("Assets/pathToYourBuiltAssetBundles/unifiedraytracing");
    
    // Load the RayTracingResources
    rtResources.LoadFromAssetBundle(asssetBundle);
    

    Create the context

    Once the RayTracingResources are loaded, use them to create the RayTracingContext.

    // Choose a backend
    var backend = RayTracingContext.IsBackendSupported(RayTracingBackend.Hardware) ? RayTracingBackend.Hardware : RayTracingBackend.Compute;
    
    // Create the context
    var context = new RayTracingContext(backend, rtResources);
    
    In This Article
    Back to top
    Copyright © 2025 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)