From 6c7714336461fababaae60305b3e948ecb9af79a Mon Sep 17 00:00:00 2001 From: Zernova Marina <36417100+mzernova@users.noreply.github.com> Date: Tue, 4 Feb 2025 09:22:45 +0000 Subject: [PATCH] Data Exchange - Fix GLTF Export Materials for edges #341 Fixed issue with edge colors --- src/RWGltf/RWGltf_CafWriter.cxx | 32 ++++++++++++++++++++++----- src/RWGltf/RWGltf_CafWriter.hxx | 8 +++++++ src/RWGltf/RWGltf_GltfMaterialMap.cxx | 6 ++++- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/RWGltf/RWGltf_CafWriter.cxx b/src/RWGltf/RWGltf_CafWriter.cxx index 4039e6eab0..e2c36f71c5 100644 --- a/src/RWGltf/RWGltf_CafWriter.cxx +++ b/src/RWGltf/RWGltf_CafWriter.cxx @@ -1935,6 +1935,19 @@ void RWGltf_CafWriter::writeImages(const RWGltf_GltfSceneNodeMap& theSceneNodeMa //================================================================================================= +void RWGltf_CafWriter::writeMaterial(RWMesh_ShapeIterator& theShapeIter, + Standard_Boolean& theIsStarted, + Standard_Integer& theAddedMaterialsNb) +{ + for (; theShapeIter.More(); theShapeIter.Next()) + { + myMaterialMap->AddMaterial(myWriter.get(), theShapeIter.Style(), theIsStarted); + theAddedMaterialsNb++; + } +} + +//================================================================================================= + void RWGltf_CafWriter::writeMaterials(const RWGltf_GltfSceneNodeMap& theSceneNodeMap) { #ifdef HAVE_RAPIDJSON @@ -1946,12 +1959,21 @@ void RWGltf_CafWriter::writeMaterials(const RWGltf_GltfSceneNodeMap& theSceneNod for (RWGltf_GltfSceneNodeMap::Iterator aSceneNodeIter(theSceneNodeMap); aSceneNodeIter.More(); aSceneNodeIter.Next()) { - const XCAFPrs_DocumentNode& aDocNode = aSceneNodeIter.Value(); - for (RWMesh_FaceIterator aFaceIter(aDocNode.RefLabel, TopLoc_Location(), true, aDocNode.Style); - aFaceIter.More(); - aFaceIter.Next()) + const XCAFPrs_DocumentNode& aDocNode = aSceneNodeIter.Value(); + Standard_Integer anAddedMaterialsNb = 0; + { + RWMesh_FaceIterator aFaceIter(aDocNode.RefLabel, TopLoc_Location(), true, aDocNode.Style); + writeMaterial(aFaceIter, anIsStarted, anAddedMaterialsNb); + } + if (anAddedMaterialsNb == 0) + { + RWMesh_EdgeIterator anEdgeIter(aDocNode.RefLabel, TopLoc_Location(), true, aDocNode.Style); + writeMaterial(anEdgeIter, anIsStarted, anAddedMaterialsNb); + } + if (anAddedMaterialsNb == 0) { - myMaterialMap->AddMaterial(myWriter.get(), aFaceIter.Style(), anIsStarted); + RWMesh_VertexIterator VertexIter(aDocNode.RefLabel, TopLoc_Location(), true, aDocNode.Style); + writeMaterial(VertexIter, anIsStarted, anAddedMaterialsNb); } } if (anIsStarted) diff --git a/src/RWGltf/RWGltf_CafWriter.hxx b/src/RWGltf/RWGltf_CafWriter.hxx index 49abf3b183..fed2d845c7 100644 --- a/src/RWGltf/RWGltf_CafWriter.hxx +++ b/src/RWGltf/RWGltf_CafWriter.hxx @@ -353,6 +353,14 @@ protected: //! @param[out] theMaterialMap map of materials, filled with materials Standard_EXPORT virtual void writeMaterials(const RWGltf_GltfSceneNodeMap& theSceneNodeMap); + //! Write RWGltf_GltfRootElement_Materials section. + //! @param[in] theShapeIter Shape iterator to traverse shapes + //! @param[out] theIsStarted Flag indicating that writing material has been started + //! @param[out] theAddedMaterialsNb Number of added materials + Standard_EXPORT virtual void writeMaterial(RWMesh_ShapeIterator& theShapeIter, + Standard_Boolean& theIsStarted, + Standard_Integer& theAddedMaterialsNb); + //! Write RWGltf_GltfRootElement_Meshes section. //! @param[in] theSceneNodeMap ordered map of scene nodes //! @param[in] theMaterialMap map of materials diff --git a/src/RWGltf/RWGltf_GltfMaterialMap.cxx b/src/RWGltf/RWGltf_GltfMaterialMap.cxx index 4f33e87a7c..691f82c980 100644 --- a/src/RWGltf/RWGltf_GltfMaterialMap.cxx +++ b/src/RWGltf/RWGltf_GltfMaterialMap.cxx @@ -269,7 +269,7 @@ void RWGltf_GltfMaterialMap::AddMaterial(RWGltf_GltfOStreamWriter* theWriter, #ifdef HAVE_RAPIDJSON if (theWriter == NULL || ((theStyle.Material().IsNull() || theStyle.Material()->IsEmpty()) - && !theStyle.IsSetColorSurf())) + && !theStyle.IsSetColorSurf() && !theStyle.IsSetColorCurv())) { return; } @@ -387,6 +387,10 @@ void RWGltf_GltfMaterialMap::DefineMaterial(const XCAFPrs_Style& theStyle, aPbrMat.BaseColor.SetAlpha(theStyle.GetColorSurfRGBA().Alpha()); } } + else if (theStyle.IsSetColorCurv()) + { + aPbrMat.BaseColor.SetRGB(theStyle.GetColorCurv()); + } myWriter->StartObject(); { myWriter->Key("name");