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
5 changed files with 72 additions and 26 deletions
Showing only changes of commit 058eacc7a5 - Show all commits

View File

@ -301,9 +301,7 @@ void BlenderSceneDelegate::update_objects(Object *object)
if (obj_data) { if (obj_data) {
obj_data->update_parent(); obj_data->update_parent();
obj_data->update(); obj_data->update();
return; obj_data->update_visibility();
}
if (view3d && !BKE_object_is_visible_in_viewport(view3d, object)) {
return; return;
} }
objects_[id] = ObjectData::create(this, object, id); objects_[id] = ObjectData::create(this, object, id);
@ -336,14 +334,10 @@ void BlenderSceneDelegate::update_instancers(Object *object)
if ((object->transflag & OB_DUPLI) == 0) { if ((object->transflag & OB_DUPLI) == 0) {
return; return;
} }
if (view3d && !BKE_object_is_visible_in_viewport(view3d, object)) {
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();
i_data->insert(); i_data->insert();
i_data->update_visibility();
} }
void BlenderSceneDelegate::update_world() void BlenderSceneDelegate::update_world()
@ -388,8 +382,8 @@ void BlenderSceneDelegate::check_updates()
switch (GS(id->name)) { switch (GS(id->name)) {
case ID_OB: { case ID_OB: {
Object *object = (Object *)id; Object *object = (Object *)id;
update_objects(object);
update_instancers(object); update_instancers(object);
update_objects(object);
} break; } break;
case ID_MA: { case ID_MA: {
@ -406,6 +400,10 @@ void BlenderSceneDelegate::check_updates()
} break; } break;
case ID_SCE: { case ID_SCE: {
if (id->recalc & ID_RECALC_COPY_ON_WRITE && !(id->recalc & ID_RECALC_SELECT)) {
do_update_collection = true;
do_update_visibility = true;
}
if (id->recalc & ID_RECALC_BASE_FLAGS) { if (id->recalc & ID_RECALC_BASE_FLAGS) {
do_update_visibility = true; do_update_visibility = true;
} }
@ -440,7 +438,7 @@ void BlenderSceneDelegate::add_new_objects()
{ {
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;
@ -535,9 +533,7 @@ void BlenderSceneDelegate::update_visibility()
{ {
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 which were invisible before and not added yet */

View File

@ -4,6 +4,9 @@
#include <pxr/base/gf/vec2f.h> #include <pxr/base/gf/vec2f.h>
#include <pxr/imaging/hd/light.h> #include <pxr/imaging/hd/light.h>
#include "BKE_object.h"
#include "DEG_depsgraph_query.h"
#include "blender_scene_delegate.h" #include "blender_scene_delegate.h"
#include "instancer.h" #include "instancer.h"
@ -33,6 +36,23 @@ bool InstancerData::is_supported(Object *object)
return false; return false;
} }
bool InstancerData::is_visible(BlenderSceneDelegate *scene_delegate,
Object *object,
Object *child_object)
{
bool ret = true;
eEvaluationMode deg_mode = DEG_get_mode(scene_delegate->depsgraph);
ret = BKE_object_visibility(object, deg_mode) & (OB_VISIBLE_SELF | OB_VISIBLE_INSTANCES);
if (ret && child_object && child_object->transflag & OB_DUPLI)
{
int flag = deg_mode == DAG_EVAL_VIEWPORT ? OB_DUPLI_FLAG_VIEWPORT : OB_DUPLI_FLAG_RENDER;
ret = child_object->duplicator_visibility_flag & flag;
}
return ret;
}
void InstancerData::init() void InstancerData::init()
{ {
ID_LOG(2, ""); ID_LOG(2, "");
@ -83,9 +103,12 @@ pxr::VtValue InstancerData::get_data(pxr::TfToken const &key) const
return ret; return ret;
} }
bool InstancerData::update_visibility() bool InstancerData::update_visibility(Object *child_object)
{ {
bool ret = ObjectData::update_visibility(); bool prev_visible = visible;
visible = is_visible(scene_delegate_, (Object *)id, child_object);
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,9 @@ 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,
Object *child_object);
void init() override; void init() override;
void insert() override; void insert() override;
@ -32,7 +35,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() override; bool update_visibility(Object *child_object);
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

@ -2,6 +2,7 @@
* Copyright 2011-2022 Blender Foundation */ * Copyright 2011-2022 Blender Foundation */
#include "BKE_object.h" #include "BKE_object.h"
#include "DEG_depsgraph_query.h"
#include "blender_scene_delegate.h" #include "blender_scene_delegate.h"
#include "light.h" #include "light.h"
@ -59,14 +60,31 @@ bool ObjectData::is_supported(Object *object)
return false; return false;
} }
bool ObjectData::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);
if (ret && object->transflag & OB_DUPLI) {
ret = object->duplicator_visibility_flag & OB_DUPLI_FLAG_VIEWPORT;
}
}
/*
else {
if (object->transflag & OB_DUPLI) {
ret = object->duplicator_visibility_flag & OB_DUPLI_FLAG_RENDER;
}
if (object->parent && (object->parent->transflag & OB_DUPLI)) {
ret = false;
}
}*/
return ret;
}
bool ObjectData::update_visibility() bool ObjectData::update_visibility()
{ {
if (!scene_delegate_->view3d) {
return false;
}
bool prev_visible = visible; bool prev_visible = visible;
visible = BKE_object_is_visible_in_viewport(scene_delegate_->view3d, (Object *)id); visible = is_visible(scene_delegate_, (Object *)id);
bool ret = visible != prev_visible; bool ret = visible != prev_visible;
if (ret) { if (ret) {
ID_LOG(2, ""); ID_LOG(2, "");
@ -76,19 +94,24 @@ bool ObjectData::update_visibility()
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();
break;
} }
i_data->update_visibility((Object *)id);
break;
} }
} }
} }

View File

@ -22,6 +22,7 @@ class ObjectData : public IdData {
Object *object, Object *object,
pxr::SdfPath const &prim_id); 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);
virtual bool update_visibility(); virtual bool update_visibility();
void update_parent(); void update_parent();