# generateTangents function

void generateTangents(
1. Vector4List tangents,
2. Vector3List positions,
3. Vector3List normals,
4. Vector2List texCoords,
5. Uint16List indices
)

Generate vertex tangents based on a list of `positions`, `normals`, `texCoords` and `indices`. `tangents` is assumed to be zeroed out, and much match `positions`, `normals`, and `texCoords` in length. `indices` is assumed to represent a triangle list. Tangents are returned as Vector4s. The X, Y, and Z component represent the tangent and the W component represents the direction of the bitangent which can be generated as: vec4 bitangent = cross(normal, tangent.xyz) * tangent.w; Derived from the granddaddy of all tangent generation functions: http://www.terathon.com/code/tangent.html

## Implementation

``````void generateTangents(Vector4List tangents, Vector3List positions,
Vector3List normals, Vector2List texCoords, Uint16List indices) {
final p0 = Vector3.zero(),
p1 = Vector3.zero(),
p2 = Vector3.zero(),
n = Vector3.zero(),
t = Vector3.zero(),
udir = Vector3.zero(),
vdir = Vector3.zero();

final uv0 = Vector2.zero(), uv1 = Vector2.zero(), uv2 = Vector2.zero();

final tan = Vector4.zero();

final tan0 = Vector3List(positions.length),
tan1 = Vector3List(positions.length);

for (var i = 0; i < indices.length; i += 3) {
final i0 = indices[i], i1 = indices[i + 1], i2 = indices[i + 2];
positions

texCoords

p1.sub(p0);
p2.sub(p0);

uv1.sub(uv0);
uv2.sub(uv0);

final r = 1.0 / (uv1.x * uv2.y - uv2.x * uv1.y);

udir.setValues((uv2.y * p1.x - uv1.y * p2.x) * r,
(uv2.y * p1.y - uv1.y * p2.y) * r, (uv2.y * p1.z - uv1.y * p2.z) * r);
vdir.setValues((uv1.x * p2.x - uv2.x * p1.x) * r,
(uv1.x * p2.y - uv2.x * p1.y) * r, (uv1.x * p2.z - uv2.x * p1.z) * r);

}

for (var i = 0; i < tangents.length; ++i) {

p1
..setFrom(n)
..scale(n.dot(t));
p0
..setFrom(t)
..sub(p1)
..normalize();