BLEN-359: Implement updates for instances in viewport #20

Merged
Bogdan Nagirniak merged 20 commits from BLEN-359_1 into hydra-render 2023-04-05 11:47:37 +02:00
3 changed files with 37 additions and 10 deletions
Showing only changes of commit aa3c52a375 - Show all commits

View File

@ -134,6 +134,9 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility)
}
if (data.dupli_object_current != nullptr) {
if (!InstancerData::supported(data.dupli_object_current->ob)) {
continue;
}
add_update_instancer(data.dupli_object_current);
continue;
}
@ -204,7 +207,7 @@ void BlenderSceneDelegate::add_update_instancer(DupliObject *dupli)
if (!i_data) {
objects[id] = InstancerData::create(this, dupli->ob);
i_data = instancer_data(id, true);
// i_data->update_visibility(view3d);
i_data->update_visibility(view3d);
}
i_data->add_instance(dupli);

View File

@ -66,16 +66,28 @@ GfMatrix4d InstancerData::transform()
return transforms[0];
}
bool InstancerData::update_visibility(View3D *view3d)
{
if (!view3d) {
return false;
}
Object *obj = (Object *)id;
if (!obj->parent) {
return false;
}
bool prev_visible = visible;
visible = BKE_object_is_visible_in_viewport(view3d, obj->parent);
return visible != prev_visible;
}
VtValue InstancerData::get_data(TfToken const &key) const
{
CLOG_INFO(LOG_BSD, 3, "%s [%s]", id->name, key.GetText());
if (key == HdInstancerTokens->instanceTransform) {
pxr::VtMatrix4dArray t = transforms;
/* USD hides the prototype mesh when instancing in contrary to the Blender,
so we must add it back implicitly */
t[0] = pxr::GfMatrix4d(1.0);
return VtValue(transforms);
return VtValue(calc_instance_transforms());
}
return MeshData::get_data(key);
}
@ -124,7 +136,7 @@ size_t InstancerData::sample_instancer_primvar(TfToken const &key,
void InstancerData::add_instance(DupliObject *dupli)
{
CLOG_INFO(LOG_BSD, 2, "%s [%d]", id->name, dupli->random_id);
transforms.push_back(/*ObjectData::transform().GetInverse() **/ gf_matrix_from_transform(dupli->mat));
transforms.push_back(gf_matrix_from_transform(dupli->mat));
}
void InstancerData::insert()
@ -135,7 +147,6 @@ void InstancerData::insert()
if (face_vertex_counts.empty()) {
return;
}
scene_delegate->GetRenderIndex().InsertInstancer(scene_delegate, instancer_id);
}
@ -154,8 +165,19 @@ void InstancerData::remove()
void InstancerData::update()
{
CLOG_INFO(LOG_BSD, 2, "%s", id->name);
MeshData::update();
}
pxr::VtMatrix4dArray InstancerData::calc_instance_transforms() const
{
pxr::VtMatrix4dArray ret;
/* USD hides the prototype mesh when instancing in contrary to the Blender,
so we must add it back implicitly */
ret.push_back(pxr::GfMatrix4d(1.0));
for (size_t i = 1; i < transforms.size(); ++i) {
ret.push_back(transforms[0].GetInverse() * transforms[i]);
}
return ret;
}
} // namespace blender::render::hydra

View File

@ -23,6 +23,7 @@ public:
void update() override;
pxr::VtValue get_data(pxr::TfToken const &key) const override;
pxr::GfMatrix4d transform() override;
bool update_visibility(View3D *view3d) override;
pxr::GfMatrix4d instancer_transform();
pxr::HdPrimvarDescriptorVector instancer_primvar_descriptors(pxr::HdInterpolation interpolation);
@ -37,6 +38,7 @@ public:
pxr::SdfPath instancer_id;
private:
pxr::VtMatrix4dArray calc_instance_transforms() const;
pxr::VtMatrix4dArray transforms;
};