docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Create an acceleration structure

    IRayTracingAccelStruct is the data structure used to represent a collection of instances and geometries that are used for GPU ray tracing.

    Create an acceleration structure with the following code:

    var options = new AccelerationStructureOptions();
    IRayTracingAccelStruct rtAccelStruct = rtContext.CreateAccelerationStructure(options);
    

    Build options

    AccelerationStructureOptions allows you to configure the build algorithm. The trade-off is as follows: A faster build results in worse ray tracing performance, and conversely, a slower build can improve ray tracing performance.

    The following fields can be configured:

    Field Description
    buildFlags Adjust the buildFlags to prioritize either faster construction of the acceleration structure or faster ray tracing.
    useCPUBuild (Compute backend) When set to true, Unity builds the acceleration structure on the CPU instead of the GPU. This option has no effect when using the Hardware backend. CPU-based builds use a more advanced algorithm, resulting in a higher-quality acceleration structure, which enhances overall ray tracing performance.

    The following example demonstrates how to get the best ray tracing performance. These options make the acceleration structure longer to build, however.

    var options = new AccelerationStructureOptions() {
        buildFlags = BuildFlags.PreferFastTrace,
        useCPUBuild = true
    }
    

    Populate the acceleration structure

    Unlike RayTracingAccelerationStructure, there is no support for automatic synchronization between the acceleration structure's instances and the scene instances. You need to add them manually, for example:

    var instanceDesc = new MeshInstanceDesc(mesh, 0 /*subMeshIndex*/);
    instanceDesc.localToWorldMatrix = /* desired transform */;
    rtAccelStruct.AddInstance(instanceDesc);
    

    To trace rays against all the Mesh-based GameObjects in your scene, add them to the IRayTracingAccelStruct. For example:

    var meshRenderers = UnityEngine.Object.FindObjectsByType<MeshRenderer>(FindObjectsSortMode.None);
    foreach (var renderer in meshRenderers)
    {
        var mesh = renderer.GetComponent<MeshFilter>().sharedMesh;
        int subMeshCount = mesh.subMeshCount;
    
        for (int i = 0; i < subMeshCount; ++i)
        {
            var instanceDesc = new MeshInstanceDesc(mesh, i);
            instanceDesc.localToWorldMatrix = renderer.transform.localToWorldMatrix;
            rtAccelStruct.AddInstance(instanceDesc);
        }
    }
    
    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)