Skip to content

Commit 2624b33

Browse files
w-czerskimichalpelkapatrykantosz
authored
Wc/snap to terrain - CsvSpawner: Make "snap to terrain" based on collision layer. (#74)
CsvSpawner snap to object based on a collision layer --------- Signed-off-by: Michał Pełka <[email protected]> Signed-off-by: Wojciech Czerski <[email protected]> Signed-off-by: Patryk Antosz <[email protected]> Co-authored-by: Michał Pełka <[email protected]> Co-authored-by: Patryk Antosz <[email protected]>
1 parent 31d1788 commit 2624b33

File tree

6 files changed

+112
-32
lines changed

6 files changed

+112
-32
lines changed

Gems/CsvSpawner/Code/Source/CsvSpawner/CsvSpawnerComponent.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,19 @@ namespace CsvSpawner
4747
AZ::TickBus ::Handler::BusDisconnect();
4848
}
4949

50+
int CsvSpawnerComponent::GetTickOrder()
51+
{
52+
return AZ::TICK_LAST;
53+
}
54+
5055
void CsvSpawnerComponent::Activate()
5156
{
52-
AZ::TickBus ::Handler::BusConnect();
57+
AZ::TickBus::Handler::BusConnect();
5358
}
5459

5560
void CsvSpawnerComponent::Deactivate()
5661
{
57-
AZ::TickBus ::Handler::BusDisconnect();
62+
AZ::TickBus::Handler::BusDisconnect();
5863
}
5964

6065
} // namespace CsvSpawner

Gems/CsvSpawner/Code/Source/CsvSpawner/CsvSpawnerComponent.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ namespace CsvSpawner
4848
private:
4949
// AZ::TickBus::Handler overrides
5050
void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
51+
int GetTickOrder() override;
5152

5253
AZStd::unordered_map<AZStd::string, CsvSpawnableAssetConfiguration> m_spawnableAssetConfigurations; //!< List of assets to spawn
5354
AZStd::vector<CsvSpawnableEntityInfo> m_spawnableEntityInfo; //!< List of entities to spawn

Gems/CsvSpawner/Code/Source/CsvSpawner/CsvSpawnerEditorComponent.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
#include "CsvSpawnerComponent.h"
1414
#include "CsvSpawnerCsvParser.h"
1515
#include "CsvSpawnerUtils.h"
16+
1617
#include <AzCore/Component/TransformBus.h>
1718
#include <AzCore/IO/Path/Path.h>
1819
#include <AzCore/Serialization/EditContext.h>
1920
#include <AzFramework/Physics/Common/PhysicsTypes.h>
2021
#include <AzToolsFramework/API/EditorAssetSystemAPI.h>
2122
#include <AzToolsFramework/Viewport/ViewportMessages.h>
23+
#include <thread>
2224

2325
namespace CsvSpawner
2426
{
@@ -83,18 +85,15 @@ namespace CsvSpawner
8385
AzFramework::ViewportDebugDisplayEventBus::Handler::BusConnect(AzToolsFramework::GetEntityContextId());
8486
}
8587

86-
AZ::TickBus::QueueFunction(
87-
[this]()
88-
{
89-
SpawnEntities();
90-
});
88+
AZ::TickBus::Handler::BusConnect();
9189
}
9290

9391
void CsvSpawnerEditorComponent::Deactivate()
9492
{
9593
m_spawnedTickets.clear();
9694

9795
AzFramework::ViewportDebugDisplayEventBus::Handler::BusDisconnect();
96+
AZ::TickBus::Handler::BusDisconnect();
9897
AzToolsFramework::Components::EditorComponentBase::Deactivate();
9998
}
10099

@@ -122,6 +121,23 @@ namespace CsvSpawner
122121
m_spawnedTickets.clear();
123122
}
124123

