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:
- Create the shader asset file.
- Load the shader.
Create the shader asset file
To create a unified ray tracing shader:
Launch the Unity Editor.
In the Project window, open the Assets folder.
Open or create the folder in which you want to create your shader.
Open the context menu (right-click) and select Create > Shader > Unified Ray Tracing Shader.
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 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);
}