From c270fdcb0353a4499166d3ab031038a1a5903dc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Thu, 2 Sep 2021 12:30:18 +0200 Subject: [PATCH] Fixed parentTransform not causing update of child transform (also improved performance) --- GlitchyEngine/src/World/TransformSystem.bf | 25 +++++++++++++--------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/GlitchyEngine/src/World/TransformSystem.bf b/GlitchyEngine/src/World/TransformSystem.bf index 4dc593b..f10df5e 100644 --- a/GlitchyEngine/src/World/TransformSystem.bf +++ b/GlitchyEngine/src/World/TransformSystem.bf @@ -9,20 +9,24 @@ namespace GlitchyEngine.World _frame++; for(var entity in world.Enumerate(typeof(TransformComponent))) + for(var (entity, transform) in world.Enumerate()) + { + UpdateEntity(entity, transform, world); + } { UpdateEntity(entity, world); } } - private static TransformComponent* UpdateEntity(Entity entity, EcsWorld world) + private static void UpdateEntity(Entity entity, TransformComponent* transform, EcsWorld world) { // Todo: this probably needs a rewrite as it may scale poorly with deep hierarchies! - var transform = world.GetComponent(entity); - - // If tranform was updated this frame -> skip + // transform was updated this frame -> skip if(transform.Frame == _frame) - return transform; + return; + + transform.Frame = _frame; var parent = world.GetComponent(entity); @@ -30,7 +34,7 @@ namespace GlitchyEngine.World { transform.LocalTransform = .Translation(transform.Position) * .RotationX(transform.Rotation.X) * .RotationY(transform.Rotation.Y) * .RotationZ(transform.Rotation.Z) * .Scaling(transform.Scale); - transform.Frame = _frame; + transform.IsDirty = false; if(parent == null) @@ -41,17 +45,18 @@ namespace GlitchyEngine.World if(parent != null) { - var parentTransform = UpdateEntity(parent.Entity, world); + var parentEntity = parent.Entity; + var parentTransform = world.GetComponent(parentEntity); + UpdateEntity(parentEntity, parentTransform, world); + + // TODO: I think we unnecessarily recalculate world transform every frame // Parent transform is newer than our transform or was updated this frame if(parentTransform.Frame >= transform.Frame) { transform.WorldTransform = parentTransform.WorldTransform * transform.LocalTransform; - transform.Frame = parentTransform.Frame; } } - - return transform; } } }