BLEN-299: Export instances #11

Merged
Bogdan Nagirniak merged 7 commits from BLEN-299 into hydra-render 2023-03-05 09:37:12 +01:00
4 changed files with 75 additions and 7 deletions
Showing only changes of commit 9cff11e44c - Show all commits

View File

@ -105,6 +105,14 @@ VtIntArray BlenderSceneDelegate::GetInstanceIndices(SdfPath const &instancerId,
return ret;
}
GfMatrix4d BlenderSceneDelegate::GetInstancerTransform(SdfPath const &instancerId)
{
LOG(INFO) << "GetInstancerTransform: " << instancerId.GetString();
// TODO: add a separate object for instancer for cleaner handling code
// Actual instancer transform is get here
return GfMatrix4d(1.0);
}
size_t BlenderSceneDelegate::SampleInstancerTransform(SdfPath const &instancerId, size_t maxSampleCount,
float *sampleTimes, GfMatrix4d *sampleValues)
{
@ -115,6 +123,19 @@ size_t BlenderSceneDelegate::SampleInstancerTransform(SdfPath const &instancerId
return ret;
}
size_t BlenderSceneDelegate::SamplePrimvar(SdfPath const &id, TfToken const &key, size_t maxSampleCount,
float *sampleTimes, VtValue *sampleValues)
{
// TODO: add a separate object for instancer for cleaner handling code
if (id.GetName() == "Instancer") {
MeshData *m_data = mesh_data(id.GetParentPath());
if (m_data) {
return m_data->sample_instancer_primvar(key, maxSampleCount, sampleTimes, sampleValues);
}
}
return HdSceneDelegate::SamplePrimvar(id, key, maxSampleCount, sampleTimes, sampleValues);
}
void BlenderSceneDelegate::update_collection(bool remove, bool visibility)
{
if (visibility) {
@ -370,6 +391,10 @@ VtValue BlenderSceneDelegate::Get(SdfPath const& id, TfToken const& key)
{
LOG(INFO) << "Get: " << id.GetString() << " " << key.GetString();
ObjectData *obj_data = object_data(id);
// TODO: add a separate object for instancer for cleaner handling code
if (!obj_data && id.GetName() == "Instancer") {
obj_data = object_data(id.GetParentPath());
}
if (obj_data) {
return obj_data->get_data(key);
}
@ -387,6 +412,12 @@ HdPrimvarDescriptorVector BlenderSceneDelegate::GetPrimvarDescriptors(SdfPath co
if (mesh_data(id)) {
return mesh_data(id)->primvar_descriptors(interpolation);
}
// TODO: add a separate object for instancer for cleaner handling code
else if (id.GetName() == "Instancer") {
if (MeshData *data = mesh_data(id.GetParentPath())) {
return data->instancer_primvar_descriptors(interpolation);
}
}
HdPrimvarDescriptorVector primvars;
return primvars;
}
@ -412,6 +443,12 @@ GfMatrix4d BlenderSceneDelegate::GetTransform(SdfPath const& id)
if (obj_data) {
return obj_data->transform();
}
// TODO: add a separate object for instancer for cleaner handling code
else if (id.GetName() == "Instancer") {
if (MeshData *mesh = mesh_data(id.GetParentPath())) {
return mesh->transform().GetInverse();
}
}
if (id == WorldData::prim_id(this)) {
return world_data->transform();
}

View File

@ -34,8 +34,11 @@ public:
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;
pxr::GfMatrix4d GetInstancerTransform(pxr::SdfPath const &instancerId);
size_t SampleInstancerTransform(pxr::SdfPath const &instancerId, size_t maxSampleCount,
float *sampleTimes, pxr::GfMatrix4d *sampleValues) override;
size_t SamplePrimvar(pxr::SdfPath const &id, pxr::TfToken const &key, size_t maxSampleCount,
float *sampleTimes, pxr::VtValue *sampleValues) override;
private:
ObjectData *object_data(pxr::SdfPath const &id);

View File

@ -44,6 +44,9 @@ VtValue MeshData::get_data(TfToken const &key)
else if (key == HdPrimvarRoleTokens->textureCoordinate) {
ret = uvs;
}
else if (key == HdInstancerTokens->instanceTransform) {
ret = instances;
}
return ret;
}
@ -82,6 +85,16 @@ HdPrimvarDescriptorVector MeshData::primvar_descriptors(HdInterpolation interpol
return primvars;
}
HdPrimvarDescriptorVector MeshData::instancer_primvar_descriptors(HdInterpolation interpolation)
{
HdPrimvarDescriptorVector primvars;
if (interpolation == HdInterpolationInstance) {
primvars.emplace_back(HdInstancerTokens->instanceTransform, interpolation,
HdPrimvarRoleTokens->none);
}
return primvars;
}
VtIntArray MeshData::instance_indices()
{
VtIntArray ret(instances.size());
@ -93,12 +106,21 @@ VtIntArray MeshData::instance_indices()
size_t MeshData::sample_instancer_transform(size_t maxSampleCount, float *sampleTimes, GfMatrix4d *sampleValues)
{
size_t n = std::min(instances.size(), maxSampleCount);
for (size_t i = 0; i < n; ++i) {
sampleTimes[i] = 0.0f;
sampleValues[i] = gf_matrix_from_transform(instances[i]->mat);
*sampleTimes = 0.0f;
*sampleValues = GfMatrix4d(1.0);
return 1;
}
size_t MeshData::sample_instancer_primvar(TfToken const &key, size_t maxSampleCount, float *sampleTimes, VtValue *sampleValues)
{
if (key == HdInstancerTokens->instanceTransform) {
if (maxSampleCount > 0) {
sampleTimes[0] = 0.0f;
sampleValues[0] = instances;
return 1;
}
return n;
}
return 0;
}
void MeshData::add_instance(DupliObject *dupli)
@ -108,7 +130,11 @@ void MeshData::add_instance(DupliObject *dupli)
scene_delegate->GetRenderIndex().InsertInstancer(scene_delegate, instancer_id);
LOG(INFO) << "Add instancer: " << name() << " id=" << instancer_id.GetAsString();
}
instances.push_back(dupli);
if (instances.empty()) {
// USD hides the prototype mesh when instancing in contrary to the Blender, so we must add it back implicitly
instances.push_back(GfMatrix4d(1.0));
}
instances.push_back(transform().GetInverse() * gf_matrix_from_transform(dupli->mat));
LOG(INFO) << "Add instance: " << instancer_id.GetAsString() << " " << dupli->random_id;
}

View File

@ -25,8 +25,10 @@ public:
Material *material();
pxr::HdMeshTopology mesh_topology();
pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation);
pxr::HdPrimvarDescriptorVector instancer_primvar_descriptors(pxr::HdInterpolation interpolation);
pxr::VtIntArray instance_indices();
size_t sample_instancer_transform(size_t maxSampleCount, float *sampleTimes, pxr::GfMatrix4d *sampleValues);
size_t sample_instancer_primvar(pxr::TfToken const &key, size_t maxSampleCount, float *sampleTimes, pxr::VtValue *sampleValues);
void add_instance(DupliObject *dupli);
@ -42,7 +44,7 @@ public:
pxr::VtVec3fArray normals;
pxr::VtVec2fArray uvs;
std::vector<DupliObject *> instances;
pxr::VtMatrix4dArray instances;
};