using System;
namespace UnityEngine.Rendering.UnifiedRayTracing
{
///
/// Parameters used to configure the creation of instances that are part of a .
///
public struct MeshInstanceDesc
{
///
/// The Mesh used to build this instance's geometry.
///
public Mesh mesh;
///
/// The index of the sub-mesh (MeshInstanceDesc references a single sub-mesh).
///
public int subMeshIndex;
///
/// The transformation matrix of the instance.
///
public Matrix4x4 localToWorldMatrix;
///
/// The instance mask.
///
///
/// Instances in the acceleration structure contain an 8-bit user defined instance mask.
/// The TraceRayClosestHit/TraceRayAnyHit HLSL functions have an 8-bit input parameter, InstanceInclusionMask which gets ANDed with the instance mask from
/// any instance that is a candidate for intersection during acceleration structure traversal on the GPU.
/// If the result of the AND operation is zero, the GPU ignores the intersection.
///
public uint mask;
///
/// Instance identifier. Can be accessed in the HLSL via the instanceID member of Hit (Hit is returned by the TraceRayClosestHit/TraceRayAnyHit HLSL functions).
///
public uint instanceID;
///
/// Whether front/back face culling for this ray tracing instance is enabled. Default value: true.
///
public bool enableTriangleCulling;
///
/// Whether to flip the way triangles face in this ray tracing instance. Default value: false.
///
public bool frontTriangleCounterClockwise;
///
/// Whether the geometry is considered opaque. Default value: true.
///
///
/// When an instance's opaqueGeometry field is set to false, the AnyHitExecute shader function will be invoked during the ray traversal when a hit is found.
/// This alows the user to programmatically decide whether to reject or accept the candidate hit. This feature can, for example, be used to implement alpha cutout transparency.
/// For best performance, prefer to set this parameter to false for as many geometries as possibe.
///
public bool opaqueGeometry;
///
/// Creates a MeshInstanceDesc.
///
/// The Mesh used to build this instance's geometry.
/// The index of the sub-mesh (MeshInstanceDesc references a single sub-mesh).
public MeshInstanceDesc(Mesh mesh, int subMeshIndex = 0)
{
this.mesh = mesh;
this.subMeshIndex = subMeshIndex;
localToWorldMatrix = Matrix4x4.identity;
mask = 0xFFFFFFFF;
instanceID = 0xFFFFFFFF;
enableTriangleCulling = true;
frontTriangleCounterClockwise = false;
opaqueGeometry = true;
}
}
///
/// A data structure used to represent a collection of instances and geometries that are used for GPU ray tracing.
/// It can be created by calling .
///
public interface IRayTracingAccelStruct : IDisposable
{
///
/// Adds an instance to the RayTracingAccelerationStructure.
///
/// The parameters describing this instance.
/// A value representing a handle that you can use to perform later actions (e.g. RemoveInstance...)
/// Thrown if the instance cannot be added. Inspect for the reason.
int AddInstance(MeshInstanceDesc meshInstance);
///
/// Removes an instance.
///
/// The handle associated with an instance.
void RemoveInstance(int instanceHandle);
///
/// Removes all ray tracing instances from the acceleration structure.
///
void ClearInstances();
///
/// Updates the transformation of an instance.
///
/// The handle associated with an instance.
/// The new transformation matrix of the instance.
void UpdateInstanceTransform(int instanceHandle, Matrix4x4 localToWorldMatrix);
///
/// Updates the instance ID of an instance.
///
/// The handle associated with an instance.
/// The new instance ID.
void UpdateInstanceID(int instanceHandle, uint instanceID);
///
/// Updates the instance mask of an instance.
///
///
/// Ray tracing instances in the acceleration structure contain an 8-bit user defined instance mask.
/// The TraceRay() HLSL function has an 8-bit input parameter, InstanceInclusionMask which gets ANDed with the instance mask from
/// any ray tracing instance that is a candidate for intersection during acceleration structure traversal on the GPU.
/// If the result of the AND operation is zero, the GPU ignores the intersection.
///
/// The handle associated with an instance.
/// The new mask.
void UpdateInstanceMask(int instanceHandle, uint mask);
///
/// Adds a command in cmd to build this acceleration structure on the GPU.
///
///
/// Depending on the backend, the GPU build algorithm can require additional GPU storage that is supplied through the scratchBuffer parameter.
/// Its required size can be queried by calling .
///
/// CommandBuffer to register the build command to.
/// Temporary buffer used during the build.
/// Thrown if the build failed. Inspect for the reason.
void Build(CommandBuffer cmd, GraphicsBuffer scratchBuffer);
///
/// Returns the minimum buffer size that is required by the scratchBuffer parameter of .
///
/// The minimum size in bytes.
ulong GetBuildScratchBufferRequiredSizeInBytes();
}
}