DefaultAnyHit.hlsl 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include "Common.hlsl"
  2. StructuredBuffer<int> textureIdBuffer : register(t1, space2);
  3. StructuredBuffer<int> indexBuffer : register(t1, space3);
  4. StructuredBuffer<VertexData> vertexData : register(t1, space4);
  5. StructuredBuffer<int> polygonSizeBuffer : register(t1, space5);
  6. [shader("anyhit")]
  7. void AnyHit(inout RayHits rayPayload, SimpleBlocksAttributes attrib)
  8. {
  9. if (rayPayload.hitCount < 0)
  10. { // shadow ray
  11. rayPayload.hitCount = 0;
  12. // TODO: transparent objects should lead to half shadows
  13. AcceptHitAndEndSearch();
  14. return;
  15. }
  16. int instanceId = InstanceID();
  17. int currentTriangle = PrimitiveIndex();
  18. int textureId = 0;
  19. int i;
  20. for (i = 0; currentTriangle >= 0 && polygonSizeBuffer[i] > 0; i++)
  21. {
  22. currentTriangle -= polygonSizeBuffer[i];
  23. textureId = textureIdBuffer[i];
  24. }
  25. if (polygonSizeBuffer[i] <= 0)
  26. {
  27. // Could not find geometry index, ignore hit
  28. IgnoreHit();
  29. return;
  30. }
  31. Texture2D<float4> texture = textures[textureId];
  32. int index = PrimitiveIndex() * 3;
  33. // Bounds check for vertex indices
  34. if (indexBuffer[index] < 0 || indexBuffer[index + 1] < 0 || indexBuffer[index + 2] < 0)
  35. {
  36. IgnoreHit();
  37. return;
  38. }
  39. VertexData v0 = vertexData[indexBuffer[index]];
  40. VertexData v1 = vertexData[indexBuffer[index + 1]];
  41. VertexData v2 = vertexData[indexBuffer[index + 2]];
  42. float3 normal = v0.normal * (1 - attrib.bary.x - attrib.bary.y) + v1.normal * attrib.bary.x + v2.normal * attrib.bary.y;
  43. float2 texcoord = v0.texcoord * (1 - attrib.bary.x - attrib.bary.y) + v1.texcoord * attrib.bary.x + v2.texcoord * attrib.bary.y;
  44. float4 color = texture.SampleLevel(gSampler, texcoord, 0);
  45. if (color.w == 0.f)
  46. {
  47. IgnoreHit();
  48. return;
  49. }
  50. SingleHit hit;
  51. hit.color = color;
  52. hit.normal = normal;
  53. hit.distance = RayTCurrent();
  54. hit.dayLight = 0xFFFFFF;
  55. hit.dynamicLight = 0xFFFFFF;
  56. applyHit(rayPayload, hit);
  57. if (color.w < 1.f)
  58. {
  59. IgnoreHit();
  60. return;
  61. }
  62. }