A custom Ray Tracing Shader plugin for Unreal Engine 5, demonstrating how to implement Ray Generation and Pixel Shaders using FSceneViewExtensionBase.
This plugin provides examples of how to inject custom ray tracing passes into the Unreal Engine rendering pipeline. It includes implementations for:
- Ray Generation Shader: Renders a custom shadow pass into a texture and composites it with the scene. It demonstrates how to access material attributes and per-instance primitive data directly on the GPU.
- Pixel Shader: Demonstrates a raster-based approach (Inline Ray Tracing).
The implementation uses FSimpleShadowViewExtension to manage the render passes.
- Custom Ray Generation Shader (
SimpleShadowRG):- Uses a Ray Tracing Pipeline State Object (RTPSO).
- Dispatches rays to compute shadows.
- Accesses Material Data: Reads attributes like Metallic/Roughness from the hit payload.
- Accesses Primitive Data: Retrieves instance transforms and CustomPrimitiveData via GPUScene buffers.
- Outputs to a UAV texture.
- Custom Pixel Shader (
SimpleShadowPS):- Raster pass integration example using Inline Ray Tracing (
TraceRayInline).
- Raster pass integration example using Inline Ray Tracing (
- Runtime Toggle:
- Console variables to enable/disable shaders at runtime without recompiling.
- Unreal Engine 5.7
- Hardware Ray Tracing enabled in the DefaultEngine.ini (
r.RayTracing=True,r.RayTracing.Shadows=True). - DirectX 12 (SM6)
- Clone or download the repository into your project's
Pluginsdirectory.[ProjectRoot]/Plugins/CustomRaytracingShader - Regenerate project files and compile the plugin.
- Enable the plugin in the Unreal Engine Editor (
Edit > Plugins). - Ensure Ray Tracing is enabled in your Project Settings:
Engine > Rendering > Hardware Ray Tracing: EnabledEngine > Rendering > Support Compute Skincache: Enabled (often required for ray tracing)
The shaders are disabled by default. You can enable them using the following console variables:
To enable the custom Ray Generation shader (supports Material/Primitive data access):
r.Raytracing.CustomSimpleShadow.Enable 1To enable the custom Pixel Shader pass (Inline Ray Tracing):
r.Raytracing.CustomInlineSimpleShadow.Enable 1Warning
Crash Warning: Ensure that Ray Tracing Shadows or Ray Tracing support is fully enabled and active in your scene. Enabling these shaders without proper RHI support may cause a crash.
The core logic is located in SimpleShadowViewExtension.cpp.
FSimpleShadowRG: The Ray Generation shader class.FSimpleShadowPS: The Pixel shader class.- The
PrePostProcessPass_RenderThreadfunction handles the creation of the Render Dependency Graph (RDG) passes.
Unreal Engine 5向けのカスタムレイトレーシングシェーダープラグインです。FSceneViewExtensionBaseを使用して、Ray GenerationシェーダーとPixelシェーダーを実装する方法を示しています。
このプラグインは、Unreal Engineのレンダリングパイプラインにカスタムレイトレーシングパスを追加するサンプルを提供します。
- Ray Generation シェーダー: カスタムシャドウパスをテクスチャにレンダリングし、シーンと合成します。マテリアル属性やインスタンスごとのプリミティブデータへアクセスする方法も提示します。
- Pixel シェーダー: ラスタライズベースのアプローチ(インラインレイトレーシング)の実装例です。
FSimpleShadowViewExtensionを使用して描画パスを管理しています。
- カスタム Ray Generation シェーダー (
SimpleShadowRG):- Ray Tracing Pipeline State Object (RTPSO) を使用。
- レイをディスパッチしてシャドウを計算。
- マテリアルデータへのアクセス: ヒットペイロードからMetallicやRoughnessなどの属性を読み取ります。
- プリミティブデータへのアクセス: GPUSceneバッファを経由して、インスタンスの変換行列や CustomPrimitiveData を取得します。
- UAVテクスチャに出力。
- カスタム Pixel シェーダー (
SimpleShadowPS):- インラインレイトレーシング(
TraceRayInline)を使用したラスタパスの統合例。
- インラインレイトレーシング(
- ランタイム切り替え:
- コンソール変数を使用して、再コンパイルなしでシェーダーの有効/無効を切り替え可能。
- Unreal Engine 5 (バージョン5.7向けに構成)
- DefaultEngine.iniで Hardware Ray Tracing が有効であること (
r.RayTracing=True,r.RayTracing.Shadows=True)。 - DirectX 12 (SM6)
- リポジトリをプロジェクトの
Pluginsディレクトリにクローンまたはダウンロードします。[ProjectRoot]/Plugins/CustomRaytracingShader - プロジェクトファイルを再生成し、プラグインをコンパイルします。
- エディタでプラグインを有効にします (
Edit > Plugins). - プロジェクト設定でレイトレーシングが有効になっていることを確認してください:
Engine > Rendering > Hardware Ray Tracing: 有効Engine > Rendering > Support Compute Skincache: 有効 (推奨)
シェーダーはデフォルトで無効になっています。以下のコンソール変数を使用して有効にします。
Ray Generation シェーダー(マテリアル/プリミティブデータへのアクセス対応)を有効にする場合:
r.Raytracing.CustomSimpleShadow.Enable 1Pixel シェーダーパス(インラインレイトレーシング)を有効にする場合:
r.Raytracing.CustomInlineSimpleShadow.Enable 1Warning
クラッシュの警告: レイトレーシングシャドウまたはレイトレーシングサポートがシーン内で完全に有効になっていることを確認してください。適切なRHIサポートなしでこれらのシェーダーを有効にすると、クラッシュする可能性があります。