Unravel Engine C++ Reference
Loading...
Searching...
No Matches
model_system.cpp
Go to the documentation of this file.
1#include "model_system.h"
4
5#include <engine/ecs/ecs.h>
6#include <engine/events.h>
8
9#include <logging/logging.h>
10
11#define POOLSTL_STD_SUPPLEMENT 1
12#include <poolstl/poolstl.hpp>
13
14namespace unravel
15{
16
18{
19 APPLOG_TRACE("{}::{}", hpp::type_name_str(*this), __func__);
20 auto& ev = ctx.get_cached<events>();
21
22 ev.on_play_begin.connect(sentinel_, 1000, this, &model_system::on_play_begin);
23
24 return true;
25}
26
28{
29 APPLOG_TRACE("{}::{}", hpp::type_name_str(*this), __func__);
30
31 return true;
32}
33
35{
36 APPLOG_TRACE("{}::{}", hpp::type_name_str(*this), __func__);
37
38 auto& ec = ctx.get_cached<ecs>();
39 auto& scn = ec.get_scene();
40
41 auto view = scn.registry->view<model_component>();
42
43 // this pass can create new entities so we cannot parallelize it
44 std::for_each(view.begin(),
45 view.end(),
46 [&](entt::entity entity)
47 {
48 auto& model_comp = view.get<model_component>(entity);
49 model_comp.init_armature(true);
50 });
51}
52
54{
55 APP_SCOPE_PERF("Model/System Update");
56
58
59 // this pass can create new entities so we cannot parallelize it
60 std::for_each(view.begin(),
61 view.end(),
62 [&](entt::entity entity)
63 {
64 auto& model_comp = view.get<model_component>(entity);
65 model_comp.init_armature(false);
66 });
67}
68
70{
71 APP_SCOPE_PERF("Model/Skinning");
72 static const std::string thread_name = "Model/Skinning Pool Thread";
74
76 // this code should be thread safe as each task works with a whole hierarchy and
77 // there is no interleaving between tasks.
78 std::for_each(poolstl::par,//std::execution::par,
79 view.begin(),
80 view.end(),
81 [&](entt::entity entity)
82 {
83 APP_SCOPE_PERF_THREAD("Model/Skinning/Update Armature","Pool Thread");
84 // This is needed as we call .get on the model inside the update_armature
85 tpp::this_thread::register_this_thread(thread_name, true);
86
87 auto& model_comp = view.get<model_component>(entity);
88
89 // Cleanup stale per-view LOD data (views not accessed for 2 seconds at 60fps)
90 model_comp.cleanup_stale_lod_data(frame, 120);
91
92 if(model_comp.is_newly_created())
93 {
94 model_comp.set_last_render_frame(frame);
95 }
96
97 // Refresh pose-derived render data (submesh/bone poses, cached proxy
98 // bounds, skinning palettes) before world bounds are computed. This is
99 // change-driven, not visibility-driven: update_armature early-outs when
100 // no armature transform changed, so idle models cost a bit-scan, while
101 // culled-but-still-animating models keep their bounds fresh so they can
102 // re-enter the frustum correctly. (Skipping animation work for culled
103 // models is the animation system's job via its renderer-based culling
104 // mode, which freezes the transforms and thus also skips this refresh.)
105 model_comp.update_armature();
106
107 auto& transform_comp = view.get<transform_component>(entity);
108 model_comp.update_world_bounds(transform_comp.get_transform_global());
109 });
110
111}
112
113void model_system::on_play_begin(hpp::span<const entt::handle> entities, delta_t dt)
114{
115 for(auto entity : entities)
116 {
117 if(auto model_comp = entity.try_get<model_component>())
118 {
119 model_comp->init_armature(false);
120
121 auto& transform_comp = entity.get<transform_component>();
122
123 model_comp->update_world_bounds(transform_comp.get_transform_global());
124 }
125 }
126}
127
128} // namespace unravel
Class that contains core data for meshes.
auto deinit(rtti::context &ctx) -> bool
void on_frame_before_render(scene &scn, delta_t dt)
void on_play_begin(rtti::context &ctx)
void on_frame_update(scene &scn, delta_t dt)
auto init(rtti::context &ctx) -> bool
Component that handles transformations (position, rotation, scale, etc.) in the ACE framework.
std::chrono::duration< float > delta_t
uint16_t view
uint32_t frame
Definition graphics.cpp:23
#define APPLOG_TRACE(...)
Definition logging.h:17
uint32_t get_render_frame()
#define APP_SCOPE_PERF(name_literal)
Create a scoped performance timer that records to the timeline profiler. Only accepts string literals...
Definition profiler.h:675
entt::handle entity
auto get_cached() -> T &
Definition context.hpp:49
Manages the entity-component-system (ECS) operations for the ACE framework.
Definition ecs.h:12
auto get_scene() -> scene &
Gets the current scene.
Definition ecs.cpp:30
hpp::event< void(rtti::context &)> on_play_begin
Definition events.h:24
Represents a scene in the ACE framework, managing entities and their relationships.
Definition scene.h:70
std::unique_ptr< entt::registry > registry
The registry that manages all entities in the scene.
Definition scene.h:187