123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- /*
- * 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)
- {
- 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;
- }
- 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;
- }
- }
- }
- 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;
- if (IntersectRayAABB(ObjectRayOrigin(), ObjectRayDirection(), aabbMin, aabbMax, t, normal))
- {
- ProceduralGeometryIntersectionAttributes attrib;
- attrib.m_position = ObjectRayOrigin() + ObjectRayDirection() * t;
- attrib.m_normal = half3(normal);
- attrib.m_uv = half2(0, 0);
- attrib.m_tangent = half4(1, 0, 0, 1);
- ReportHit(t, 0, attrib);
- }
- }
|