Fix visibility of instancer object #43

Merged
Bogdan Nagirniak merged 16 commits from BLEN-405_1 into hydra-render 2023-05-24 13:44:35 +02:00
4 changed files with 48 additions and 28 deletions
Showing only changes of commit 465ef3022b - Show all commits

View File

@ -300,6 +300,7 @@ void BlenderSceneDelegate::update_objects(Object *object)
if (!ObjectData::is_supported(object)) { if (!ObjectData::is_supported(object)) {
return; return;
} }
pxr::SdfPath id = object_prim_id(object); pxr::SdfPath id = object_prim_id(object);
ObjectData *obj_data = object_data(id); ObjectData *obj_data = object_data(id);
if (obj_data) { if (obj_data) {
@ -308,12 +309,17 @@ void BlenderSceneDelegate::update_objects(Object *object)
obj_data->update_visibility(); obj_data->update_visibility();
return; return;
} }
if (!ObjectData::is_visible(this, object)) {
/* Do not export new object if it is invisible */
return;
}
objects_[id] = ObjectData::create(this, object, id); objects_[id] = ObjectData::create(this, object, id);
obj_data = object_data(id); obj_data = object_data(id);
obj_data->update_parent(); obj_data->update_parent();
obj_data->init(); obj_data->init();
obj_data->insert(); obj_data->insert();
obj_data->update_visibility();
} }
void BlenderSceneDelegate::update_instancers(Object *object) void BlenderSceneDelegate::update_instancers(Object *object)
@ -326,18 +332,26 @@ void BlenderSceneDelegate::update_instancers(Object *object)
pxr::SdfPath id = instancer_prim_id(object); pxr::SdfPath id = instancer_prim_id(object);
InstancerData *i_data = instancer_data(id); InstancerData *i_data = instancer_data(id);
if (i_data) { if (i_data) {
if (object->transflag & OB_DUPLI) { if ((object->transflag & OB_DUPLI) == 0) {
i_data->update(); /* Object isn't instancer anymore and should be removed */
}
else {
i_data->remove(); i_data->remove();
instancers_.erase(id); instancers_.erase(id);
}
return; return;
} }
i_data->update();
return;
}
if ((object->transflag & OB_DUPLI) == 0) { if ((object->transflag & OB_DUPLI) == 0) {
return; return;
} }
if (!InstancerData::is_visible(this, object)) {
/* Do not export new instancer if it is invisible */
return;
}
instancers_[id] = std::make_unique<InstancerData>(this, object, id); instancers_[id] = std::make_unique<InstancerData>(this, object, id);
i_data = instancer_data(id); i_data = instancer_data(id);
i_data->init(); i_data->init();
@ -536,16 +550,18 @@ void BlenderSceneDelegate::remove_unused_objects()
void BlenderSceneDelegate::update_visibility() void BlenderSceneDelegate::update_visibility()
{ {
/* Updating visibility of existing objects/instancers */
for (auto &it : objects_) { for (auto &it : objects_) {
it.second->update_visibility(); it.second->update_visibility();
it.second->update_parent(); }
for (auto &it : instancers_) {
it.second->update_visibility();
} }
/* Add objects which were invisible before and not added yet */ /* Add objects/instancers which were invisible before and not added yet */
DEGObjectIterSettings settings = {0}; DEGObjectIterSettings settings = {0};
settings.depsgraph = depsgraph; settings.depsgraph = depsgraph;
settings.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY | DEG_ITER_OBJECT_FLAG_VISIBLE | settings.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY | DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET;
DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET;
DEGObjectIterData data = {0}; DEGObjectIterData data = {0};
data.settings = &settings; data.settings = &settings;
data.graph = settings.depsgraph; data.graph = settings.depsgraph;

View File

@ -36,6 +36,17 @@ bool InstancerData::is_supported(Object *object)
return false; return false;
} }
bool InstancerData::is_visible(BlenderSceneDelegate *scene_delegate, Object *object)
{
bool ret = true;
if (DEG_get_mode(scene_delegate->depsgraph) == DAG_EVAL_VIEWPORT) {
ret = BKE_object_is_visible_in_viewport(scene_delegate->view3d, object);
}
else {
}
return ret;
}
bool InstancerData::is_visible(BlenderSceneDelegate *scene_delegate, bool InstancerData::is_visible(BlenderSceneDelegate *scene_delegate,
Object *object, Object *object,
Object *child_object) Object *child_object)
@ -101,11 +112,12 @@ pxr::VtValue InstancerData::get_data(pxr::TfToken const &key) const
return ret; return ret;
} }
bool InstancerData::update_visibility(Object *child_object) bool InstancerData::update_visibility()
{ {
bool prev_visible = visible; bool prev_visible = visible;
visible = is_visible(scene_delegate_, (Object *)id, child_object); visible = is_visible(scene_delegate_, (Object *)id);
bool ret = visible != prev_visible; bool ret = visible != prev_visible;
if (ret) { if (ret) {
auto &change_tracker = scene_delegate_->GetRenderIndex().GetChangeTracker(); auto &change_tracker = scene_delegate_->GetRenderIndex().GetChangeTracker();
change_tracker.MarkInstancerDirty(prim_id, pxr::HdChangeTracker::DirtyVisibility); change_tracker.MarkInstancerDirty(prim_id, pxr::HdChangeTracker::DirtyVisibility);

View File

@ -25,6 +25,7 @@ class InstancerData : public ObjectData {
public: public:
InstancerData(BlenderSceneDelegate *scene_delegate, Object *object, pxr::SdfPath const &prim_id); InstancerData(BlenderSceneDelegate *scene_delegate, Object *object, pxr::SdfPath const &prim_id);
static bool is_supported(Object *object); static bool is_supported(Object *object);
static bool is_visible(BlenderSceneDelegate *scene_delegate, Object *object);
static bool is_visible(BlenderSceneDelegate *scene_delegate, static bool is_visible(BlenderSceneDelegate *scene_delegate,
Object *object, Object *object,
Object *child_object); Object *child_object);
@ -35,7 +36,7 @@ class InstancerData : public ObjectData {
void update() override; void update() override;
pxr::VtValue get_data(pxr::TfToken const &key) const override; pxr::VtValue get_data(pxr::TfToken const &key) const override;
bool update_visibility(Object *child_object); bool update_visibility() override;
pxr::GfMatrix4d get_transform(pxr::SdfPath const &id) const; pxr::GfMatrix4d get_transform(pxr::SdfPath const &id) const;
pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation) const; pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation) const;

View File

@ -87,36 +87,27 @@ bool ObjectData::update_visibility()
{ {
bool prev_visible = visible; bool prev_visible = visible;
visible = is_visible(scene_delegate_, (Object *)id); visible = is_visible(scene_delegate_, (Object *)id);
bool ret = visible != prev_visible; return visible != prev_visible;
if (ret) {
ID_LOG(2, "");
}
return ret;
} }
void ObjectData::update_parent() void ObjectData::update_parent()
{ {
bool update_parent = false;
Object *object = (Object *)id; Object *object = (Object *)id;
if (parent_ != object->parent) { if (parent_ != object->parent) {
ID_LOG(2, ""); ID_LOG(2, "");
parent_ = object->parent; parent_ = object->parent;
update_parent = true;
}
/* Looking for corresponded instancer and update it as parent */ /* Looking for corresponded instancer and update it as parent */
for (Object *ob = parent_; ob != nullptr; ob = ob->parent) { for (Object *ob = parent_; ob != nullptr; ob = ob->parent) {
InstancerData *i_data = scene_delegate_->instancer_data( InstancerData *i_data = scene_delegate_->instancer_data(
scene_delegate_->instancer_prim_id(ob)); scene_delegate_->instancer_prim_id(ob));
if (i_data) { if (i_data) {
if (update_parent) {
i_data->update_as_parent(); i_data->update_as_parent();
}
i_data->update_visibility((Object *)id);
break; break;
} }
} }
} }
}
void ObjectData::write_transform() void ObjectData::write_transform()
{ {