124+
void CsvSpawnerEditorComponent::OnTick(float deltaTime, AZ::ScriptTimePoint time)
125+
{
126+
++m_frameCounter;
127+
128+
if (m_frameCounter == 2)
129+
{
130+
SpawnEntities();
131+
AZ::TickBus::Handler::BusDisconnect();
132+
m_frameCounter = 0;
133+
}
134+
}
135+
136+
int CsvSpawnerEditorComponent::GetTickOrder()
137+
{
138+
return AZ::TICK_LAST;
139+
}
140+
125141
void CsvSpawnerEditorComponent::SpawnEntities()
126142
{
127143
if (m_csvAssetId.IsValid() == false)

Gems/CsvSpawner/Code/Source/CsvSpawner/CsvSpawnerEditorComponent.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
#pragma once
1212

13+
#include "API/ToolsApplicationAPI.h"
1314
#include "CsvSpawnerUtils.h"
15+
1416
#include <AzCore/Asset/AssetCommon.h>
1517
#include <AzCore/Component/TickBus.h>
1618
#include <AzFramework/Entity/EntityContextBus.h>
@@ -28,6 +30,7 @@ namespace CsvSpawner
2830
class CsvSpawnerEditorComponent
2931
: public AzToolsFramework::Components::EditorComponentBase
3032
, protected AzFramework::ViewportDebugDisplayEventBus::Handler
33+
, protected AZ::TickBus::Handler
3134
{
3235
public:
3336
AZ_EDITOR_COMPONENT(CsvSpawnerEditorComponent, CsvSpawnerEditorComponentTypeId);
@@ -40,6 +43,8 @@ namespace CsvSpawner
4043
void Activate() override;
4144
void Deactivate() override;
4245
void BuildGameEntity(AZ::Entity* gameEntity) override;
46+
void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
47+
int GetTickOrder() override;
4348

4449
private:
4550
// EntityDebugDisplayEventBus::Handler overrides
@@ -50,6 +55,7 @@ namespace CsvSpawner
5055
void OnOnShowLabelsChanged();
5156

5257
void SpawnEntities();
58+
int m_frameCounter;
5359

5460
AZStd::vector<CsvSpawnerUtils::CsvSpawnableAssetConfiguration>
5561
m_spawnableAssetConfigurations; //!< List of spawnable "types" (e.g. pineCsv, oakTre, mapleCsv, etc.)

Gems/CsvSpawner/Code/Source/CsvSpawner/CsvSpawnerUtils.cpp

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
*
44
* This source code is protected under international copyright law. All rights
55
* reserved and protected by the copyright holders.
6-
* This file is confidential and only available to authorized individuals with the
7-
* permission of the copyright holders. If you encounter this file and do not have
8-
* permission, please contact the copyright holders and delete this file.
6+
* This file is confidential and only available to authorized individuals with
7+
* the permission of the copyright holders. If you encounter this file and do
8+
* not have permission, please contact the copyright holders and delete this
9+
* file.
910
*/
1011

1112
#include "CsvSpawnerUtils.h"
13+
14+
#include "AzFramework/Physics/CollisionBus.h"
15+
1216
#include <AzCore/Asset/AssetSerializer.h>
1317
#include <AzCore/Serialization/EditContext.h>
1418
#include <AzCore/Serialization/SerializeContext.h>
@@ -49,7 +53,8 @@ namespace CsvSpawner::CsvSpawnerUtils
4953
->Field("PositionStdDev", &CsvSpawnableAssetConfiguration::m_positionStdDev)
5054
->Field("RotationStdDev", &CsvSpawnableAssetConfiguration::m_rotationStdDev)
5155
->Field("ScaleStdDev", &CsvSpawnableAssetConfiguration::m_scaleStdDev)
52-
->Field("PlaceOnTerrain", &CsvSpawnableAssetConfiguration::m_placeOnTerrain);
56+
->Field("PlaceOnTerrain", &CsvSpawnableAssetConfiguration::m_placeOnTerrain)
57+
->Field("CollisionGroup", &CsvSpawnableAssetConfiguration::m_selectedCollisionGroup);
5358

5459
if (auto* editContext = serializeContext->GetEditContext())
5560
{
@@ -78,15 +83,32 @@ namespace CsvSpawner::CsvSpawnerUtils
7883
&CsvSpawnableAssetConfiguration::m_placeOnTerrain,
7984
"Place on Terrain",
8085
"Perform scene query raytrace to place on terrain")
86+
->Attribute(AZ::Edit::Attributes::ChangeNotify, &CsvSpawnableAssetConfiguration::OnPlaceOnTerrainChanged)
8187
->DataElement(
8288
AZ::Edit::UIHandlers::Default,
8389
&CsvSpawnableAssetConfiguration::m_scaleStdDev,
8490
"Scale Std. Dev.",
85-
"Scale standard deviation, in meters");
91+
"Scale standard deviation, in meters")
92+
->DataElement(
93+
AZ::Edit::UIHandlers::Default,
94+
&CsvSpawnableAssetConfiguration::m_selectedCollisionGroup,
95+
"Collision Group",
96+
"To which collision group this target will be attached")
97+
->Attribute(AZ::Edit::Attributes::ReadOnly, &CsvSpawnableAssetConfiguration::IsCollisionLayerEnabled);
8698
}
8799
}
88100
}
89101

