From d71c171412cdc7823a203063cbd62a2519276d2a Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Thu, 28 Sep 2023 13:10:58 +0200 Subject: [PATCH] Cleanup code in #1367 and fix api compatibility, revert joint changes --- Source/Engine/Audio/AudioListener.cpp | 2 +- .../Engine/Level/Actors/EnvironmentProbe.cpp | 2 +- Source/Engine/Level/Level.cpp | 11 ++++++----- Source/Engine/Level/Prefabs/Prefab.Apply.cpp | 2 +- Source/Engine/Level/Prefabs/Prefab.cpp | 2 +- Source/Engine/Level/Prefabs/PrefabManager.cpp | 18 +++++++++++++++--- Source/Engine/Level/Prefabs/PrefabManager.h | 19 +++++++++++++++++++ .../Engine/Networking/NetworkReplicator.cpp | 2 +- Source/Engine/Physics/Joints/Joint.cpp | 7 +++---- 9 files changed, 48 insertions(+), 17 deletions(-) diff --git a/Source/Engine/Audio/AudioListener.cpp b/Source/Engine/Audio/AudioListener.cpp index 4d8831591..7065ae344 100644 --- a/Source/Engine/Audio/AudioListener.cpp +++ b/Source/Engine/Audio/AudioListener.cpp @@ -66,7 +66,7 @@ void AudioListener::OnTransformChanged() _box = BoundingBox(_transform.Translation); _sphere = BoundingSphere(_transform.Translation, 0.0f); - if (IsActiveInHierarchy()) + if (IsActiveInHierarchy() && IsDuringPlay()) { AudioBackend::Listener::TransformChanged(this); } diff --git a/Source/Engine/Level/Actors/EnvironmentProbe.cpp b/Source/Engine/Level/Actors/EnvironmentProbe.cpp index 1b1a400d4..4f81a0a59 100644 --- a/Source/Engine/Level/Actors/EnvironmentProbe.cpp +++ b/Source/Engine/Level/Actors/EnvironmentProbe.cpp @@ -288,7 +288,7 @@ void EnvironmentProbe::OnTransformChanged() UpdateBounds(); - if (IsDuringPlay() && UpdateMode == ProbeUpdateMode::WhenMoved) + if (IsActiveInHierarchy() && IsDuringPlay() && UpdateMode == ProbeUpdateMode::WhenMoved) { Bake(1.0f); } diff --git a/Source/Engine/Level/Level.cpp b/Source/Engine/Level/Level.cpp index f79f4558d..07de023a3 100644 --- a/Source/Engine/Level/Level.cpp +++ b/Source/Engine/Level/Level.cpp @@ -993,7 +993,7 @@ bool Level::loadScene(rapidjson_flax::Value& data, int32 engineBuild, Scene** ou // /\ all above this has to be done on an any thread // \/ all below this has to be done on multiple threads at once - + { PROFILE_CPU_NAMED("Deserialize"); @@ -1001,10 +1001,11 @@ bool Level::loadScene(rapidjson_flax::Value& data, int32 engineBuild, Scene** ou // Load all scene objects Scripting::ObjectsLookupIdMapping.Set(&modifier.Value->IdsMapping); + SceneObject** objects = sceneObjects->Get(); for (int32 i = 1; i < objectsCount; i++) // start from 1. at index [0] was scene { auto& objData = data[i]; - auto obj = sceneObjects->At(i); + auto obj = objects[i]; if (obj) SceneObjectsFactory::Deserialize(context, obj, objData); } @@ -1016,7 +1017,6 @@ bool Level::loadScene(rapidjson_flax::Value& data, int32 engineBuild, Scene** ou // Synchronize prefab instances (prefab may have objects removed or reordered so deserialized instances need to synchronize with it) // TODO: resave and force sync scenes during game cooking so this step could be skipped in game - SceneObjectsFactory::SynchronizePrefabInstances(context, prefabSyncData); // Cache transformations @@ -1030,9 +1030,10 @@ bool Level::loadScene(rapidjson_flax::Value& data, int32 engineBuild, Scene** ou { PROFILE_CPU_NAMED("Initialize"); + SceneObject** objects = sceneObjects->Get(); for (int32 i = 0; i < sceneObjects->Count(); i++) { - SceneObject* obj = sceneObjects->At(i); + SceneObject* obj = objects[i]; if (obj) { obj->Initialize(); @@ -1498,7 +1499,7 @@ Array Level::FindActors(const Tag& tag, const bool activeOnly, Actor* ro { ScopeLock lock(ScenesLock); for (Scene* scene : Scenes) - FindActorsRecursive(scene, tag, activeOnly, result); + FindActorsRecursive(scene, tag, activeOnly, result); } return result; } diff --git a/Source/Engine/Level/Prefabs/Prefab.Apply.cpp b/Source/Engine/Level/Prefabs/Prefab.Apply.cpp index 4dda0c469..454b68006 100644 --- a/Source/Engine/Level/Prefabs/Prefab.Apply.cpp +++ b/Source/Engine/Level/Prefabs/Prefab.Apply.cpp @@ -1221,7 +1221,7 @@ bool Prefab::SyncChangesInternal(PrefabInstancesData& prefabInstancesData) { ScopeLock lock(Locker); _isCreatingDefaultInstance = true; - _defaultInstance = PrefabManager::SpawnPrefab(this,Transform::Identity, nullptr, &ObjectsCache, true); + _defaultInstance = PrefabManager::SpawnPrefab(this, Transform::Identity, nullptr, &ObjectsCache, true); _isCreatingDefaultInstance = false; } diff --git a/Source/Engine/Level/Prefabs/Prefab.cpp b/Source/Engine/Level/Prefabs/Prefab.cpp index 63a6b8071..b72f16633 100644 --- a/Source/Engine/Level/Prefabs/Prefab.cpp +++ b/Source/Engine/Level/Prefabs/Prefab.cpp @@ -43,7 +43,7 @@ Guid Prefab::GetRootObjectId() const if (prefabObjectId == basePrefabRootId) { objectIndex = i; - break; + break; } } } diff --git a/Source/Engine/Level/Prefabs/PrefabManager.cpp b/Source/Engine/Level/Prefabs/PrefabManager.cpp index 2c81f9058..6ea1be5fd 100644 --- a/Source/Engine/Level/Prefabs/PrefabManager.cpp +++ b/Source/Engine/Level/Prefabs/PrefabManager.cpp @@ -39,7 +39,7 @@ PrefabManagerService PrefabManagerServiceInstance; Actor* PrefabManager::SpawnPrefab(Prefab* prefab) { Actor* parent = Level::Scenes.Count() != 0 ? Level::Scenes.Get()[0] : nullptr; - return SpawnPrefab(prefab,Transform::Identity, parent, nullptr); + return SpawnPrefab(prefab, Transform::Identity, parent, nullptr); } Actor* PrefabManager::SpawnPrefab(Prefab* prefab, const Vector3& position) @@ -65,11 +65,23 @@ Actor* PrefabManager::SpawnPrefab(Prefab* prefab, const Transform& transform) Actor* parent = Level::Scenes.Count() != 0 ? Level::Scenes.Get()[0] : nullptr; return SpawnPrefab(prefab, transform, parent, nullptr); } + +Actor* PrefabManager::SpawnPrefab(Prefab* prefab, Actor* parent, const Transform& transform) +{ + return SpawnPrefab(prefab, transform, parent, nullptr); +} + Actor* PrefabManager::SpawnPrefab(Prefab* prefab, Actor* parent) { return SpawnPrefab(prefab, Transform::Identity, parent, nullptr); } -Actor* PrefabManager::SpawnPrefab(Prefab* prefab,const Transform& transform, Actor* parent, Dictionary* objectsCache, bool withSynchronization) + +Actor* PrefabManager::SpawnPrefab(Prefab* prefab, Actor* parent, Dictionary* objectsCache, bool withSynchronization) +{ + return SpawnPrefab(prefab, Transform::Identity, parent, objectsCache, withSynchronization); +} + +Actor* PrefabManager::SpawnPrefab(Prefab* prefab, const Transform& transform, Actor* parent, Dictionary* objectsCache, bool withSynchronization) { PROFILE_CPU_NAMED("Prefab.Spawn"); if (prefab == nullptr) @@ -178,7 +190,7 @@ Actor* PrefabManager::SpawnPrefab(Prefab* prefab,const Transform& transform, Act if (parent) parent->Children.Add(root); - //move root to right location + // Move root to the right location if (transform != Transform::Identity) root->SetTransform(transform); diff --git a/Source/Engine/Level/Prefabs/PrefabManager.h b/Source/Engine/Level/Prefabs/PrefabManager.h index 9705e7ce0..16d4a29cf 100644 --- a/Source/Engine/Level/Prefabs/PrefabManager.h +++ b/Source/Engine/Level/Prefabs/PrefabManager.h @@ -64,6 +64,15 @@ API_CLASS(Static) class FLAXENGINE_API PrefabManager /// The created actor (root) or null if failed. API_FUNCTION() static Actor* SpawnPrefab(Prefab* prefab, const Transform& transform); + /// + /// Spawns the instance of the prefab objects. Prefab will be spawned to the first loaded scene. + /// + /// The prefab asset. + /// The parent actor to add spawned object instance. Can be null to just deserialize contents of the prefab. + /// The spawn transformation in the world space. + /// The created actor (root) or null if failed. + API_FUNCTION() static Actor* SpawnPrefab(Prefab* prefab, Actor* parent, const Transform& transform); + /// /// Spawns the instance of the prefab objects. If parent actor is specified then created actors are fully initialized (OnLoad event and BeginPlay is called if parent actor is already during gameplay). /// @@ -72,6 +81,16 @@ API_CLASS(Static) class FLAXENGINE_API PrefabManager /// The created actor (root) or null if failed. API_FUNCTION() static Actor* SpawnPrefab(Prefab* prefab, Actor* parent); + /// + /// Spawns the instance of the prefab objects. If parent actor is specified then created actors are fully initialized (OnLoad event and BeginPlay is called if parent actor is already during gameplay). + /// + /// The prefab asset. + /// The parent actor to add spawned object instance. Can be null to just deserialize contents of the prefab. + /// The options output objects cache that can be filled with prefab object id mapping to deserialized object (actor or script). + /// True if perform prefab changes synchronization for the spawned objects. It will check if need to add new objects due to nested prefab modifications. + /// The created actor (root) or null if failed. + static Actor* SpawnPrefab(Prefab* prefab, Actor* parent, Dictionary* objectsCache, bool withSynchronization = false); + /// /// Spawns the instance of the prefab objects. If parent actor is specified then created actors are fully initialized (OnLoad event and BeginPlay is called if parent actor is already during gameplay). /// diff --git a/Source/Engine/Networking/NetworkReplicator.cpp b/Source/Engine/Networking/NetworkReplicator.cpp index a44072347..4d85238e3 100644 --- a/Source/Engine/Networking/NetworkReplicator.cpp +++ b/Source/Engine/Networking/NetworkReplicator.cpp @@ -803,7 +803,7 @@ void InvokeObjectSpawn(const NetworkMessageObjectSpawn& msgData, const NetworkMe NETWORK_REPLICATOR_LOG(Error, "[NetworkReplicator] Failed to find prefab {}", msgData.PrefabId.ToString()); return; } - prefabInstance = PrefabManager::SpawnPrefab(prefab, nullptr, nullptr); + prefabInstance = PrefabManager::SpawnPrefab(prefab, Transform::Identity, nullptr, nullptr); if (!prefabInstance) { NETWORK_REPLICATOR_LOG(Error, "[NetworkReplicator] Failed to spawn object type {}", msgData.PrefabId.ToString()); diff --git a/Source/Engine/Physics/Joints/Joint.cpp b/Source/Engine/Physics/Joints/Joint.cpp index 8c5cf7454..2c7d15727 100644 --- a/Source/Engine/Physics/Joints/Joint.cpp +++ b/Source/Engine/Physics/Joints/Joint.cpp @@ -146,7 +146,7 @@ void Joint::Create() if (_enableAutoAnchor && target) { // Place target anchor at the joint location - desc.Pos1 = (Target->GetOrientation() * (GetPosition() - Target->GetPosition())) + Target->GetPosition(); + desc.Pos1 = Target->GetTransform().WorldToLocal(GetPosition()); desc.Rot1 = WorldToLocal(Target->GetOrientation(), GetOrientation()); } _joint = CreateJoint(desc); @@ -197,9 +197,8 @@ Vector3 Joint::GetTargetPosition() const if (Target) { if (_enableAutoAnchor) - position = (Target->GetOrientation() * (GetPosition() - Target->GetPosition())) + Target->GetPosition(); - else - position = Target->GetOrientation() * position + Target->GetPosition(); + position = Target->GetTransform().WorldToLocal(GetPosition()); + position = Target->GetOrientation() * position + Target->GetPosition(); } return position; }