Export curves #46

Merged
Bogdan Nagirniak merged 7 commits from BLEN-364 into hydra-render 2023-05-29 11:45:41 +02:00
5 changed files with 56 additions and 37 deletions
Showing only changes of commit 27e5fe316a - Show all commits

View File

@ -575,6 +575,10 @@ void BlenderSceneDelegate::remove_unused_objects()
if (m_data) { if (m_data) {
m_data->available_materials(available_materials); m_data->available_materials(available_materials);
} }
CurvesData *c_data = dynamic_cast<CurvesData *>(it.second.get());
if (c_data) {
c_data->available_materials(available_materials);
}
} }
for (auto &it : instancers_) { for (auto &it : instancers_) {
it.second->available_materials(available_materials); it.second->available_materials(available_materials);

View File

@ -26,6 +26,7 @@ class Engine;
class BlenderSceneDelegate : public pxr::HdSceneDelegate { class BlenderSceneDelegate : public pxr::HdSceneDelegate {
friend ObjectData; /* has access to instances */ friend ObjectData; /* has access to instances */
friend CurvesData; /* has access to curves */
friend MeshData; /* has access to materials */ friend MeshData; /* has access to materials */
friend MaterialData; /* has access to objects and instancers */ friend MaterialData; /* has access to objects and instancers */

View File

@ -51,7 +51,6 @@ void CurvesData::update()
init(); init();
bits = pxr::HdChangeTracker::AllDirty; bits = pxr::HdChangeTracker::AllDirty;
} }
if (id->recalc & ID_RECALC_SHADING) { if (id->recalc & ID_RECALC_SHADING) {
write_material(); write_material();
bits |= pxr::HdChangeTracker::DirtyMaterialId | pxr::HdChangeTracker::DirtyDoubleSided; bits |= pxr::HdChangeTracker::DirtyMaterialId | pxr::HdChangeTracker::DirtyDoubleSided;
@ -75,9 +74,6 @@ pxr::VtValue CurvesData::get_data(pxr::SdfPath const &id, pxr::TfToken const &ke
if (key == pxr::HdTokens->points) { if (key == pxr::HdTokens->points) {
ret = vertices_; ret = vertices_;
} }
else if (key == pxr::HdTokens->normals) {
ret = normals_;
}
else if (key == pxr::HdPrimvarRoleTokens->textureCoordinate) { else if (key == pxr::HdPrimvarRoleTokens->textureCoordinate) {
ret = uvs_; ret = uvs_;
} }
@ -117,10 +113,6 @@ pxr::HdPrimvarDescriptorVector CurvesData::primvar_descriptors(
} }
} }
else if (interpolation == pxr::HdInterpolationFaceVarying) { else if (interpolation == pxr::HdInterpolationFaceVarying) {
if (!normals_.empty()) {
primvars.emplace_back(
pxr::HdTokens->normals, interpolation, pxr::HdPrimvarRoleTokens->normal);
}
if (!uvs_.empty()) { if (!uvs_.empty()) {
primvars.emplace_back(pxr::HdPrimvarRoleTokens->textureCoordinate, primvars.emplace_back(pxr::HdPrimvarRoleTokens->textureCoordinate,
interpolation, interpolation,
@ -158,19 +150,23 @@ void CurvesData::update_double_sided(MaterialData *mat_data)
} }
} }
void CurvesData::available_materials(std::set<pxr::SdfPath> &paths) const
{
if (mat_data_ && !mat_data_->prim_id.IsEmpty()) {
paths.insert(mat_data_->prim_id);
}
}
void CurvesData::write_curves(Curves *curves) void CurvesData::write_curves(Curves *curves)
{ {
curve_vertex_counts_.clear(); curve_vertex_counts_.clear();
width_.clear(); width_.clear();
vertices_.clear(); vertices_.clear();
uvs_.clear();
const float *radii = (const float *)CustomData_get_layer_named( const float *radii = (const float *)CustomData_get_layer_named(
&curves->geometry.point_data, CD_PROP_FLOAT, "radius"); &curves->geometry.point_data, CD_PROP_FLOAT, "radius");
const float(*positions)[3] = (const float(*)[3])CustomData_get_layer_named( const float(*positions)[3] = (const float(*)[3])CustomData_get_layer_named(
&curves->geometry.point_data, CD_PROP_FLOAT3, "position"); &curves->geometry.point_data, CD_PROP_FLOAT3, "position");
const float(*uvs)[2] = (const float(*)[2])CustomData_get_layer_named(
&curves->geometry.curve_data, CD_PROP_FLOAT2, "surface_uv_coordinate");
vertices_.reserve(curves->geometry.curve_num); vertices_.reserve(curves->geometry.curve_num);
@ -179,36 +175,53 @@ void CurvesData::write_curves(Curves *curves)
const int num_points = *(curves->geometry.curve_offsets + i + 1) - const int num_points = *(curves->geometry.curve_offsets + i + 1) -
*(curves->geometry.curve_offsets + i); *(curves->geometry.curve_offsets + i);
curve_vertex_counts_.push_back(num_points); curve_vertex_counts_.push_back(num_points);
width_.push_back(0.005f);
/* Set radius similar to Cycles if isn't set */
float radius = radii ? radii[i] : 0.01f;
width_.push_back(radius);
for (int j = 0; j < num_points; j++) { for (int j = 0; j < num_points; j++) {
const int index = first_point_index + j; const int index = first_point_index + j;
vertices_.push_back( vertices_.push_back(
pxr::GfVec3f(positions[index][0], positions[index][1], positions[index][2])); pxr::GfVec3f(positions[index][0], positions[index][1], positions[index][2]));
uvs_.push_back(pxr::GfVec2f(uvs[index][0], uvs[index][1]));
} }
} }
write_uv_maps(curves);
}
void CurvesData::write_uv_maps(Curves *curves)
{
uvs_.clear();
const float(*uvs)[2] = (const float(*)[2])CustomData_get_layer_named(
&curves->geometry.curve_data, CD_PROP_FLOAT2, "surface_uv_coordinate");
for (int i = 0; i < curves->geometry.curve_num; i++) {
uvs_.push_back(pxr::GfVec2f(uvs[i][0], uvs[i][1]));
}
} }
void CurvesData::write_material() void CurvesData::write_material()
{ {
//Object *object = (Object *)id; Object *object = (Object *)id;
//for (int i = 0; i < submeshes_.size(); ++i) { Material *mat = nullptr;
// SubMesh &m = submeshes_[i]; if (BKE_object_material_count_eval(object) > 0) {
// Material *mat = BKE_object_material_get_eval(object, m.mat_index + 1); mat = BKE_object_material_get_eval(object, object->actcol);
// if (!mat) { }
// m.mat_data = nullptr;
// continue; if (!mat) {
// } mat_data_ = nullptr;
// pxr::SdfPath p_id = scene_delegate_->material_prim_id(mat); return;
// m.mat_data = scene_delegate_->material_data(p_id); }
// if (!m.mat_data) { pxr::SdfPath p_id = scene_delegate_->material_prim_id(mat);
// scene_delegate_->materials_[p_id] = std::make_unique<MaterialData>( mat_data_ = scene_delegate_->material_data(p_id);
// scene_delegate_, mat, p_id); if (!mat_data_) {
// m.mat_data = scene_delegate_->material_data(p_id); scene_delegate_->materials_[p_id] = std::make_unique<MaterialData>(scene_delegate_, mat, p_id);
// m.mat_data->init(); mat_data_ = scene_delegate_->material_data(p_id);
// m.mat_data->insert(); mat_data_->init();
// } mat_data_->insert();
//} }
} }
} // namespace blender::render::hydra } // namespace blender::render::hydra

View File

@ -31,21 +31,18 @@ class CurvesData : public ObjectData {
pxr::SdfPath material_id() const; pxr::SdfPath material_id() const;
bool double_sided(pxr::SdfPath const &id) const; bool double_sided(pxr::SdfPath const &id) const;
void update_double_sided(MaterialData *mat_data); void update_double_sided(MaterialData *mat_data);
void available_materials(std::set<pxr::SdfPath> &paths) const;
pxr::HdCullStyle cull_style = pxr::HdCullStyleBackUnlessDoubleSided; pxr::HdCullStyle cull_style = pxr::HdCullStyleBackUnlessDoubleSided;
private: private:
void write_curves(Curves *curves); void write_curves(Curves *curves);
void write_material();
void write_uv_maps(Curves *curves); void write_uv_maps(Curves *curves);
void write_normals(Curves *curves); void write_material();
pxr::VtIntArray curve_vertex_counts_; pxr::VtIntArray curve_vertex_counts_;
pxr::VtIntArray face_vertex_indices_;
pxr::VtVec3fArray vertices_; pxr::VtVec3fArray vertices_;
pxr::VtVec3fArray positions_;
pxr::VtVec2fArray uvs_; pxr::VtVec2fArray uvs_;
pxr::VtVec3fArray normals_;
pxr::VtFloatArray width_; pxr::VtFloatArray width_;
MaterialData *mat_data_ = nullptr; MaterialData *mat_data_ = nullptr;

View File

@ -65,6 +65,10 @@ void MaterialData::update()
if (m_data) { if (m_data) {
m_data->update_double_sided(this); m_data->update_double_sided(this);
} }
CurvesData *c_data = dynamic_cast<CurvesData *>(it.second.get());
if (c_data) {
c_data->update_double_sided(this);
}
} }
for (auto &it : scene_delegate_->instancers_) { for (auto &it : scene_delegate_->instancers_) {
it.second->update_double_sided(this); it.second->update_double_sided(this);