
Mon, 02/03/2025 - 02:23
Forums:
Hello all,
I wrote following to export a shape to OBJ.
works all so fare but have trouble with texture:
when import into blender the texture is showing correct in Preview but not on the model.
I already had an import in my app and all other obj are import correct with texture. import my just exported obj shows some texture but not correct. Please see attached image.
I have a feeling something with scaling. Could someone please give me a hint?
Thanks and have a great week!
qDebug() << "write obj";
// 2. Create a document to store the shape
Handle(TDocStd_Document) aDoc = new TDocStd_Document("XmlOcaf");
Handle(XCAFDoc_ShapeTool) shapeTool = XCAFDoc_DocumentTool::ShapeTool(aDoc->Main());
Handle(XCAFDoc_LayerTool) layerTool = XCAFDoc_DocumentTool::LayerTool(aDoc->Main());
Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(aDoc->Main());
// Handle(XCAFDoc_MaterialTool) myMaterial = XCAFDoc_DocumentTool::MaterialTool (aDoc->Main());
// 4. Add the shape to the document
TDF_Label shapeLabel = shapeTool->NewShape();
//BRepMesh_IncrementalMesh(base->occBase()->aisDisplay()->Shape(), 0.1, false, 0.1, false);
shapeTool->SetShape(shapeLabel, base->occBase()->aisDisplay()->Shape());
shapeTool->ComputeSimpleShapes();
//XCAFDoc_DocumentTool::ShapeTool(aDoc->Main())->SetShape(shapeLabel, base->occBase()->aisDisplay()->Shape());
Quantity_Color color;
base->occBase()->aisDisplay()->Color(color);
myColors->SetColor(shapeLabel, color, XCAFDoc_ColorSurf);
Handle(XCAFDoc_VisMaterialTool) visMaterialTool = XCAFDoc_DocumentTool::VisMaterialTool(aDoc->Main());
TCollection_AsciiString materialName("GlassMaterial");
Graphic3d_MaterialAspect glassMaterial = Graphic3d_NameOfMaterial::Graphic3d_NameOfMaterial_Glass;
//Handle(Graphic3d_MaterialAspect) glassMaterial = new Graphic3d_MaterialAspect(Graphic3d_NameOfMaterial::Graphic3d_NameOfMaterial_Glass);
Handle(TDF_Label) materialLabel;
Handle(XCAFDoc_VisMaterial) visMaterial = new XCAFDoc_VisMaterial();
//visMaterial->FillMaterialAspect(glassMaterial);
Handle(Image_Texture) textureBaseColor = new Image_Texture(".../textures/bark_willow_02_4k.blend/textures/bark_willow_02_diff_4k.png");
Handle(Image_Texture) textureEmissive = new Image_Texture(".../textures/bark_willow_02_4k.blend/textures/bark_willow_02_disp_4k.png");
Handle(Image_Texture) textureOcclusion = new Image_Texture(".../textures/bark_willow_02_4k.blend/textures/bark_willow_02_disp_4k.png");
Handle(Image_Texture) textureNormal = new Image_Texture(".../textures/bark_willow_02_4k.blend/textures/bark_willow_02_nor_gl_4k.png");
Handle(Image_Texture) textureMetallicRoughness = new Image_Texture(".../textures/bark_willow_02_4k.blend/textures/bark_willow_02_rough_4k.png");
XCAFDoc_VisMaterialPBR pbrm;
pbrm.BaseColorTexture = textureBaseColor;
pbrm.EmissiveTexture = textureEmissive;
pbrm.OcclusionTexture = textureOcclusion;
pbrm.NormalTexture = textureNormal;
pbrm.MetallicRoughnessTexture = textureMetallicRoughness;
pbrm.IsDefined = true;
Handle(XCAFDoc_VisMaterial) matWood = new XCAFDoc_VisMaterial();
matWood->SetPbrMaterial(pbrm);
TDF_Label matLabel = visMaterialTool->AddMaterial(matWood, "wood");
visMaterialTool->SetShapeMaterial(shapeLabel, matLabel);
Standard_Real aFileUnitFactor = 1.0;
RWMesh_CoordinateSystem aSystemCoordSys = RWMesh_CoordinateSystem_Zup, aFileCoordSys = RWMesh_CoordinateSystem_Yup;
TColStd_IndexedDataMapOfStringString aFileInfo;
aFileInfo.Add("Author", "AbbieCam");
const Standard_Real aSystemUnitFactor = UnitsMethods::GetCasCadeLengthUnit() * 0.1;//.001;
RWObj_CafWriter aWriter(TCollection_AsciiString(fileName.toStdString().c_str()));
aWriter.ChangeCoordinateSystemConverter().SetInputCoordinateSystem(aSystemCoordSys);
aWriter.ChangeCoordinateSystemConverter().SetOutputCoordinateSystem(aFileCoordSys);
aWriter.ChangeCoordinateSystemConverter().SetInputLengthUnit(aSystemUnitFactor);
aWriter.ChangeCoordinateSystemConverter().SetOutputLengthUnit(aFileUnitFactor);
qDebug() << "perform" << aWriter.Perform(aDoc, aFileInfo, Message_ProgressRange());
fileName = StringEx::replaceExt(fileName, "gltf");
qDebug() << "gltf fileName" << fileName;
RWGltf_CafWriter writer(fileName.toStdString().c_str(), false);
qDebug() << "gltf written" << writer.Perform(aDoc, aFileInfo, Message_ProgressRange());
Attachments:
Mon, 02/03/2025 - 13:25
How the shape with mapped texture are created? How texture coordinates are defined on it?
Could you share some simplified model sample?
Wed, 02/05/2025 - 00:52
Like this:
please let me know if you need more info.
Thanks!
Wed, 02/05/2025 - 08:34
I still don't see where model geometry / triangulation with UV texture coordinates comes from nor a sample model to take a look.
Note that texture sampling parameters are currently not exported / imported by OCCT (neither for glTF nor for OBJ), therefore clamping to edge parameter will be lost.
XCAFPrs_Texture
by default is initialized to clamp texture coordinates, not repeat.I don't know what is the default behavior of Blender, but I guess it might be different for preview/final renderers. Ideally, it is better defining texture mapping (UV coordinates + image) that wouldn't require repeat sampling for proper rendering - in this way it will render in the same way with and without clamping option.
By the way - why are you using OBJ format instead of glTF, which should be better for transferring model from OCCT to Blender?
Thu, 02/13/2025 - 19:47
Sorry for late answer
you got me going... First I was going after vtk.js to visualize my models online but your comment made me take a closer look.
now I am using threejs and show gltf scenes. Thank you for the tip!!
The textures still didn't work with my version (OCCT 7.7.0) so i updated to 7.9 beta2 because I read somewhere that some bug fixes with gltf are done.
but now I can't produce any gltf anymore. lol
I opened an other thread here for that:
https://dev.opencascade.org/content/gltf-writer-unavailable-occt-790beta2
Thanks!
Fri, 02/14/2025 - 22:13
i spent the last 5 days to get that going and really need help The texture is exported with the gltf file. importing in blender (just to see what is going on) shows me the texture on the exported box BUT WAY TOO SMALL as you can see on blender2.jpg in blender1.jpg i apply the texture from the exported box to the standard cube of blender and the texture is fine.
to be in a "normal size" i scale down the export with
this corrects the size of the exported box but then the texture is even smaller and I can not even zoom in so far to see it on the exported box.
I really don't know what more information you need from me. The only scale i do is with the writer.
wait a minute - scaling...
OMG - found it.
I am using in my application 1 unit as a mm. So the box was exported with 200x500x3500. now i did the same export as 0.2x0.5x3.5 and the texture is PERFECT
so final question to this?
How can I scale down without scaling the texture ? Obviously aWriter.ChangeCoordinateSystemConverter() doesn't work for me
Thanks!
Fri, 02/14/2025 - 22:49
As a result the scaling is 1.0 Needs to have different scale factor as input and output.
Fri, 02/14/2025 - 23:38
yes, i wrote this as an example since I load the values from the settings. Here the values I am actually using:
This is scaling the model to the correct size but also textures!
Fri, 02/14/2025 - 23:52
I assume no possible way without modification of textures (revers conversion) or source code.
Please do not over-use the caps lock.
Sat, 02/15/2025 - 00:01
what do you mean with "revers conversion"
Sat, 02/15/2025 - 00:11
Scale texture before conversion in the reversed coef. Or modify source code.
Sat, 02/15/2025 - 00:13
"Or modify source code." what source code. RWGltf_CafWriter?
I don't understand why texture looks good and texture scaling is working on AIS_Shape but not when exporting.
Sat, 02/15/2025 - 00:27
If you needs urgent consulting or urgent bug fixes - you can request technical support. Technical Support - OCCT3D
OCCT is a open-source and you can debug and analyze issues in case of problem.
The issue I guess with scaling 2D nodes of mesh. You can modify source code to avoid their scaling during export.
Sat, 02/15/2025 - 00:33
if I know it is a bug I can stop spending time to try to fix in my code.
So it is a bug in RWGltf_CafWriter?
ok
Sat, 02/15/2025 - 01:09
No, it is not a bug. It is one of the possible approaches without other options.