From b2be356609ab92cd2154cfe936f7790e084a5b69 Mon Sep 17 00:00:00 2001 From: wmayer Date: Tue, 3 Oct 2023 11:47:28 +0200 Subject: [PATCH] Import: Support of colors of the glTF format --- src/Mod/Import/App/ReaderGltf.cpp | 94 ++++++++++++++++++++++++++----- src/Mod/Import/App/ReaderGltf.h | 4 ++ 2 files changed, 83 insertions(+), 15 deletions(-) diff --git a/src/Mod/Import/App/ReaderGltf.cpp b/src/Mod/Import/App/ReaderGltf.cpp index 47da47f2ce..4fd2449352 100644 --- a/src/Mod/Import/App/ReaderGltf.cpp +++ b/src/Mod/Import/App/ReaderGltf.cpp @@ -25,28 +25,36 @@ #include "PreCompiled.h" #ifndef _PreComp_ #include +#if OCC_VERSION_HEX >= 0x070500 +#include +#include +#include +#include #include #include #include +#include #include -#if OCC_VERSION_HEX >= 0x070500 -#include -#include +#include +#include #endif #endif #include "ReaderGltf.h" +#include "Tools.h" #include #include #include using namespace Import; -ReaderGltf::ReaderGltf(const Base::FileInfo& file) // NOLINT +// NOLINTNEXTLINE +ReaderGltf::ReaderGltf(const Base::FileInfo& file) : file {file} {} -void ReaderGltf::read(Handle(TDocStd_Document) hDoc) // NOLINT +// NOLINTNEXTLINE +void ReaderGltf::read(Handle(TDocStd_Document) hDoc) { #if OCC_VERSION_HEX >= 0x070500 const double unit = 0.001; // mm @@ -62,16 +70,7 @@ void ReaderGltf::read(Handle(TDocStd_Document) hDoc) // NOLINT throw Base::FileException("Cannot read from file: ", file); } - Handle(XCAFDoc_ShapeTool) aShapeTool = XCAFDoc_DocumentTool::ShapeTool(hDoc->Main()); - TDF_LabelSequence labels; - aShapeTool->GetShapes(labels); - for (Standard_Integer i = 1; i <= labels.Length(); i++) { - auto label = labels.Value(i); - TopoDS_Shape shape = aShapeTool->GetShape(label); - if (!shape.IsNull()) { - aShapeTool->SetShape(label, fixShape(shape)); - } - } + processDocument(hDoc); #else (void)hDoc; @@ -79,6 +78,71 @@ void ReaderGltf::read(Handle(TDocStd_Document) hDoc) // NOLINT #endif } +// NOLINTNEXTLINE +void ReaderGltf::processDocument(Handle(TDocStd_Document) hDoc) +{ +#if OCC_VERSION_HEX >= 0x070500 + Handle(XCAFDoc_ShapeTool) aShapeTool = XCAFDoc_DocumentTool::ShapeTool(hDoc->Main()); + + TDF_LabelSequence shapeLabels; + aShapeTool->GetShapes(shapeLabels); + for (Standard_Integer i = 1; i <= shapeLabels.Length(); i++) { + auto topLevelshape = shapeLabels.Value(i); + TopoDS_Shape shape = aShapeTool->GetShape(topLevelshape); + if (!shape.IsNull()) { + TDF_LabelSequence subShapeLabels; + if (XCAFDoc_ShapeTool::GetSubShapes(topLevelshape, subShapeLabels)) { + TopoDS_Shape compound = processSubShapes(hDoc, subShapeLabels); + aShapeTool->SetShape(topLevelshape, compound); + } + else { + aShapeTool->SetShape(topLevelshape, fixShape(shape)); + } + } + } +#endif +} + +// NOLINTNEXTLINE +TopoDS_Shape ReaderGltf::processSubShapes(Handle(TDocStd_Document) hDoc, + const TDF_LabelSequence& subShapeLabels) +{ + TopoDS_Compound compound; +#if OCC_VERSION_HEX >= 0x070500 + Handle(XCAFDoc_ShapeTool) aShapeTool = XCAFDoc_DocumentTool::ShapeTool(hDoc->Main()); + Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool(hDoc->Main()); + Handle(XCAFDoc_VisMaterialTool) aVisTool = XCAFDoc_DocumentTool::VisMaterialTool(hDoc->Main()); + + BRep_Builder builder; + builder.MakeCompound(compound); + for (Standard_Integer i = 1; i <= subShapeLabels.Length(); i++) { + auto faceLabel = subShapeLabels.Value(i); + + // OCCT handles colors of a glTF with material labels but the ImportOCAF(2) class + // expects color labels. Thus, the material labels are converted into color labels. + Handle(XCAFDoc_VisMaterial) aVisMat = aVisTool->GetShapeMaterial(faceLabel); + + Quantity_ColorRGBA rgba; + bool hasVisMat {false}; + if (!aVisMat.IsNull()) { + rgba = aVisMat->BaseColor(); + hasVisMat = true; + } + + TopoDS_Shape face = aShapeTool->GetShape(faceLabel); + TopoDS_Shape fixed = fixShape(face); + builder.Add(compound, fixed); + aShapeTool->SetShape(faceLabel, fixed); + + if (hasVisMat) { + aColorTool->SetColor(faceLabel, rgba, XCAFDoc_ColorSurf); + } + } +#endif + + return {std::move(compound)}; +} + bool ReaderGltf::cleanup() const { return clean; diff --git a/src/Mod/Import/App/ReaderGltf.h b/src/Mod/Import/App/ReaderGltf.h index 5925100bba..2b6e9f9895 100644 --- a/src/Mod/Import/App/ReaderGltf.h +++ b/src/Mod/Import/App/ReaderGltf.h @@ -27,6 +27,7 @@ #include #include #include +#include #include namespace Import @@ -43,6 +44,9 @@ public: private: TopoDS_Shape fixShape(TopoDS_Shape); + void processDocument(Handle(TDocStd_Document) hDoc); + TopoDS_Shape processSubShapes(Handle(TDocStd_Document) hDoc, + const TDF_LabelSequence& subShapeLabels); private: Base::FileInfo file;