DefaultAnyHit.hlsl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include "Common.hlsl"
  2. Texture2D<float4> textures[] : register(t0, space1);
  3. SamplerState gSampler : register(s0, space0);
  4. struct VertexData
  5. {
  6. float2 texcoord;
  7. float3 normal;
  8. };
  9. StructuredBuffer<int> textureIdBuffer : register(t1, space2);
  10. StructuredBuffer<int> indexBuffer : register(t1, space3);
  11. StructuredBuffer<VertexData> vertexData : register(t1, space4);
  12. StructuredBuffer<int> polygonSizeBuffer : register(t1, space5);
  13. [shader("anyhit")]
  14. void AnyHit(inout HitInfo payload, SimpleBlocksAttributes attrib)
  15. {
  16. //payload.colorAndDistance = float4(1, 1, 1, 1.0);
  17. int instanceId = InstanceID();
  18. int currentTriangle = PrimitiveIndex();
  19. int textureId = 0;
  20. for (int i = 0; currentTriangle >= 0; i++)
  21. {
  22. currentTriangle -= polygonSizeBuffer[i];
  23. textureId = textureIdBuffer[i];
  24. }
  25. Texture2D<float4> texture = textures[textureId];
  26. int index = PrimitiveIndex() * 3;
  27. VertexData v0 = vertexData[indexBuffer[index]];
  28. VertexData v1 = vertexData[indexBuffer[index + 1]];
  29. VertexData v2 = vertexData[indexBuffer[index + 2]];
  30. float2 texcoord = v0.texcoord * (1 - attrib.bary.x - attrib.bary.y) + v1.texcoord * attrib.bary.x + v2.texcoord * attrib.bary.y;
  31. float4 color = texture.SampleLevel(gSampler, texcoord, 0);
  32. if (color.w == 0.f)
  33. {
  34. IgnoreHit();
  35. }
  36. float distance = RayTCurrent();
  37. bool found = false;
  38. for (int i = 0; i < payload.hitCount; i++)
  39. {
  40. if (payload.distance[i] > distance)
  41. {
  42. found = true;
  43. float4 tmpColor = payload.color[i];
  44. payload.color[i] = color;
  45. float tmpDistance = payload.distance[i];
  46. payload.distance[i] = distance;
  47. int tmpDayLight = payload.dayLight[i];
  48. payload.dayLight[i] = 0xFFFFFF;
  49. int tmpDynamicLight = payload.dynamicLight[i];
  50. payload.dynamicLight[i] = 0xFFFFFF;
  51. if (color.w == 1.f)
  52. {
  53. payload.hitCount = i + 1;
  54. }
  55. else
  56. {
  57. for (int j = i + 1; j < payload.hitCount + 1 && j < MAX_TRANSPACENT_HITS; j++)
  58. {
  59. float4 tmpColor2 = payload.color[j];
  60. float tmpDistance2 = payload.distance[j];
  61. int tmpDayLight2 = payload.dayLight[j];
  62. int tmpDynamicLight2 = payload.dynamicLight[j];
  63. payload.color[j] = tmpColor;
  64. payload.distance[j] = tmpDistance;
  65. payload.dayLight[j] = tmpDayLight;
  66. payload.dynamicLight[j] = tmpDynamicLight;
  67. tmpColor = tmpColor2;
  68. tmpDistance = tmpDistance2;
  69. tmpDayLight = tmpDayLight2;
  70. tmpDynamicLight = tmpDynamicLight2;
  71. }
  72. }
  73. break;
  74. }
  75. else
  76. {
  77. if (payload.color[i].w == 1.f)
  78. {
  79. found = true;
  80. break;
  81. }
  82. }
  83. }
  84. if (!found && payload.hitCount < MAX_TRANSPACENT_HITS)
  85. {
  86. payload.color[payload.hitCount] = color;
  87. payload.distance[payload.hitCount] = distance;
  88. payload.dayLight[payload.hitCount] = 0xFFFFFF;
  89. payload.dynamicLight[payload.hitCount] = 0xFFFFFF;
  90. payload.hitCount++;
  91. }
  92. if (color.w < 1.f)
  93. {
  94. IgnoreHit();
  95. }
  96. }