Support several instancers on one object #24

Merged
Bogdan Nagirniak merged 16 commits from BLEN-383 into hydra-render 2023-04-19 02:46:24 +02:00
3 changed files with 59 additions and 37 deletions
Showing only changes of commit a1bbf2512f - Show all commits

View File

@ -65,10 +65,8 @@ pxr::SdfPath BlenderSceneDelegate::GetInstancerId(pxr::SdfPath const &prim_id)
pxr::SdfPathVector BlenderSceneDelegate::GetInstancerPrototypes(pxr::SdfPath const &instancer_id) pxr::SdfPathVector BlenderSceneDelegate::GetInstancerPrototypes(pxr::SdfPath const &instancer_id)
{ {
CLOG_INFO(LOG_BSD, 3, "%s", instancer_id.GetText()); CLOG_INFO(LOG_BSD, 3, "%s", instancer_id.GetText());
pxr::SdfPathVector paths;
InstancerData *i_data = instancer_data(instancer_id); InstancerData *i_data = instancer_data(instancer_id);
paths.push_back(i_data->m_data->p_id); return i_data->prototypes();
return paths;
} }
pxr::VtIntArray BlenderSceneDelegate::GetInstanceIndices(pxr::SdfPath const &instancer_id, pxr::VtIntArray BlenderSceneDelegate::GetInstanceIndices(pxr::SdfPath const &instancer_id,
@ -76,7 +74,7 @@ pxr::VtIntArray BlenderSceneDelegate::GetInstanceIndices(pxr::SdfPath const &ins
{ {
CLOG_INFO(LOG_BSD, 3, "%s, %s", instancer_id.GetText(), prototype_id.GetText()); CLOG_INFO(LOG_BSD, 3, "%s, %s", instancer_id.GetText(), prototype_id.GetText());
InstancerData *i_data = instancer_data(instancer_id); InstancerData *i_data = instancer_data(instancer_id);
return i_data->indices(); return i_data->indices(prototype_id);
} }
pxr::GfMatrix4d BlenderSceneDelegate::GetInstancerTransform(pxr::SdfPath const &instancer_id) pxr::GfMatrix4d BlenderSceneDelegate::GetInstancerTransform(pxr::SdfPath const &instancer_id)
@ -213,7 +211,7 @@ ObjectData *BlenderSceneDelegate::object_data(pxr::SdfPath const &id)
it = objects.find(id.GetParentPath()); it = objects.find(id.GetParentPath());
if (it != objects.end()) { if (it != objects.end()) {
InstancerData *i_data = (InstancerData *)it->second.get(); InstancerData *i_data = (InstancerData *)it->second.get();
return i_data->m_data.get(); return i_data->object_data(id);
} }
return nullptr; return nullptr;
} }
@ -244,13 +242,13 @@ InstancerData *BlenderSceneDelegate::instancer_data(pxr::SdfPath const &id)
InstancerData *BlenderSceneDelegate::instancer_data(Object *object) InstancerData *BlenderSceneDelegate::instancer_data(Object *object)
{ {
InstancerData *i_data; //InstancerData *i_data;
for (auto &it : objects) { //for (auto &it : objects) {
i_data = dynamic_cast<InstancerData *>(it.second.get()); // i_data = dynamic_cast<InstancerData *>(it.second.get());
if (i_data && i_data->is_base(object)) { // if (i_data && i_data->is_base(object)) {
return i_data; // return i_data;
} // }
} //}
return nullptr; return nullptr;
} }

View File

@ -95,36 +95,52 @@ pxr::HdPrimvarDescriptorVector InstancerData::primvar_descriptors(pxr::HdInterpo
return primvars; return primvars;
} }
pxr::VtIntArray InstancerData::indices() pxr::VtIntArray InstancerData::indices(pxr::SdfPath const &id)
{ {
pxr::VtIntArray ret(transforms.size()); return instances[id].indices;
for (size_t i = 0; i < ret.size(); ++i) {
ret[i] = i;
}
return ret;
} }
bool InstancerData::is_base(Object *object) const ObjectData *InstancerData::object_data(pxr::SdfPath const &id)
{ {
return (ID *)object == id; return instances[id].obj_data.get();
}
pxr::SdfPathVector InstancerData::prototypes()
{
pxr::SdfPathVector paths;
for (auto &it : instances) {
paths.push_back(it.first);
}
return paths;
} }
void InstancerData::set_instances() void InstancerData::set_instances()
{ {
transforms.clear(); transforms.clear();
for (auto &it : instances) {
it.second.indices.clear();
}
int index = 0;
Instance *inst;
pxr::SdfPath path;
ListBase *lb = object_duplilist(scene_delegate->depsgraph, scene_delegate->scene, (Object *)id); ListBase *lb = object_duplilist(scene_delegate->depsgraph, scene_delegate->scene, (Object *)id);
LISTBASE_FOREACH (DupliObject *, dupli, lb) { LISTBASE_FOREACH (DupliObject *, dupli, lb) {
if (!m_data) { path = ObjectData::prim_id(scene_delegate, dupli->ob);
/* TODO: We create instances only for object in first dupli. path = p_id.AppendElementString(path.GetName());
Instances should be created for all objects */ auto it = instances.find(path);
m_data = ObjectData::create(scene_delegate, dupli->ob); if (it == instances.end()) {
m_data->p_id = p_id.AppendElementString(m_data->p_id.GetName()); inst = &instances[path];
inst->obj_data = ObjectData::create(scene_delegate, dupli->ob);
inst->obj_data->p_id = path;
} }
if (m_data->id != (ID *)dupli->ob) { else {
continue; inst = &it->second;
} }
transforms.push_back(gf_matrix_from_transform(dupli->mat)); transforms.push_back(gf_matrix_from_transform(dupli->mat));
ID_LOG(2, "Instance %s %d", m_data->id->name, dupli->random_id); inst->indices.push_back(index);
ID_LOG(2, "Instance %s %d", inst->obj_data->id->name, index);
++index;
} }
free_object_duplilist(lb); free_object_duplilist(lb);
} }
@ -133,13 +149,17 @@ void InstancerData::insert()
{ {
ID_LOG(2, ""); ID_LOG(2, "");
scene_delegate->GetRenderIndex().InsertInstancer(scene_delegate, p_id); scene_delegate->GetRenderIndex().InsertInstancer(scene_delegate, p_id);
m_data->insert(); for (auto &it : instances) {
it.second.obj_data->insert();
}
} }
void InstancerData::remove() void InstancerData::remove()
{ {
ID_LOG(2, ""); ID_LOG(2, "");
m_data->remove(); for (auto &it : instances) {
it.second.obj_data->remove();
}
scene_delegate->GetRenderIndex().RemoveInstancer(p_id); scene_delegate->GetRenderIndex().RemoveInstancer(p_id);
} }

