generateTangents function

void generateTangents (Vector4List tangents, Vector3List positions, Vector3List normals, Vector2List texCoords, 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 Vector3 p0 = new Vector3.zero(),
p1 = new Vector3.zero(),
p2 = new Vector3.zero(),
n = new Vector3.zero(),
t = new Vector3.zero(),
udir = new Vector3.zero(),
vdir = new Vector3.zero();

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

final Vector4 tan = new Vector4.zero();

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

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

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

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

final double 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 (int i = 0; i < tangents.length; ++i) {

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