SimpleBlocksAnyHit.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> indexOffsetBuffer : register(t1, space5);
  6. [shader("anyhit")]
  7. void SimpleBlocksAnyHit(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 geometryIndex = GeometryIndex();
  19. // Bounds check for geometryIndex
  20. if (geometryIndex < 0)
  21. {
  22. IgnoreHit();
  23. return;
  24. }
  25. int textureId = textureIdBuffer[geometryIndex];
  26. Texture2D<float4> texture = textures[textureId];
  27. int indexOffset = indexOffsetBuffer[geometryIndex];
  28. int index = indexOffset + currentTriangle * 3;
  29. // Bounds check for indices
  30. if (indexBuffer[index] < 0 || indexBuffer[index + 1] < 0 || indexBuffer[index + 2] < 0)
  31. {
  32. IgnoreHit();
  33. return;
  34. }
  35. VertexData v0 = vertexData[indexBuffer[index]];
  36. VertexData v1 = vertexData[indexBuffer[index + 1]];
  37. VertexData v2 = vertexData[indexBuffer[index + 2]];
  38. float3 normal = v0.normal * (1 - attrib.bary.x - attrib.bary.y) + v1.normal * attrib.bary.x + v2.normal * attrib.bary.y;
  39. float2 texcoord = v0.texcoord * (1 - attrib.bary.x - attrib.bary.y) + v1.texcoord * attrib.bary.x + v2.texcoord * attrib.bary.y;
  40. float4 color = texture.SampleLevel(gSampler, texcoord, 0);
  41. if (color.w == 0.f)
  42. {
  43. IgnoreHit();
  44. return;
  45. }
  46. SingleHit hit;
  47. hit.color = color;
  48. hit.normal = normal;
  49. hit.distance = RayTCurrent();
  50. hit.dayLight = 0xFFFFFF;
  51. hit.dynamicLight = 0xFFFFFF;
  52. applyHit(rayPayload, hit);
  53. if (color.w < 1.f)
  54. {
  55. IgnoreHit();
  56. return;
  57. }
  58. }