[LYN-3099] Fixed some issues with vegetation planting on surfaces (#1554)

* [LYN-3099] Fix vegetation raycasts to use bounded ray queries instead of FLT_MAX.
Raycasts with a distance of FLT_MAX sometimes overflowed deep in IntersectSegmentTriangleCCW, so it's better to have strict start/end positional queries.  We have specific starts and ends anyways, so it's a safer approach anyways.
This also adds support for Non-Uniform Scale for meshes, since it was clearly not working correctly in vegetation when testing various scaled meshes.

* Addressed PR feedback
This commit is contained in:
Mike Balfour
2021-06-24 12:54:48 -05:00
committed by GitHub
parent 2a1a523091
commit c873ddac45
6 changed files with 65 additions and 21 deletions
@@ -11,23 +11,28 @@
namespace SurfaceData
{
bool GetMeshRayIntersection(
const AZ::RPI::ModelAsset& meshAsset, const AZ::Transform& meshTransform, const AZ::Transform& meshTransformInverse,
const AZ::Vector3& rayOrigin, const AZ::Vector3& rayDirection, AZ::Vector3& outPosition, AZ::Vector3& outNormal)
const AZ::RPI::ModelAsset& meshAsset, const AZ::Transform& meshTransform,
const AZ::Transform& meshTransformInverse, const AZ::Vector3& nonUniformScale,
const AZ::Vector3& rayStart, const AZ::Vector3& rayEnd,
AZ::Vector3& outPosition, AZ::Vector3& outNormal)
{
AZ_PROFILE_FUNCTION(AZ::Debug::ProfileCategory::Entity);
const AZ::Vector3 clampedScale = nonUniformScale.GetMax(AZ::Vector3(AZ::MinTransformScale));
// Transform everything into model space
const AZ::Vector3 rayOriginLocal = meshTransformInverse.TransformPoint(rayOrigin);
const AZ::Vector3 rayDirectionLocal = meshTransformInverse.TransformVector(rayDirection).GetNormalized();
float distance = FLT_MAX;
const AZ::Vector3 rayStartLocal = meshTransformInverse.TransformPoint(rayStart) / clampedScale;
const AZ::Vector3 rayEndLocal = meshTransformInverse.TransformPoint(rayEnd) / clampedScale;
const AZ::Vector3 rayDirectionLocal = (rayEndLocal - rayStartLocal).GetNormalized();
float distance = rayEndLocal.GetDistance(rayStartLocal);
AZ::Vector3 normalLocal;
if (meshAsset.LocalRayIntersectionAgainstModel(rayOriginLocal, rayDirectionLocal, distance, normalLocal))
if (meshAsset.LocalRayIntersectionAgainstModel(rayStartLocal, rayDirectionLocal, distance, normalLocal))
{
// Transform everything back to world space
outPosition = meshTransform.TransformPoint(rayOriginLocal + (rayDirectionLocal * distance));
outNormal = meshTransform.TransformVector(normalLocal);
outPosition = meshTransform.TransformPoint((rayStartLocal + (rayDirectionLocal * distance)) * clampedScale);
outNormal = meshTransform.TransformVector(normalLocal * clampedScale);
return true;
}