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)
{
CLOG_INFO(LOG_BSD, 3, "%s", instancer_id.GetText());
pxr::SdfPathVector paths;
InstancerData *i_data = instancer_data(instancer_id);
paths.push_back(i_data->m_data->p_id);
return paths;
return i_data->prototypes();
}
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());
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)
@ -213,7 +211,7 @@ ObjectData *BlenderSceneDelegate::object_data(pxr::SdfPath const &id)
it = objects.find(id.GetParentPath());
if (it != objects.end()) {
InstancerData *i_data = (InstancerData *)it->second.get();
return i_data->m_data.get();
return i_data->object_data(id);
}
return nullptr;
}
@ -244,13 +242,13 @@ InstancerData *BlenderSceneDelegate::instancer_data(pxr::SdfPath const &id)
InstancerData *BlenderSceneDelegate::instancer_data(Object *object)
{
InstancerData *i_data;
for (auto &it : objects) {
i_data = dynamic_cast<InstancerData *>(it.second.get());
if (i_data && i_data->is_base(object)) {
return i_data;
}
}
//InstancerData *i_data;
//for (auto &it : objects) {
// i_data = dynamic_cast<InstancerData *>(it.second.get());
// if (i_data && i_data->is_base(object)) {
// return i_data;
// }
//}
return nullptr;
}

View File

@ -95,36 +95,52 @@ pxr::HdPrimvarDescriptorVector InstancerData::primvar_descriptors(pxr::HdInterpo
return primvars;
}
pxr::VtIntArray InstancerData::indices()
pxr::VtIntArray InstancerData::indices(pxr::SdfPath const &id)
{
pxr::VtIntArray ret(transforms.size());
for (size_t i = 0; i < ret.size(); ++i) {
ret[i] = i;
}
return ret;
return instances[id].indices;
}
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()
{
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_FOREACH (DupliObject *, dupli, lb) {
if (!m_data) {
/* TODO: We create instances only for object in first dupli.
Instances should be created for all objects */
m_data = ObjectData::create(scene_delegate, dupli->ob);
m_data->p_id = p_id.AppendElementString(m_data->p_id.GetName());
path = ObjectData::prim_id(scene_delegate, dupli->ob);
path = p_id.AppendElementString(path.GetName());
auto it = instances.find(path);
if (it == instances.end()) {
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) {
continue;
else {
inst = &it->second;
}
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);
}
@ -133,13 +149,17 @@ void InstancerData::insert()
{
ID_LOG(2, "");
scene_delegate->GetRenderIndex().InsertInstancer(scene_delegate, p_id);
m_data->insert();
for (auto &it : instances) {
it.second.obj_data->insert();
}
}
void InstancerData::remove()
{
ID_LOG(2, "");
m_data->remove();
for (auto &it : instances) {
it.second.obj_data->remove();
}
scene_delegate->GetRenderIndex().RemoveInstancer(p_id);
}

View File

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