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