mirror of
https://github.com/enduro2d/enduro2d.git
synced 2025-12-15 08:15:38 +07:00
remove materials from model asset
This commit is contained in:
@@ -38,14 +38,7 @@ namespace e2d
|
|||||||
model& assign(const model& other);
|
model& assign(const model& other);
|
||||||
|
|
||||||
model& set_mesh(const mesh_asset::ptr& mesh);
|
model& set_mesh(const mesh_asset::ptr& mesh);
|
||||||
model& set_material(std::size_t index, const material_asset::ptr& material);
|
|
||||||
|
|
||||||
model& set_materials(vector<material_asset::ptr>&& materials) noexcept;
|
|
||||||
model& set_materials(const vector<material_asset::ptr>& materials);
|
|
||||||
|
|
||||||
const mesh_asset::ptr& mesh() const noexcept;
|
const mesh_asset::ptr& mesh() const noexcept;
|
||||||
const material_asset::ptr& material(std::size_t index) const;
|
|
||||||
std::size_t material_count() const noexcept;
|
|
||||||
|
|
||||||
// It can only be called from the main thread
|
// It can only be called from the main thread
|
||||||
void regenerate_geometry();
|
void regenerate_geometry();
|
||||||
@@ -53,7 +46,6 @@ namespace e2d
|
|||||||
private:
|
private:
|
||||||
mesh_asset::ptr mesh_;
|
mesh_asset::ptr mesh_;
|
||||||
render::geometry geometry_;
|
render::geometry geometry_;
|
||||||
vector<material_asset::ptr> materials_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
void swap(model& l, model& r) noexcept;
|
void swap(model& l, model& r) noexcept;
|
||||||
|
|||||||
@@ -1,6 +1,3 @@
|
|||||||
{
|
{
|
||||||
"mesh" : "gnome.e2d_mesh",
|
"mesh" : "gnome.e2d_mesh"
|
||||||
"materials" : [
|
|
||||||
"gnome_material.json"
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,11 +24,7 @@ namespace
|
|||||||
"required" : [ "mesh" ],
|
"required" : [ "mesh" ],
|
||||||
"additionalProperties" : false,
|
"additionalProperties" : false,
|
||||||
"properties" : {
|
"properties" : {
|
||||||
"mesh" : { "$ref": "#/common_definitions/address" },
|
"mesh" : { "$ref": "#/common_definitions/address" }
|
||||||
"materials" : {
|
|
||||||
"type" : "array",
|
|
||||||
"items" : { "$ref" : "#/common_definitions/address" }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})json";
|
})json";
|
||||||
|
|
||||||
@@ -59,31 +55,10 @@ namespace
|
|||||||
auto mesh_p = library.load_asset_async<mesh_asset>(
|
auto mesh_p = library.load_asset_async<mesh_asset>(
|
||||||
path::combine(parent_address, root["mesh"].GetString()));
|
path::combine(parent_address, root["mesh"].GetString()));
|
||||||
|
|
||||||
vector<stdex::promise<material_asset::load_result>> materials_p;
|
return mesh_p.then([
|
||||||
|
](const mesh_asset::load_result& mesh){
|
||||||
if ( root.HasMember("materials") ) {
|
|
||||||
E2D_ASSERT(root["materials"].IsArray());
|
|
||||||
const auto& materials_json = root["materials"];
|
|
||||||
|
|
||||||
materials_p.reserve(materials_json.Size());
|
|
||||||
for ( rapidjson::SizeType i = 0; i < materials_json.Size(); ++i ) {
|
|
||||||
E2D_ASSERT(materials_json[i].IsString());
|
|
||||||
materials_p.emplace_back(
|
|
||||||
library.load_asset_async<material_asset>(
|
|
||||||
path::combine(parent_address, materials_json[i].GetString())));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return stdex::make_tuple_promise(std::make_tuple(
|
|
||||||
std::move(mesh_p),
|
|
||||||
stdex::make_all_promise(materials_p)))
|
|
||||||
.then([](const std::tuple<
|
|
||||||
mesh_asset::load_result,
|
|
||||||
vector<material_asset::load_result>
|
|
||||||
>& results){
|
|
||||||
model content;
|
model content;
|
||||||
content.set_mesh(std::get<0>(results));
|
content.set_mesh(mesh);
|
||||||
content.set_materials(std::get<1>(results));
|
|
||||||
return the<deferrer>().do_in_main_thread([
|
return the<deferrer>().do_in_main_thread([
|
||||||
content = std::move(content)
|
content = std::move(content)
|
||||||
]() mutable {
|
]() mutable {
|
||||||
|
|||||||
@@ -166,14 +166,12 @@ namespace e2d
|
|||||||
void model::clear() noexcept {
|
void model::clear() noexcept {
|
||||||
mesh_.reset();
|
mesh_.reset();
|
||||||
geometry_.clear();
|
geometry_.clear();
|
||||||
materials_.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void model::swap(model& other) noexcept {
|
void model::swap(model& other) noexcept {
|
||||||
using std::swap;
|
using std::swap;
|
||||||
swap(mesh_, other.mesh_);
|
swap(mesh_, other.mesh_);
|
||||||
swap(geometry_, other.geometry_);
|
swap(geometry_, other.geometry_);
|
||||||
swap(materials_, other.materials_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
model& model::assign(model&& other) noexcept {
|
model& model::assign(model&& other) noexcept {
|
||||||
@@ -189,7 +187,6 @@ namespace e2d
|
|||||||
model m;
|
model m;
|
||||||
m.mesh_ = other.mesh_;
|
m.mesh_ = other.mesh_;
|
||||||
m.geometry_ = other.geometry_;
|
m.geometry_ = other.geometry_;
|
||||||
m.materials_ = other.materials_;
|
|
||||||
swap(m);
|
swap(m);
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
@@ -201,39 +198,10 @@ namespace e2d
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
model& model::set_material(std::size_t index, const material_asset::ptr& material) {
|
|
||||||
if ( materials_.size() <= index ) {
|
|
||||||
materials_.resize(index + 1);
|
|
||||||
}
|
|
||||||
materials_[index] = material;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
model& model::set_materials(vector<material_asset::ptr>&& materials) noexcept {
|
|
||||||
materials_ = std::move(materials);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
model& model::set_materials(const vector<material_asset::ptr>& materials) {
|
|
||||||
materials_ = materials;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
const mesh_asset::ptr& model::mesh() const noexcept {
|
const mesh_asset::ptr& model::mesh() const noexcept {
|
||||||
return mesh_;
|
return mesh_;
|
||||||
}
|
}
|
||||||
|
|
||||||
const material_asset::ptr& model::material(std::size_t index) const {
|
|
||||||
if ( index < materials_.size() ) {
|
|
||||||
return materials_[index];
|
|
||||||
}
|
|
||||||
throw bad_model_access();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::size_t model::material_count() const noexcept {
|
|
||||||
return materials_.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
void model::regenerate_geometry() {
|
void model::regenerate_geometry() {
|
||||||
if ( mesh_ ) {
|
if ( mesh_ ) {
|
||||||
geometry_ = make_geometry(mesh_->content());
|
geometry_ = make_geometry(mesh_->content());
|
||||||
@@ -254,21 +222,8 @@ namespace e2d
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool operator==(const model& l, const model& r) noexcept {
|
bool operator==(const model& l, const model& r) noexcept {
|
||||||
if ( l.mesh() != r.mesh() ) {
|
return l.mesh() == r.mesh()
|
||||||
return false;
|
&& l.geometry() == r.geometry();
|
||||||
}
|
|
||||||
if ( l.geometry() != r.geometry() ) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if ( l.material_count() != r.material_count() ) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
for ( std::size_t i = 0; i < l.material_count(); ++i ) {
|
|
||||||
if ( l.material(i) != r.material(i) ) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator!=(const model& l, const model& r) noexcept {
|
bool operator!=(const model& l, const model& r) noexcept {
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ namespace e2d { namespace render_system_impl
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( !mdl_r.model() || !mdl_r.model()->content().mesh()) {
|
if ( !mdl_r.model() || !mdl_r.model()->content().mesh() ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,11 +99,11 @@ namespace e2d { namespace render_system_impl
|
|||||||
|
|
||||||
const std::size_t submesh_count = math::min(
|
const std::size_t submesh_count = math::min(
|
||||||
msh.indices_submesh_count(),
|
msh.indices_submesh_count(),
|
||||||
mdl.material_count());
|
node_r.materials().size());
|
||||||
|
|
||||||
for ( std::size_t i = 0, first_index = 0; i < submesh_count; ++i ) {
|
for ( std::size_t i = 0, first_index = 0; i < submesh_count; ++i ) {
|
||||||
const std::size_t index_count = msh.indices(i).size();
|
const std::size_t index_count = msh.indices(i).size();
|
||||||
const material_asset::ptr& mat = mdl.material(i);
|
const material_asset::ptr& mat = node_r.materials()[i];
|
||||||
if ( mat ) {
|
if ( mat ) {
|
||||||
render_.execute(render::draw_command(
|
render_.execute(render::draw_command(
|
||||||
mat->content(),
|
mat->content(),
|
||||||
|
|||||||
@@ -1,6 +1,3 @@
|
|||||||
{
|
{
|
||||||
"mesh" : "mesh.e2d_mesh",
|
"mesh" : "mesh.e2d_mesh"
|
||||||
"materials" : [
|
|
||||||
"material.json"
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -179,8 +179,6 @@ TEST_CASE("library"){
|
|||||||
auto model_res = l.load_asset<model_asset>("model.json");
|
auto model_res = l.load_asset<model_asset>("model.json");
|
||||||
REQUIRE(model_res);
|
REQUIRE(model_res);
|
||||||
REQUIRE(model_res->content().mesh());
|
REQUIRE(model_res->content().mesh());
|
||||||
REQUIRE(model_res->content().material_count() == 1);
|
|
||||||
REQUIRE(model_res->content().material(0));
|
|
||||||
REQUIRE_FALSE(model_res->content().mesh()->content().vertices().empty());
|
REQUIRE_FALSE(model_res->content().mesh()->content().vertices().empty());
|
||||||
REQUIRE(model_res->content().mesh()->content().indices_submesh_count() == 1);
|
REQUIRE(model_res->content().mesh()->content().indices_submesh_count() == 1);
|
||||||
REQUIRE_FALSE(model_res->content().mesh()->content().indices(0).empty());
|
REQUIRE_FALSE(model_res->content().mesh()->content().indices(0).empty());
|
||||||
|
|||||||
Reference in New Issue
Block a user