Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the GLTF writer to support writing textured models #6101

Draft
wants to merge 20 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
a09c8bc
Fix for Issue #5924:
dbs4261 Feb 20, 2023
01fdc2e
Updated CHANGELOG.md with info about fixing Issue #5924
dbs4261 Feb 20, 2023
6e156c1
Updates to work with changes to the translation of tensor mesh materials
dbs4261 Apr 13, 2023
1ffe35b
Added code to break a TriangleMesh into one mesh per material in a tr…
dbs4261 Feb 21, 2023
c763d91
Added tcb::span dependency to support std::span behaivor until c++20 …
dbs4261 Feb 23, 2023
6c06ce6
Add Model.cpp to the build this has code to separate a TriangleMesh b…
dbs4261 Feb 28, 2023
339fde8
Updated TinyGLTF version
dbs4261 Feb 28, 2023
23d5f6d
Updated FileGLTF and ModelIO to export a TriangleMeshModel to GLTF in…
dbs4261 Feb 28, 2023
55e7ed4
Added a test program that should be removed before merging.
dbs4261 Feb 28, 2023
e65cb0e
Fix to export textures in a GLB file
dbs4261 Mar 1, 2023
104b77c
Added extension for unlit materials to GLTF exporter
dbs4261 Apr 12, 2023
465a073
Renamed functions to match with existing naming scheme. Added docstri…
dbs4261 Apr 20, 2023
c1a9de6
Corrected a copy vs reference error
dbs4261 Apr 20, 2023
3a036bd
Added support for the KHR_materials_unlit extension in the GLTF Model…
dbs4261 Apr 20, 2023
dffeff1
Added python bindings to creating a TriangleMeshModel from a Triangle…
dbs4261 Apr 20, 2023
9ef2a54
Formatter changes
dbs4261 Apr 21, 2023
58708c3
Fixed bug where iterators weren't dereferenced when checking if more …
dbs4261 Apr 26, 2023
83f693a
Fixed bug in small meshes where most accessors need to be aligned to …
dbs4261 Apr 29, 2023
0d12154
Fixed bug in material conversion that would segfault when no texture …
dbs4261 Apr 29, 2023
65ecead
FINALLY fixed issue with per-vertex UV conversion. It was not checkin…
dbs4261 Apr 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Formatter changes
dbs4261 committed Apr 21, 2023
commit 9ef2a5456fae9067f559a83bdb38c29daec5c6f2
45 changes: 28 additions & 17 deletions cpp/open3d/io/ModelIO.cpp
Original file line number Diff line number Diff line change
@@ -25,7 +25,8 @@ MeshWithPerVertexUVs(const geometry::TriangleMesh& mesh) {
return {mesh, {}};
}
if (mesh.HasAdjacencyList()) {
utility::LogWarning("[MeshWithPerVertexUVs] This mesh contains "
utility::LogWarning(
"[MeshWithPerVertexUVs] This mesh contains "
"an adjacency list that are not handled in this function");
}
geometry::TriangleMesh out = mesh;
@@ -40,18 +41,20 @@ MeshWithPerVertexUVs(const geometry::TriangleMesh& mesh) {
if (vertex_uvs[triangle(i)] == InvalidUV) {
vertex_uvs[triangle(i)] = out.triangle_uvs_[3 * tidx + i];
} else {
if (vertex_uvs[triangle(i)] != out.triangle_uvs_[3 * tidx + i]){
if (vertex_uvs[triangle(i)] !=
out.triangle_uvs_[3 * tidx + i]) {
assert(true);
if (vertex_remap.count(triangle(i))) {
for (int remap_tidx: vertex_remap[(int)tidx]) {
for (int remap_tidx : vertex_remap[(int)tidx]) {
if (vertex_uvs[remap_tidx] ==
out.triangle_uvs_[3 * tidx + i]) {
triangle(i) = remap_tidx;
break;
}
}
} else {
vertex_uvs.emplace_back(out.triangle_uvs_[3 * tidx + 1]);
vertex_uvs.emplace_back(
out.triangle_uvs_[3 * tidx + 1]);
out.vertices_.emplace_back(out.vertices_[triangle(i)]);
if (mesh.HasVertexColors()) {
out.vertex_colors_.emplace_back(
@@ -63,7 +66,8 @@ MeshWithPerVertexUVs(const geometry::TriangleMesh& mesh) {
}
vertex_remap[triangle(i)].emplace_back(
out.vertices_.size() - 1);
triangle(i) = static_cast<int>(out.vertices_.size() - 1);
triangle(i) =
static_cast<int>(out.vertices_.size() - 1);
}
}
}
@@ -73,7 +77,7 @@ MeshWithPerVertexUVs(const geometry::TriangleMesh& mesh) {
return {out, vertex_uvs};
}

} // namesapce detail
} // namespace detail

bool ReadModelUsingAssimp(const std::string& filename,
visualization::rendering::TriangleMeshModel& model,
@@ -94,8 +98,8 @@ bool ReadTriangleModel(const std::string& filename,
}

bool HasPerVertexUVs(const geometry::TriangleMesh& mesh) {
std::vector<Eigen::Vector2d> vertex_uvs(
mesh.vertices_.size(), Eigen::Vector2d(-1, -1));
std::vector<Eigen::Vector2d> vertex_uvs(mesh.vertices_.size(),
Eigen::Vector2d(-1, -1));
for (std::size_t tidx = 0; tidx < mesh.triangles_.size(); ++tidx) {
const auto& triangle = mesh.triangles_[tidx];
for (int i = 0; i < 3; ++i) {
@@ -112,33 +116,40 @@ bool HasPerVertexUVs(const geometry::TriangleMesh& mesh) {
return true;
}

bool WriteTriangleModel(const std::string& filename,
const visualization::rendering::TriangleMeshModel& model) {
bool WriteTriangleModel(
const std::string& filename,
const visualization::rendering::TriangleMeshModel& model) {
const std::string ext =
utility::filesystem::GetFileExtensionInLowerCase(filename);
// Validate model for output
for (const auto& mesh_info: model.meshes_) {
for (const auto& mesh_info : model.meshes_) {
if (!HasPerVertexUVs(*mesh_info.mesh)) {
utility::LogWarning("Cannot export model because mesh {} needs "
utility::LogWarning(
"Cannot export model because mesh {} needs "
"to be converted to have per-vertex uvs instead "
"of per-triangle uvs", mesh_info.mesh_name);
"of per-triangle uvs",
mesh_info.mesh_name);
return false;
}
auto mat_it = std::minmax_element(
mesh_info.mesh->triangle_material_ids_.begin(),
mesh_info.mesh->triangle_material_ids_.end());
if (mat_it.first != mat_it.second) {
utility::LogWarning("Cannot export model because mesh {} has more "
"than one material", mesh_info.mesh_name);
utility::LogWarning(
"Cannot export model because mesh {} has more "
"than one material",
mesh_info.mesh_name);
return false;
}
}

if (ext == "gltf" || ext == "glb") {
return WriteTriangleModelToGLTF(filename, model);
} else {
utility::LogWarning("Unsupported file format {}. "
"Currently only gltf and glb are supported", ext);
utility::LogWarning(
"Unsupported file format {}. "
"Currently only gltf and glb are supported",
ext);
return false;
}
}
9 changes: 5 additions & 4 deletions cpp/open3d/io/ModelIO.h
Original file line number Diff line number Diff line change
@@ -7,11 +7,10 @@

#pragma once

#include <Eigen/Core>
#include <functional>
#include <string>

#include <Eigen/Core>

namespace open3d {
namespace geometry {
class TriangleMesh;
@@ -55,11 +54,13 @@ bool ReadTriangleModel(const std::string& filename,
visualization::rendering::TriangleMeshModel& model,
ReadTriangleModelOptions params = {});

bool WriteTriangleModel(const std::string& filename,
bool WriteTriangleModel(
const std::string& filename,
const visualization::rendering::TriangleMeshModel& model);

// Implemented in FileGLTF.cpp
bool WriteTriangleModelToGLTF(const std::string& filename,
bool WriteTriangleModelToGLTF(
const std::string& filename,
const visualization::rendering::TriangleMeshModel& mesh_model);

} // namespace io
Loading