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
4 changed files with 352 additions and 439 deletions
Showing only changes of commit 26fe283bed - Show all commits

View File

@ -20,24 +20,6 @@ BlenderSceneDelegate::BlenderSceneDelegate(pxr::HdRenderIndex *parent_index,
{
}
void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont)
{
bool is_populated = depsgraph_ != nullptr;
depsgraph_ = deps;
context_ = cont;
scene_ = DEG_get_input_scene(depsgraph_);
view3d_ = CTX_wm_view3d(context_);
if (is_populated) {
check_updates();
}
else {
add_new_objects();
update_world();
}
}
pxr::HdMeshTopology BlenderSceneDelegate::GetMeshTopology(pxr::SdfPath const &id)
{
CLOG_INFO(LOG_BSD, 3, "%s", id.GetText());
@ -45,6 +27,21 @@ pxr::HdMeshTopology BlenderSceneDelegate::GetMeshTopology(pxr::SdfPath const &id
return m_data->mesh_topology();
}
pxr::GfMatrix4d BlenderSceneDelegate::GetTransform(pxr::SdfPath const &id)
{
CLOG_INFO(LOG_BSD, 3, "%s", id.GetText());
ObjectData *obj_data = object_data(id);
if (obj_data) {
return obj_data->transform();
}
if (id == WorldData::prim_id(this)) {
return world_data_->transform();
}
return pxr::GfMatrix4d();
}
pxr::VtValue BlenderSceneDelegate::Get(pxr::SdfPath const &id, pxr::TfToken const &key)
{
CLOG_INFO(LOG_BSD, 3, "%s, %s", id.GetText(), key.GetText());
@ -67,6 +64,19 @@ pxr::VtValue BlenderSceneDelegate::Get(pxr::SdfPath const &id, pxr::TfToken cons
return pxr::VtValue();
}
pxr::VtValue BlenderSceneDelegate::GetLightParamValue(pxr::SdfPath const &id,
pxr::TfToken const &key)
{
LightData *l_data = light_data(id);
if (l_data) {
return l_data->get_data(key);
}
if (id == WorldData::prim_id(this)) {
return world_data_->get_data(key);
}
return pxr::VtValue();
}
pxr::HdPrimvarDescriptorVector BlenderSceneDelegate::GetPrimvarDescriptors(
pxr::SdfPath const &id, pxr::HdInterpolation interpolation)
{
@ -94,55 +104,194 @@ pxr::VtValue BlenderSceneDelegate::GetMaterialResource(pxr::SdfPath const &id)
{
MaterialData *mat_data = material_data(id);
if (mat_data) {
return mat_data->material_resource();
return mat_data->get_material_resource();
}
return pxr::VtValue();
}
pxr::GfMatrix4d BlenderSceneDelegate::GetTransform(pxr::SdfPath const &id)
bool BlenderSceneDelegate::GetVisible(pxr::SdfPath const &id)
{
CLOG_INFO(LOG_BSD, 3, "%s", id.GetText());
if (id == WorldData::prim_id(this)) {
return true;
}
return object_data(id)->visible;
}
pxr::SdfPath BlenderSceneDelegate::GetInstancerId(pxr::SdfPath const &prim_id)
{
CLOG_INFO(LOG_BSD, 3, "%s", prim_id.GetText());
InstancerData *i_data = instancer_data(prim_id.GetParentPath());
if (i_data) {
return i_data->p_id_;
}
return pxr::SdfPath();
}
pxr::SdfPathVector BlenderSceneDelegate::GetInstancerPrototypes(pxr::SdfPath const &instancer_id)
{
CLOG_INFO(LOG_BSD, 3, "%s", instancer_id.GetText());
InstancerData *i_data = instancer_data(instancer_id);
return i_data->prototypes();
}
pxr::VtIntArray BlenderSceneDelegate::GetInstanceIndices(pxr::SdfPath const &instancer_id,
pxr::SdfPath const &prototype_id)
{
CLOG_INFO(LOG_BSD, 3, "%s, %s", instancer_id.GetText(), prototype_id.GetText());
InstancerData *i_data = instancer_data(instancer_id);
return i_data->indices(prototype_id);
}
pxr::GfMatrix4d BlenderSceneDelegate::GetInstancerTransform(pxr::SdfPath const &instancer_id)
{
CLOG_INFO(LOG_BSD, 3, "%s", instancer_id.GetText());
InstancerData *i_data = instancer_data(instancer_id);
return i_data->transform();
}
void BlenderSceneDelegate::populate(Depsgraph * deps, bContext * cont)
{
bool is_populated = depsgraph_ != nullptr;
depsgraph_ = deps;
context_ = cont;
scene_ = DEG_get_input_scene(depsgraph_);
view3d_ = CTX_wm_view3d(context_);
if (is_populated) {
check_updates();
}
else {
add_new_objects();
update_world();
}
}
void BlenderSceneDelegate::clear()
{
for (auto &it : objects_) {
it.second->remove();
}
for (auto &it : instancers_) {
it.second->remove();
}
for (auto &it : materials_) {
it.second->remove();
}
objects_.clear();
instancers_.clear();
materials_.clear();
}
ObjectData *BlenderSceneDelegate::object_data(pxr::SdfPath const &id)
{
auto it = objects_.find(id);
if (it != objects_.end()) {
return it->second.get();
}
InstancerData *i_data = instancer_data(id.GetParentPath());
if (i_data) {
return i_data->object_data(id);
}
return nullptr;
}
MeshData *BlenderSceneDelegate::mesh_data(pxr::SdfPath const &id)
{
return dynamic_cast<MeshData *>(object_data(id));
}
LightData *BlenderSceneDelegate::light_data(pxr::SdfPath const &id)
{
return dynamic_cast<LightData *>(object_data(id));
}
MaterialData *BlenderSceneDelegate::material_data(pxr::SdfPath const &id)
{
auto it = materials_.find(id);
if (it == materials_.end()) {
return nullptr;
}
return it->second.get();
}
InstancerData *BlenderSceneDelegate::instancer_data(pxr::SdfPath const &id)
{
auto it = instancers_.find(id);
if (it != instancers_.end()) {
return it->second.get();
}
return nullptr;
}
void BlenderSceneDelegate::update_objects(Object *object)
{
if (!ObjectData::is_supported(object)) {
return;
}
pxr::SdfPath id = ObjectData::prim_id(this, object);
ObjectData *obj_data = object_data(id);
if (obj_data) {
return obj_data->transform();
obj_data->update();
return;
}
if (view3d_ && !BKE_object_is_visible_in_viewport(view3d_, object)) {
return;
}
objects_[id] = ObjectData::create(this, object);
obj_data = object_data(id);
obj_data->update_visibility(view3d_);
}
if (id == WorldData::prim_id(this)) {
return world_data->transform();
}
return pxr::GfMatrix4d();
}
pxr::VtValue BlenderSceneDelegate::GetLightParamValue(pxr::SdfPath const &id,
pxr::TfToken const &key)
void BlenderSceneDelegate::update_instancers(Object *object)
{
LightData *l_data = light_data(id);
if (l_data) {
return l_data->get_data(key);
}
if (id == WorldData::prim_id(this)) {
return world_data->get_data(key);
}
return pxr::VtValue();
/* Check object inside instancers */
for (auto &it : instancers_) {
it.second->check_update(object);
}
pxr::SdfPath id = InstancerData::prim_id(this, object);
InstancerData *i_data = instancer_data(id);
if (i_data) {
if (object->transflag & OB_DUPLI) {
i_data->update();
}
else {
i_data->remove();
instancers_.erase(id);
}
return;
}
if ((object->transflag & OB_DUPLI) == 0) {
return;
}
if (view3d_ && !BKE_object_is_visible_in_viewport(view3d_, object)) {
return;
}
instancers_[id] = InstancerData::create(this, object);
i_data = instancer_data(id);
i_data->update_visibility(view3d_);
}
void BlenderSceneDelegate::update_world()
{
World *world = scene->world;
if (!world_data) {
World *world = scene_->world;
if (!world_data_) {
if (world) {
world_data = WorldData::create(this, world, context);
world_data_ = WorldData::create(this, world, context_);
}
}
else {
if (world) {
world_data->update(world);
world_data_->update(world);
}
else {
world_data->remove();
world_data = nullptr;
world_data_->remove();
world_data_ = nullptr;
}
}
}
@ -154,7 +303,7 @@ void BlenderSceneDelegate::check_updates()
bool do_update_visibility = false;
bool do_update_world = false;
unsigned int scene_recalc = scene_->id->recalc;
unsigned int scene_recalc = scene_->id.recalc;
if (scene_recalc) {
/* Checking scene updates */
CLOG_INFO(LOG_BSD,
@ -223,308 +372,8 @@ void BlenderSceneDelegate::check_updates()
}
}
void BlenderSceneDelegate::clear()
void BlenderSceneDelegate::add_new_objects()
{
for (auto &it : materials_) {
it.second->remove();
}
for (auto &it : objects_) {
it.second->remove();
}
materials_.clear();
objects_.clear();
}
pxr::HdMeshTopology BlenderSceneDelegate::GetMeshTopology(pxr::SdfPath const &id)
{
DEGObjectIterSettings settings = {0};
settings.depsgraph = depsgraph;
settings.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY | DEG_ITER_OBJECT_FLAG_VISIBLE |
DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET;
DEGObjectIterData data = {0};
data.settings = &settings;
data.graph = settings.depsgraph;
data.flag = settings.flags;
ITER_BEGIN (DEG_iterator_objects_begin,
DEG_iterator_objects_next,
DEG_iterator_objects_end,
&data,
Object *,
object) {
pxr::GfMatrix4d BlenderSceneDelegate::GetTransform(pxr::SdfPath const &id)
{
CLOG_INFO(LOG_BSD, 3, "%s", id.GetText());
ObjectData *obj_data = object_data(id);
if (obj_data) {
return obj_data->transform();
}
if (id == WorldData::prim_id(this)) {
return world_data_->transform();
}
InstancerData *i_data = instancer_data(id);
if (i_data) {
return i_data->transform();
}
return pxr::GfMatrix4d();
}
pxr::VtValue BlenderSceneDelegate::Get(pxr::SdfPath const &id, pxr::TfToken const &key)
{
CLOG_INFO(LOG_BSD, 3, "%s, %s", id.GetText(), key.GetText());
update_objects(object);
update_instancers(object);
}
ITER_END;
}
pxr::VtValue BlenderSceneDelegate::GetLightParamValue(pxr::SdfPath const &id,
pxr::TfToken const &key)
{
LightData *l_data = light_data(id);
if (l_data) {
return l_data->get_data(key);
}
if (id == WorldData::prim_id(this)) {
return world_data_->get_data(key);
}
return pxr::VtValue();
}
pxr::HdPrimvarDescriptorVector BlenderSceneDelegate::GetPrimvarDescriptors(
pxr::SdfPath const &id, pxr::HdInterpolation interpolation)
{
/* Get available objects */
std::set<std::string> available_objects;
DEGObjectIterSettings settings = {0};
settings.depsgraph = depsgraph;
settings.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY | DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET;
DEGObjectIterData data = {0};
data.settings = &settings;
data.graph = settings.depsgraph;
data.flag = settings.flags;
ITER_BEGIN (DEG_iterator_objects_begin,
DEG_iterator_objects_next,
DEG_iterator_objects_end,
&data,
Object *,
object) {
if (!ObjectData::supported(object)) {
continue;
}
available_objects.insert(ObjectData::prim_id(this, object).GetName());
available_objects.insert(InstancerData::prim_id(this, object).GetName());
}
ITER_END;
/* Remove unused instancers */
for (auto it = instancers.begin(); it != instancers.end(); ++it) {
if (available_objects.find(it->first.GetName()) != available_objects.end()) {
/* Remove objects from instancers */
it->second->check_remove(available_objects);
continue;
}
it->second->remove();
instancers.erase(it);
it = instancers.begin();
}
/* Remove unused objects */
for (auto it = objects.begin(); it != objects.end(); ++it) {
if (available_objects.find(it->first.GetName()) != available_objects.end()) {
continue;
}
it->second->remove();
objects.erase(it);
it = objects.begin();
}
pxr::VtValue BlenderSceneDelegate::GetMaterialResource(pxr::SdfPath const &id)
{
MaterialData *mat_data = material_data(id);
if (mat_data) {
return mat_data->get_material_resource();
}
for (auto &it : instancers) {
it.second->available_materials(available_materials);
}
for (auto it = materials.begin(); it != materials.end(); ++it) {
if (available_materials.find(it->first) != available_materials.end()) {
continue;
}
it->second->remove();
materials.erase(it);
it = materials.begin();
}
}
bool BlenderSceneDelegate::GetVisible(pxr::SdfPath const &id)
{
if (id == WorldData::prim_id(this)) {
return true;
}
return object_data(id)->visible;
}
pxr::SdfPath BlenderSceneDelegate::GetInstancerId(pxr::SdfPath const &prim_id)
{
CLOG_INFO(LOG_BSD, 3, "%s", prim_id.GetText());
InstancerData *i_data = instancer_data(prim_id, true);
if (i_data) {
return i_data->instancer_id;
}
return pxr::SdfPath();
}
pxr::SdfPathVector BlenderSceneDelegate::GetInstancerPrototypes(pxr::SdfPath const &instancer_id)
{
CLOG_INFO(LOG_BSD, 3, "%s", instancer_id.GetText());
pxr::SdfPathVector paths;
paths.push_back(instancer_id.GetParentPath());
return paths;
}
pxr::VtIntArray BlenderSceneDelegate::GetInstanceIndices(pxr::SdfPath const &instancer_id,
pxr::SdfPath const &prototype_id)
{
CLOG_INFO(LOG_BSD, 3, "%s, %s", instancer_id.GetText(), prototype_id.GetText());
InstancerData *i_data = instancer_data(instancer_id);
return i_data->instance_indices();
}
pxr::GfMatrix4d BlenderSceneDelegate::GetInstancerTransform(pxr::SdfPath const &instancer_id)
{
CLOG_INFO(LOG_BSD, 3, "%s", instancer_id.GetText());
InstancerData *i_data = instancer_data(instancer_id);
return i_data->transform();
}
ObjectData *BlenderSceneDelegate::object_data(pxr::SdfPath const &id)
{
auto it = objects_.find(id);
if (it == objects_.end()) {
return nullptr;
}
return it->second.get();
}
MeshData *BlenderSceneDelegate::mesh_data(pxr::SdfPath const &id)
{
return dynamic_cast<MeshData *>(object_data(id));
}
LightData *BlenderSceneDelegate::light_data(pxr::SdfPath const &id)
{
return dynamic_cast<LightData *>(object_data(id));
}
MaterialData *BlenderSceneDelegate::material_data(pxr::SdfPath const &id)
{
auto it = materials_.find(id);
if (it == materials_.end()) {
return nullptr;
}
return it->second.get();
}
InstancerData *BlenderSceneDelegate::instancer_data(pxr::SdfPath const &id, bool base_prim)
{
if (base_prim) {
return dynamic_cast<InstancerData *>(object_data(id));
}
return dynamic_cast<InstancerData *>(object_data(id.GetParentPath()));
}
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;
}
}
return nullptr;
}
void BlenderSceneDelegate::add_update_object(Object *object)
{
if ((object->transflag & OB_DUPLI) && InstancerData::is_supported(object)) {
add_update_instancer(object);
}
InstancerData *i_data = instancer_data(object);
if (i_data) {
i_data->update();
}
pxr::SdfPath id = ObjectData::prim_id(this, object);
ObjectData *obj_data = object_data(id);
if (obj_data) {
obj_data->update();
return;
}
if (view3d_ && !BKE_object_is_visible_in_viewport(view3d_, object)) {
return;
}
objects_[id] = ObjectData::create(this, object);
obj_data = object_data(id);
obj_data->update_visibility(view3d_);
}
void BlenderSceneDelegate::add_update_instancer(Object *object)
{
pxr::SdfPath id = InstancerData::prim_id(this, object);
InstancerData *i_data = instancer_data(id, true);
if (i_data) {
i_data->update();
return;
}
objects_[id] = InstancerData::create(this, object);
i_data = instancer_data(id, true);
i_data->update_visibility(view3d_);
}
void BlenderSceneDelegate::update_world()
{
World *world = scene_->world;
if (!world_data_) {
if (world) {
world_data_ = WorldData::create(this, world, context_);
}
}
else {
if (world) {
world_data_->update(world);
}
else {
world_data_->remove();
world_data_ = nullptr;
}
}
}
void BlenderSceneDelegate::update_collection(bool remove, bool visibility)
{
if (visibility) {
/* Check and update visibility */
for (auto &obj : objects_) {
obj.second->update_visibility(view3d_);
}
}
/* Export of new visible objects which were not exported before */
std::set<pxr::SdfPath> available_objects;
pxr::SdfPath id;
DEGObjectIterSettings settings = {0};
settings.depsgraph = depsgraph_;
settings.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY | DEG_ITER_OBJECT_FLAG_VISIBLE |
@ -540,29 +389,57 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility)
Object *,
object) {
CLOG_INFO(LOG_BSD, 2, "Add %s", ((ID *)object)->name);
if (!ObjectData::is_supported(object)) {
continue;
}
id = ObjectData::prim_id(this, object);
if (remove) {
available_objects.insert(id);
if ((object->transflag & OB_DUPLI) && InstancerData::is_supported(object)) {
available_objects.insert(InstancerData::prim_id(this, object));
update_objects(object);
update_instancers(object);
}
ITER_END;
}
if (!object_data(id)) {
add_update_object(object);
void BlenderSceneDelegate::remove_unused_objects()
{
/* Get available objects */
std::set<std::string> available_objects;
DEGObjectIterSettings settings = {0};
settings.depsgraph = depsgraph_;
settings.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY | DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET;
DEGObjectIterData data = {0};
data.settings = &settings;
data.graph = settings.depsgraph;
data.flag = settings.flags;
ITER_BEGIN (DEG_iterator_objects_begin,
DEG_iterator_objects_next,
DEG_iterator_objects_end,
&data,
Object *,
object) {
if (!ObjectData::is_supported(object)) {
continue;
}
available_objects.insert(ObjectData::prim_id(this, object).GetName());
available_objects.insert(InstancerData::prim_id(this, object).GetName());
}
ITER_END;
if (remove) {
/* remove unused objects */
/* Remove unused instancers */
for (auto it = instancers_.begin(); it != instancers_.end(); ++it) {
if (available_objects.find(it->first.GetName()) != available_objects.end()) {
/* Remove objects from instancers */
it->second->check_remove(available_objects);
continue;
}
it->second->remove();
instancers_.erase(it);
it = instancers_.begin();
}
/* Remove unused objects */
for (auto it = objects_.begin(); it != objects_.end(); ++it) {
if (available_objects.find(it->first) != available_objects.end()) {
if (available_objects.find(it->first.GetName()) != available_objects.end()) {
continue;
}
it->second->remove();
@ -570,10 +447,10 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility)
it = objects_.begin();
}
/* remove unused materials */
/* Remove unused materials */
std::set<pxr::SdfPath> available_materials;
for (auto &obj : objects_) {
MeshData *m_data = dynamic_cast<MeshData *>(obj.second.get());
for (auto &it : objects_) {
MeshData *m_data = dynamic_cast<MeshData *>(it.second.get());
if (!m_data) {
continue;
}
@ -582,6 +459,9 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility)
available_materials.insert(mat_id);
}
}
for (auto &it : instancers_) {
it.second->available_materials(available_materials);
}
for (auto it = materials_.begin(); it != materials_.end(); ++it) {
if (available_materials.find(it->first) != available_materials.end()) {
continue;
@ -591,6 +471,44 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility)
it = materials_.begin();
}
}
void BlenderSceneDelegate::update_visibility()
{
for (auto &it : objects_) {
it.second->update_visibility(view3d_);
}
for (auto &it : instancers_) {
it.second->update_visibility(view3d_);
}
/* Add objects which were invisible before and not added yet */
DEGObjectIterSettings settings = {0};
settings.depsgraph = depsgraph_;
settings.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY | DEG_ITER_OBJECT_FLAG_VISIBLE |
DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET;
DEGObjectIterData data = {0};
data.settings = &settings;
data.graph = settings.depsgraph;
data.flag = settings.flags;
ITER_BEGIN (DEG_iterator_objects_begin,
DEG_iterator_objects_next,
DEG_iterator_objects_end,
&data,
Object *,
object) {
if (!ObjectData::is_supported(object)) {
continue;
}
if (!object_data(ObjectData::prim_id(this, object))) {
update_objects(object);
}
if (!instancer_data(InstancerData::prim_id(this, object))) {
update_instancers(object);
}
}
ITER_END;
}
} // namespace blender::render::hydra

View File

@ -12,8 +12,11 @@
namespace blender::render::hydra {
class BlenderSceneDelegate;
class InstancerData;
class IdData {
friend InstancerData;
public:
IdData(BlenderSceneDelegate *scene_delegate, ID *id);
virtual ~IdData() = default;

View File

@ -10,12 +10,10 @@
namespace blender::render::hydra {
InstancerData::InstancerData(BlenderSceneDelegate *scene_delegate, Object *object)
: MeshData(scene_delegate, object), parent_obj_(object)
: ObjectData(scene_delegate, object)
{
id_ = nullptr;
p_id_ = prim_id(scene_delegate, object);
instancer_id = p_id_.AppendElementString("Instancer");
CLOG_INFO(LOG_BSD, 2, "%s, instancer_id=%s", ((ID *)parent_obj_)->name, instancer_id.GetText());
ID_LOG(2, "");
}
bool InstancerData::is_supported(Object *object)
@ -52,13 +50,6 @@ pxr::SdfPath InstancerData::prim_id(BlenderSceneDelegate *scene_delegate, Object
return scene_delegate->GetDelegateID().AppendElementString(str);
}
InstancerData::InstancerData(BlenderSceneDelegate *scene_delegate, Object *object)
: ObjectData(scene_delegate, object)
{
p_id = prim_id(scene_delegate, object);
ID_LOG(2, "");
}
void InstancerData::init()
{
ID_LOG(2, "");
@ -68,19 +59,19 @@ void InstancerData::init()
void InstancerData::insert()
{
ID_LOG(2, "");
scene_delegate->GetRenderIndex().InsertInstancer(scene_delegate, p_id);
for (auto &it : instances) {
scene_delegate_->GetRenderIndex().InsertInstancer(scene_delegate_, p_id_);
for (auto &it : instances_) {
it.second.obj_data->insert();
}
}
void InstancerData::remove()
{
CLOG_INFO(LOG_BSD, 2, "%s", p_id.GetText());
for (auto &it : instances) {
CLOG_INFO(LOG_BSD, 2, "%s", p_id_.GetText());
for (auto &it : instances_) {
it.second.obj_data->remove();
}
scene_delegate->GetRenderIndex().RemoveInstancer(p_id);
scene_delegate_->GetRenderIndex().RemoveInstancer(p_id_);
}
void InstancerData::update()
@ -89,17 +80,17 @@ void InstancerData::update()
pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean;
Object *object = (Object *)id;
if ((id->recalc & ID_RECALC_GEOMETRY) || (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) {
Object *object = (Object *)id_;
if ((id_->recalc & ID_RECALC_GEOMETRY) || (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) {
set_instances();
bits |= pxr::HdChangeTracker::AllDirty;
}
else if (id->recalc & ID_RECALC_TRANSFORM) {
else if (id_->recalc & ID_RECALC_TRANSFORM) {
set_instances();
bits |= pxr::HdChangeTracker::DirtyTransform;
}
if (bits != pxr::HdChangeTracker::Clean) {
scene_delegate->GetRenderIndex().GetChangeTracker().MarkInstancerDirty(p_id, bits);
scene_delegate_->GetRenderIndex().GetChangeTracker().MarkInstancerDirty(p_id_, bits);
}
}
@ -108,7 +99,7 @@ pxr::VtValue InstancerData::get_data(pxr::TfToken const &key) const
ID_LOG(3, "%s", key.GetText());
pxr::VtValue ret;
if (key == pxr::HdInstancerTokens->instanceTransform) {
ret = transforms;
ret = transforms_;
}
return ret;
}
@ -126,12 +117,12 @@ bool InstancerData::update_visibility(View3D *view3d)
bool ret = ObjectData::update_visibility(view3d);
if (ret) {
scene_delegate->GetRenderIndex().GetChangeTracker().MarkInstancerDirty(
p_id, pxr::HdChangeTracker::DirtyVisibility);
for (auto &it : instances) {
scene_delegate_->GetRenderIndex().GetChangeTracker().MarkInstancerDirty(
p_id_, pxr::HdChangeTracker::DirtyVisibility);
for (auto &it : instances_) {
it.second.obj_data->visible = visible;
scene_delegate->GetRenderIndex().GetChangeTracker().MarkRprimDirty(
it.second.obj_data->p_id, pxr::HdChangeTracker::DirtyVisibility);
scene_delegate_->GetRenderIndex().GetChangeTracker().MarkRprimDirty(
it.second.obj_data->p_id_, pxr::HdChangeTracker::DirtyVisibility);
}
}
return ret;
@ -149,18 +140,18 @@ pxr::HdPrimvarDescriptorVector InstancerData::primvar_descriptors(pxr::HdInterpo
pxr::VtIntArray InstancerData::indices(pxr::SdfPath const &id)
{
return instances[id].indices;
return instances_[id].indices;
}
ObjectData *InstancerData::object_data(pxr::SdfPath const &id)
{
return instances[id].obj_data.get();
return instances_[id].obj_data.get();
}
pxr::SdfPathVector InstancerData::prototypes()
{
pxr::SdfPathVector paths;
for (auto &it : instances) {
for (auto &it : instances_) {
paths.push_back(it.first);
}
return paths;
@ -168,10 +159,10 @@ pxr::SdfPathVector InstancerData::prototypes()
void InstancerData::check_update(Object *object)
{
pxr::SdfPath path = ObjectData::prim_id(scene_delegate, object);
path = p_id.AppendElementString(path.GetName());
auto it = instances.find(path);
if (it == instances.end()) {
pxr::SdfPath path = ObjectData::prim_id(scene_delegate_, object);
path = p_id_.AppendElementString(path.GetName());
auto it = instances_.find(path);
if (it == instances_.end()) {
return;
}
ObjectData *obj_data = it->second.obj_data.get();
@ -183,32 +174,32 @@ void InstancerData::check_update(Object *object)
bits |= pxr::HdChangeTracker::DirtyTransform;
}
if (bits != pxr::HdChangeTracker::Clean) {
scene_delegate->GetRenderIndex().GetChangeTracker().MarkInstancerDirty(p_id, bits);
scene_delegate_->GetRenderIndex().GetChangeTracker().MarkInstancerDirty(p_id_, bits);
}
}
void InstancerData::check_remove(std::set<std::string> &available_objects)
{
bool ret = false;
for (auto it = instances.begin(); it != instances.end(); ++it) {
for (auto it = instances_.begin(); it != instances_.end(); ++it) {
if (available_objects.find(it->first.GetName()) != available_objects.end()) {
continue;
}
it->second.obj_data->remove();
instances.erase(it);
it = instances.begin();
instances_.erase(it);
it = instances_.begin();
ret = true;
}
if (ret) {
set_instances();
scene_delegate->GetRenderIndex().GetChangeTracker().MarkInstancerDirty(p_id,
scene_delegate_->GetRenderIndex().GetChangeTracker().MarkInstancerDirty(p_id_,
pxr::HdChangeTracker::AllDirty);
}
}
void InstancerData::available_materials(std::set<pxr::SdfPath> &paths)
{
for (auto &it : instances) {
for (auto &it : instances_) {
pxr::SdfPath mat_id = ((MeshData *)it.second.obj_data.get())->material_id();
if (!mat_id.IsEmpty()) {
paths.insert(mat_id);
@ -218,25 +209,25 @@ void InstancerData::available_materials(std::set<pxr::SdfPath> &paths)
void InstancerData::set_instances()
{
transforms.clear();
for (auto &it : 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 *lb = object_duplilist(scene_delegate_->depsgraph_, scene_delegate_->scene_, (Object *)id_);
LISTBASE_FOREACH (DupliObject *, dupli, lb) {
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];
if (!supported(dupli->ob)) {
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];
if (!is_supported(dupli->ob)) {
continue;
}
inst->obj_data = std::make_unique<InstanceMeshData>(scene_delegate, dupli->ob, path);
inst->obj_data = std::make_unique<InstanceMeshData>(scene_delegate_, dupli->ob, path);
inst->obj_data->init();
inst->obj_data->insert();
}
@ -245,19 +236,19 @@ void InstancerData::set_instances()
}
transforms_.push_back(gf_matrix_from_transform(dupli->mat));
inst->indices.push_back(index);
ID_LOG(2, "Instance %s %d", inst->obj_data->id->name, index);
ID_LOG(2, "Instance %s %d", inst->obj_data->id_->name, index);
++index;
}
free_object_duplilist(lb);
/* Remove intances without indices */
for (auto it = instances.begin(); it != instances.end(); ++it) {
for (auto it = instances_.begin(); it != instances_.end(); ++it) {
if (!it->second.indices.empty()) {
continue;
}
it->second.obj_data->remove();
instances.erase(it);
it = instances.begin();
instances_.erase(it);
it = instances_.begin();
}
}
@ -266,7 +257,7 @@ InstanceMeshData::InstanceMeshData(BlenderSceneDelegate *scene_delegate,
pxr::SdfPath const &p_id)
: MeshData(scene_delegate, object)
{
this->p_id = p_id;
this->p_id_ = p_id;
}
pxr::GfMatrix4d InstanceMeshData::transform()

View File

@ -10,6 +10,8 @@
namespace blender::render::hydra {
class InstancerData : public ObjectData {
friend BlenderSceneDelegate;
struct Instance {
std::unique_ptr<ObjectData> obj_data;
pxr::VtIntArray indices;
@ -39,13 +41,12 @@ class InstancerData : public ObjectData {
void check_update(Object *object);
void check_remove(std::set<std::string> &available_objects);
void available_materials(std::set<pxr::SdfPath> &paths);
bool is_base(Object *object) const;
private:
void set_instances();
pxr::TfHashMap<pxr::SdfPath, Instance, pxr::SdfPath::Hash> instances;
pxr::VtMatrix4dArray transforms;
pxr::TfHashMap<pxr::SdfPath, Instance, pxr::SdfPath::Hash> instances_;
pxr::VtMatrix4dArray transforms_;
};
using InstancerDataMap =