Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Tetragrama/Components/HierarchyViewUIComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,11 @@ namespace Tetragrama::Components
Vec3f new_pos, new_rot, new_scale;
if (DecomposeTransformComponent(transform, new_pos, new_rot, new_scale))
{
tc->PreviousPosition = tc->Position;
tc->Position = new_pos;
tc->Rotation = new_rot;
tc->Scale = new_scale;
tc->PreviousPosition = new_pos;

// Keep RenderScene in sync — TransformComponent and Instances are not auto-bridged yet.
auto* mc = actor->GetComponent<MeshComponent>();
if (mc && mc->RenderInstanceId != UINT32_MAX)
scene->SetInstanceTransform(mc->RenderInstanceId, transform);
Expand Down
45 changes: 45 additions & 0 deletions Tetragrama/EditorScene.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include <Tetragrama/EditorScene.h>
#include <ZEngine/ECS/Components/LightComponent.h>
#include <ZEngine/ECS/Components/MeshComponent.h>
#include <ZEngine/ECS/Components/NameComponent.h>
#include <ZEngine/ECS/Components/TransformComponent.h>
#include <ZEngine/Engine.h>
#include <ZEngine/Helpers/MemoryOperations.h>
#include <ZEngine/Importers/AssetCodec.h>
#include <ZEngine/Managers/AssetManager.h>
#include <ZEngine/Rendering/BuiltinMeshes.h>
using namespace ZEngine::Core::Containers;
using namespace ZEngine::ECS::Components;
using namespace ZEngine::Managers;
Expand All @@ -29,6 +32,48 @@ namespace Tetragrama
// Allocate a sub-arena for the instance list.
LocalArena.CreateSubArena(ZMega(4), &InstanceArena);
Instances.init(&InstanceArena, 64);

// Spawn a default directional light so new scenes are not dark.
// Rotation: -60° pitch (mostly downward), 30° yaw (slight horizontal angle).
auto* ctx = ZEngine::Engine::GetContext();
if (ctx && ctx->ActorManager)
{
ZEngine::Rendering::RegisterBuiltinMeshes(&LocalArena);

const uuids::uuid light_uuid = ZEngine::Rendering::BuiltinMeshUUIDParsed(ZEngine::Rendering::BuiltinMeshID::DirectionalLightIcon);
if (light_uuid.is_nil())
return;

constexpr cstring default_light_name = "DirectionalLight";

ZEngine::ECS::ActorHandle handle = ctx->ActorManager->Create();
ZEngine::ECS::Actor* actor = ctx->ActorManager->Access(handle);
if (!actor)
return;

NameComponent nc = {};
ZEngine::Helpers::secure_strncpy(nc.Value, sizeof(nc.Value), default_light_name, ZEngine::Helpers::secure_strlen(default_light_name));
actor->AddComponent<NameComponent>(nc);

TransformComponent tc = {};
tc.Rotation.x = -1.047f;
tc.Rotation.y = 0.524f;
actor->AddComponent<TransformComponent>(tc);

LightComponent lc = {};
lc.LightType = LightComponent::Type::Directional;
lc.Intensity = 3.f;
lc.Color[0] = 1.f;
lc.Color[1] = 1.f;
lc.Color[2] = 1.f;
actor->AddComponent<LightComponent>(lc);

uint32_t render_id = AddMeshInstance(light_uuid, default_light_name);
MeshComponent mc = {};
mc.MeshUUID = light_uuid;
mc.RenderInstanceId = render_id;
actor->AddComponent<MeshComponent>(mc);
}
}

bool EditorScene::HasPendingChange() const
Expand Down
8 changes: 8 additions & 0 deletions ZEngine/ZEngine/Applications/AppRenderPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ namespace ZEngine::Applications
SceneRenderer->UpdateRMMBindings(gpu_data);
}

if (Device->RRM)
{
auto* rrm = reinterpret_cast<Rendering::RenderResourceManager*>(Device->RRM);
auto* gpu_buf = SceneRenderer->RenderSceneData;
if (gpu_buf->LightBuffer.Handle)
rrm->UpdateBuffer(gpu_buf->LightBuffer, &scene->PendingLights, sizeof(Rendering::Scenes::LightArrayUBO));
}

SceneRenderer->DrawScene(frame_index, thread_index, CurrentCmdBuf, camera);
}

Expand Down
52 changes: 52 additions & 0 deletions ZEngine/ZEngine/ECS/Systems/LightSyncSystem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <ZEngine/Core/Maths/Matrix.h>
#include <ZEngine/ECS/Components/LightComponent.h>
#include <ZEngine/ECS/Components/TransformComponent.h>
#include <ZEngine/ECS/Systems/LightSyncSystem.h>

using namespace ZEngine::ECS::Components;
using namespace ZEngine::Rendering::Scenes;
using namespace ZEngine::Core::Maths;

