11 return !mesh_lods_.empty();
16 if(mesh_lods_.size() > lod)
18 auto lodMesh = mesh_lods_[lod];
24 for(
unsigned int i = lod; i < mesh_lods_.size(); ++i)
26 auto lodMesh = mesh_lods_[i];
32 for(
unsigned int i = lod; i > 0; --i)
34 auto lodMesh = mesh_lods_[i];
46 if(lod >= mesh_lods_.size())
48 mesh_lods_.resize(lod + 1);
49 recalulate_lod_limits();
51 mesh_lods_[lod] =
mesh;
53 resize_materials(
mesh);
58 if(index >= materials_.size())
60 materials_.resize(index + 1);
63 materials_[index] = std::move(
material);
68 if(index >= material_instances_.size())
70 material_instances_.resize(index + 1,
nullptr);
73 material_instances_[index] = std::move(
material);
83 auto sz1 = lods.size();
84 auto sz2 = mesh_lods_.size();
90 recalulate_lod_limits();
93 if(!mesh_lods_.empty())
95 auto&
mesh = mesh_lods_[0];
96 resize_materials(
mesh);
107 return material_instances_;
113 materials_ = materials;
118 material_instances_ = materials;
123 if(materials_.size() <= index)
128 return materials_[index];
133 if(index < material_instances_.size())
135 auto instance = material_instances_[index];
142 auto instance = get_material(index);
143 if(instance.is_valid())
145 return instance.get();
153 if(index >= material_instances_.size())
155 auto asset_instance = get_material_instance(index);
157 material_instances_.resize(index + 1,
nullptr);
158 material_instances_[index] = asset_instance->clone();
161 auto& instance = material_instances_[index];
165 auto asset_instance = get_material_instance(index);
170 instance = asset_instance->clone();
175 instance = std::make_shared<pbr_material>();
190 lod_limits_ = limits;
196 const std::vector<pose_mat4>& skinning_matrices_per_palette,
200 const auto lod_mesh =
get_lod(lod);
206 auto mesh = lod_mesh.get();
214 if(non_skinned_submeshes_count > 0)
228 auto render_submesh = [
this](
const std::shared_ptr<unravel::mesh>&
mesh,
230 const math::mat4& matrix,
244 for(
const auto& index : indices)
246 const auto& submesh = submeshes[index];
248 if(index < pose.transforms.size())
250 const auto& world = pose.transforms[index];
259 params.preserve_state = &index != &indices.back();
266 render_submesh(
mesh, i, world_transform, submesh_transforms, params, callbacks);
276 if(skinned_submeshes_count > 0 && !skinning_matrices_per_palette.empty())
290 auto render_submesh_skinned = [
this](
const std::shared_ptr<unravel::mesh>&
mesh,
292 const std::vector<pose_mat4>& skinning_matrices_per_palette,
307 for(
const auto& index : indices)
309 const auto& submesh = submeshes[index];
310 const auto& skinning_matrices = skinning_matrices_per_palette[index];
314 params.preserve_state = &index != &indices.back();
321 render_submesh_skinned(
mesh, i, skinning_matrices_per_palette, params, callbacks);
331void model::recalulate_lod_limits()
333 float upper_limit = 100.0f;
335 lod_limits_.reserve(mesh_lods_.size());
337 float initial = 0.1f;
338 float step = initial / float(mesh_lods_.size());
339 for(
size_t i = 0; i < mesh_lods_.size(); ++i)
341 float lower_limit = 0.0f;
343 if(mesh_lods_.size() - 1 != i)
345 lower_limit = upper_limit * (initial - ((i)*step));
349 upper_limit = lower_limit;
355 const auto m = mesh.
get();
356 auto submeshes =
m->get_data_groups_count();
357 if(materials_.size() != submeshes)
Base class for materials used in rendering.
std::shared_ptr< material > sptr
Main class representing a 3D mesh with support for different LODs, submeshes, and skinning.
auto get_bone_palettes() const -> const bone_palette_array_t &
Retrieves the compiled bone combination palette data if this mesh has been bound as a skin.
auto get_skinned_submeshes_indices(uint32_t data_group_id) const -> const submesh_array_indices_t &
auto get_non_skinned_submeshes_indices(uint32_t data_group_id) const -> const submesh_array_indices_t &
auto get_non_skinned_submeshes_count() const -> size_t
void bind_render_buffers_for_submesh(const submesh *submesh)
Binds the mesh data for rendering the selected batch of primitives.
auto get_skin_bind_data() const -> const skin_bind_data &
Retrieves the skin bind data if this mesh has been bound as a skin.
auto get_data_groups_count() const -> size_t
Gets the number of data groups(materials) for this mesh.
auto get_submeshes() const -> const submesh_array_t &
Retrieves information about the submesh of the mesh associated with the specified data group identifi...
auto get_skinned_submeshes_count() const -> size_t
auto get_material(uint32_t index) const -> asset_handle< material >
Gets the material for the specified index.
void set_lod_limits(const std::vector< urange32_t > &limits)
Sets the LOD limits.
auto get_lod(uint32_t lod) const -> asset_handle< mesh >
Gets the LOD (Level of Detail) mesh for the specified level.
static auto fallback_material() -> asset_handle< material > &
Gets the fallback material.
void set_material(asset_handle< material > material, uint32_t index)
Sets the material for the specified index.
auto is_valid() const -> bool
Checks if the model is valid.
auto get_lod_limits() const -> const std::vector< urange32_t > &
Gets the LOD limits.
void set_materials(const std::vector< asset_handle< material > > &materials)
Sets the materials.
void set_material_instances(const std::vector< material::sptr > &materials)
static auto default_material() -> asset_handle< material > &
Gets the default material.
void submit(const math::mat4 &world_transform, const pose_mat4 &submesh_transforms, const pose_mat4 &bone_transforms, const std::vector< pose_mat4 > &skinning_matrices, unsigned int lod, const submit_callbacks &callbacks) const
Submits the model for rendering.
auto get_material_instance(uint32_t index) const -> material::sptr
auto get_material_instances() const -> const std::vector< material::sptr > &
void set_material_instance(material::sptr material, uint32_t index)
void set_lods(const std::vector< asset_handle< mesh > > &lods)
Sets the LOD meshes.
auto get_or_emplace_material_instance(uint32_t index) -> material::sptr
void set_lod(asset_handle< mesh > mesh, uint32_t lod)
Sets the LOD (Level of Detail) mesh for the specified level.
auto get_materials() const -> const std::vector< asset_handle< material > > &
Gets all the materials.
auto get_lods() const -> const std::vector< asset_handle< mesh > > &
Gets all the LOD meshes.
void set_world_transform(const void *_mtx, uint16_t _num)
Represents a handle to an asset, providing access and management functions.
auto get(bool wait=true) const -> std::shared_ptr< T >
Gets the shared pointer to the asset.
Parameters for the submit callbacks.
bool skinned
Indicates if the model is skinned.
Callbacks for submitting the model for rendering.
std::function< void(const params &info, const material &)> setup_params_per_submesh
Callback for setting up per submesh.
std::function< void(const params &info)> setup_begin
Callback for setup begin.
std::function< void(const params &info)> setup_params_per_instance
Callback for setting up per instance.
std::function< void(const params &info)> setup_end
Callback for setup end.