Transform system overhaul
Fixed child rotations Added editor overhauls
This commit is contained in:
@@ -43,70 +43,61 @@ namespace Nuake {
|
||||
Matrix4 cameraTransform = camera.CameraInstance->GetTransformRotation();
|
||||
}
|
||||
|
||||
// Calculate all local transforms
|
||||
auto localTransformView = m_Scene->m_Registry.view<TransformComponent>();
|
||||
for (auto tv : localTransformView)
|
||||
{
|
||||
TransformComponent& transform = localTransformView.get<TransformComponent>(tv);
|
||||
|
||||
Matrix4 localTransform = Matrix4(1.0f);
|
||||
localTransform = glm::translate(localTransform, transform.Translation);
|
||||
localTransform = glm::rotate(localTransform, glm::radians(transform.Rotation.x), Vector3(1, 0, 0));
|
||||
localTransform = glm::rotate(localTransform, glm::radians(transform.Rotation.y), Vector3(0, 1, 0));
|
||||
localTransform = glm::rotate(localTransform, glm::radians(transform.Rotation.z), Vector3(0, 0, 1));
|
||||
localTransform = glm::scale(localTransform, transform.Scale);
|
||||
|
||||
transform.LocalTransform = localTransform;
|
||||
}
|
||||
|
||||
// Calculate all global transforms
|
||||
auto transformView = m_Scene->m_Registry.view<ParentComponent, TransformComponent>();
|
||||
for (auto e : transformView)
|
||||
{
|
||||
auto [parent, transform] = transformView.get<ParentComponent, TransformComponent>(e);
|
||||
|
||||
if (!parent.HasParent)
|
||||
{
|
||||
// If no parents, then globalTransform is local transform.
|
||||
transform.GlobalTransform = transform.LocalTransform;
|
||||
}
|
||||
|
||||
Entity currentParent = Entity((entt::entity)e, m_Scene);
|
||||
|
||||
Matrix4 globalTransform = Matrix4(1.0f);
|
||||
Vector3 globalPosition = Vector3();
|
||||
Vector3 globalRotation = Vector3();
|
||||
Vector3 globalScale = Vector3();
|
||||
if (parent.HasParent)
|
||||
Matrix4 globalTransform = transform.LocalTransform;
|
||||
Vector3 globalPosition = transform.Translation;
|
||||
Vector3 globalRotation = transform.Rotation;
|
||||
Vector3 globalScale = transform.Scale;
|
||||
|
||||
ParentComponent parentComponent = currentParent.GetComponent<ParentComponent>();
|
||||
while (parentComponent.HasParent)
|
||||
{
|
||||
globalTransform = glm::translate(globalTransform, transform.Translation);
|
||||
TransformComponent& transformComponent = parentComponent.Parent.GetComponent<TransformComponent>();
|
||||
|
||||
globalTransform = glm::rotate(globalTransform, glm::radians(transform.Rotation.x), Vector3(1, 0, 0));
|
||||
globalTransform = glm::rotate(globalTransform, glm::radians(transform.Rotation.y), Vector3(0, 1, 0));
|
||||
globalTransform = glm::rotate(globalTransform, glm::radians(transform.Rotation.z), Vector3(0, 0, 1));
|
||||
globalTransform = transformComponent.LocalTransform * globalTransform;
|
||||
|
||||
globalTransform = glm::scale(globalTransform, transform.Scale);
|
||||
|
||||
ParentComponent parentComponent = currentParent.GetComponent<ParentComponent>();
|
||||
while (parentComponent.HasParent)
|
||||
{
|
||||
TransformComponent& transformComponent = parentComponent.Parent.GetComponent<TransformComponent>();
|
||||
|
||||
Matrix4 currentParentMatrix = Matrix4(1.0f);
|
||||
|
||||
currentParentMatrix = glm::translate(currentParentMatrix, transformComponent.Translation);
|
||||
|
||||
currentParentMatrix = glm::rotate(currentParentMatrix, glm::radians(transformComponent.Rotation.x), Vector3(1, 0, 0));
|
||||
currentParentMatrix = glm::rotate(currentParentMatrix, glm::radians(transformComponent.Rotation.y), Vector3(0, 1, 0));
|
||||
currentParentMatrix = glm::rotate(currentParentMatrix, glm::radians(transformComponent.Rotation.z), Vector3(0, 0, 1));
|
||||
|
||||
currentParentMatrix = glm::scale(currentParentMatrix, transformComponent.Scale);
|
||||
|
||||
globalTransform = currentParentMatrix * globalTransform;
|
||||
|
||||
globalPosition += transformComponent.Translation;
|
||||
globalRotation += transformComponent.Rotation;
|
||||
globalScale *= transformComponent.Scale;
|
||||
globalPosition += transformComponent.Translation;
|
||||
globalRotation += transformComponent.Rotation;
|
||||
globalScale *= transformComponent.Scale;
|
||||
|
||||
parentComponent = parentComponent.Parent.GetComponent<ParentComponent>();
|
||||
}
|
||||
parentComponent = parentComponent.Parent.GetComponent<ParentComponent>();
|
||||
}
|
||||
|
||||
|
||||
transform.GlobalTranslation = globalPosition + transform.Translation;
|
||||
transform.GlobalRotation = globalRotation + transform.Rotation;
|
||||
transform.GlobalScale = globalScale * transform.Scale;
|
||||
transform.GlobalTranslation = globalPosition;
|
||||
transform.GlobalRotation = globalRotation;
|
||||
transform.GlobalScale = globalScale;
|
||||
|
||||
transform.Transform = globalTransform;
|
||||
}
|
||||
else
|
||||
{
|
||||
transform.GlobalTranslation = transform.Translation;
|
||||
transform.GlobalRotation = transform.Rotation;
|
||||
transform.Scale = transform.Scale;
|
||||
|
||||
globalTransform = glm::translate(globalTransform, transform.Translation);
|
||||
globalTransform = glm::rotate(globalTransform, glm::radians(transform.Rotation.x), Vector3(1, 0, 0));
|
||||
globalTransform = glm::rotate(globalTransform, glm::radians(transform.Rotation.y), Vector3(0, 1, 0));
|
||||
globalTransform = glm::rotate(globalTransform, glm::radians(transform.Rotation.z), Vector3(0, 0, 1));
|
||||
globalTransform = glm::scale(globalTransform, transform.Scale);
|
||||
transform.Transform = globalTransform;
|
||||
}
|
||||
transform.GlobalTransform = globalTransform;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user