michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: "use strict"; michael@0: michael@0: function test() { michael@0: let v1 = vec3.create(); michael@0: michael@0: ok(v1, "Should have created a vector with vec3.create()."); michael@0: is(v1.length, 3, "A vec3 should have 3 elements."); michael@0: michael@0: ok(isApproxVec(v1, [0, 0, 0]), michael@0: "When created, a vec3 should have the values default to 0."); michael@0: michael@0: vec3.set([1, 2, 3], v1); michael@0: ok(isApproxVec(v1, [1, 2, 3]), michael@0: "The vec3.set() function didn't set the values correctly."); michael@0: michael@0: vec3.zero(v1); michael@0: ok(isApproxVec(v1, [0, 0, 0]), michael@0: "The vec3.zero() function didn't set the values correctly."); michael@0: michael@0: let v2 = vec3.create([4, 5, 6]); michael@0: ok(isApproxVec(v2, [4, 5, 6]), michael@0: "When cloning arrays, a vec3 should have the values copied."); michael@0: michael@0: let v3 = vec3.create(v2); michael@0: ok(isApproxVec(v3, [4, 5, 6]), michael@0: "When cloning vectors, a vec3 should have the values copied."); michael@0: michael@0: vec3.add(v2, v3); michael@0: ok(isApproxVec(v2, [8, 10, 12]), michael@0: "The vec3.add() function didn't set the x value correctly."); michael@0: michael@0: vec3.subtract(v2, v3); michael@0: ok(isApproxVec(v2, [4, 5, 6]), michael@0: "The vec3.subtract() function didn't set the values correctly."); michael@0: michael@0: vec3.negate(v2); michael@0: ok(isApproxVec(v2, [-4, -5, -6]), michael@0: "The vec3.negate() function didn't set the values correctly."); michael@0: michael@0: vec3.scale(v2, -2); michael@0: ok(isApproxVec(v2, [8, 10, 12]), michael@0: "The vec3.scale() function didn't set the values correctly."); michael@0: michael@0: vec3.normalize(v1); michael@0: ok(isApproxVec(v1, [0, 0, 0]), michael@0: "Normalizing a vector with zero length should return [0, 0, 0]."); michael@0: michael@0: vec3.normalize(v2); michael@0: ok(isApproxVec(v2, [ michael@0: 0.4558423161506653, 0.5698028802871704, 0.6837634444236755 michael@0: ]), "The vec3.normalize() function didn't set the values correctly."); michael@0: michael@0: vec3.cross(v2, v3); michael@0: ok(isApproxVec(v2, [ michael@0: 5.960464477539063e-8, -1.1920928955078125e-7, 5.960464477539063e-8 michael@0: ]), "The vec3.cross() function didn't set the values correctly."); michael@0: michael@0: vec3.dot(v2, v3); michael@0: ok(isApproxVec(v2, [ michael@0: 5.960464477539063e-8, -1.1920928955078125e-7, 5.960464477539063e-8 michael@0: ]), "The vec3.dot() function didn't set the values correctly."); michael@0: michael@0: ok(isApproxVec([vec3.length(v2)], [1.4600096599955427e-7]), michael@0: "The vec3.length() function didn't calculate the value correctly."); michael@0: michael@0: vec3.direction(v2, v3); michael@0: ok(isApproxVec(v2, [ michael@0: -0.4558422863483429, -0.5698028802871704, -0.6837634444236755 michael@0: ]), "The vec3.direction() function didn't set the values correctly."); michael@0: michael@0: vec3.lerp(v2, v3, 0.5); michael@0: ok(isApproxVec(v2, [ michael@0: 1.7720788717269897, 2.2150986194610596, 2.65811824798584 michael@0: ]), "The vec3.lerp() function didn't set the values correctly."); michael@0: michael@0: michael@0: vec3.project([100, 100, 10], [0, 0, 100, 100], michael@0: mat4.create(), mat4.perspective(45, 1, 0.1, 100), v1); michael@0: ok(isApproxVec(v1, [-1157.10693359375, 1257.10693359375, 0]), michael@0: "The vec3.project() function didn't set the values correctly."); michael@0: michael@0: vec3.unproject([100, 100, 1], [0, 0, 100, 100], michael@0: mat4.create(), mat4.perspective(45, 1, 0.1, 100), v1); michael@0: ok(isApproxVec(v1, [ michael@0: 41.420406341552734, -41.420406341552734, -99.99771118164062 michael@0: ]), "The vec3.project() function didn't set the values correctly."); michael@0: michael@0: michael@0: let ray = vec3.createRay([10, 10, 0], [100, 100, 1], [0, 0, 100, 100], michael@0: mat4.create(), mat4.perspective(45, 1, 0.1, 100)); michael@0: michael@0: ok(isApproxVec(ray.origin, [ michael@0: -0.03313708305358887, 0.03313708305358887, -0.1000000014901161 michael@0: ]), "The vec3.createRay() function didn't create the position correctly."); michael@0: ok(isApproxVec(ray.direction, [ michael@0: 0.35788586614428364, -0.35788586614428364, -0.862458934459091 michael@0: ]), "The vec3.createRay() function didn't create the direction correctly."); michael@0: michael@0: michael@0: is(vec3.str([0, 0, 0]), "[0, 0, 0]", michael@0: "The vec3.str() function didn't work properly."); michael@0: }