From 2973da92037a6db9d0de192da67fc099f65096bc Mon Sep 17 00:00:00 2001 From: wmayer Date: Sat, 22 Oct 2022 23:31:12 +0200 Subject: [PATCH] Mesh: support to add transparencies to a mesh --- src/Mod/Mesh/Gui/ViewProvider.cpp | 57 +++++++++++++++++++++++++++---- src/Mod/Mesh/Gui/ViewProvider.h | 2 ++ 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/src/Mod/Mesh/Gui/ViewProvider.cpp b/src/Mod/Mesh/Gui/ViewProvider.cpp index 429e28e83b..8b906739ff 100644 --- a/src/Mod/Mesh/Gui/ViewProvider.cpp +++ b/src/Mod/Mesh/Gui/ViewProvider.cpp @@ -24,6 +24,7 @@ #include "PreCompiled.h" #ifndef _PreComp_ +# include # include # include # include @@ -382,6 +383,7 @@ void ViewProviderMesh::onChanged(const App::Property* prop) } else if (prop == &Coloring) { tryColorPerVertexOrFace(Coloring.getValue()); + tryTransparency(Coloring.getValue()); } else if (prop == &SelectionStyle) { pcHighlight->style = SelectionStyle.getValue() ? Gui::SoFCSelection::BOX @@ -585,6 +587,47 @@ void ViewProviderMesh::setColorPerFace(const App::PropertyColorList* prop) pcShapeMaterial->diffuseColor.finishEditing(); } +App::PropertyFloatList* ViewProviderMesh::getTransparencyProperty() const +{ + if (pcObject) { + App::Property* prop = pcObject->getPropertyByName("Transparency"); + if (prop && prop->getTypeId() == App::PropertyFloatList::getClassTypeId()) { + App::PropertyFloatList* transp = static_cast(prop); + return transp; + } + } + + return nullptr; // no such property found +} + +void ViewProviderMesh::tryTransparency(bool on) +{ + if (on) { + App::PropertyFloatList* prop = getTransparencyProperty(); + if (prop) { + const Mesh::PropertyMeshKernel& meshProp = static_cast(pcObject)->Mesh; + const Mesh::MeshObject& mesh = meshProp.getValue(); + int numFacets = static_cast(mesh.countFacets()); + + if (prop->getSize() == numFacets) { + const auto& values = prop->getValue(); + std::vector transp; + transp.reserve(values.size()); + std::transform(values.cbegin(), values.cend(), std::back_inserter(transp), [](double v) -> float { + // force values in range [0, 1] + return std::clamp(v, 0.0f, 1.0f); + }); + setFacetTransparency(transp); + } + } + } + else { + pcMatBinding->value = SoMaterialBinding::OVERALL; + float trans = Transparency.getValue()/100.0f; + pcShapeMaterial->transparency.setValue(trans); + } +} + void ViewProviderMesh::setDisplayMode(const char* ModeName) { if (strcmp("Shaded",ModeName)==0) { @@ -1813,12 +1856,14 @@ void ViewProviderMesh::fillHole(Mesh::FacetIndex uFacet) void ViewProviderMesh::setFacetTransparency(const std::vector& facetTransparency) { - App::Color c = ShapeColor.getValue(); - pcShapeMaterial->diffuseColor.setNum(facetTransparency.size()); - SbColor* cols = pcShapeMaterial->diffuseColor.startEditing(); - for (std::size_t index = 0; index < facetTransparency.size(); ++index) - cols[index].setValue(c.r, c.g, c.b); - pcShapeMaterial->diffuseColor.finishEditing(); + if (pcShapeMaterial->diffuseColor.getNum() != int(facetTransparency.size())) { + App::Color c = ShapeColor.getValue(); + pcShapeMaterial->diffuseColor.setNum(facetTransparency.size()); + SbColor* cols = pcShapeMaterial->diffuseColor.startEditing(); + for (std::size_t index = 0; index < facetTransparency.size(); ++index) + cols[index].setValue(c.r, c.g, c.b); + pcShapeMaterial->diffuseColor.finishEditing(); + } pcShapeMaterial->transparency.setNum(facetTransparency.size()); float* tran = pcShapeMaterial->transparency.startEditing(); diff --git a/src/Mod/Mesh/Gui/ViewProvider.h b/src/Mod/Mesh/Gui/ViewProvider.h index f3a8c92046..a9afed61f2 100644 --- a/src/Mod/Mesh/Gui/ViewProvider.h +++ b/src/Mod/Mesh/Gui/ViewProvider.h @@ -196,9 +196,11 @@ protected: void highlightColors(); bool canHighlightColors() const; App::PropertyColorList* getColorProperty() const; + App::PropertyFloatList* getTransparencyProperty() const; void tryColorPerVertexOrFace(bool); void setColorPerVertex(const App::PropertyColorList*); void setColorPerFace(const App::PropertyColorList*); + void tryTransparency(bool); virtual SoShape* getShapeNode() const; virtual SoNode* getCoordNode() const;