Skip to content

Commit

Permalink
Write directly to GLB from TGeometries (#6404)
Browse files Browse the repository at this point in the history
Uses ASSIMP for gltf2 binary file output. Support triangle_uvs, vertex colors, vertex normals and material. Compression not supported.
  • Loading branch information
errissa authored Nov 1, 2023
1 parent ad0edd0 commit ed962e4
Show file tree
Hide file tree
Showing 8 changed files with 371 additions and 7 deletions.
1 change: 0 additions & 1 deletion 3rdparty/assimp/assimp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ ExternalProject_Add(
-DCMAKE_CXX_FLAGS:STRING=${assimp_cmake_cxx_flags}
-DBUILD_SHARED_LIBS=OFF
-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
-DASSIMP_NO_EXPORT=ON
-DASSIMP_BUILD_ASSIMP_TOOLS=OFF
-DASSIMP_BUILD_TESTS=OFF
-DASSIMP_INSTALL_PDB=OFF
Expand Down
16 changes: 10 additions & 6 deletions cpp/open3d/io/file_format/FileASSIMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ void LoadTextures(const std::string& filename,
if (mat->GetTextureCount(type) > 0) {
aiString path;
mat->GetTexture(type, 0, &path);

// If the texture is an embedded texture, use `GetEmbeddedTexture`.
if (auto texture = scene->GetEmbeddedTexture(path.C_Str())) {
if (texture->CheckFormat("png")) {
Expand All @@ -91,13 +92,10 @@ void LoadTextures(const std::string& filename,
if (image->HasData()) {
img = image;
}
}

else {
} else {
utility::LogWarning(
"This format of image is not supported.");
}

}
// Else, build the path to it.
else {
Expand All @@ -122,7 +120,12 @@ void LoadTextures(const std::string& filename,
}
};

texture_loader(aiTextureType_DIFFUSE, maps.albedo);
// Prefer BASE_COLOR texture as assimp now uses it for PBR workflows
if (mat->GetTextureCount(aiTextureType_BASE_COLOR) > 0) {
texture_loader(aiTextureType_BASE_COLOR, maps.albedo);
} else {
texture_loader(aiTextureType_DIFFUSE, maps.albedo);
}
texture_loader(aiTextureType_NORMALS, maps.normal);
// Assimp may place ambient occlusion texture in AMBIENT_OCCLUSION if
// format has AO support. Prefer that texture if it is preset. Otherwise,
Expand Down Expand Up @@ -415,7 +418,8 @@ bool ReadModelUsingAssimp(const std::string& filename,
mat->Get(AI_MATKEY_SHEEN, o3d_mat.base_reflectance);

mat->Get(AI_MATKEY_CLEARCOAT_THICKNESS, o3d_mat.base_clearcoat);
mat->Get(AI_MATKEY_CLEARCOAT_ROUGHNESS,
mat->Get(AI_MATKEY_CLEARCOAT_FACTOR, o3d_mat.base_clearcoat);
mat->Get(AI_MATKEY_CLEARCOAT_ROUGHNESS_FACTOR,
o3d_mat.base_clearcoat_roughness);
mat->Get(AI_MATKEY_ANISOTROPY, o3d_mat.base_anisotropy);
aiString alpha_mode;
Expand Down
4 changes: 4 additions & 0 deletions cpp/open3d/t/io/ImageIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ bool WriteImageToPNG(const std::string &filename,
const geometry::Image &image,
int quality = kOpen3DImageIODefaultQuality);

bool WriteImageToPNGInMemory(std::vector<uint8_t> &output_buffer,
const geometry::Image &image,
int quality = kOpen3DImageIODefaultQuality);

bool ReadImageFromJPG(const std::string &filename, geometry::Image &image);

bool WriteImageToJPG(const std::string &filename,
Expand Down
1 change: 1 addition & 0 deletions cpp/open3d/t/io/TriangleMeshIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ static const std::unordered_map<
const bool)>>
file_extension_to_trianglemesh_write_function{
{"npz", WriteTriangleMeshToNPZ},
{"glb", WriteTriangleMeshUsingASSIMP},
};

std::shared_ptr<geometry::TriangleMesh> CreateMeshFromFile(
Expand Down
9 changes: 9 additions & 0 deletions cpp/open3d/t/io/TriangleMeshIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ bool ReadTriangleMeshFromNPZ(const std::string &filename,
geometry::TriangleMesh &mesh,
const open3d::io::ReadTriangleMeshOptions &params);

bool WriteTriangleMeshUsingASSIMP(const std::string &filename,
const geometry::TriangleMesh &mesh,
const bool write_ascii,
const bool compressed,
const bool write_vertex_normals,
const bool write_vertex_colors,
const bool write_triangle_uvs,
const bool print_progress);

bool WriteTriangleMeshToNPZ(const std::string &filename,
const geometry::TriangleMesh &mesh,
const bool write_ascii,
Expand Down
Loading

0 comments on commit ed962e4

Please sign in to comment.