Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- uint IntersectNode(float4 origin, float4 direction, uint octantInverse, float maxDistance, float4 node0, float4 node1, float4 node2, float4 node3, float4 node4)
- {
- float3 p = node0.xyz;
- uint emask = asuint(node0.w);
- uint eX = ExtractByte(emask, 0);
- uint eY = ExtractByte(emask, 1);
- uint eZ = ExtractByte(emask, 2);
- float3 adjDirection = float3(
- asfloat(eX << 23) / direction.x,
- asfloat(eY << 23) / direction.y,
- asfloat(eZ << 23) / direction.z
- );
- float3 adjOrigin = (p - origin.xyz) / direction.xyz;
- uint hitMask = 0;
- [unroll]
- for (int i = 0; i < 2; i++)
- {
- uint meta4 = asuint(i == 0 ? node1.z : node1.w);
- uint isInner4 = (meta4 & (meta4 << 1)) & 0x10101010;
- uint innerMask4 = (((isInner4 << 3) >> 7) & 0x01010101) * 0xff;
- uint bitIndex4 = (meta4 ^ (octantInverse & innerMask4)) & 0x1F1F1F1F;
- uint childBits4 = (meta4 >> 5) & 0x07070707;
- uint qLoX = asuint(i == 0 ? node2.x : node2.y);
- uint qHiX = asuint(i == 0 ? node2.z : node2.w);
- uint qLoY = asuint(i == 0 ? node3.x : node3.y);
- uint qHiY = asuint(i == 0 ? node3.z : node3.w);
- uint qLoZ = asuint(i == 0 ? node4.x : node4.y);
- uint qHiZ = asuint(i == 0 ? node4.z : node4.w);
- uint xMin = direction.x < 0.0f ? qHiX : qLoX;
- uint xMax = direction.x < 0.0f ? qLoX : qHiX;
- uint yMin = direction.y < 0.0f ? qHiY : qLoY;
- uint yMax = direction.y < 0.0f ? qLoY : qHiY;
- uint zMin = direction.z < 0.0f ? qHiZ : qLoZ;
- uint zMax = direction.z < 0.0f ? qLoZ : qHiZ;
- [unroll]
- for (int j = 0; j < 4; j++)
- {
- float3 tmin3 = float3(
- float(ExtractByte(xMin, j)),
- float(ExtractByte(yMin, j)),
- float(ExtractByte(zMin, j)));
- float3 tmax3 = float3(
- float(ExtractByte(xMax, j)),
- float(ExtractByte(yMax, j)),
- float(ExtractByte(zMax, j)));
- tmin3 = tmin3 * adjDirection + adjOrigin;
- tmax3 = tmax3 * adjDirection + adjOrigin;
- float tmin = max(max(tmin3.x, tmin3.y), max(tmin3.z, 0.0f));
- float tmax = min(min(tmax3.x, tmax3.y), min(tmax3.z, maxDistance));
- bool intersection = tmin < tmax;
- [branch]
- if (intersection)
- {
- uint childBits = ExtractByte(childBits4, j);
- uint bitIndex = ExtractByte(bitIndex4, j);
- hitMask |= childBits << bitIndex;
- }
- }
- }
- return hitMask;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement