forked from blender/blender
BLEN-299: Export instances #11
@ -78,6 +78,43 @@ bool BlenderSceneDelegate::GetVisible(SdfPath const &id)
|
||||
return object_data(id)->visible;
|
||||
}
|
||||
|
||||
pxr::GfRange3d BlenderSceneDelegate::GetExtent(pxr::SdfPath const &id)
|
||||
{
|
||||
LOG(INFO) << "GetExtent: " << id.GetString();
|
||||
return pxr::GfRange3d();
|
||||
}
|
||||
|
||||
bool BlenderSceneDelegate::GetDoubleSided(pxr::SdfPath const &id)
|
||||
{
|
||||
LOG(INFO) << "GetDoubleSided: " << id.GetString();
|
||||
return false;
|
||||
}
|
||||
|
||||
pxr::HdCullStyle BlenderSceneDelegate::GetCullStyle(pxr::SdfPath const &id)
|
||||
{
|
||||
LOG(INFO) << "GetCullStyle: " << id.GetString();
|
||||
return pxr::HdCullStyle();
|
||||
}
|
||||
|
||||
pxr::SdfPath BlenderSceneDelegate::GetInstancerId(pxr::SdfPath const &primId)
|
||||
{
|
||||
LOG(INFO) << "GetInstancerId: " << primId.GetString();
|
||||
return pxr::SdfPath();
|
||||
}
|
||||
|
||||
pxr::SdfPathVector BlenderSceneDelegate::GetInstancerPrototypes(pxr::SdfPath const &instancerId)
|
||||
{
|
||||
LOG(INFO) << "GetInstancerPrototypes: " << instancerId.GetString();
|
||||
return pxr::SdfPathVector();
|
||||
}
|
||||
|
||||
pxr::VtIntArray BlenderSceneDelegate::GetInstanceIndices(pxr::SdfPath const &instancerId,
|
||||
pxr::SdfPath const &prototypeId)
|
||||
{
|
||||
LOG(INFO) << "GetInstanceIndices: " << instancerId.GetString() << " " << prototypeId.GetString();
|
||||
return pxr::VtIntArray();
|
||||
}
|
||||
|
||||
void BlenderSceneDelegate::update_collection(bool remove, bool visibility)
|
||||
{
|
||||
if (visibility) {
|
||||
@ -91,6 +128,7 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility)
|
||||
|
||||
/* Export of new visible objects which were not exported before */
|
||||
std::set<SdfPath> available_objects;
|
||||
SdfPath id;
|
||||
|
||||
DEGObjectIterSettings settings = {0};
|
||||
settings.depsgraph = depsgraph;
|
||||
@ -105,15 +143,15 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility)
|
||||
DEG_iterator_objects_end,
|
||||
&data, Object *, object) {
|
||||
if (data.dupli_object_current != nullptr) {
|
||||
InstanceData i_data(this, data.dupli_object_current);
|
||||
LOG(INFO) << "Instance: " << i_data.name() << " " << i_data.random_id();
|
||||
add_update_instance(data.dupli_object_current, false);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!ObjectData::supported(object)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
SdfPath id = ObjectData::prim_id(this, object);
|
||||
id = ObjectData::prim_id(this, object);
|
||||
if (remove) {
|
||||
available_objects.insert(id);
|
||||
}
|
||||
@ -191,6 +229,22 @@ void BlenderSceneDelegate::add_update_object(Object *object, bool geometry, bool
|
||||
}
|
||||
}
|
||||
|
||||
void BlenderSceneDelegate::add_update_instance(DupliObject *dupli, bool transform)
|
||||
{
|
||||
SdfPath id = InstanceData::prim_id(this, dupli);
|
||||
InstanceData *i_data = instance_data(id);
|
||||
if (!i_data) {
|
||||
instances[id] = InstanceData::init(this, dupli);
|
||||
i_data = instance_data(id);
|
||||
i_data->insert_prim();
|
||||
return;
|
||||
}
|
||||
|
||||
if (transform) {
|
||||
i_data->mark_prim_dirty(IdData::DirtyBits::DirtyTransform);
|
||||
}
|
||||
}
|
||||
|
||||
ObjectData *BlenderSceneDelegate::object_data(SdfPath const &id)
|
||||
{
|
||||
auto it = objects.find(id);
|
||||
@ -219,6 +273,15 @@ MaterialData *BlenderSceneDelegate::material_data(SdfPath const &id)
|
||||
return it->second.get();
|
||||
}
|
||||
|
||||
InstanceData *BlenderSceneDelegate::instance_data(SdfPath const &id)
|
||||
{
|
||||
auto it = instances.find(id);
|
||||
if (it == instances.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
return it->second.get();
|
||||
}
|
||||
|
||||
void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont)
|
||||
{
|
||||
bool is_populated = depsgraph != nullptr;
|
||||
|
@ -32,14 +32,22 @@ public:
|
||||
pxr::SdfPath GetMaterialId(pxr::SdfPath const &rprimId) override;
|
||||
pxr::VtValue GetMaterialResource(pxr::SdfPath const &materialId) override;
|
||||
bool GetVisible(pxr::SdfPath const &id) override;
|
||||
pxr::GfRange3d GetExtent(pxr::SdfPath const &id) override;
|
||||
bool GetDoubleSided(pxr::SdfPath const &id) override;
|
||||
pxr::HdCullStyle GetCullStyle(pxr::SdfPath const &id) override;
|
||||
pxr::SdfPath GetInstancerId(pxr::SdfPath const &primId) override;
|
||||
pxr::SdfPathVector GetInstancerPrototypes(pxr::SdfPath const &instancerId) override;
|
||||
pxr::VtIntArray GetInstanceIndices(pxr::SdfPath const &instancerId, pxr::SdfPath const &prototypeId) override;
|
||||
|
||||
private:
|
||||
private:
|
||||
ObjectData *object_data(pxr::SdfPath const &id);
|
||||
MeshData *mesh_data(pxr::SdfPath const &id);
|
||||
LightData *light_data(pxr::SdfPath const &id);
|
||||
MaterialData *material_data(pxr::SdfPath const &id);
|
||||
InstanceData *instance_data(pxr::SdfPath const &id);
|
||||
|
||||
void add_update_object(Object *object, bool geometry, bool transform, bool shading);
|
||||
void add_update_instance(DupliObject *dupli, bool transform);
|
||||
void set_material(MeshData &mesh_data);
|
||||
void update_material(Material *material);
|
||||
void update_world();
|
||||
|
@ -1,38 +1,88 @@
|
||||
/* SPDX-License-Identifier: Apache-2.0
|
||||
* Copyright 2011-2022 Blender Foundation */
|
||||
|
||||
#include <pxr/imaging/hd/instancer.h>
|
||||
|
||||
#include "glog/logging.h"
|
||||
|
||||
#include "../utils.h"
|
||||
#include "instance.h"
|
||||
|
||||
using namespace pxr;
|
||||
|
||||
namespace blender::render::hydra {
|
||||
std::unique_ptr<InstanceData> InstanceData::init(pxr::HdSceneDelegate *scene_delegate,
|
||||
DupliObject *dupli)
|
||||
{
|
||||
return std::make_unique<InstanceData>(scene_delegate, dupli);
|
||||
}
|
||||
|
||||
pxr::SdfPath InstanceData::prim_id(pxr::HdSceneDelegate *scene_delegate, DupliObject *dupli)
|
||||
{
|
||||
char str[16];
|
||||
snprintf(str, 16, "I_%08lx", dupli->random_id);
|
||||
return scene_delegate->GetDelegateID().AppendElementString(str);
|
||||
}
|
||||
|
||||
InstanceData::InstanceData(pxr::HdSceneDelegate *scene_delegate, DupliObject *dupli)
|
||||
: IdData(scene_delegate, (ID *)dupli->ob_data)
|
||||
: IdData(scene_delegate, (ID *)dupli->ob)
|
||||
, dupli(dupli)
|
||||
{
|
||||
}
|
||||
|
||||
int InstanceData::random_id()
|
||||
pxr::GfMatrix4d InstanceData::transform()
|
||||
{
|
||||
return dupli->random_id;
|
||||
SdfPath p_id = prim_id(scene_delegate, dupli);
|
||||
LOG(INFO) << "Transform instance: " << name() << " " << p_id.GetName();
|
||||
|
||||
return gf_matrix_from_transform(dupli->mat);
|
||||
}
|
||||
|
||||
VtValue InstanceData::get_data(TfToken const &key)
|
||||
{
|
||||
SdfPath p_id = prim_id(scene_delegate, dupli);
|
||||
LOG(INFO) << "Get data instance: " << name() << " " << p_id.GetName()
|
||||
<< " [" << key.GetString() << "]";
|
||||
|
||||
return VtValue();
|
||||
}
|
||||
|
||||
void InstanceData::insert_prim()
|
||||
{
|
||||
SdfPath p_id = prim_id(scene_delegate, dupli);
|
||||
scene_delegate->GetRenderIndex().InsertSprim(HdPrimTypeTokens->instance, scene_delegate, p_id);
|
||||
LOG(INFO) << "Add instance: " << name() << " id=" << p_id.GetAsString();
|
||||
}
|
||||
|
||||
void InstanceData::remove_prim()
|
||||
{
|
||||
SdfPath p_id = prim_id(scene_delegate, dupli);
|
||||
scene_delegate->GetRenderIndex().RemoveSprim(HdPrimTypeTokens->instance, p_id);
|
||||
LOG(INFO) << "Remove instance: " << name() << " id=" << p_id.GetAsString();
|
||||
}
|
||||
|
||||
void InstanceData::mark_prim_dirty(DirtyBits dirty_bits)
|
||||
{
|
||||
SdfPath p_id = prim_id(scene_delegate, dupli);
|
||||
HdDirtyBits bits = HdChangeTracker::Clean;
|
||||
switch (dirty_bits) {
|
||||
case DirtyBits::DirtyTransform:
|
||||
bits = HdChangeTracker::DirtyTransform;
|
||||
break;
|
||||
case DirtyBits::DirtyVisibility:
|
||||
bits = HdChangeTracker::DirtyVisibility;
|
||||
break;
|
||||
case DirtyBits::DirtyMaterial:
|
||||
bits = HdChangeTracker::DirtyMaterialId;
|
||||
break;
|
||||
case DirtyBits::AllDirty:
|
||||
bits = HdChangeTracker::AllDirty;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id, bits);
|
||||
LOG(INFO) << "Update mesh: " << name() << " [" << (int)dirty_bits << "]";
|
||||
}
|
||||
|
||||
} // namespace blender::render::hydra
|
||||
|
@ -11,9 +11,12 @@ namespace blender::render::hydra {
|
||||
|
||||
class InstanceData: public IdData {
|
||||
public:
|
||||
static std::unique_ptr<InstanceData> init(pxr::HdSceneDelegate *scene_delegate, DupliObject *dupli);
|
||||
static pxr::SdfPath prim_id(pxr::HdSceneDelegate *scene_delegate, DupliObject *dupli);
|
||||
|
||||
InstanceData(pxr::HdSceneDelegate *scene_delegate, DupliObject *dupli);
|
||||
|
||||
int random_id();
|
||||
pxr::GfMatrix4d transform();
|
||||
|
||||
pxr::VtValue get_data(pxr::TfToken const &key) override;
|
||||
void insert_prim() override;
|
||||
@ -26,4 +29,6 @@ public:
|
||||
|
||||
using InstanceDataMap = pxr::TfHashMap<pxr::SdfPath, std::unique_ptr<InstanceData>, pxr::SdfPath::Hash>;
|
||||
|
||||
|
||||
|
||||
} // namespace blender::render::hydra
|
||||
|
@ -11,7 +11,6 @@
|
||||
#include "BKE_object.h"
|
||||
#include "BKE_material.h"
|
||||
|
||||
|
||||
#include "mesh.h"
|
||||
|
||||
using namespace pxr;
|
||||
|
Loading…
Reference in New Issue
Block a user