Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wc/snap to terrain - CsvSpawner: Make "snap to terrain" based on collision layer. #74

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.vscode/**
.idea/**
59 changes: 56 additions & 3 deletions Gems/CsvSpawner/Code/Source/CsvSpawner/CsvSpawnerUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ namespace CsvSpawner::CsvSpawnerUtils
->Field("PositionStdDev", &CsvSpawnableAssetConfiguration::m_positionStdDev)
->Field("RotationStdDev", &CsvSpawnableAssetConfiguration::m_rotationStdDev)
->Field("ScaleStdDev", &CsvSpawnableAssetConfiguration::m_scaleStdDev)
->Field("PlaceOnTerrain", &CsvSpawnableAssetConfiguration::m_placeOnTerrain);
->Field("PlaceOnTerrain", &CsvSpawnableAssetConfiguration::m_placeOnTerrain)
->Field("CollisionLayer", &CsvSpawnableAssetConfiguration::m_selectedCollisionLayer);

if (auto* editContext = serializeContext->GetEditContext())
{
Expand Down Expand Up @@ -78,15 +79,31 @@ namespace CsvSpawner::CsvSpawnerUtils
&CsvSpawnableAssetConfiguration::m_placeOnTerrain,
"Place on Terrain",
"Perform scene query raytrace to place on terrain")
->Attribute(AZ::Edit::Attributes::ChangeNotify, &CsvSpawnableAssetConfiguration::OnPlaceOnTerrainChanged)
->DataElement(
AZ::Edit::UIHandlers::Default,
&CsvSpawnableAssetConfiguration::m_scaleStdDev,
"Scale Std. Dev.",
"Scale standard deviation, in meters");
"Scale standard deviation, in meters")
->DataElement(AZ::Edit::UIHandlers::Default,
&CsvSpawnableAssetConfiguration::m_selectedCollisionLayer,
"Collision Layer",
"To which collision layer this target will be attached")
->Attribute(AZ::Edit::Attributes::ReadOnly, &CsvSpawnableAssetConfiguration::IsCollisionLayerEnabled);
}
}
}

bool CsvSpawnableAssetConfiguration::IsCollisionLayerEnabled() const
{
return !m_placeOnTerrain;
}

AZ::Crc32 CsvSpawnableAssetConfiguration::OnPlaceOnTerrainChanged()
{
return AZ::Edit::PropertyRefreshLevels::EntireTree;
}

AZStd::unordered_map<AZStd::string, CsvSpawnableAssetConfiguration> GetSpawnableAssetFromVector(
AZStd::vector<CsvSpawnableAssetConfiguration> spawnableAssetConfigurations)
{
Expand Down Expand Up @@ -161,6 +178,42 @@ namespace CsvSpawner::CsvSpawnerUtils
return hitPosition;
}

AZStd::optional<AZ::Vector3> RaytraceTerrain(
const AZ::Vector3& location, const AzPhysics::SceneHandle sceneHandle, const AZ::Vector3& gravityDirection, float maxDistance, AzPhysics::CollisionLayer collisionLayer)
{
AZStd::optional<AZ::Vector3> hitPosition = AZStd::nullopt;

if (sceneHandle == AzPhysics::InvalidSceneHandle)
michalpelka marked this conversation as resolved.
Show resolved Hide resolved
{
return hitPosition;
}

auto* physicsSystem = AZ::Interface<AzPhysics::SystemInterface>::Get();
auto* sceneInterface = AZ::Interface<AzPhysics::SceneInterface>::Get();
AZ_Assert(physicsSystem, "Unable to get physics system interface");
AZ_Assert(sceneInterface, "Unable to get physics scene interface");

if (!sceneInterface || !physicsSystem)
{
return hitPosition;
}

AzPhysics::RayCastRequest request;
request.m_start = location;
request.m_direction = gravityDirection;
request.m_distance = maxDistance;
request.m_collisionGroup.SetLayer(collisionLayer, true);

AzPhysics::SceneQueryHits result = sceneInterface->QueryScene(sceneHandle, &request);

if (!result.m_hits.empty())
{
hitPosition = result.m_hits.front().m_position;
}

return hitPosition;
}

AZStd::unordered_map<int, AzFramework::EntitySpawnTicket> SpawnEntities(
const AZStd::vector<CsvSpawnableEntityInfo>& entitiesToSpawn,
const AZStd::unordered_map<AZStd::string, CsvSpawnableAssetConfiguration>& spawnableAssetConfiguration,
Expand Down Expand Up @@ -212,7 +265,7 @@ namespace CsvSpawner::CsvSpawnerUtils
if (spawnConfig.m_placeOnTerrain)
{
const AZStd::optional<AZ::Vector3> hitPosition =
RaytraceTerrain(transform.GetTranslation(), sceneHandle, -AZ::Vector3::CreateAxisZ(), 1000.0f);
RaytraceTerrain(transform.GetTranslation(), sceneHandle, -AZ::Vector3::CreateAxisZ(), 1000.0f, spawnConfig.m_selectedCollisionLayer);
if (hitPosition.has_value())
{
transform.SetTranslation(hitPosition.value());
Expand Down
5 changes: 5 additions & 0 deletions Gems/CsvSpawner/Code/Source/CsvSpawner/CsvSpawnerUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ namespace CsvSpawner::CsvSpawnerUtils
AZ::Vector3 m_rotationStdDev{ 0.f }; //!< Standard deviation for rotation
float m_scaleStdDev{ 0.1f }; //!< Standard deviation for scale
bool m_placeOnTerrain{ false }; //!< Whether to raytrace to terrain and place the entity on the terrain
AzPhysics::CollisionLayer m_selectedCollisionLayer; //!< To which collision layer this target will be attached

private:
bool IsCollisionLayerEnabled() const;
AZ::Crc32 OnPlaceOnTerrainChanged();
};

//! This function create map of spawnable asset configuration from vector of spawnable asset configuration, where
Expand Down