#include "Common.hlsl" StructuredBuffer textureIdBuffer : register(t1, space2); StructuredBuffer indexBuffer : register(t1, space3); StructuredBuffer vertexData : register(t1, space4); StructuredBuffer polygonSizeBuffer : register(t1, space5); [shader("anyhit")] void AnyHit(inout RayHits rayPayload, SimpleBlocksAttributes attrib) { if (rayPayload.hitCount < 0) { // shadow ray rayPayload.hitCount = 0; // TODO: transparent objects should lead to half shadows AcceptHitAndEndSearch(); return; } int instanceId = InstanceID(); int currentTriangle = PrimitiveIndex(); int textureId = 0; int i; for (i = 0; currentTriangle >= 0 && polygonSizeBuffer[i] > 0; i++) { currentTriangle -= polygonSizeBuffer[i]; textureId = textureIdBuffer[i]; } if (polygonSizeBuffer[i] <= 0) { // Could not find geometry index, ignore hit IgnoreHit(); return; } Texture2D texture = textures[textureId]; int index = PrimitiveIndex() * 3; // Bounds check for vertex 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; } SingleHit hit; hit.color = color; hit.normal = normal; hit.distance = RayTCurrent(); hit.dayLight = 0xFFFFFF; hit.dynamicLight = 0xFFFFFF; applyHit(rayPayload, hit); if (color.w < 1.f) { IgnoreHit(); return; } }