12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #include <Atom/Features/RayTracing/RayTracingIntersectionAttributes.azsli>
- #include <Atom/Features/SrgSemantics.azsli>
- // https://github.com/erich666/GraphicsGems/blob/master/gems/RayBox.c
- bool IntersectRayAABB(float3 rayOrigin, float3 rayDirection, float3 aabbMin, float3 aabbMax, out float t, out float3 normal, out float2 uv)
- {
- t = 0.f;
- normal = float3(1, 0, 0);
- float3 maxT;
- for (int i = 0; i < 3; i++)
- {
- if ((rayOrigin[i] < aabbMin[i] || rayOrigin[i] > aabbMax[i]) && rayDirection[i] != 0)
- {
- maxT[i] = ((rayOrigin[i] < aabbMin[i] ? aabbMin[i] : aabbMax[i]) - rayOrigin[i]) / rayDirection[i];
- }
- else
- {
- maxT[i] = -1;
- }
- }
- int whichPlane = 0;
- for (int i = 1; i < 3; i++)
- {
- if (maxT[whichPlane] < maxT[i])
- {
- whichPlane = i;
- }
- }
- t = maxT[whichPlane];
- if (t < 0)
- {
- return false;
- }
- int uvAxis = 0;
- for (int i = 0; i < 3; i++)
- {
- if (whichPlane != i)
- {
- float coord = rayOrigin[i] + t * rayDirection[i];
- if (coord < aabbMin[i] || coord > aabbMax[i])
- {
- return false;
- }
- uv[uvAxis++] = (coord - aabbMin[i]) / (aabbMax[i] - aabbMin[i]);
- }
- }
- normal = float3(0, 0, 0);
- normal[whichPlane] = rayDirection[whichPlane] > 0 ? -1 : 1;
- return true;
- }
- [shader("intersection")]
- void ObbIntersection()
- {
- const float3 aabbMin = float3(-1, -1, -1);
- const float3 aabbMax = float3(1, 1, 1);
- float t;
- float3 normal;
- float2 uv;
- if (IntersectRayAABB(ObjectRayOrigin(), ObjectRayDirection(), aabbMin, aabbMax, t, normal, uv))
- {
- ProceduralGeometryIntersectionAttributes attrib;
- attrib.SetPosition(ObjectRayOrigin() + ObjectRayDirection() * t);
- attrib.SetNormal(normal);
- attrib.SetUv(uv);
- attrib.SetTangent(float4(1, 0, 0, 1));
- ReportHit(t, 0, attrib);
- }
- }
|