[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:
@@ -129,9 +129,16 @@ namespace Vegetation
|
||||
}
|
||||
}
|
||||
|
||||
MeshBlockerComponent::MeshBlockerComponent()
|
||||
: AreaComponentBase()
|
||||
, m_nonUniformScaleChangedHandler([this]([[maybe_unused]] const AZ::Vector3& scale) { this->OnCompositionChanged(); })
|
||||
{
|
||||
}
|
||||
|
||||
MeshBlockerComponent::MeshBlockerComponent(const MeshBlockerConfig& configuration)
|
||||
: AreaComponentBase(configuration)
|
||||
, m_configuration(configuration)
|
||||
, m_nonUniformScaleChangedHandler([this]([[maybe_unused]] const AZ::Vector3& scale) { this->OnCompositionChanged(); })
|
||||
{
|
||||
}
|
||||
|
||||
@@ -139,6 +146,9 @@ namespace Vegetation
|
||||
{
|
||||
AZ::Render::MeshComponentNotificationBus::Handler::BusConnect(GetEntityId());
|
||||
|
||||
AZ::NonUniformScaleRequestBus::Event(
|
||||
GetEntityId(), &AZ::NonUniformScaleRequests::RegisterScaleChangedEvent, m_nonUniformScaleChangedHandler);
|
||||
|
||||
UpdateMeshData();
|
||||
m_refresh = false;
|
||||
|
||||
@@ -153,6 +163,7 @@ namespace Vegetation
|
||||
{
|
||||
AreaComponentBase::Deactivate(); //must deactivate base first to ensure AreaRequestBus disconnect waits for other threads
|
||||
|
||||
m_nonUniformScaleChangedHandler.Disconnect();
|
||||
SurfaceData::SurfaceDataSystemNotificationBus::Handler::BusDisconnect();
|
||||
|
||||
m_refresh = false;
|
||||
@@ -260,9 +271,10 @@ namespace Vegetation
|
||||
|
||||
AZ::Vector3 outPosition;
|
||||
AZ::Vector3 outNormal;
|
||||
const AZ::Vector3 rayOrigin(point.m_position.GetX(), point.m_position.GetY(), m_meshBoundsForIntersection.GetMax().GetZ());
|
||||
const AZ::Vector3 rayDirection = -AZ::Vector3::CreateAxisZ();
|
||||
bool intersected = SurfaceData::GetMeshRayIntersection(*mesh, m_meshWorldTM, m_meshWorldTMInverse, rayOrigin, rayDirection, outPosition, outNormal) &&
|
||||
const AZ::Vector3 rayStart(point.m_position.GetX(), point.m_position.GetY(), m_meshBoundsForIntersection.GetMax().GetZ());
|
||||
const AZ::Vector3 rayEnd(point.m_position.GetX(), point.m_position.GetY(), m_meshBoundsForIntersection.GetMin().GetZ());
|
||||
bool intersected = SurfaceData::GetMeshRayIntersection(
|
||||
*mesh, m_meshWorldTM, m_meshWorldTMInverse, m_meshNonUniformScale, rayStart, rayEnd, outPosition, outNormal) &&
|
||||
m_meshBoundsForIntersection.Contains(outPosition);
|
||||
m_cachedRayHits[point.m_handle] = intersected;
|
||||
return intersected;
|
||||
@@ -377,10 +389,10 @@ namespace Vegetation
|
||||
m_meshBoundsForIntersection.GetMin().GetZ() + m_meshBoundsForIntersection.GetExtents().GetZ() * m_configuration.m_meshHeightPercentMax);
|
||||
|
||||
AZ::Vector3 cornerMin = m_meshBoundsForIntersection.GetMin();
|
||||
cornerMin.SetZ(heights.first);
|
||||
cornerMin.SetZ(heights.first - s_rayAABBHeightPadding);
|
||||
|
||||
AZ::Vector3 cornerMax = m_meshBoundsForIntersection.GetMax();
|
||||
cornerMax.SetZ(heights.second);
|
||||
cornerMax.SetZ(heights.second + s_rayAABBHeightPadding);
|
||||
|
||||
m_meshBoundsForIntersection.Set(cornerMin, cornerMax);
|
||||
}
|
||||
@@ -392,6 +404,9 @@ namespace Vegetation
|
||||
AZ::TransformBus::EventResult(m_meshWorldTM, GetEntityId(), &AZ::TransformBus::Events::GetWorldTM);
|
||||
m_meshWorldTMInverse = m_meshWorldTM.GetInverse();
|
||||
|
||||
m_meshNonUniformScale = AZ::Vector3::CreateOne();
|
||||
AZ::NonUniformScaleRequestBus::EventResult(m_meshNonUniformScale, GetEntityId(), &AZ::NonUniformScaleRequests::GetScale);
|
||||
|
||||
AreaComponentBase::OnCompositionChanged();
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
#include <AzCore/Asset/AssetCommon.h>
|
||||
#include <AzCore/Component/Component.h>
|
||||
#include <AzCore/Component/NonUniformScaleBus.h>
|
||||
#include <AzCore/Component/TickBus.h>
|
||||
#include <Vegetation/Ebuses/AreaRequestBus.h>
|
||||
#include <Vegetation/Ebuses/MeshBlockerRequestBus.h>
|
||||
@@ -62,7 +63,7 @@ namespace Vegetation
|
||||
static void Reflect(AZ::ReflectContext* context);
|
||||
|
||||
MeshBlockerComponent(const MeshBlockerConfig& configuration);
|
||||
MeshBlockerComponent() = default;
|
||||
MeshBlockerComponent();
|
||||
~MeshBlockerComponent() = default;
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
@@ -122,6 +123,8 @@ namespace Vegetation
|
||||
MeshBlockerConfig m_configuration;
|
||||
AZStd::atomic_bool m_refresh{ false };
|
||||
|
||||
AZ::NonUniformScaleChangedEvent::Handler m_nonUniformScaleChangedHandler; ///< Responds to changes in non-uniform scale.
|
||||
|
||||
// cached data
|
||||
mutable AZStd::recursive_mutex m_cacheMutex;
|
||||
AZ::Data::Asset<AZ::Data::AssetData> m_meshAssetData;
|
||||
@@ -129,9 +132,12 @@ namespace Vegetation
|
||||
AZ::Transform m_meshWorldTMInverse = AZ::Transform::CreateIdentity();
|
||||
AZ::Aabb m_meshBounds = AZ::Aabb::CreateNull();
|
||||
AZ::Aabb m_meshBoundsForIntersection = AZ::Aabb::CreateNull();
|
||||
AZ::Vector3 m_meshNonUniformScale = AZ::Vector3::CreateOne();
|
||||
bool m_meshVisible = false;
|
||||
|
||||
using CachedRayHits = AZStd::unordered_map<ClaimHandle, bool>;
|
||||
CachedRayHits m_cachedRayHits;
|
||||
|
||||
static constexpr float s_rayAABBHeightPadding = 0.1f;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user