Skip to content

Commit 7cdbdae

Browse files
authored
♻️ refactor ecs interface (#27)
1 parent 9ff6a86 commit 7cdbdae

30 files changed

Lines changed: 849 additions & 651 deletions

File tree

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@
181181
"IDXGI",
182182
"indegree",
183183
"Indexs",
184+
"initializable",
184185
"ISPC",
185186
"lightmap",
186187
"LINELIST",

examples/editor/editor.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ void Editor::SceneGraphViewer() {
152152
node_flags |= ImGuiTreeNodeFlags_Selected;
153153
}
154154

155-
if (entity.HasComponent<asset::RelationShip>()) {
156-
if (entity.GetComponent<asset::RelationShip>().GetChildren().empty()) {
155+
if (entity.Has<asset::RelationShip>()) {
156+
if (entity.Get<asset::RelationShip>().GetChildren().empty()) {
157157
node_flags |= ImGuiTreeNodeFlags_Leaf;
158158
} else {
159159
node_flags |= ImGuiTreeNodeFlags_OpenOnArrow;
@@ -162,8 +162,8 @@ void Editor::SceneGraphViewer() {
162162

163163
std::pmr::string name;
164164
std::pmr::string name_id;
165-
if (entity.HasComponent<asset::MetaInfo>()) {
166-
name = entity.GetComponent<asset::MetaInfo>().name;
165+
if (entity.Has<asset::MetaInfo>()) {
166+
name = entity.Get<asset::MetaInfo>().name;
167167
name_id = fmt::format("{}-{}", name, entity);
168168
} else {
169169
name = fmt::format("{}", entity);
@@ -176,7 +176,7 @@ void Editor::SceneGraphViewer() {
176176

177177
if (node_open && !(node_flags & ImGuiTreeNodeFlags_Leaf)) {
178178
// print children
179-
for (const auto child : entity.GetComponent<asset::RelationShip>().GetChildren()) {
179+
for (const auto child : entity.Get<asset::RelationShip>().GetChildren()) {
180180
print_node(child);
181181
}
182182
ImGui::TreePop();
@@ -212,8 +212,8 @@ void Editor::SceneNodeModifier() {
212212

213213
} else {
214214
if (ImGui::BeginTabBar("SceneNodeProperties")) {
215-
if (m_SelectedEntity.HasComponent<asset::Transform>()) {
216-
auto& transform = m_SelectedEntity.GetComponent<asset::Transform>();
215+
if (m_SelectedEntity.Has<asset::Transform>()) {
216+
auto& transform = m_SelectedEntity.Get<asset::Transform>();
217217
if (ImGui::BeginTabItem("Transform")) {
218218
static bool local = true;
219219
ImGui::Checkbox("Local", &local);
@@ -228,7 +228,7 @@ void Editor::SceneNodeModifier() {
228228
transform.rotation = math::euler_to_quaternion(euler);
229229

230230
ImGui::Text("Scale");
231-
ImGui::DragFloat3("##Scale", transform.scale, 0.1f, 0.0f);
231+
ImGui::DragFloat3("##Scale", transform.scaling, 0.1f, 0.0f);
232232
} else {
233233
auto [global_position, global_rotation, global_scaling] = decompose(transform.world_matrix);
234234

@@ -249,15 +249,15 @@ void Editor::SceneNodeModifier() {
249249
auto [local_position, local_rotation, local_scaling] = decompose(new_local_transform);
250250
transform.position = local_position;
251251
transform.rotation = local_rotation;
252-
transform.scale = local_scaling;
252+
transform.scaling = local_scaling;
253253
}
254254

255255
ImGui::EndTabItem();
256256
}
257257
}
258258

259-
if (m_SelectedEntity.HasComponent<asset::MeshComponent>() && ImGui::BeginTabItem("Materials")) {
260-
const auto mesh = m_SelectedEntity.GetComponent<asset::MeshComponent>().mesh;
259+
if (m_SelectedEntity.Has<asset::MeshComponent>() && ImGui::BeginTabItem("Materials")) {
260+
const auto mesh = m_SelectedEntity.Get<asset::MeshComponent>().mesh;
261261

262262
std::pmr::unordered_set<asset::MaterialInstance*> material_instances;
263263
for (const auto& sub_mesh : mesh->sub_meshes) {
@@ -332,8 +332,8 @@ void Editor::SceneNodeModifier() {
332332
ImGui::EndTabItem();
333333
}
334334

335-
if (m_SelectedEntity.HasComponent<CameraComponent>() && ImGui::BeginTabItem("Camera")) {
336-
auto& parameters = m_SelectedEntity.GetComponent<CameraComponent>().camera->parameters;
335+
if (m_SelectedEntity.Has<CameraComponent>() && ImGui::BeginTabItem("Camera")) {
336+
auto& parameters = m_SelectedEntity.Get<CameraComponent>().camera->parameters;
337337

338338
ImGui::Text("Fov");
339339
ImGui::DragFloat("##Fov", &parameters.horizontal_fov, 0.1f, 0.0f, 180.0f);
@@ -345,8 +345,8 @@ void Editor::SceneNodeModifier() {
345345
ImGui::EndTabItem();
346346
}
347347

348-
if (m_SelectedEntity.HasComponent<LightComponent>() && ImGui::BeginTabItem("Light")) {
349-
auto& parameters = m_SelectedEntity.GetComponent<LightComponent>().light->parameters;
348+
if (m_SelectedEntity.Has<LightComponent>() && ImGui::BeginTabItem("Light")) {
349+
auto& parameters = m_SelectedEntity.Get<LightComponent>().light->parameters;
350350
ImGui::Text("Intensity");
351351
ImGui::DragFloat("##Intensity", &parameters.intensity, 0.1f, 0.0f, 1000.0f);
352352
ImGui::Text("Color");

examples/editor/scene_viewport.cpp

Lines changed: 67 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,81 @@ using namespace hitagi::math;
99
using namespace hitagi::asset;
1010

1111
void SceneViewPort::Tick() {
12-
m_GuiManager.DrawGui([&]() {
12+
m_Engine.GuiManager().DrawGui([&]() {
1313
if (ImGui::Begin("Scene Viewer", &m_Open)) {
14-
if (m_CurrentScene) {
15-
const auto v_min = ImGui::GetWindowContentRegionMin();
16-
const auto v_max = ImGui::GetWindowContentRegionMax();
17-
const auto window_size = math::vec2u{
18-
static_cast<std::uint32_t>(v_max.x - v_min.x),
19-
static_cast<std::uint32_t>(v_max.y - v_min.y)};
20-
21-
auto& render_graph = m_Render.GetRenderGraph();
22-
23-
auto scene_render_texture = render_graph.Create(gfx::TextureDesc{
24-
.name = "Scene Render Texture",
25-
.width = window_size.x,
26-
.height = window_size.y,
27-
.format = gfx::Format::R8G8B8A8_UNORM,
28-
.clear_value = math::Color{0.0f, 0.0f, 0.0f, 1.0f},
29-
.usages = gfx::TextureUsageFlags::RenderTarget | gfx::TextureUsageFlags::SRV,
30-
});
31-
32-
auto camera = m_Camera.GetComponent<asset::CameraComponent>().camera;
33-
auto camera_transform = m_Camera.GetComponent<asset::Transform>().world_matrix;
34-
35-
camera->parameters.aspect = static_cast<float>(window_size.x) / static_cast<float>(window_size.y);
36-
37-
if (window_size.x != 0 && window_size.y != 0) {
38-
m_Render.RenderScene(m_CurrentScene, *camera, camera_transform, scene_render_texture);
39-
ImGui::Image(m_GuiManager.ReadTexture(scene_render_texture), ImVec2(window_size.x, window_size.y));
40-
}
41-
}
14+
MoveCamera();
15+
RenderScene();
4216
}
4317
ImGui::End();
4418
});
4519

4620
RuntimeModule::Tick();
4721
}
4822

23+
void SceneViewPort::MoveCamera() const {
24+
if (m_CurrentScene) {
25+
auto& input_manager = m_Engine.App().GetInputManager();
26+
27+
auto camera = m_Camera;
28+
29+
const auto& camera_param = camera.Get<asset::CameraComponent>().camera->parameters;
30+
auto& camera_transform = camera.Get<asset::Transform>();
31+
32+
auto delta_time = m_Engine.GetDeltaTime().count();
33+
auto speed = 10.0f * delta_time;
34+
35+
if (input_manager.GetBool(hid::VirtualKeyCode::KEY_W)) {
36+
camera_transform.Translate(camera_param.look_dir * speed);
37+
}
38+
if (input_manager.GetBool(hid::VirtualKeyCode::KEY_S)) {
39+
camera_transform.Translate(-camera_param.look_dir * speed);
40+
}
41+
if (input_manager.GetBool(hid::VirtualKeyCode::KEY_A)) {
42+
camera_transform.Translate(-math::normalize(math::cross(camera_param.look_dir, camera_param.up)) * speed);
43+
}
44+
if (input_manager.GetBool(hid::VirtualKeyCode::KEY_D)) {
45+
camera_transform.Translate(math::normalize(math::cross(camera_param.look_dir, camera_param.up)) * speed);
46+
}
47+
48+
if (input_manager.GetBool(hid::VirtualKeyCode::MOUSE_R_BUTTON)) {
49+
const auto mouse_delta = math::vec2f{input_manager.GetFloatDelta(hid::MouseEvent::MOVE_X), input_manager.GetFloatDelta(hid::MouseEvent::MOVE_Y)};
50+
camera_transform.Rotate(get_rotation(math::rotate_z(-mouse_delta.x * 0.01f) * math::rotate_x(-mouse_delta.y * 0.01f)));
51+
}
52+
}
53+
}
54+
55+
void SceneViewPort::RenderScene() const {
56+
if (m_CurrentScene) {
57+
const auto v_min = ImGui::GetWindowContentRegionMin();
58+
const auto v_max = ImGui::GetWindowContentRegionMax();
59+
const auto window_size = math::vec2u{
60+
static_cast<std::uint32_t>(v_max.x - v_min.x),
61+
static_cast<std::uint32_t>(v_max.y - v_min.y)};
62+
63+
auto& render = m_Engine.Renderer();
64+
auto& render_graph = render.GetRenderGraph();
65+
66+
auto scene_render_texture = render_graph.Create(gfx::TextureDesc{
67+
.name = "Scene Render Texture",
68+
.width = window_size.x,
69+
.height = window_size.y,
70+
.format = gfx::Format::R8G8B8A8_UNORM,
71+
.clear_value = math::Color{0.0f, 0.0f, 0.0f, 1.0f},
72+
.usages = gfx::TextureUsageFlags::RenderTarget | gfx::TextureUsageFlags::SRV,
73+
});
74+
75+
auto camera = m_Camera.Get<asset::CameraComponent>().camera;
76+
auto camera_transform = m_Camera.Get<asset::Transform>().world_matrix;
77+
78+
camera->parameters.aspect = static_cast<float>(window_size.x) / static_cast<float>(window_size.y);
79+
80+
if (window_size.x != 0 && window_size.y != 0) {
81+
render.RenderScene(m_CurrentScene, *camera, camera_transform, scene_render_texture);
82+
ImGui::Image(m_Engine.GuiManager().ReadTexture(scene_render_texture), ImVec2(window_size.x, window_size.y));
83+
}
84+
}
85+
}
86+
4987
void SceneViewPort::SetScene(std::shared_ptr<asset::Scene> scene) noexcept {
5088
m_CurrentScene = std::move(scene);
5189
if (m_CurrentScene != nullptr) {

examples/editor/scene_viewport.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace hitagi {
55
class SceneViewPort : public RuntimeModule {
66
public:
77
SceneViewPort(const Engine& engine)
8-
: RuntimeModule("SceneViewPort"), m_Render(engine.Renderer()), m_GuiManager(engine.GuiManager()) {}
8+
: RuntimeModule("SceneViewPort"), m_Engine(engine) {}
99

1010
void Tick() final;
1111

@@ -14,8 +14,10 @@ class SceneViewPort : public RuntimeModule {
1414
inline auto GetScene() const noexcept { return m_CurrentScene; };
1515

1616
private:
17-
render::IRenderer& m_Render;
18-
gui::GuiManager& m_GuiManager;
17+
void MoveCamera() const;
18+
void RenderScene() const;
19+
20+
const Engine& m_Engine;
1921
bool m_Open = true;
2022
std::shared_ptr<asset::Scene> m_CurrentScene = nullptr;
2123
ecs::Entity m_Camera;

examples/playground/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ auto main(int argc, char** argv) -> int {
4646

4747
auto camera = std::make_shared<asset::Camera>(asset::Camera::Parameters{});
4848
auto camera_entity = scene->CreateCameraEntity(camera, {}, {}, "");
49-
auto& camera_transform = camera_entity.GetComponent<asset::Transform>();
49+
auto& camera_transform = camera_entity.Get<asset::Transform>();
5050

5151
auto light = scene->CreateLightEntity(std::make_shared<asset::Light>(asset::Light::Parameters{}), {}, {}, "");
5252
auto cube_mesh = asset::MeshFactory::Cube();
@@ -60,10 +60,10 @@ auto main(int argc, char** argv) -> int {
6060
engine.GuiManager().DrawGui([&]() {
6161
static bool open = true;
6262
if (ImGui::Begin("Cube info", &open)) {
63-
auto& cube_transform = cube.GetComponent<asset::Transform>();
63+
auto& cube_transform = cube.Get<asset::Transform>();
6464

6565
ImGui::DragFloat3("Cube position", cube_transform.position, 0.01);
66-
ImGui::DragFloat3("Cube scaling", cube_transform.scale, 0.01);
66+
ImGui::DragFloat3("Cube scaling", cube_transform.scaling, 0.01);
6767

6868
ImGui::Separator();
6969

hitagi/asset/include/hitagi/asset/meta_info.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace hitagi::asset {
66
struct MetaInfo {
7+
MetaInfo(std::string_view name) : name(name) {}
8+
79
std::pmr::string name;
810
};
911
static_assert(ecs::Component<MetaInfo>);

hitagi/asset/include/hitagi/asset/transform.hpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
namespace hitagi::asset {
99

1010
struct RelationShip {
11-
ecs::Entity parent = {};
11+
RelationShip(ecs::Entity parent = {}) : parent(parent) {}
12+
13+
ecs::Entity parent;
1214

1315
const auto& GetChildren() const noexcept { return children; }
1416

@@ -24,17 +26,22 @@ struct RelationShipSystem {
2426
};
2527

2628
struct Transform {
29+
Transform(math::vec3f position = math::vec3f(0.0f), math::quatf rotation = math::quatf::identity(), math::vec3f scaling = math::vec3f(1.0f))
30+
: position(position),
31+
rotation(rotation),
32+
scaling(scaling) {}
33+
2734
math::vec3f position;
28-
math::quatf rotation = math::quatf::identity();
29-
math::vec3f scale = math::vec3f(1.0f);
35+
math::quatf rotation;
36+
math::vec3f scaling;
3037

31-
math::mat4f world_matrix;
38+
math::mat4f world_matrix = math::mat4f::identity();
3239

33-
inline void ApplyScale(float value) noexcept { scale = value; }
40+
inline void ApplyScale(float value) noexcept { scaling = value; }
3441
inline void Translate(const math::vec3f& value) noexcept { position += value; }
3542
inline void Rotate(const math::quatf& value) noexcept { rotation = value * rotation; }
3643

37-
inline auto ToMatrix() const noexcept { return math::translate(position) * math::rotate(rotation) * math::scale(scale); }
44+
inline auto ToMatrix() const noexcept { return math::translate(position) * math::rotate(rotation) * math::scale(scaling); }
3845
};
3946

4047
struct TransformSystem {

hitagi/asset/src/scene.cpp

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,13 @@ void Scene::Update() {
1818
auto Scene::CreateEmptyEntity(math::mat4f transform, ecs::Entity parent, std::string_view name) -> ecs::Entity {
1919
auto& em = m_World.GetEntityManager();
2020

21-
const auto [translation, rotation, scale] = math::decompose(transform);
21+
const auto [translation, rotation, scaling] = math::decompose(transform);
2222
if (!parent && m_RootEntity) parent = m_RootEntity;
2323

24-
ecs::Entity entity;
25-
26-
entity = em.Create<MetaInfo, Transform, RelationShip>();
27-
entity.GetComponent<RelationShip>().parent = parent;
28-
entity.GetComponent<MetaInfo>().name = name;
29-
30-
auto& local_transform = entity.GetComponent<Transform>();
31-
local_transform.position = translation;
32-
local_transform.rotation = rotation;
33-
local_transform.scale = scale;
24+
ecs::Entity entity = em.Create();
25+
entity.Emplace<MetaInfo>(name);
26+
entity.Emplace<Transform>(translation, rotation, scaling);
27+
entity.Emplace<RelationShip>(parent);
3428

3529
return entity;
3630
}
@@ -39,8 +33,8 @@ auto Scene::CreateMeshEntity(std::shared_ptr<Mesh> mesh, math::mat4f transform,
3933
assert(mesh != nullptr);
4034

4135
auto entity = CreateEmptyEntity(transform, parent, name);
42-
entity.Attach<MeshComponent>();
43-
entity.GetComponent<MeshComponent>().mesh = std::move(mesh);
36+
entity.Emplace<MeshComponent>();
37+
entity.Get<MeshComponent>().mesh = std::move(mesh);
4438

4539
return m_MeshEntities.emplace_back(entity);
4640
}
@@ -49,8 +43,8 @@ auto Scene::CreateCameraEntity(std::shared_ptr<Camera> camera, math::mat4f trans
4943
assert(camera != nullptr);
5044

5145
auto entity = CreateEmptyEntity(transform, parent, name);
52-
entity.Attach<CameraComponent>();
53-
entity.GetComponent<CameraComponent>().camera = std::move(camera);
46+
entity.Emplace<CameraComponent>();
47+
entity.Get<CameraComponent>().camera = std::move(camera);
5448

5549
m_CurrentCamera = entity;
5650
return m_CameraEntities.emplace_back(entity);
@@ -60,8 +54,8 @@ auto Scene::CreateLightEntity(std::shared_ptr<Light> light, math::mat4f transfor
6054
assert(light != nullptr);
6155

6256
auto entity = CreateEmptyEntity(transform, parent, name);
63-
entity.Attach<LightComponent>();
64-
entity.GetComponent<LightComponent>().light = std::move(light);
57+
entity.Emplace<LightComponent>();
58+
entity.Get<LightComponent>().light = std::move(light);
6559

6660
return m_LightEntities.emplace_back(entity);
6761
}
@@ -70,8 +64,8 @@ auto Scene::CreateSkeletonEntity(std::shared_ptr<Skeleton> skeleton, math::mat4f
7064
assert(skeleton != nullptr);
7165

7266
auto entity = CreateEmptyEntity(transform, parent, name);
73-
entity.Attach<SkeletonComponent>();
74-
entity.GetComponent<SkeletonComponent>().skeleton = std::move(skeleton);
67+
entity.Emplace<SkeletonComponent>();
68+
entity.Get<SkeletonComponent>().skeleton = std::move(skeleton);
7569

7670
return entity;
7771
}

hitagi/asset/src/transform.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ void RelationShipSystem::OnUpdate(ecs::Schedule& schedule) {
1010
schedule.Request("attach_parent", [](const ecs::Entity entity, RelationShip& relation_ship) {
1111
if (relation_ship.prev_parent != relation_ship.parent) { // parent changed
1212
if (relation_ship.prev_parent) {
13-
auto& prev_parent = relation_ship.prev_parent.GetComponent<RelationShip>();
13+
auto& prev_parent = relation_ship.prev_parent.Get<RelationShip>();
1414
prev_parent.children.erase(entity);
1515
}
1616
if (relation_ship.parent) {
17-
auto& parent = relation_ship.parent.GetComponent<RelationShip>();
17+
auto& parent = relation_ship.parent.Get<RelationShip>();
1818
parent.children.insert(entity);
1919
}
2020
relation_ship.prev_parent = relation_ship.parent;
@@ -36,9 +36,9 @@ void TransformSystem::OnUpdate(ecs::Schedule& schedule) {
3636
[](const Transform& transform, const RelationShip& relation_ship) {
3737
const std::function<void(ecs::Entity, const math::mat4f)> recursive_update =
3838
[&](ecs::Entity entity, const math::mat4f parent_transform) {
39-
auto& transform = entity.GetComponent<Transform>();
39+
auto& transform = entity.Get<Transform>();
4040
transform.world_matrix = parent_transform * transform.world_matrix;
41-
for (auto child : entity.GetComponent<RelationShip>().GetChildren()) {
41+
for (auto child : entity.Get<RelationShip>().GetChildren()) {
4242
recursive_update(child, transform.world_matrix);
4343
}
4444
};

0 commit comments

Comments
 (0)