View File

@ -10,6 +10,11 @@
namespace blender::render::hydra { namespace blender::render::hydra {
class InstancerData : public ObjectData { class InstancerData : public ObjectData {
struct Instance {
std::unique_ptr<ObjectData> obj_data;
pxr::VtIntArray indices;
};
public: public:
static bool supported(Object *object); static bool supported(Object *object);
static std::unique_ptr<InstancerData> create(BlenderSceneDelegate *scene_delegate, static std::unique_ptr<InstancerData> create(BlenderSceneDelegate *scene_delegate,
@ -27,16 +32,15 @@ class InstancerData : public ObjectData {
bool update_visibility(View3D *view3d) override; bool update_visibility(View3D *view3d) override;
pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation); pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation);
pxr::VtIntArray indices(); pxr::VtIntArray indices(pxr::SdfPath const &id);
ObjectData *object_data(pxr::SdfPath const &id);
bool is_base(Object *object) const; pxr::SdfPathVector prototypes();
std::unique_ptr<ObjectData> m_data;
private: private:
pxr::VtMatrix4dArray transforms;
void set_instances(); void set_instances();
pxr::TfHashMap<pxr::SdfPath, Instance, pxr::SdfPath::Hash> instances;
pxr::VtMatrix4dArray transforms;
}; };
} // namespace blender::render::hydra } // namespace blender::render::hydra