From 6ab2e540a32a2ce6827d98452aefb4484971acc1 Mon Sep 17 00:00:00 2001 From: NoriteSC <53096989+NoriteSC@users.noreply.github.com> Date: Fri, 25 Aug 2023 14:50:56 +0200 Subject: [PATCH 1/2] Bug Fixes script and actors not having connect transform in on awake and in on start join auto anchor having wrong anchor because was including scale fixed selection do to changes to transform accessibility ? --- Source/Editor/Viewport/EditorViewport.cs | 6 +-- Source/Engine/Level/Level.cpp | 17 +++++---- Source/Engine/Level/Prefabs/Prefab.Apply.cpp | 4 +- Source/Engine/Level/Prefabs/Prefab.cpp | 2 +- Source/Engine/Level/Prefabs/PrefabManager.cpp | 37 +++++++++---------- Source/Engine/Level/Prefabs/PrefabManager.h | 8 ++-- .../Engine/Networking/NetworkReplicator.cpp | 2 +- Source/Engine/Physics/Joints/Joint.cpp | 9 +++-- Source/Engine/Physics/Joints/Joint.h | 2 +- 9 files changed, 42 insertions(+), 45 deletions(-) diff --git a/Source/Editor/Viewport/EditorViewport.cs b/Source/Editor/Viewport/EditorViewport.cs index ad7e06ba5..19f272c40 100644 --- a/Source/Editor/Viewport/EditorViewport.cs +++ b/Source/Editor/Viewport/EditorViewport.cs @@ -898,16 +898,14 @@ namespace FlaxEditor.Viewport ivp.Invert(); // Create near and far points - var nearPoint = new Vector3(mousePosition, 0.0f); var farPoint = new Vector3(mousePosition, 1.0f); - viewport.Unproject(ref nearPoint, ref ivp, out nearPoint); viewport.Unproject(ref farPoint, ref ivp, out farPoint); // Create direction vector - Vector3 direction = farPoint - nearPoint; + Vector3 direction = farPoint; direction.Normalize(); - return new Ray(nearPoint + viewOrigin, direction); + return new Ray(position, direction); } /// diff --git a/Source/Engine/Level/Level.cpp b/Source/Engine/Level/Level.cpp index 587d20331..f79f4558d 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"); @@ -1016,8 +1016,16 @@ 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 + { + PROFILE_CPU_NAMED("Cache Transform"); + + scene->OnTransformChanged(); + } + // Initialize scene objects { PROFILE_CPU_NAMED("Initialize"); @@ -1039,13 +1047,6 @@ bool Level::loadScene(rapidjson_flax::Value& data, int32 engineBuild, Scene** ou } } - // Cache transformations - { - PROFILE_CPU_NAMED("Cache Transform"); - - scene->OnTransformChanged(); - } - // /\ all above this has to be done on an any thread // \/ all below this has to be done on a main thread diff --git a/Source/Engine/Level/Prefabs/Prefab.Apply.cpp b/Source/Engine/Level/Prefabs/Prefab.Apply.cpp index 53453afe9..5ee303c9b 100644 --- a/Source/Engine/Level/Prefabs/Prefab.Apply.cpp +++ b/Source/Engine/Level/Prefabs/Prefab.Apply.cpp @@ -1229,14 +1229,14 @@ bool Prefab::SyncChangesInternal(PrefabInstancesData& prefabInstancesData) { ScopeLock lock(Locker); _isCreatingDefaultInstance = true; - _defaultInstance = PrefabManager::SpawnPrefab(this, nullptr, &ObjectsCache, true); + _defaultInstance = PrefabManager::SpawnPrefab(this,Transform::Identity, nullptr, &ObjectsCache, true); _isCreatingDefaultInstance = false; } // Instantiate prefab instance from prefab (default spawning logic) // Note: it will get any added or removed objects from the nested prefabs // TODO: try to optimize by using recreated default instance to ApplyAllInternal (will need special path there if apply is done with default instance to unlink it instead of destroying) - const auto targetActor = PrefabManager::SpawnPrefab(this, nullptr, nullptr, true); + const auto targetActor = PrefabManager::SpawnPrefab(this, Transform::Identity, nullptr, nullptr, true); if (targetActor == nullptr) { LOG(Warning, "Failed to instantiate default prefab instance from changes synchronization."); diff --git a/Source/Engine/Level/Prefabs/Prefab.cpp b/Source/Engine/Level/Prefabs/Prefab.cpp index c2c2463b2..63a6b8071 100644 --- a/Source/Engine/Level/Prefabs/Prefab.cpp +++ b/Source/Engine/Level/Prefabs/Prefab.cpp @@ -76,7 +76,7 @@ Actor* Prefab::GetDefaultInstance() _isCreatingDefaultInstance = true; // Instantiate objects from prefab (default spawning logic) - _defaultInstance = PrefabManager::SpawnPrefab(this, nullptr, &ObjectsCache); + _defaultInstance = PrefabManager::SpawnPrefab(this, Transform::Identity, nullptr, &ObjectsCache); _isCreatingDefaultInstance = false; return _defaultInstance; diff --git a/Source/Engine/Level/Prefabs/PrefabManager.cpp b/Source/Engine/Level/Prefabs/PrefabManager.cpp index a2b148f28..2c81f9058 100644 --- a/Source/Engine/Level/Prefabs/PrefabManager.cpp +++ b/Source/Engine/Level/Prefabs/PrefabManager.cpp @@ -39,42 +39,37 @@ PrefabManagerService PrefabManagerServiceInstance; Actor* PrefabManager::SpawnPrefab(Prefab* prefab) { Actor* parent = Level::Scenes.Count() != 0 ? Level::Scenes.Get()[0] : nullptr; - return SpawnPrefab(prefab, parent, nullptr); + return SpawnPrefab(prefab,Transform::Identity, parent, nullptr); } Actor* PrefabManager::SpawnPrefab(Prefab* prefab, const Vector3& position) { - auto instance = SpawnPrefab(prefab); - if (instance) - instance->SetPosition(position); - return instance; + Actor* parent = Level::Scenes.Count() != 0 ? Level::Scenes.Get()[0] : nullptr; + return SpawnPrefab(prefab, Transform(position), parent, nullptr); } Actor* PrefabManager::SpawnPrefab(Prefab* prefab, const Vector3& position, const Quaternion& rotation) { - auto instance = SpawnPrefab(prefab); - if (instance) - instance->SetTransform(Transform(position, rotation, instance->GetScale())); - return instance; + Actor* parent = Level::Scenes.Count() != 0 ? Level::Scenes.Get()[0] : nullptr; + return SpawnPrefab(prefab, Transform(position, rotation), parent, nullptr); } Actor* PrefabManager::SpawnPrefab(Prefab* prefab, const Vector3& position, const Quaternion& rotation, const Vector3& scale) { - auto instance = SpawnPrefab(prefab); - if (instance) - instance->SetTransform(Transform(position, rotation, scale)); - return instance; + Actor* parent = Level::Scenes.Count() != 0 ? Level::Scenes.Get()[0] : nullptr; + return SpawnPrefab(prefab, Transform(position, rotation, scale), parent, nullptr); } Actor* PrefabManager::SpawnPrefab(Prefab* prefab, const Transform& transform) { - auto instance = SpawnPrefab(prefab); - if (instance) - instance->SetTransform(transform); - return instance; + Actor* parent = Level::Scenes.Count() != 0 ? Level::Scenes.Get()[0] : nullptr; + return SpawnPrefab(prefab, transform, parent, nullptr); } - -Actor* PrefabManager::SpawnPrefab(Prefab* prefab, Actor* parent, Dictionary* objectsCache, bool withSynchronization) +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) { PROFILE_CPU_NAMED("Prefab.Spawn"); if (prefab == nullptr) @@ -183,6 +178,10 @@ Actor* PrefabManager::SpawnPrefab(Prefab* prefab, Actor* parent, DictionaryChildren.Add(root); + //move root to right location + if (transform != Transform::Identity) + root->SetTransform(transform); + // Link actors hierarchy for (int32 i = 0; i < sceneObjects->Count(); i++) { diff --git a/Source/Engine/Level/Prefabs/PrefabManager.h b/Source/Engine/Level/Prefabs/PrefabManager.h index 0ff87536f..9705e7ce0 100644 --- a/Source/Engine/Level/Prefabs/PrefabManager.h +++ b/Source/Engine/Level/Prefabs/PrefabManager.h @@ -70,20 +70,18 @@ API_CLASS(Static) class FLAXENGINE_API PrefabManager /// The prefab asset. /// The parent actor to add spawned object instance. Can be null to just deserialize contents of the prefab. /// The created actor (root) or null if failed. - API_FUNCTION() static Actor* SpawnPrefab(Prefab* prefab, Actor* parent) - { - return SpawnPrefab(prefab, parent, nullptr); - } + 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. + /// prefab instance transform. /// 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); + static Actor* SpawnPrefab(Prefab* prefab, const Transform& transform, Actor* parent, Dictionary* objectsCache, bool withSynchronization = false); #if USE_EDITOR diff --git a/Source/Engine/Networking/NetworkReplicator.cpp b/Source/Engine/Networking/NetworkReplicator.cpp index 529e6460b..6a9f9c29b 100644 --- a/Source/Engine/Networking/NetworkReplicator.cpp +++ b/Source/Engine/Networking/NetworkReplicator.cpp @@ -1738,7 +1738,7 @@ void NetworkInternal::OnNetworkMessageObjectSpawn(NetworkEvent& event, NetworkCl 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 2c7d15727..6fd2d5a7f 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->GetTransform().WorldToLocal(GetPosition()); + desc.Pos1 = (Target->GetOrientation() * (GetPosition() - Target->GetPosition())) + Target->GetPosition(); desc.Rot1 = WorldToLocal(Target->GetOrientation(), GetOrientation()); } _joint = CreateJoint(desc); @@ -158,7 +158,7 @@ void Joint::Create() void Joint::OnJointBreak() { - JointBreak(); + JointBreak(this); } void Joint::Delete() @@ -197,8 +197,9 @@ Vector3 Joint::GetTargetPosition() const if (Target) { if (_enableAutoAnchor) - position = Target->GetTransform().WorldToLocal(GetPosition()); - position = Target->GetOrientation() * position + Target->GetPosition(); + position = (Target->GetOrientation() * (GetPosition() - Target->GetPosition())) + Target->GetPosition(); + else + position = Target->GetOrientation() * position + Target->GetPosition(); } return position; } diff --git a/Source/Engine/Physics/Joints/Joint.h b/Source/Engine/Physics/Joints/Joint.h index f96e47b71..cc310786c 100644 --- a/Source/Engine/Physics/Joints/Joint.h +++ b/Source/Engine/Physics/Joints/Joint.h @@ -163,7 +163,7 @@ public: /// /// Occurs when a joint gets broken during simulation. /// - API_EVENT() Action JointBreak; + API_EVENT() Delegate JointBreak; /// /// Called by the physics system when joint gets broken. From 1cb3c798e5a7763c8594baed1c6339f68277b3b5 Mon Sep 17 00:00:00 2001 From: NoriteSC <53096989+NoriteSC@users.noreply.github.com> Date: Fri, 25 Aug 2023 19:12:04 +0200 Subject: [PATCH 2/2] Revent --- Source/Editor/Viewport/EditorViewport.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Source/Editor/Viewport/EditorViewport.cs b/Source/Editor/Viewport/EditorViewport.cs index 19f272c40..ad7e06ba5 100644 --- a/Source/Editor/Viewport/EditorViewport.cs +++ b/Source/Editor/Viewport/EditorViewport.cs @@ -898,14 +898,16 @@ namespace FlaxEditor.Viewport ivp.Invert(); // Create near and far points + var nearPoint = new Vector3(mousePosition, 0.0f); var farPoint = new Vector3(mousePosition, 1.0f); + viewport.Unproject(ref nearPoint, ref ivp, out nearPoint); viewport.Unproject(ref farPoint, ref ivp, out farPoint); // Create direction vector - Vector3 direction = farPoint; + Vector3 direction = farPoint - nearPoint; direction.Normalize(); - return new Ray(position, direction); + return new Ray(nearPoint + viewOrigin, direction); } ///