namespace ZEngine::ECS::Systems
{
void SyncECSToLights(Scene& scene, Rendering::Scenes::RenderScene& render_scene)
{
LightArrayUBO lights = {};

scene.ForEach<TransformComponent, LightComponent>([&](EntityID, TransformComponent& tc, LightComponent& lc) {
if (lc.LightType == LightComponent::Type::Directional && lights.DirectionalCount < 4)
{
// Build rotation-only matrix and extract forward direction (column 2).
// ComposeTransformMatrix uses YXZ Euler order; column 2 = (-sy, sx*cy, cx*cy).
Mat4f rot = ComposeTransformMatrix(Vec3f(0.f, 0.f, 0.f), tc.Rotation, Vec3f(1.f, 1.f, 1.f));

auto& dir = lights.DirectionalLights[lights.DirectionalCount++];
dir.Direction.x = rot(0, 2);
dir.Direction.y = rot(1, 2);
dir.Direction.z = rot(2, 2);
dir.Direction.w = 0.f;
dir.Color.x = lc.Color[0];
dir.Color.y = lc.Color[1];
dir.Color.z = lc.Color[2];
dir.Color.w = 1.f;
dir.Intensity = lc.Intensity;
}
else if (lc.LightType == LightComponent::Type::Point && lights.PointCount < 8)
{
auto& pt = lights.PointLights[lights.PointCount++];
pt.Position.x = tc.Position.x;
pt.Position.y = tc.Position.y;
pt.Position.z = tc.Position.z;
pt.Position.w = 1.f;
pt.Color.x = lc.Color[0];
pt.Color.y = lc.Color[1];
pt.Color.z = lc.Color[2];
pt.Color.w = 1.f;
pt.Intensity = lc.Intensity;
pt.Radius = lc.Range;
}
});

render_scene.PendingLights = lights;
}
} // namespace ZEngine::ECS::Systems
11 changes: 11 additions & 0 deletions ZEngine/ZEngine/ECS/Systems/LightSyncSystem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once
#include <ZEngine/ECS/Scene.h>
#include <ZEngine/Rendering/Scenes/RenderScene.h>

namespace ZEngine::ECS::Systems
{
// Builds a LightArrayUBO from all entities with LightComponent + TransformComponent
// and stores it in RenderScene::PendingLights for upload by GraphicRenderer::DrawScene.
// Called from Engine::MainThreadRun after the fixed-step loop, before PrepareScene.
void SyncECSToLights(Scene& scene, Rendering::Scenes::RenderScene& render_scene);
} // namespace ZEngine::ECS::Systems
23 changes: 23 additions & 0 deletions ZEngine/ZEngine/ECS/Systems/TransformSyncSystem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <ZEngine/Core/Maths/Matrix.h>
#include <ZEngine/ECS/Components/MeshComponent.h>
#include <ZEngine/ECS/Components/TransformComponent.h>
#include <ZEngine/ECS/Systems/TransformSyncSystem.h>

namespace ZEngine::ECS::Systems
{
void SyncECSToRenderScene(Scene& scene, float alpha, Rendering::Scenes::RenderScene& render_scene)
{
using namespace Components;
using namespace Core::Maths;

// Use Position directly — interpolation between PreviousPosition and Position
// is deferred until fixed-timestep physics/simulation systems are active.
// Applying alpha here conflicts with immediate gizmo-driven position updates.
scene.ForEach<TransformComponent, MeshComponent>([&](EntityID, TransformComponent& tc, MeshComponent& mc) {
if (mc.RenderInstanceId == UINT32_MAX)
return;
Mat4f mat = ComposeTransformMatrix(tc.Position, tc.Rotation, tc.Scale);
render_scene.SetInstanceTransform(mc.RenderInstanceId, mat);
});
}
} // namespace ZEngine::ECS::Systems
11 changes: 11 additions & 0 deletions ZEngine/ZEngine/ECS/Systems/TransformSyncSystem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once
#include <ZEngine/ECS/Scene.h>
#include <ZEngine/Rendering/Scenes/RenderScene.h>

namespace ZEngine::ECS::Systems
{
// Propagates ECS TransformComponent + MeshComponent into RenderScene each frame.
// Uses alpha for fixed-timestep position interpolation (PreviousPosition → Position).
// Called from Engine::MainThreadRun after the fixed-step loop, before PrepareScene.
void SyncECSToRenderScene(Scene& scene, float alpha, Rendering::Scenes::RenderScene& render_scene);
} // namespace ZEngine::ECS::Systems
8 changes: 8 additions & 0 deletions ZEngine/ZEngine/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <ZEngine/Core/VFS/VFSContext.h>
#include <ZEngine/Core/VFS/VFSDiskBackend.h>
#include <ZEngine/Core/VFS/VFSPath.h>
#include <ZEngine/ECS/Systems/LightSyncSystem.h>
#include <ZEngine/ECS/Systems/TransformSyncSystem.h>
#include <ZEngine/Engine.h>
#include <ZEngine/Engine/FixedTimestepAccumulator.h>
#include <ZEngine/Engine/FrameRateCap.h>
Expand Down Expand Up @@ -278,6 +280,12 @@ namespace ZEngine
pipeline->FillOverlayPayload(r_payload.UIOverlay);
}

if (g_engine_ctx->Scene && g_app->CurrentScene)
{
ECS::Systems::SyncECSToRenderScene(*g_engine_ctx->Scene, alpha, *g_app->CurrentScene);
ECS::Systems::SyncECSToLights(*g_engine_ctx->Scene, *g_app->CurrentScene);
}

g_app->PrepareScene(r_payload);

pipeline->MailBoxBufferHead.value.store(next, std::memory_order_release);
Expand Down
Loading
Loading