Unravel Engine C++ Reference
Loading...
Searching...
No Matches
model_component.h
Go to the documentation of this file.
1#pragma once
6#include <unordered_map>
7namespace unravel
8{
9class material;
10
15class model_component : public component_crtp<model_component, owned_component>
16{
17public:
18
24 static void on_create_component(entt::registry& r, entt::entity e);
25
31 static void on_destroy_component(entt::registry& r, entt::entity e);
32
37 void set_enabled(bool enabled);
42 void set_casts_shadow(bool cast_shadow);
43
48 void set_static(bool is_static);
49
54 auto is_enabled() const -> bool;
55
60 auto casts_shadow() const -> bool;
61
66 auto is_static() const -> bool;
67
72 auto get_model() const -> const model&;
73
78 void set_model(const model& model);
79
84 auto get_bone_transforms() const -> const pose_mat4&;
85
90 auto get_submesh_transforms() const -> const submesh_pose_mat4&;
91
96 auto get_armature_entities() const -> const std::vector<entt::handle>&;
97 auto get_armature_by_index(size_t index) const -> entt::handle;
98
104 auto get_armature_index_by_name_cached(const std::string& node_name) const -> int;
105
106 auto get_skinning_transforms() const -> const std::vector<pose_mat4>&;
107
111 auto init_armature(bool force) -> bool;
112
130 auto update_armature() -> bool;
131
136 void mark_pose_dirty() noexcept;
137
142 void set_armature_entities(const std::vector<entt::handle>& submesh_entities);
143
152 auto get_world_bounds() const -> const math::bbox&;
153 auto get_world_bounds_transform() const -> const math::transform&;
154
155 void update_world_bounds(const math::transform& world_transform);
156
163 auto get_local_bounds(uint32_t lod_index) const -> const math::bbox&;
164
169 auto get_render_proxies() const -> const submesh_render_proxies&;
170
175 auto get_submesh_material_overrides() const -> const std::vector<material::sptr>&;
176
181 auto get_submit_extras(bool shadow_pass = false) const -> model_submit_extras;
182
183 void set_last_render_frame(uint64_t frame);
184 auto is_newly_created() const noexcept -> bool;
185 auto get_last_render_frame() const noexcept -> uint64_t;
186 auto was_used_last_frame() const noexcept -> bool;
187
188 auto is_skinned() const -> bool;
189 auto get_bind_pose() const -> const animation_pose&;
190
194 auto get_armature_root_entity() const -> entt::handle;
195
199 auto get_facing_adjustment_rotation() const -> math::quat;
200
208 auto get_lod_data_for_camera(const camera* cam, uint64_t current_frame) -> lod_data&;
209
216 void cleanup_stale_lod_data(uint64_t current_frame, uint64_t max_frames_inactive = 120);
217
218private:
219 auto create_armature(bool force) -> bool;
220
225 void rebuild_armature_cache();
226
230 bool enabled_ = true;
231
235 bool static_ = true;
236
240 bool casts_shadow_ = true;
241
245 model model_;
246
250 std::vector<entt::handle> armature_entities_;
251
256 std::unordered_map<std::string, size_t> armature_name_to_index_;
257
261 animation_pose bind_pose_;
265 pose_mat4 bone_pose_;
266
270 submesh_pose_mat4 submesh_pose_;
271
275 std::vector<pose_mat4> skinning_pose_;
276
281 submesh_render_proxies render_proxies_;
282
286 std::vector<material::sptr> submesh_material_overrides_;
287
291 math::bbox world_bounds_;
292
296 math::transform world_bounds_transform_;
297
303 math::bbox pose_local_bounds_;
304
314 math::bbox culling_bounds_local_;
315
321 entt::handle bounds_anchor_;
322
327 uint64_t captured_proxies_version_{~0ULL};
328
334 bool render_proxies_stale_{false};
335
339 uint64_t last_render_frame_{};
340
345 bool pose_dirty_{true};
346
353 std::unordered_map<uintptr_t, per_camera_lod_state> per_camera_lod_data_;
354};
355
356struct bone_component : public component_crtp<bone_component>
357{
358 uint32_t bone_index{};
359};
360
366{
369 uint32_t submesh_index{0};
370
373 uint32_t stable_id{0};
374
377
379 bool casts_shadow{true};
380
382 bool enabled{true};
383};
384
385struct submesh_component : public component_crtp<submesh_component>
386{
390 std::vector<submesh_entry> entries{};
391
396 void migrate_legacy_indices(const std::vector<uint32_t>& legacy_indices)
397 {
398 if(!entries.empty() || legacy_indices.empty())
399 {
400 return;
401 }
402
403 entries.reserve(legacy_indices.size());
404 for(uint32_t index : legacy_indices)
405 {
407 entry.submesh_index = index;
408 entries.emplace_back(entry);
409 }
410 }
411};
412
413} // namespace unravel
General purpose transformation class designed to maintain each component of the transformation separa...
Definition transform.hpp:27
Class representing a camera. Contains functionality for manipulating and updating a camera....
Definition camera.h:62
Class that contains core data for meshes.
static void on_create_component(entt::registry &r, entt::entity e)
Called when the component is created.
auto get_facing_adjustment_rotation() const -> math::quat
Local rotation of the armature root (used for root motion / IK remapping).
auto was_used_last_frame() const noexcept -> bool
auto get_submesh_material_overrides() const -> const std::vector< material::sptr > &
Per-submesh material overrides (indexed by submesh index; null = model material), resolved from the s...
auto get_armature_by_index(size_t index) const -> entt::handle
auto get_last_render_frame() const noexcept -> uint64_t
auto get_submesh_transforms() const -> const submesh_pose_mat4 &
Gets the submesh transforms.
auto get_bone_transforms() const -> const pose_mat4 &
Gets the bone transforms.
void set_enabled(bool enabled)
Sets whether the model is enabled.
auto get_submit_extras(bool shadow_pass=false) const -> model_submit_extras
Convenience: builds the submit extras referencing the retained proxy data.
auto is_static() const -> bool
Checks if the model is static.
auto get_lod_data_for_camera(const camera *cam, uint64_t current_frame) -> lod_data &
Gets the per-view LOD data for a specific camera/view. Creates a new entry if this is the first acces...
auto is_newly_created() const noexcept -> bool
auto get_bind_pose() const -> const animation_pose &
auto casts_shadow() const -> bool
Checks if the model casts shadows.
auto init_armature(bool force) -> bool
Updates the armature of the model.
static void on_destroy_component(entt::registry &r, entt::entity e)
Called when the component is destroyed.
auto get_model() const -> const model &
Gets the model.
void cleanup_stale_lod_data(uint64_t current_frame, uint64_t max_frames_inactive=120)
Cleans up stale per-view LOD data entries that haven't been accessed recently. Call this periodically...
auto get_armature_index_by_name_cached(const std::string &node_name) const -> int
Gets armature index by name using cached lookup (O(1)).
auto is_enabled() const -> bool
Checks if the model is enabled.
void mark_pose_dirty() noexcept
Forces the next update_armature call to run a full refresh even when no armature transform is dirty (...
auto get_render_proxies() const -> const submesh_render_proxies &
Retained render proxies: cached world-space per-submesh bounds (including animated bounds for skinned...
auto is_skinned() const -> bool
auto get_skinning_transforms() const -> const std::vector< pose_mat4 > &
void set_model(const model &model)
Sets the model.
auto get_world_bounds_transform() const -> const math::transform &
auto get_local_bounds(uint32_t lod_index) const -> const math::bbox &
Gets the bind-pose local bounding box of the mesh asset for a given LOD.
auto get_armature_root_entity() const -> entt::handle
Armature root entity (first node of the imported skeleton hierarchy).
auto get_armature_entities() const -> const std::vector< entt::handle > &
Gets the armature entities.
void set_static(bool is_static)
Sets whether the model is static.
auto get_world_bounds() const -> const math::bbox &
Gets the pose-aware world-space bounding box for this model.
void update_world_bounds(const math::transform &world_transform)
void set_armature_entities(const std::vector< entt::handle > &submesh_entities)
Sets the armature entities.
auto update_armature() -> bool
Refreshes pose-derived render data: submesh/bone poses, cached world-space render-proxy bounds,...
void set_last_render_frame(uint64_t frame)
void set_casts_shadow(bool cast_shadow)
Sets whether the model casts shadows.
Structure describing a LOD group (set of meshes), LOD transitions, and their materials.
Definition model.h:275
uint16_t index
uint32_t frame
Definition graphics.cpp:23
Thread-safe handle to an asset.
Storage for box vector values and wraps up common functionality.
Definition bbox.h:21
CRTP (Curiously Recurring Template Pattern) base structure for components.
Contains level of detail (LOD) data for an entity per view. Uses distance-based hysteresis for stable...
Definition model.h:34
Optional retained render data consumed by the model submit paths.
Definition model.h:254
std::vector< submesh_entry > entries
void migrate_legacy_indices(const std::vector< uint32_t > &legacy_indices)
Migrates legacy serialized data that only stored bare submesh indices. Creates default-valued entries...
Per-submesh render settings authored on the armature node entity that owns the submesh.
bool casts_shadow
Whether this submesh instance casts shadows.
uint32_t stable_id
asset_handle< material > material_override
Optional material override for this submesh instance (null = model material).
bool enabled
Whether this submesh instance is rendered at all.
uint32_t submesh_index
Retained per-model render proxy data used by the culling/LOD code.
bool enabled