Home

Awesome

RayTracingDynamicMeshGeometry

Unity sample project using dynamic Mesh geometries in a RayTracingAccelerationStructure (RTAS).

<img src="Images/GameView.png" width="1280">

Description

The project uses RayTracingAccelerationStructure.AddInstance function to add ray tracing instances that use dynamic geometries to the RTAS. The function signature used is:

int AddInstance(ref Rendering.RayTracingMeshInstanceConfig config, Matrix4x4 matrix, Nullable<Matrix4x4> prevMatrix, uint id);

There are 2 dynamic Meshes in the Scene - the left Mesh is animated on the CPU in C# while the right one on the GPU in a compute shader. Check MeshInstanceDynamicGeometry.cs. After animation, CommandBuffer.BuildRayTracingAccelerationStructure is called where the acceleration structures (BLAS) associated with the 2 geometries are built on the GPU.

The ray generation shader in MeshInstanceDynamicGeometry.raytrace casts the rays into the view frustum by calling TraceRay and generates the resulting image into a RenderTexture which is displayed in the Game View output.

The hit shader used by the 2 geometries is MeshInstanceDynamicGeometry.shader, second SubShader.

After opening the project, switch to Game view and press Play to animate the geometries.

Prerequisites

Resources