102+
bool CsvSpawnableAssetConfiguration::IsCollisionLayerEnabled() const
103+
{
104+
return !m_placeOnTerrain;
105+
}
106+
107+
AZ::Crc32 CsvSpawnableAssetConfiguration::OnPlaceOnTerrainChanged()
108+
{
109+
return AZ::Edit::PropertyRefreshLevels::EntireTree;
110+
}
111+
90112
AZStd::unordered_map<AZStd::string, CsvSpawnableAssetConfiguration> GetSpawnableAssetFromVector(
91113
AZStd::vector<CsvSpawnableAssetConfiguration> spawnableAssetConfigurations)
92114
{
@@ -127,10 +149,15 @@ namespace CsvSpawner::CsvSpawnerUtils
127149
}
128150

129151
AZStd::optional<AZ::Vector3> RaytraceTerrain(
130-
const AZ::Vector3& location, const AzPhysics::SceneHandle sceneHandle, const AZ::Vector3& gravityDirection, float maxDistance)
152+
const AZ::Vector3& location,
153+
const AzPhysics::SceneHandle sceneHandle,
154+
const AZ::Vector3& gravityDirection,
155+
float maxDistance,
156+
const AzPhysics::CollisionGroup& collisionGroup)
131157
{
132158
AZStd::optional<AZ::Vector3> hitPosition = AZStd::nullopt;
133159

160+
AZ_Assert(sceneHandle == AzPhysics::InvalidSceneHandle, "Unable to get scene handle");
134161
if (sceneHandle == AzPhysics::InvalidSceneHandle)
135162
{
136163
return hitPosition;
@@ -150,6 +177,7 @@ namespace CsvSpawner::CsvSpawnerUtils
150177
request.m_start = location;
151178
request.m_direction = gravityDirection;
152179
request.m_distance = maxDistance;
180+
request.m_collisionGroup = collisionGroup;
153181

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

@@ -211,21 +239,29 @@ namespace CsvSpawner::CsvSpawnerUtils
211239

212240
if (spawnConfig.m_placeOnTerrain)
213241
{
242+
// Get collision group chosen from editor
243+
AzPhysics::CollisionGroup collisionGroup;
244+
Physics::CollisionRequestBus::BroadcastResult(
245+
collisionGroup, &Physics::CollisionRequests::GetCollisionGroupById, spawnConfig.m_selectedCollisionGroup);
246+
214247
const AZStd::optional<AZ::Vector3> hitPosition =
215-
RaytraceTerrain(transform.GetTranslation(), sceneHandle, -AZ::Vector3::CreateAxisZ(), 1000.0f);
248+
RaytraceTerrain(transform.GetTranslation(), sceneHandle, -AZ::Vector3::CreateAxisZ(), 1000.0f, collisionGroup);
249+
216250
if (hitPosition.has_value())
217251
{
218252
transform.SetTranslation(hitPosition.value());
219253
}
220254
else
221255
{
222-
continue; // Skip this entity if we can't find a valid position and place on terrain is enabled.
256+
continue; // Skip this entity if we can't find a valid position and
257+
// place on terrain is enabled.
223258
}
224259
}
225260
AZ_Assert(spawner, "Unable to get spawnable entities definition");
226261
AzFramework::SpawnAllEntitiesOptionalArgs optionalArgs;
227262
AzFramework::EntitySpawnTicket ticket(spawnable);
228-
// Set the pre-spawn callback to set the name of the root entity to the name of the spawnable
263+
// Set the pre-spawn callback to set the name of the root entity to the name
264+
// of the spawnable
229265
optionalArgs.m_preInsertionCallback = [transform](auto id, auto view)
230266
{
231267
if (view.empty())

Gems/CsvSpawner/Code/Source/CsvSpawner/CsvSpawnerUtils.h

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
*
44
* This source code is protected under international copyright law. All rights
55
* reserved and protected by the copyright holders.
6-
* This file is confidential and only available to authorized individuals with the
7-
* permission of the copyright holders. If you encounter this file and do not have
8-
* permission, please contact the copyright holders and delete this file.
6+
* This file is confidential and only available to authorized individuals with
7+
* the permission of the copyright holders. If you encounter this file and do
8+
* not have permission, please contact the copyright holders and delete this
9+
* file.
910
*/
1011

1112
#pragma once
1213

14+
#include "AzFramework/Physics/Collision/CollisionGroups.h"
15+
#include "AzFramework/Physics/Collision/CollisionLayers.h"
1316
#include "CsvSpawner/CsvSpawnerTypeIds.h"
1417

1518
#include <AzCore/Asset/AssetCommon.h>
@@ -24,8 +27,8 @@ namespace CsvSpawner::CsvSpawnerUtils
2427

2528
//! Information about a locations to spawn an entity.
2629
//! This information is generated from CSV file and is used to spawn the entity.
27-
//! @param m_name is the name of the spawnable entity configuration and should be identical to name in @class
28-
//! SpawnableAssetConfiguration.
30+
//! @param m_name is the name of the spawnable entity configuration and should
31+
//! be identical to name in @class SpawnableAssetConfiguration.
2932
class CsvSpawnableEntityInfo
3033
{
3134
public:
@@ -37,15 +40,18 @@ namespace CsvSpawner::CsvSpawnerUtils
3740
unsigned int m_id; //!< Optional ID for the entity
3841
AZ::Transform m_transform; //!< Transform of the entity, mandatory
3942
AZStd::string m_name; //!< Name of the spawnable entity configuration, mandatory
40-
uint64_t m_seed{ 0 }; //!< Optional seed value for randomization, in the context of one spawnable entity configuration, optional
43+
uint64_t m_seed{ 0 }; //!< Optional seed value for randomization, in the context
44+
//!< of one spawnable entity configuration, optional
4145
};
4246

4347
//! Configuration for a spawnable asset
44-
//! This configuration is used to configure the spawnable asset before spawning it.
45-
//! The @param m_name is used to identify the spawnable asset configuration and should be identical to name given in CSV file.
46-
//! The class allows user to specify multiple spawnable assets for a given name and randomization parameters for position, rotation and
47-
//! scale. If more then one spawnable asset is specified for a given name, then one of the spawnable assets is randomly selected for
48-
//! spawning.
48+
//! This configuration is used to configure the spawnable asset before spawning
49+
//! it. The @param m_name is used to identify the spawnable asset configuration
50+
//! and should be identical to name given in CSV file. The class allows user to
51+
//! specify multiple spawnable assets for a given name and randomization
52+
//! parameters for position, rotation and scale. If more then one spawnable
53+
//! asset is specified for a given name, then one of the spawnable assets is
54+
//! randomly selected for spawning.
4955
class CsvSpawnableAssetConfiguration
5056
{
5157
public:
@@ -59,12 +65,21 @@ namespace CsvSpawner::CsvSpawnerUtils
5965
AZ::Vector3 m_positionStdDev{ 0.f }; //!< Standard deviation for position
6066
AZ::Vector3 m_rotationStdDev{ 0.f }; //!< Standard deviation for rotation
6167
float m_scaleStdDev{ 0.1f }; //!< Standard deviation for scale
62-
bool m_placeOnTerrain{ false }; //!< Whether to raytrace to terrain and place the entity on the terrain
68+
bool m_placeOnTerrain{ false }; //!< Whether to raytrace to terrain and place
69+
//!< the entity on the terrain
70+
AzPhysics::CollisionGroups::Id m_selectedCollisionGroup; //!< To which collision group this target will
71+
//!< be attached
72+
73+
private:
74+
[[nodiscard]] bool IsCollisionLayerEnabled() const;
75+
static AZ::Crc32 OnPlaceOnTerrainChanged();
6376
};
6477

65-
//! This function create map of spawnable asset configuration from vector of spawnable asset configuration, where
66-
//! the key is the name of the spawnable asset configuration
67-
//! @param spawnableAssetConfigurations - vector of spawnable asset configuration
78+
//! This function create map of spawnable asset configuration from vector of
79+
//! spawnable asset configuration, where the key is the name of the spawnable
80+
//! asset configuration
81+
//! @param spawnableAssetConfigurations - vector of spawnable asset
82+
//! configuration
6883
//! @return map of spawnable asset configuration
6984
AZStd::unordered_map<AZStd::string, CsvSpawnableAssetConfiguration> GetSpawnableAssetFromVector(
7085
AZStd::vector<CsvSpawnableAssetConfiguration> spawnableAssetConfigurations);
@@ -74,7 +89,8 @@ namespace CsvSpawner::CsvSpawnerUtils
7489
//! @param spawnableAssetConfiguration - map of spawnable asset configuration
7590
//! @param defaultSeed - default seed value
7691
//! @param parentId - parent entity id to set for new entities
77-
//! @param physicsSceneName - physics scene name (AzPhysics::DefaultPhysicsSceneName or AzPhysics::EditorPhysicsSceneName)
92+
//! @param physicsSceneName - physics scene name
93+
//! (AzPhysics::DefaultPhysicsSceneName or AzPhysics::EditorPhysicsSceneName)
7894
//! @return map of spawn created tickets
7995
AZStd::unordered_map<int, AzFramework::EntitySpawnTicket> SpawnEntities(
8096
const AZStd::vector<CsvSpawnableEntityInfo>& entitiesToSpawn,

0 commit comments

Comments
 (0)