Skip to content

Tangent Space and Ambient Occlusion - #7364

Merged
ssheorey merged 17 commits into
mainfrom
ss/ao
May 19, 2026
Merged

ssheorey merged 17 commits into
mainfrom
ss/ao

Conversation

@ssheorey

@ssheorey ssheorey commented Nov 7, 2025 •

Copy link
Copy Markdown
Member

Type

  • Bug fix (non-breaking change which fixes an issue): Fixes #
  • New feature (non-breaking change which adds functionality). Resolves #
  • Breaking change (fix or feature that would cause existing functionality to not work as expected) Resolves #

Motivation and Context

Normal maps are saved in tangent space. This PR adds functions to compute the tangent space of a mesh and convert a normal map between world space and tangent space.
Ambient occlusion allows meshes to be visualized properly without any color information. It can also be used for simple de-lighting.

Checklist:

  • I have run python util/check_style.py --apply to apply Open3D code style
    to my code.
  • This PR changes Open3D behavior or adds new functionality.
    • Both C++ (Doxygen) and Python (Sphinx / Google style) documentation is
      updated accordingly.
    • I have added or updated C++ and / or Python unit tests OR included test
      results
      (e.g. screenshots or numbers) here.
  • I will follow up and update the code if CI fails.
  • For fork PRs, I have selected Allow edits from maintainers.

Description

This PR adds support for tangent space computation and ambient occlusion rendering to triangle meshes. The key changes include implementing MikkTSpace for tangent space calculation, enabling normal map transformations between world and tangent space, and adding ambient occlusion texture generation using ray casting.

Key changes:

  • Integration of MikkTSpace library for computing tangent space vectors
  • New methods for ambient occlusion computation and normal map transformation
  • Addition of quasirandom sequence generation and cross product operations for tensor math

Baked Tangents
baked_tangents
Baked normals
baked_normals
Original World space normal map
world_normal_map

[Computed] Tangent space normal map
tangent_normal_map

[Computed] Restored world space normal map
world_normal_map_restored

AO for torus (64 samples, infinity hit)
Screenshot 2026-05-17 at 01-29-06 glTF Sample Viewer

@update-docs

update-docs Bot commented Nov 7, 2025

Copy link
Copy Markdown

Thanks for submitting this pull request! The maintainers of this repository would appreciate if you could update the CHANGELOG.md based on your changes.

@ssheorey
ssheorey requested a review from Copilot November 7, 2025 00:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for tangent space computation and ambient occlusion rendering to triangle meshes. The key changes include implementing MikkTSpace for tangent space calculation, enabling normal map transformations between world and tangent space, and adding ambient occlusion texture generation using ray casting.

Key changes:

  • Integration of MikkTSpace library for computing tangent space vectors
  • New methods for ambient occlusion computation and normal map transformation
  • Addition of quasirandom sequence generation and cross product operations for tensor math
  • Wayland compatibility fixes for GLFW window positioning

Reviewed Changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
cpp/open3d/t/geometry/TriangleMesh.cpp Implements ComputeTangentSpace(), TransformNormalMap(), and ComputeAmbientOcclusion() methods
cpp/open3d/t/geometry/kernel/mikktspace.h Adds MikkTSpace library header for tangent space computation
cpp/open3d/t/geometry/kernel/mikktspace.c Adds MikkTSpace library implementation (1899 lines)
cpp/open3d/core/Tensor.cpp Implements Quasirandom(), Cross(), and Norm() tensor operations
cpp/tests/t/geometry/TriangleMesh.cpp Adds comprehensive tests for tangent space and ambient occlusion
cpp/pybind/t/geometry/trianglemesh.cpp Exposes new mesh methods to Python bindings
cpp/open3d/visualization/visualizer/Visualizer.cpp Adds Wayland platform checks for window positioning
cpp/open3d/t/io/file_format/FileASSIMP.cpp Updates texture export to include ambient occlusion maps

πŸ’‘ Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cpp/open3d/core/Tensor.h Resolved
Comment thread cpp/open3d/core/Tensor.h Resolved
Comment thread cpp/open3d/core/Tensor.h Resolved
Comment thread cpp/open3d/core/Tensor.h Resolved
Comment thread cpp/open3d/core/Tensor.cpp Resolved
Comment on lines +168 to +169
// bool visible = !(flags & FLAG_HIDDEN);
glfwWindowHint(GLFW_VISIBLE, GLFW_TRUE);

Copilot AI Nov 7, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commented-out code for handling window visibility should either be removed or replaced with a TODO comment explaining why it was disabled. Leaving dead code without explanation reduces maintainability.

Copilot uses AI. Check for mistakes.
Comment on lines +195 to +199
//if (!visible) {
// glfwHideWindow(glfw_window);
//} else {
// glfwShowWindow(glfw_window);
//}

Copilot AI Nov 7, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Large block of commented-out code without explanation. If this code is no longer needed, it should be removed. If it's kept for reference, add a comment explaining why it's disabled.

Suggested change
//if (!visible) {
// glfwHideWindow(glfw_window);
//} else {
// glfwShowWindow(glfw_window);
//}

