Support multimaterials on one mesh #44

Merged
Bogdan Nagirniak merged 11 commits from BLEN-417 into hydra-render 2023-05-24 07:07:39 +02:00
2 changed files with 25 additions and 4 deletions
Showing only changes of commit 9510f62faa - Show all commits

View File

@ -38,7 +38,6 @@ void MeshData::init()
BKE_object_to_mesh_clear(object); BKE_object_to_mesh_clear(object);
} }
write_material();
write_transform(); write_transform();
} }
Review

The order is important here?

The order is important here?
Review

No

No
@ -73,7 +72,7 @@ void MeshData::update()
} }
else { else {
if (id->recalc & ID_RECALC_SHADING) { if (id->recalc & ID_RECALC_SHADING) {
write_material(); write_material(nullptr);
bits |= pxr::HdChangeTracker::DirtyMaterialId | pxr::HdChangeTracker::DirtyDoubleSided; bits |= pxr::HdChangeTracker::DirtyMaterialId | pxr::HdChangeTracker::DirtyDoubleSided;
} }
if (id->recalc & ID_RECALC_TRANSFORM) { if (id->recalc & ID_RECALC_TRANSFORM) {
@ -189,6 +188,7 @@ void MeshData::write_mesh(Mesh *mesh)
vertices_.clear(); vertices_.clear();
normals_.clear(); normals_.clear();
uvs_.clear(); uvs_.clear();
face_material_indices_.clear();
BKE_mesh_calc_normals_split(mesh); BKE_mesh_calc_normals_split(mesh);
int tris_len = BKE_mesh_runtime_looptri_len(mesh); int tris_len = BKE_mesh_runtime_looptri_len(mesh);
@ -219,9 +219,10 @@ void MeshData::write_mesh(Mesh *mesh)
write_normals(mesh); write_normals(mesh);
write_uv_maps(mesh); write_uv_maps(mesh);
write_material(mesh);
} }
void MeshData::write_material() void MeshData::write_material(Mesh *mesh)
{ {
Object *object = (Object *)id; Object *object = (Object *)id;
Material *mat = nullptr; Material *mat = nullptr;
@ -233,6 +234,7 @@ void MeshData::write_material()
mat_data_ = nullptr; mat_data_ = nullptr;
return; return;
} }
pxr::SdfPath p_id = scene_delegate_->material_prim_id(mat); pxr::SdfPath p_id = scene_delegate_->material_prim_id(mat);
mat_data_ = scene_delegate_->material_data(p_id); mat_data_ = scene_delegate_->material_data(p_id);
if (!mat_data_) { if (!mat_data_) {
@ -241,6 +243,18 @@ void MeshData::write_material()
mat_data_->init(); mat_data_->init();
mat_data_->insert(); mat_data_->insert();
} }
if (mesh) {
face_material_indices_.clear();
const int *material_indices = BKE_mesh_material_indices(mesh);
const int *looptri_polys = BKE_mesh_runtime_looptri_polys_ensure(mesh);
int poly_ind;
blender::Span<MLoopTri> loopTris = mesh->looptris();
for (size_t i = 0; i < loopTris.size(); ++i) {
poly_ind = looptri_polys[i];
face_material_indices_.push_back(material_indices[poly_ind]);
}
}
} }
void MeshData::write_uv_maps(Mesh *mesh) void MeshData::write_uv_maps(Mesh *mesh)

View File

@ -14,6 +14,12 @@
namespace blender::render::hydra { namespace blender::render::hydra {
class MeshData : public ObjectData { class MeshData : public ObjectData {
struct MaterialMesh {
pxr::VtIntArray face_vertex_counts;
pxr::VtIntArray face_vertex_indices;
MaterialData *mat_data = nullptr;
};
public: public:
MeshData(BlenderSceneDelegate *scene_delegate, Object *object, pxr::SdfPath const &prim_id); MeshData(BlenderSceneDelegate *scene_delegate, Object *object, pxr::SdfPath const &prim_id);
@ -35,7 +41,7 @@ class MeshData : public ObjectData {
private: private:
void write_mesh(Mesh *mesh); void write_mesh(Mesh *mesh);
void write_material(); void write_material(Mesh *mesh);
void write_uv_maps(Mesh *mesh); void write_uv_maps(Mesh *mesh);
void write_normals(Mesh *mesh); void write_normals(Mesh *mesh);
@ -45,6 +51,7 @@ class MeshData : public ObjectData {
pxr::VtVec2fArray uvs_; pxr::VtVec2fArray uvs_;
pxr::VtVec3fArray normals_; pxr::VtVec3fArray normals_;
pxr::VtIntArray face_material_indices_;
MaterialData *mat_data_ = nullptr; MaterialData *mat_data_ = nullptr;
}; };