Reparenting objects maintain world transforms
This commit is contained in:
parent
0625ca6bcf
commit
314d497b66
|
@ -10,7 +10,7 @@ Collapsed=0
|
||||||
|
|
||||||
[Window][Hierarchy Panel]
|
[Window][Hierarchy Panel]
|
||||||
Pos=0,142
|
Pos=0,142
|
||||||
Size=571,918
|
Size=349,918
|
||||||
Collapsed=0
|
Collapsed=0
|
||||||
DockId=0x00000004,0
|
DockId=0x00000004,0
|
||||||
|
|
||||||
|
@ -20,29 +20,29 @@ Size=400,400
|
||||||
Collapsed=0
|
Collapsed=0
|
||||||
|
|
||||||
[Window][Inspector]
|
[Window][Inspector]
|
||||||
Pos=1649,48
|
Pos=1483,48
|
||||||
Size=271,1012
|
Size=437,1012
|
||||||
Collapsed=0
|
Collapsed=0
|
||||||
DockId=0x00000006,0
|
DockId=0x00000006,0
|
||||||
|
|
||||||
[Window][Profiler]
|
[Window][Profiler]
|
||||||
Pos=0,48
|
Pos=0,48
|
||||||
Size=571,92
|
Size=349,92
|
||||||
Collapsed=0
|
Collapsed=0
|
||||||
DockId=0x00000003,0
|
DockId=0x00000003,0
|
||||||
|
|
||||||
[Window][Viewport]
|
[Window][Viewport]
|
||||||
Pos=573,48
|
Pos=351,48
|
||||||
Size=1074,1012
|
Size=1130,1012
|
||||||
Collapsed=0
|
Collapsed=0
|
||||||
DockId=0x00000002,0
|
DockId=0x00000002,0
|
||||||
|
|
||||||
[Docking][Data]
|
[Docking][Data]
|
||||||
DockSpace ID=0xC5C9B8AB Window=0xBE4044E9 Pos=8,79 Size=1920,1012 Split=X
|
DockSpace ID=0xC5C9B8AB Window=0xBE4044E9 Pos=8,79 Size=1920,1012 Split=X
|
||||||
DockNode ID=0x00000005 Parent=0xC5C9B8AB SizeRef=1992,1036 Split=X
|
DockNode ID=0x00000005 Parent=0xC5C9B8AB SizeRef=1481,1036 Split=X
|
||||||
DockNode ID=0x00000001 Parent=0x00000005 SizeRef=571,1036 Split=Y Selected=0x1E6EB881
|
DockNode ID=0x00000001 Parent=0x00000005 SizeRef=349,1036 Split=Y Selected=0x1E6EB881
|
||||||
DockNode ID=0x00000003 Parent=0x00000001 SizeRef=225,94 Selected=0x1E6EB881
|
DockNode ID=0x00000003 Parent=0x00000001 SizeRef=225,94 Selected=0x1E6EB881
|
||||||
DockNode ID=0x00000004 Parent=0x00000001 SizeRef=225,940 Selected=0xE096E5AE
|
DockNode ID=0x00000004 Parent=0x00000001 SizeRef=225,940 Selected=0xE096E5AE
|
||||||
DockNode ID=0x00000002 Parent=0x00000005 SizeRef=1074,1036 CentralNode=1 Selected=0x13926F0B
|
DockNode ID=0x00000002 Parent=0x00000005 SizeRef=1130,1036 CentralNode=1 Selected=0x13926F0B
|
||||||
DockNode ID=0x00000006 Parent=0xC5C9B8AB SizeRef=271,1036 Selected=0xE7039252
|
DockNode ID=0x00000006 Parent=0xC5C9B8AB SizeRef=437,1036 Selected=0xE7039252
|
||||||
|
|
||||||
|
|
|
@ -77,9 +77,9 @@ namespace Sandbox
|
||||||
customMat->SetProperty("data.alpha", 0.1f);
|
customMat->SetProperty("data.alpha", 0.1f);
|
||||||
|
|
||||||
// Create Stress Test Objects
|
// Create Stress Test Objects
|
||||||
static const SHVec3 TEST_OBJ_SCALE = SHVec3::One * 0.5f;
|
static const SHVec3 TEST_OBJ_SCALE = SHVec3::One;
|
||||||
constexpr int NUM_ROWS = 10;
|
constexpr int NUM_ROWS = 2;
|
||||||
constexpr int NUM_COLS = 10;
|
constexpr int NUM_COLS = 1;
|
||||||
static const SHVec3 TEST_OBJ_SPACING = { 0.1f, 0.1f, 0.1f };
|
static const SHVec3 TEST_OBJ_SPACING = { 0.1f, 0.1f, 0.1f };
|
||||||
static const SHVec3 TEST_OBJ_START_POS = { -(NUM_COLS / 2 * TEST_OBJ_SPACING.x) + 1.0f, -2.0f, -1.0f };
|
static const SHVec3 TEST_OBJ_START_POS = { -(NUM_COLS / 2 * TEST_OBJ_SPACING.x) + 1.0f, -2.0f, -1.0f };
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,6 @@
|
||||||
#include "Scene/SHSceneManager.h"
|
#include "Scene/SHSceneManager.h"
|
||||||
#include "ECS_Base/Managers/SHComponentManager.h"
|
#include "ECS_Base/Managers/SHComponentManager.h"
|
||||||
#include "ECS_Base/Managers/SHEntityManager.h"
|
#include "ECS_Base/Managers/SHEntityManager.h"
|
||||||
#include "Tools/SHException.h"
|
|
||||||
|
|
||||||
namespace SHADE
|
namespace SHADE
|
||||||
{
|
{
|
||||||
|
@ -47,7 +46,9 @@ namespace SHADE
|
||||||
|
|
||||||
void SHTransformSystem::Init()
|
void SHTransformSystem::Init()
|
||||||
{
|
{
|
||||||
|
std::shared_ptr thisReceiver { std::make_shared<SHEventReceiverSpec<SHTransformSystem>>(this, &SHTransformSystem::ChangeParent) };
|
||||||
|
ReceiverPtr receiver = std::dynamic_pointer_cast<SHEventReceiver>(thisReceiver);
|
||||||
|
SHEventManager::SubscribeTo(SH_SCENEGRAPH_CHANGE_PARENT_EVENT, receiver);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SHTransformSystem::Exit()
|
void SHTransformSystem::Exit()
|
||||||
|
@ -59,6 +60,44 @@ namespace SHADE
|
||||||
/* Private Function Member Definitions */
|
/* Private Function Member Definitions */
|
||||||
/*-----------------------------------------------------------------------------------*/
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
SHEventHandle SHTransformSystem::ChangeParent(SHEventPtr changeParentEvent)
|
||||||
|
{
|
||||||
|
const auto& eventData = reinterpret_cast<const SHEventSpec<SHSceneGraphChangeParentEvent>*>(changeParentEvent.get());
|
||||||
|
|
||||||
|
// Get Current Respective Components
|
||||||
|
auto* tf = SHComponentManager::GetComponent<SHTransformComponent>(eventData->data->entityID);
|
||||||
|
const auto* PARENT = SHComponentManager::GetComponent_s<SHTransformComponent>(eventData->data->newParentID);
|
||||||
|
|
||||||
|
// Recompute local transform and store localToWorld Matrix
|
||||||
|
SHMatrix localToWorld = SHMatrix::Identity;
|
||||||
|
SHMatrix worldToLocal = SHMatrix::Identity;
|
||||||
|
|
||||||
|
if (PARENT != nullptr) // Not the root
|
||||||
|
{
|
||||||
|
localToWorld = PARENT->GetTRS();
|
||||||
|
worldToLocal = SHMatrix::Inverse(localToWorld);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Maintain World Transform and recompute Local Transform
|
||||||
|
|
||||||
|
// Compute Local Position
|
||||||
|
tf->local.position = SHVec3::Transform(tf->world.position, worldToLocal);
|
||||||
|
|
||||||
|
// Compute Local Rotation
|
||||||
|
tf->local.rotation = tf->world.rotation;
|
||||||
|
if (PARENT)
|
||||||
|
tf->local.rotation -= PARENT->GetLocalRotation();
|
||||||
|
|
||||||
|
// Compute Local Scale
|
||||||
|
tf->local.scale = tf->world.scale;
|
||||||
|
if (PARENT)
|
||||||
|
tf->local.scale /= PARENT->GetLocalScale();
|
||||||
|
|
||||||
|
tf->local.trs = localToWorld;
|
||||||
|
|
||||||
|
return eventData->handle;
|
||||||
|
}
|
||||||
|
|
||||||
void SHTransformSystem::UpdateEntity(const SHSceneNode* node)
|
void SHTransformSystem::UpdateEntity(const SHSceneNode* node)
|
||||||
{
|
{
|
||||||
const auto* NODE_TRANSFORM = SHComponentManager::GetComponent_s<SHTransformComponent>(node->GetEntityID());
|
const auto* NODE_TRANSFORM = SHComponentManager::GetComponent_s<SHTransformComponent>(node->GetEntityID());
|
||||||
|
|
|
@ -84,6 +84,8 @@ namespace SHADE
|
||||||
/* Function Members */
|
/* Function Members */
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
SHEventHandle ChangeParent (SHEventPtr changeParentEvent);
|
||||||
|
|
||||||
static void UpdateEntity (const SHSceneNode* node);
|
static void UpdateEntity (const SHSceneNode* node);
|
||||||
static void UpdateTransform (SHTransformComponent& tf, const SHTransformComponent* parent = nullptr);
|
static void UpdateTransform (SHTransformComponent& tf, const SHTransformComponent* parent = nullptr);
|
||||||
};
|
};
|
||||||
|
|
|
@ -367,8 +367,9 @@ namespace SHADE
|
||||||
|
|
||||||
const SHSceneGraphChangeParentEvent EVENT_DATA
|
const SHSceneGraphChangeParentEvent EVENT_DATA
|
||||||
{
|
{
|
||||||
.oldParentID = NODE_ITER->second->GetParent()->GetEntityID(),
|
.entityID = entityID
|
||||||
.newParentID = parent->GetEntityID()
|
, .oldParentID = NODE_ITER->second->GetParent()->GetEntityID()
|
||||||
|
, .newParentID = parent ? parent->GetEntityID() : root->GetEntityID()
|
||||||
};
|
};
|
||||||
|
|
||||||
if (parent == nullptr)
|
if (parent == nullptr)
|
||||||
|
@ -412,8 +413,9 @@ namespace SHADE
|
||||||
|
|
||||||
const SHSceneGraphChangeParentEvent EVENT_DATA
|
const SHSceneGraphChangeParentEvent EVENT_DATA
|
||||||
{
|
{
|
||||||
.oldParentID = NODE_ITER->second->GetParent()->GetEntityID(),
|
.entityID = entityID
|
||||||
.newParentID = parent
|
, .oldParentID = NODE_ITER->second->GetParent()->GetEntityID()
|
||||||
|
, .newParentID = parent
|
||||||
};
|
};
|
||||||
|
|
||||||
SHSceneNode* currentNode = NODE_ITER->second;
|
SHSceneNode* currentNode = NODE_ITER->second;
|
||||||
|
|
|
@ -163,6 +163,7 @@ namespace SHADE
|
||||||
|
|
||||||
struct SHSceneGraphChangeParentEvent
|
struct SHSceneGraphChangeParentEvent
|
||||||
{
|
{
|
||||||
|
EntityID entityID;
|
||||||
EntityID oldParentID;
|
EntityID oldParentID;
|
||||||
EntityID newParentID;
|
EntityID newParentID;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue