Hit.hlsl 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  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("closesthit")]
  14. void ClosestHit(inout HitInfo payload, Attributes attrib)
  15. {
  16. //payload.colorAndDistance = float4(1, 1, 1, 1.0);
  17. int currentTriangle = PrimitiveIndex();
  18. int textureId = 0;
  19. for (int i = 0; currentTriangle >= 0; i++)
  20. {
  21. currentTriangle -= polygonSizeBuffer[i];
  22. textureId = textureIdBuffer[i];
  23. }
  24. int index = PrimitiveIndex() * 3;
  25. VertexData v0 = vertexData[indexBuffer[index]];
  26. VertexData v1 = vertexData[indexBuffer[index + 1]];
  27. VertexData v2 = vertexData[indexBuffer[index + 2]];
  28. float2 texcoord = v0.texcoord * (1 - attrib.bary.x - attrib.bary.y) + v1.texcoord * attrib.bary.x + v2.texcoord * attrib.bary.y;
  29. float4 color = textures[textureId].SampleLevel(gSampler, texcoord, 0);
  30. payload.colorAndDistance = float4(color.rgb, RayTCurrent());
  31. }