diff --git a/spec/gl-matrix/mat3-spec.js b/spec/gl-matrix/mat3-spec.js index 66184208..cfd8ed14 100644 --- a/spec/gl-matrix/mat3-spec.js +++ b/spec/gl-matrix/mat3-spec.js @@ -83,6 +83,21 @@ describe("mat3", function() { }); }); + describe("fromEuler", function() { + beforeEach(function() { + let row = 0; + let pitch = 0; + let yaw = 0; + result = mat3.fromEuler(out, row, pitch, yaw); + }); + + it("should return out", function() { expect(result).toBe(out); }); + + it("should place a 3x3 identity matrix into out", function() { + expect(out).toBeEqualish(identity); + }); + }); + describe("fromMat4", function() { beforeEach(function() { result = mat3.fromMat4(out, [ 1, 2, 3, 4, diff --git a/src/mat3.js b/src/mat3.js index 3f1ed4f6..17e9e940 100644 --- a/src/mat3.js +++ b/src/mat3.js @@ -573,6 +573,39 @@ export function fromQuat(out, q) { return out; } +/** + * Calculates a 3x3 matrix from the given euler angles + * + * @param {mat3} out mat3 receiving operation result + * @param {Number} roll Roll angle to create matrix from + * @param {Number} pitch Pitch angle to create matrix from + * @param {Number} yaw Yaw angle to create matrix from + * + * @returns {mat3} out + */ +export function fromEuler(out, roll, pitch, yaw) { + const cp = Math.cos(pitch); + const sp = Math.sin(pitch); + const sr = Math.sin(roll); + const cr = Math.cos(roll); + const sy = Math.sin(yaw); + const cy = Math.cos(yaw); + + out[0] = cp * cy; + out[1] = (sr * sp * cy) - (cr * sy); + out[2] = (cr * sp * cy) + (sr * sy); + + out[3] = cp * sy; + out[4] = (sr * sp * sy) + (cr * cy); + out[5] = (cr * sp * sy) - (sr * cy); + + out[6] = -sp; + out[7] = sr * cp; + out[8] = cr * cp; + + return out; +} + /** * Calculates a 3x3 normal matrix (transpose inverse) from the 4x4 matrix *