Files
2026-01-08 20:43:08 +05:00

2.4 KiB

Create a unified ray tracing shader

Depending on the backend you choose in the RayTracingContext, write your ray tracing code in either a .raytrace or a .compute shader.

To create both a .raytrace shader and a .compute shader, write your shader code in a unified ray tracing shader (.urtshader).

To create a shader, follow these steps:

  1. Create the shader asset file.
  2. Load the shader.

Create the shader asset file

To create a unified ray tracing shader:

  1. Launch the Unity Editor.

  2. In the Project window, open the Assets folder.

  3. Open or create the folder in which you want to create your shader.

  4. Open the context menu (right-click) and select Create > Shader > Unified Ray Tracing Shader.

  5. Enter a name for the shader.

Ray tracing occurs in the RayGenExecute function. You can edit the example code snippet.

For more information about writing a ray tracing shader, refer to Write your shader code.

Load the shader

To load the .urtshader file in the Editor, use the following code snippet:

IRayTracingShader shader = rtContext.LoadRayTracingShader("Assets/yourShader.urtshader");

To load the shader in the Player, add the .urtshader shader to an xref:UnityEngine.AssetBundle then load it with the following:

// Load the AssetBundle 
var asssetBundle = AssetBundle.LoadFromFile("Assets/pathToYourBuiltAssetBundles/yourAssetBundle");

// Load the shader
IRayTracingShader shader = rtContext.LoadRayTracingShaderFromAssetBundle(asssetBundle, "Assets/yourShader.urtshader");

To have more control, load the underlying Compute or Ray Tracing shader asset yourself and pass it to RayTracingContext.CreateRayTracingShader.

RayTracingContext.LoadRayTracingShaderFromAssetBundle is a convenience function that performs the following operations:

public IRayTracingShader LoadRayTracingShaderFromAssetBundle(AssetBundle assetBundle, string name)
{
    Object asset = assetBundle.LoadAsset(name, BackendHelpers.GetTypeOfShader(BackendType));
    return CreateRayTracingShader(asset);
}