[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:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user