Add a ray cast API to the terrain system, implement it, and use it so entities can be placed on top of terrain. (#6895)

* Add a ray cast API to the terrain system, implement it, and use it so entities can be placed on top of terrain.

Still to do:
- Unit tests
- Profiling/benchmarks
- Optimization (if needed, may not be now after updating to use the iterative approach, but one option could be to use SIMD to ray cast against both triangles at once)

Signed-off-by: bosnichd <bosnichd@amazon.com>

* Fix typos.

Signed-off-by: bosnichd <bosnichd@amazon.com>

* Added a FindNearestIntersectionIterative to use in place of the first-pass FindnearestIntersectionRecursive attempt.

Signed-off-by: bosnichd <bosnichd@amazon.com>

* Remove some functions that are no longer being used.

Signed-off-by: bosnichd <bosnichd@amazon.com>

* Remove a unicode character from a comment to fix the validation build.

Signed-off-by: bosnichd <bosnichd@amazon.com>

* Remove another unicode character from a comment to fix the validation build.

Signed-off-by: bosnichd <bosnichd@amazon.com>

* Update to bail out as soon as t > 1.0f

Signed-off-by: bosnichd <bosnichd@amazon.com>
This commit is contained in:
bosnichd
2022-01-24 13:47:44 -07:00
committed by GitHub
parent f8ac3071a3
commit f34d3a822e
9 changed files with 494 additions and 5 deletions
@@ -7,6 +7,7 @@
*/
#include <AzFramework/Render/IntersectorInterface.h>
#include <AzFramework/Terrain/TerrainDataRequestBus.h>
#include <AzToolsFramework/Viewport/ViewportMessages.h>
namespace AzToolsFramework
@@ -66,15 +67,19 @@ namespace AzToolsFramework
AZ::Vector3 FindClosestPickIntersection(const AzFramework::RenderGeometry::RayRequest& rayRequest, const float defaultDistance)
{
AzFramework::RenderGeometry::RayResult renderGeometryIntersectionResult;
// attempt a ray intersection with any visible mesh or terrain and return the intersection position if successful
AZ::EBusReduceResult<AzFramework::RenderGeometry::RayResult, AzFramework::RenderGeometry::RayResultClosestAggregator> renderGeometryIntersectionResult;
AzFramework::RenderGeometry::IntersectorBus::EventResult(
renderGeometryIntersectionResult, AzToolsFramework::GetEntityContextId(),
&AzFramework::RenderGeometry::IntersectorBus::Events::RayIntersect, rayRequest);
AzFramework::Terrain::TerrainDataRequestBus::BroadcastResult(
renderGeometryIntersectionResult,
&AzFramework::Terrain::TerrainDataRequests::GetClosestIntersection,
rayRequest);
// attempt a ray intersection with any visible mesh and return the intersection position if successful
if (renderGeometryIntersectionResult)
if (renderGeometryIntersectionResult.value)
{
return renderGeometryIntersectionResult.m_worldPosition;
return renderGeometryIntersectionResult.value.m_worldPosition;
}
else
{