| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- #include "Common.hlsl"
- StructuredBuffer<int> textureIdBuffer : register(t1, space2);
- StructuredBuffer<int> indexBuffer : register(t1, space3);
- StructuredBuffer<VertexData> vertexData : register(t1, space4);
- StructuredBuffer<int> polygonSizeBuffer : register(t1, space5);
- ByteAddressBuffer lightBuffer : register(t1, space6);
- [shader("anyhit")]
- void AnyHit(inout RayHits rayPayload, SimpleBlocksAttributes attrib)
- {
- 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<float4> 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;
- }
- 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, 0);
- hit.dynamicLight = getLight(lightBuffer, 3);
- applyHit(rayPayload, hit);
- if (color.w < 1.f)
- {
- IgnoreHit();
- return;
- }
- }
|