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

Add utility for computing tangents #532

Closed
prideout opened this issue Nov 27, 2018 · 4 comments
Closed

Add utility for computing tangents #532

prideout opened this issue Nov 27, 2018 · 4 comments
Assignees
Labels
enhancement New feature or request

Comments

@prideout
Copy link
Contributor

Per discussion with Mathias and Aaron, we'd like to add a static utility method to VertexBuffer that makes it easier for clients to supply a buffer for TANGENTS.

It could look like this:

/**
 * Convenience function that consumes normal vectors (and, optionally, tangent vectors)
 * and produces quaternions that can be passed into a TANGENTS buffer.
 *
 * The given target buffer must be preallocated with at least count * sizeof(math::quat) bytes.
 *
 * Normals are required but tangents are optional, in which case this function tries to generate
 * reasonable tangents. The given normals should be unit length.
 *
 * If supplied, the tangent vectors should be unit length and should be orthogonal to the normals.
 * The w component of the tangent is a sign (-1 or +1) indicating handedness of the basis.
 *
 * Note that some applications and file formats (e.g. Blender and glTF) use mikktspace, which
 * requires full topology information and therefore cannot be computed here. This function
 * exists for simple use cases only.
 */
static BufferDescriptor&& populateTangentQuaternions(BufferDescriptor&& targetBuffer,
        size_t count,
        const math::float3* normals,
        size_t normalsStride = 0,
        const math::float4* tangents = nullptr,
        size_t tangentsStride = 0);
@prideout prideout self-assigned this Nov 27, 2018
@prideout prideout added the enhancement New feature or request label Nov 27, 2018
@romainguy
Copy link
Collaborator

That makes sense. Note that we also have a public function to compute the quaternion tangent frame from a TBN matrix. It's in mat3.h.

quatf q = mat3f::packTangentFrame({tangent, bitangent, normal});

@bejado
Copy link
Member

bejado commented Nov 27, 2018

What algorithm will the function use to generate the tangents if only normals are provided?

@prideout
Copy link
Contributor Author

We'll simply have a fixed random vector like (0,1,0) and cross the normal with it, a bit like this although we should probably check for when the random vector is too close to the normal, in which case we'd choose a different random vector.

@romainguy
Copy link
Collaborator

MeshAssimp.cpp does that (minus switching vector when too close to the normal)

prideout added a commit that referenced this issue Nov 27, 2018
I tested this by writing a quick-and-dirty Sphere demo.
prideout added a commit that referenced this issue Nov 27, 2018
I tested this by writing a quick-and-dirty Sphere demo.
prideout added a commit that referenced this issue Nov 27, 2018
I tested this by writing a quick-and-dirty Sphere demo.
AdrianAtGoogle pushed a commit to AdrianAtGoogle/filament that referenced this issue Jan 30, 2019
I tested this by writing a quick-and-dirty Sphere demo.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants