Class ComputeRaycast
Provides functionality to perform GPU-accelerated raycasting operations against mesh geometry using compute shaders.
public class ComputeRaycast
- Inheritance
-
ComputeRaycast
- Inherited Members
Remarks
Use this class to initialize compute-based raycasting with a specific render device and to perform intersection tests between rays and meshes. The class manages the necessary GPU resources and compute shader setup for efficient raycast queries. For one-off or stateless raycasts, the static method can be used without instantiating the class.
Constructors
ComputeRaycast()
Creates a new instance of the ComputeRaycast class, initializing the internal raytest compute shader.
public ComputeRaycast()
Methods
Dispose(IRenderDevice)
Releases all resources used by the ComputeRaycast instance and associated rendering resources.
public void Dispose(IRenderDevice renderer)
Parameters
rendererIRenderDeviceThe render device used to release GPU buffers and related resources. Cannot be null.
Remarks
Call this method when the ComputeRaycast instance is no longer needed to free GPU and shader resources. After calling Dispose, the instance should not be used.
Init(IRenderDevice)
Initializes the ComputeRaycast with the specified render device, setting up necessary GPU resources.
public void Init(IRenderDevice renderDevice)
Parameters
renderDeviceIRenderDevice
PerformRaycast(MeshRay, Transform, Mesh)
Performs a raycast against the specified mesh using a compute shader and returns the result of the intersection, if any.
public ComputeHitResult PerformRaycast(MeshRay ray, Transform transform, Mesh mesh)
Parameters
rayMeshRayThe ray, in world space, to test for intersection with the mesh.
transformTransformThe transformation to apply to the mesh before performing the raycast. Typically represents the mesh's world transform.
meshMeshThe mesh to test for intersection with the ray.
Returns
- ComputeHitResult
A ComputeHitResult structure containing information about the intersection. If no intersection is found, the TriangleIndex property of the result is set to -1.
Remarks
This method uses GPU compute shaders to perform the raycast operation, which can efficiently handle complex meshes. The mesh's local transform is combined with the provided transform before testing. The result includes the hit position, normal, and triangle index if an intersection occurs.
Exceptions
- InvalidOperationException
Thrown if the raycast system has not been initialized with a render device.
PerformRaycast(MeshRay, Transform, Mesh, IRenderDevice, ComputeShader)
Performs a raycast against the specified mesh using a compute shader and returns the result of the intersection, if any.
public static ComputeHitResult PerformRaycast(MeshRay ray, Transform transform, Mesh mesh, IRenderDevice renderer, ComputeShader shader)
Parameters
rayMeshRayThe ray, in world space, to test for intersection with the mesh.
transformTransformThe world transform to apply to the mesh before performing the raycast.
meshMeshThe mesh to test for intersection with the ray.
rendererIRenderDeviceThe render device used to manage GPU resources and dispatch the compute shader.
shaderComputeShaderThe compute shader used to perform the ray-mesh intersection test.
Returns
- ComputeHitResult
A ComputeHitResult structure containing information about the intersection. If no intersection is found, the TriangleIndex field will be -1.
Remarks
This method uses GPU compute shaders to efficiently test for ray-mesh intersections. The mesh's local transform is combined with the provided world transform before the raycast. The caller is responsible for ensuring that the mesh and shader are compatible and properly initialized.