Import: Support of colors of the glTF format
This commit is contained in:
@@ -25,28 +25,36 @@
|
||||
#include "PreCompiled.h"
|
||||
#ifndef _PreComp_
|
||||
#include <Standard_Version.hxx>
|
||||
#if OCC_VERSION_HEX >= 0x070500
|
||||
#include <BRep_Builder.hxx>
|
||||
#include <Message_ProgressRange.hxx>
|
||||
#include <Quantity_ColorRGBA.hxx>
|
||||
#include <RWGltf_CafReader.hxx>
|
||||
#include <TDF_Label.hxx>
|
||||
#include <TDF_TagSource.hxx>
|
||||
#include <XCAFDoc_DocumentTool.hxx>
|
||||
#include <XCAFDoc_ColorTool.hxx>
|
||||
#include <XCAFDoc_ShapeTool.hxx>
|
||||
#if OCC_VERSION_HEX >= 0x070500
|
||||
#include <Message_ProgressRange.hxx>
|
||||
#include <RWGltf_CafReader.hxx>
|
||||
#include <XCAFDoc_VisMaterial.hxx>
|
||||
#include <XCAFDoc_VisMaterialTool.hxx>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#include "ReaderGltf.h"
|
||||
#include "Tools.h"
|
||||
#include <Base/Exception.h>
|
||||
#include <Mod/Part/App/TopoShape.h>
|
||||
#include <Mod/Part/App/Tools.h>
|
||||
|
||||
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;
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
#include <Mod/Import/ImportGlobal.h>
|
||||
#include <Base/FileInfo.h>
|
||||
#include <TDocStd_Document.hxx>
|
||||
#include <TDF_LabelSequence.hxx>
|
||||
#include <TopoDS_Shape.hxx>
|
||||
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user