| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- #include "Common.hlsl"
- StructuredBuffer<int> textureIdBuffer : register(t1, space2);
- StructuredBuffer<int> indexBuffer : register(t1, space3);
- StructuredBuffer<VertexData> vertexData : register(t1, space4);
- StructuredBuffer<int> indexOffsetBuffer : register(t1, space5);
- ByteAddressBuffer lightBuffer : register(t1, space6);
- [shader("anyhit")]
- void SimpleBlocksAnyHit(inout RayHits rayPayload, SimpleBlocksAttributes attrib)
- {
- int currentTriangle = PrimitiveIndex();
- int geometryIndex = GeometryIndex();
- // Bounds check for geometryIndex
- if (geometryIndex < 0)
- {
- IgnoreHit();
- return;
- }
- int textureId = textureIdBuffer[geometryIndex];
- Texture2D<float4> texture = textures[textureId];
- int indexOffset = indexOffsetBuffer[geometryIndex];
- int index = indexOffset + currentTriangle * 3;
- // Bounds check for indices
- if (indexBuffer[index] < 0 || indexBuffer[index + 1] < 0 || indexBuffer[index + 2] < 0)
- {
- IgnoreHit();
- return;
- }
- VertexData v0 = vertexData[indexBuffer[index]];
- VertexData v1 = vertexData[indexBuffer[index + 1]];
- VertexData v2 = vertexData[indexBuffer[index + 2]];
- float3 normal = v0.normal * (1 - attrib.bary.x - attrib.bary.y) + v1.normal * attrib.bary.x + v2.normal * attrib.bary.y;
- float2 texcoord = v0.texcoord * (1 - attrib.bary.x - attrib.bary.y) + v1.texcoord * attrib.bary.x + v2.texcoord * attrib.bary.y;
- float4 color = texture.SampleLevel(gSampler, texcoord, 0);
- if (color.w == 0.f)
- {
- IgnoreHit();
- return;
- }
- if (rayPayload.hitCount < 0)
- { // shadow ray
- rayPayload.hitCount = 0;
- // TODO: transparent objects should lead to half shadows
- AcceptHitAndEndSearch();
- return;
- }
- SingleHit hit;
- hit.color = color;
- hit.normal = normal;
- hit.distance = RayTCurrent();
- hit.dayLight = getLight(lightBuffer, geometryIndex * 6);
- hit.dynamicLight = getLight(lightBuffer, geometryIndex * 6 + 3);
- applyHit(rayPayload, hit);
- if (color.w < 1.f)
- {
- IgnoreHit();
- return;
- }
- }
|