Copilot uses AI. Check for mistakes.
@ssheorey ssheorey added this to the v0.20 milestone Mar 4, 2026
@ssheorey
ssheorey requested a review from Copilot May 17, 2026 06:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 16 out of 16 changed files in this pull request and generated 15 comments.

Comments suppressed due to low confidence (1)

cpp/open3d/core/Tensor.cpp:1

  • Broadcasting + axis handling is inconsistent when tensors have different ranks. Example: a shape (3,) and b shape (N,3) with default axis=-1: Slice(axis,...) on a resolves to axis 0, but on dst_tensor (rank 2) resolves to axis 1, leading to incorrect slicing or runtime errors. Consider normalizing axis relative to the broadcasted/output ndim and mapping it consistently for both inputs (e.g., treat inputs as left-padded with 1s and adjust the axis accordingly), or move the target axis to the last dimension, compute, then move it back.
// ----------------------------------------------------------------------------

Comment thread cpp/tests/t/geometry/TriangleMesh.cpp Outdated
Comment on lines +1622 to +1629
// Visual inspection.
t::io::WriteImage("baked_tangents.png",
mesh.GetMaterial()
.GetTextureMap("tangents")
.To(core::UInt8, false, 127.5, 127.5));
t::io::WriteImage("baked_normals.png",
mesh.GetMaterial().GetTextureMap("normals").To(
core::UInt8, false, 127.5, 127.5));
Comment thread cpp/tests/t/geometry/TriangleMesh.cpp Outdated
Comment on lines +1695 to +1696
t::io::WriteTriangleMesh("torus_ao.glb", mesh);
t::io::WriteImage("torus_ao_texture.png", mesh.GetMaterial().GetAOMap());
Comment thread cpp/open3d/t/geometry/TriangleMesh.cpp Resolved
Comment on lines +1911 to +1923
// Bake positions and normals into textures.
const float margin =
(tex_width + 511.f) / 512.f; // = ceil(tex_width / 512)
auto baked_textures =
BakeVertexAttrTextures(tex_width, {"positions", "normals"}, margin,
/*fill=*/0.0, /*update_material=*/false);
Tensor position_map =
baked_textures["positions"].To(GetDevice(), core::Float32);
Tensor normal_map =
baked_textures["normals"].To(GetDevice(), core::Float32);

RaycastingScene rcs;
rcs.AddTriangles(*this);
Comment thread cpp/open3d/t/geometry/TriangleMesh.cpp Outdated
Tensor valid_positions = positions.IndexGet({valid_indices});
Tensor valid_normals = normals.IndexGet({valid_indices});

Tensor origins = valid_positions.Expand({n_rays, n_valid_pixels, 3});
Comment thread cpp/open3d/t/geometry/TriangleMesh.h Resolved
Comment thread cpp/open3d/core/Tensor.h Resolved
Comment thread cpp/open3d/core/Tensor.h Resolved
Comment on lines +168 to +169
// bool visible = !(flags & FLAG_HIDDEN);
glfwWindowHint(GLFW_VISIBLE, GLFW_TRUE);
std::string ToString() const;

/// Returns the texture map map
/// Returns the texture maps
@ssheorey
ssheorey requested a review from Copilot May 17, 2026 09:36
@ssheorey
ssheorey marked this pull request as ready for review May 17, 2026 09:43
@ssheorey
ssheorey requested a review from benjaminum May 17, 2026 09:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 15 out of 15 changed files in this pull request and generated 16 comments.

Comments suppressed due to low confidence (1)

cpp/open3d/t/geometry/TriangleMesh.h:1

  • The new API docs are inconsistent with the implementation: ComputeTangentSpace() only sets a tangents attribute (N,4) and does not add a bitangents vertex attribute, and TransformNormalMap() does not require a precomputed bitangents attribute (it reconstructs B from the tangent sign and cross(N, T)). Update the docstrings to match actual requirements/outputs (e.g., document the (N,4) tangents-with-sign convention and remove the bitangents requirement/claim).
// ----------------------------------------------------------------------------

Comment thread cpp/open3d/t/geometry/TriangleMesh.cpp Resolved
Comment thread cpp/open3d/t/geometry/TriangleMesh.cpp Resolved
Comment thread cpp/open3d/t/geometry/TriangleMesh.cpp Resolved
Comment thread cpp/open3d/t/geometry/TriangleMesh.cpp Resolved
Comment thread cpp/open3d/t/geometry/TriangleMesh.cpp Resolved
Comment thread cpp/pybind/core/tensor.cpp Resolved
Comment thread cpp/pybind/core/tensor.cpp Resolved
Comment thread cpp/pybind/core/tensor.cpp Resolved
Comment thread cpp/pybind/core/tensor.cpp Resolved
Comment thread cpp/pybind/core/tensor.cpp Resolved
Switch to tbb parallel reduce, for export depth image, since AppleClang lacks execution policy.
@ssheorey
ssheorey merged commit 5c35475 into main May 19, 2026
32 of 33 checks passed
@ssheorey
ssheorey deleted the ss/ao branch May 19, 2026 23:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants