Hi,
I am trying to simulate a simple lidar scanner with DXR. I am running this in a custom pass volume. The problem I have is that the closestHit shader never gets triggered. I am only getting misses. I am guessing my accelerationStructure is somehow messed up. Can somebody give me a hint what I am doing wrong? I am stuck on this for 3 hours now.
To build the AS I just loop through some tagged objects and add their MeshRenderers. I then call build and pass the AS to the shader.
Custom Pass Execute Function:
protected override void Execute(ScriptableRenderContext renderContext, CommandBuffer cmd, HDCamera camera, CullingResults cullingResult)
{
if (scanner != null)
{
if (EditorApplication.isPlaying)
{
RayTracingAccelerationStructure accelerationStructure = new RayTracingAccelerationStructure();
GameObject[] obstacles = GameObject.FindGameObjectsWithTag("ObstacleRaytraced");
foreach(GameObject obj in obstacles) {
MeshRenderer ren = obj.GetComponent<MeshRenderer>();
accelerationStructure.AddInstance(ren);
}
//accelerationStructure.Update();
accelerationStructure.Build();
//cmd.BuildRayTracingAccelerationStructure(accelerationStructure);
cmd.SetRayTracingAccelerationStructure(shader, "_RaytracingAccelerationStructure", accelerationStructure);
cmd.SetRayTracingShaderPass(shader, "LidarPass");
cmd.SetRayTracingTextureParam(shader, Shader.PropertyToID("RenderTarget"), m_renderTex);
Vector4 position = scanner.RayOrigin.position;
Vector4 dir = scanner.RayOrigin.forward;
cmd.SetRayTracingVectorParam(shader, Shader.PropertyToID("originPos"), position);
cmd.SetRayTracingVectorParam(shader, Shader.PropertyToID("originDir"), dir);
cmd.DispatchRays(shader, "MyRaygenShader", scanner.SamplesPerScan, 1, 1);
RenderTexture.active = m_renderTex;
m_resultTex.ReadPixels(new Rect(0, 0, scanner.SamplesPerScan, 1), 0, 0);
var data = m_resultTex.GetPixels();
RenderTexture.active = null;
if (debug)
{
//Debug.Log("Size: " + m_resultTex.width);
for (int x = 0; x < m_resultTex.width; x++)
{
if (data[x] != new Color(0, 0, 0, 0))
Debug.Log(data[x]);
}
}
}
}
}
Raytrace Shader:
RWTexture2D<float4> RenderTarget;
float4 originPos;
float4 originDir;
#pragma max_recursion_depth 1
//Includes omitted
//...
struct RayPayload { float4 color; uint2 launchIdx; float3 position;};
[shader("raygeneration")]
void MyRaygenShader(){
uint2 launchIdx = DispatchRaysIndex().xy;
//uint2 launchDim = DispatchRaysDimensions().xy;
RayDesc ray;
ray.Origin = originPos;
ray.Direction = normalize(originDir);
ray.TMin = 0;
ray.TMax = _RaytracingRayMaxLength;
RayPayload payload;
payload.color = float4(0, 0, 0, 0);
payload.position = float3(0,0,0);
TraceRay(_RaytracingAccelerationStructure, RAY_FLAG_NONE, 0xFF, 0, 1, 0, ray, payload);
RenderTarget[launchIdx] = payload.color;
}
[shader("miss")]
void Miss(inout RayPayload payload : SV_RayPayload){
//rayDirection = WorldRayDirection();
payload.color = float4(0,0,0,0);
}
[shader("closesthit")]
void ClosestHit(inout RayPayload payload : SV_RayPayload, AttributeData attributeData : SV_IntersectionAttributes)
{
payload.color= float4(1, 1, 1, 1);
}
Thanks a lot for your help!