Skip to content

vecX.transformMatY over arrays #137

@skrat

Description

@skrat

Would it be useful to include versions of vec3.transformMat4 and friends, that operate over array of vectors? Ie. one big array containing many vectors (a matrix). I'm often implementing this and would really help to have it in a library. For this to work and be flexible, all is needed is another version of those functions, that takes an offset into the input array, eg.

/**
 * Transforms the vec3 with a mat4.
 * 4th vector component is implicitly '1'
 *
 * @param {vec3} out the receiving vector
 * @param {vec3} a the vector to transform
 * @param {mat4} m matrix to transform with
 * @param {number} offset offset into input and output arrays
 * @returns {vec3} out
 */
vec3.transformMat4 = function(out, a, m, offset) {
    var x = a[0 + offset], y = a[1 + offset], z = a[2 + offset],
        w = m[3] * x + m[7] * y + m[11] * z + m[15];
    w = w || 1.0;
    out[0 + offset] = (m[0] * x + m[4] * y + m[8] * z + m[12]) / w;
    out[1 + offset] = (m[1] * x + m[5] * y + m[9] * z + m[13]) / w;
    out[2 + offset] = (m[2] * x + m[6] * y + m[10] * z + m[14]) / w;
    return out;
};

I tried to use the standard transformMat4 + using typed arrays' .subarray method, but that's ridiculously slower than using offsets.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions