diff --git a/cocos/core/vmath/quat.js b/cocos/core/vmath/quat.js deleted file mode 100644 index d5694a765c..0000000000 --- a/cocos/core/vmath/quat.js +++ /dev/null @@ -1,834 +0,0 @@ -import vec3 from './vec3'; -import vec4 from './vec4'; -import mat3 from './mat3'; -import { toDegree } from './utils'; - -let halfToRad = 0.5 * Math.PI / 180.0; - -/** - * Mathematical quaternion. - * - * A quaternion is a hypercomplex number represented by w + xi + yj + zk, where - * x, y, z and w are real numbers(called here its components), and i, j, and k are the fundamental quaternion units. - */ -class quat { - /** - * Creates a quaternion, with components specified separately. - * - * @param {number} x - Value assigned to x component. - * @param {number} y - Value assigned to y component. - * @param {number} z - Value assigned to z component. - * @param {number} w - Value assigned to w component. - */ - constructor(x = 0, y = 0, z = 0, w = 1) { - /** - * The x component. - * @type {number} - * */ - this.x = x; - - /** - * The y component. - * @type {number} - * */ - this.y = y; - - /** - * The z component. - * @type {number} - * */ - this.z = z; - - /** - * The w component. - * @type {number} - * */ - this.w = w; - } - - /** - * Creates a quaternion, with components specified separately. - * - * @param {number} x - Value assigned to x component. - * @param {number} y - Value assigned to y component. - * @param {number} z - Value assigned to z component. - * @param {number} w - Value assigned to w component. - * @return {quat} The newly created quaternion. - */ - static create(x = 0, y = 0, z = 0, w = 1) { - return new quat(x, y, z, w); - } - - /** - * Clone a quaternion. - * - * @param {quat} a - Quaternion to clone. - * @returns {quat} The newly created quaternion. - */ - static clone(a) { - return new quat(a.x, a.y, a.z, a.w); - } - - /** - * Copy content of a quaternion into another. - * - * @param {quat} out - Quaternion to modified. - * @param {quat} a - The specified quaternion. - * @returns {quat} out. - */ - static copy(out, a) { - return vec4.copy(out, a); - } - - /** - * Sets the components of a quaternion to the given values. - * - * @param {quat} out - The quaternion to modified. - * @param {Number} x - Value set to x component. - * @param {Number} y - Value set to y component. - * @param {Number} z - Value set to z component. - * @param {Number} w - Value set to w component. - * @returns {quat} out. - */ - static set(out, x, y, z, w) { - out.x = x; - out.y = y; - out.z = z; - out.w = w; - return out; - } - - /** - * Sets a quaternion as identity quaternion. - * - * @param {quat} out - Quaternion to set. - * @returns {quat} out. - */ - static identity(out) { - out.x = 0; - out.y = 0; - out.z = 0; - out.w = 1; - return out; - } - - /** - * Sets a quaternion to represent the shortest rotation from one - * vector to another. - * - * Both vectors are assumed to be unit length. - * - * @param {quat} out - Quaternion to set. - * @param {vec3} a - The initial vector. - * @param {vec3} b - The destination vector. - * @returns {quat} out. - */ - static rotationTo(out, a, b) { - let rotationToIIFE = (function () { - let tmpvec3 = vec3.create(0, 0, 0); - let xUnitVec3 = vec3.create(1, 0, 0); - let yUnitVec3 = vec3.create(0, 1, 0); - - return function (out, a, b) { - let dot = vec3.dot(a, b); - if (dot < -0.999999) { - vec3.cross(tmpvec3, xUnitVec3, a); - if (vec3.magnitude(tmpvec3) < 0.000001) { - vec3.cross(tmpvec3, yUnitVec3, a); - } - vec3.normalize(tmpvec3, tmpvec3); - quat.fromAxisAngle(out, tmpvec3, Math.PI); - return out; - } else if (dot > 0.999999) { - out.x = 0; - out.y = 0; - out.z = 0; - out.w = 1; - return out; - } else { - vec3.cross(tmpvec3, a, b); - out.x = tmpvec3.x; - out.y = tmpvec3.y; - out.z = tmpvec3.z; - out.w = 1 + dot; - return quat.normalize(out, out); - } - }; - })(); - - return rotationToIIFE(out, a, b); - } - - /** - * Gets the rotation axis and angle for a given - * quaternion. If a quaternion is created with - * fromAxisAngle, this method will return the same - * values as provided in the original parameter list - * OR functionally equivalent values. - * Example: The quaternion formed by axis [0, 0, 1] and - * angle -90 is the same as the quaternion formed by - * [0, 0, 1] and 270. This method favors the latter. - * @param {vec3} out_axis - Vector to store the rotation axis. - * @param {quat} q - Quaternion to be decomposed. - * @return {Number} - Angle, in radians, of the rotation. - */ - static getAxisAngle(out_axis, q) { - let rad = Math.acos(q.w) * 2.0; - let s = Math.sin(rad / 2.0); - if (s != 0.0) { - out_axis.x = q.x / s; - out_axis.y = q.y / s; - out_axis.z = q.z / s; - } else { - // If s is zero, return any axis (no rotation - axis does not matter) - out_axis.x = 1; - out_axis.y = 0; - out_axis.z = 0; - } - return rad; - } - - /** - * Multiply two quaternions. - * - * @param {quat} out - Quaternion to store result. - * @param {quat} a - The first operand. - * @param {quat} b - The second operand. - * @returns {quat} out. - */ - static multiply(out, a, b) { - let ax = a.x, ay = a.y, az = a.z, aw = a.w, - bx = b.x, by = b.y, bz = b.z, bw = b.w; - - out.x = ax * bw + aw * bx + ay * bz - az * by; - out.y = ay * bw + aw * by + az * bx - ax * bz; - out.z = az * bw + aw * bz + ax * by - ay * bx; - out.w = aw * bw - ax * bx - ay * by - az * bz; - return out; - } - - /** - * Alias of {@link quat.multiply}. - */ - static mul(out, a, b) { - return quat.multiply(out, a, b); - } - - /** - * Scales a quaternion with a number. - * - * @param {quat} out - Quaternion to store result. - * @param {quat} a - Quaternion to scale. - * @param {quat} b - The scale number. - * @returns {quat} out. - * */ - static scale(out, a, b) { - out.x = a.x * b; - out.y = a.y * b; - out.z = a.z * b; - out.w = a.w * b; - return out; - } - - /** - * Rotates a quaternion by the given angle about the X axis. - * - * @param {quat} out - Quaternion to store result. - * @param {quat} a - Quaternion to rotate. - * @param {number} rad - Angle (in radians) to rotate. - * @returns {quat} out. - */ - static rotateX(out, a, rad) { - rad *= 0.5; - - let ax = a.x, ay = a.y, az = a.z, aw = a.w, - bx = Math.sin(rad), bw = Math.cos(rad); - - out.x = ax * bw + aw * bx; - out.y = ay * bw + az * bx; - out.z = az * bw - ay * bx; - out.w = aw * bw - ax * bx; - return out; - } - - /** - * Rotates a quaternion by the given angle about the Y axis. - * - * @param {quat} out - Quaternion to store result. - * @param {quat} a - Quaternion to rotate. - * @param {number} rad - Angle (in radians) to rotate. - * @returns {quat} out. - */ - static rotateY(out, a, rad) { - rad *= 0.5; - - let ax = a.x, ay = a.y, az = a.z, aw = a.w, - by = Math.sin(rad), bw = Math.cos(rad); - - out.x = ax * bw - az * by; - out.y = ay * bw + aw * by; - out.z = az * bw + ax * by; - out.w = aw * bw - ay * by; - return out; - } - - /** - * Rotates a quaternion by the given angle about the Z axis. - * - * @param {quat} out - Quaternion to store result. - * @param {quat} a - Quaternion to rotate. - * @param {number} rad - Angle (in radians) to rotate. - * @returns {quat} out. - */ - static rotateZ(out, a, rad) { - rad *= 0.5; - - let ax = a.x, ay = a.y, az = a.z, aw = a.w, - bz = Math.sin(rad), bw = Math.cos(rad); - - out.x = ax * bw + ay * bz; - out.y = ay * bw - ax * bz; - out.z = az * bw + aw * bz; - out.w = aw * bw - az * bz; - return out; - } - - /** - * Rotates a quaternion by the given angle about a world space axis. - * - * @param {quat} out - Quaternion to store result. - * @param {quat} rot - Quaternion to rotate. - * @param {vec3} axis - The axis around which to rotate in world space. - * @param {number} rad - Angle (in radians) to rotate. - * @returns {quat} out. - */ - static rotateAround(out, rot, axis, rad) { - let rotateAroundIIFE = (function () { - let v3_tmp = vec3.create(0, 0, 0); - let q_tmp = quat.create(); - - return function (out, rot, axis, rad) { - // get inv-axis (local to rot) - quat.invert(q_tmp, rot); - vec3.transformQuat(v3_tmp, axis, q_tmp); - // rotate by inv-axis - quat.fromAxisAngle(q_tmp, v3_tmp, rad); - quat.mul(out, rot, q_tmp); - - return out; - }; - })(); - return rotateAroundIIFE(out, rot, axis, rad); - } - - /** - * Rotates a quaternion by the given angle about a local space axis. - * - * @param {quat} out - Quaternion to store result. - * @param {quat} rot - Quaternion to rotate. - * @param {vec3} axis - The axis around which to rotate in local space. - * @param {number} rad - Angle (in radians) to rotate. - * @returns {quat} out. - */ - static rotateAroundLocal(out, rot, axis, rad) { - let rotateAroundLocalIIFE = (function () { - let q_tmp = quat.create(); - - return function (out, rot, axis, rad) { - quat.fromAxisAngle(q_tmp, axis, rad); - quat.mul(out, rot, q_tmp); - - return out; - }; - })(); - - return rotateAroundLocalIIFE(out, rot, axis, rad); - } - - /** - * Calculates the W component of a quaternion from the X, Y, and Z components. - * Assumes that quaternion is 1 unit in length. - * Any existing W component will be ignored. - * - * @param {quat} out - Quaternion to store result. - * @param {quat} a - Quaternion to calculate W. - * @returns {quat} out. - */ - static calculateW(out, a) { - let x = a.x, y = a.y, z = a.z; - - out.x = x; - out.y = y; - out.z = z; - out.w = Math.sqrt(Math.abs(1.0 - x * x - y * y - z * z)); - return out; - } - - /** - * Calculates the dot product of two quaternions. - * - * @param {quat} a - The first operand. - * @param {quat} b - The second operand. - * @returns {Number} - The dot product of a and b. - */ - static dot(a, b) { - return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; - } - - /** - * Performs a linear interpolation between two quaternions. - * - * @param {quat} out - Quaternion to store result. - * @param {quat} a - The first operand. - * @param {quat} b - The second operand. - * @param {Number} t - The interpolation coefficient. - * @returns {quat} out. - */ - static lerp(out, a, b, t) { - let ax = a.x, - ay = a.y, - az = a.z, - aw = a.w; - out.x = ax + t * (b.x - ax); - out.y = ay + t * (b.y - ay); - out.z = az + t * (b.z - az); - out.w = aw + t * (b.w - aw); - return out; - } - - /** - * Performs a spherical linear interpolation between two quaternions. - * - * @param {quat} out - Quaternion to store result. - * @param {quat} a - The first operand. - * @param {quat} b - The second operand. - * @param {Number} t - The interpolation coefficient. - * @returns {quat} out. - */ - static slerp(out, a, b, t) { - // benchmarks: - // http://jsperf.com/quaternion-slerp-implementations - - let ax = a.x, ay = a.y, az = a.z, aw = a.w, - bx = b.x, by = b.y, bz = b.z, bw = b.w; - - let omega, cosom, sinom, scale0, scale1; - - // calc cosine - cosom = ax * bx + ay * by + az * bz + aw * bw; - // adjust signs (if necessary) - if (cosom < 0.0) { - cosom = -cosom; - bx = -bx; - by = -by; - bz = -bz; - bw = -bw; - } - // calculate coefficients - if ((1.0 - cosom) > 0.000001) { - // standard case (slerp) - omega = Math.acos(cosom); - sinom = Math.sin(omega); - scale0 = Math.sin((1.0 - t) * omega) / sinom; - scale1 = Math.sin(t * omega) / sinom; - } else { - // "from" and "to" quaternions are very close - // ... so we can do a linear interpolation - scale0 = 1.0 - t; - scale1 = t; - } - // calculate final values - out.x = scale0 * ax + scale1 * bx; - out.y = scale0 * ay + scale1 * by; - out.z = scale0 * az + scale1 * bz; - out.w = scale0 * aw + scale1 * bw; - - return out; - } - - /** - * Performs a spherical linear interpolation with two control points. - * - * @param {quat} out - Quaternion to store result. - * @param {quat} a - The first operand. - * @param {quat} b - The second operand. - * @param {quat} c - The third operand. - * @param {quat} d - The fourth operand. - * @param {Number} t - The interpolation coefficient. - * @returns {quat} out - */ - static sqlerp(out, a, b, c, d, t) { - let sqlerpIIFE = (function () { - let temp1 = quat.create(); - let temp2 = quat.create(); - - return function (out, a, b, c, d, t) { - quat.slerp(temp1, a, d, t); - quat.slerp(temp2, b, c, t); - quat.slerp(out, temp1, temp2, 2 * t * (1 - t)); - - return out; - }; - }()); - return sqlerpIIFE(out, a, b, c, d, t); - } - - /** - * Calculates the inverse of a quaternion. - * - * @param {quat} out - Quaternion to store result. - * @param {quat} a - Quaternion to calculate inverse of. - * @returns {quat} out. - */ - static invert(out, a) { - let a0 = a.x, a1 = a.y, a2 = a.z, a3 = a.w; - let dot = a0 * a0 + a1 * a1 + a2 * a2 + a3 * a3; - let invDot = dot ? 1.0 / dot : 0; - - // TODO: Would be faster to return [0,0,0,0] immediately if dot == 0 - - out.x = -a0 * invDot; - out.y = -a1 * invDot; - out.z = -a2 * invDot; - out.w = a3 * invDot; - return out; - } - - /** - * Calculates the conjugate of a quaternion. - * If the quaternion is normalized, this function is faster than quat.inverse and produces the same result. - * - * @param {quat} out - Quaternion to store result. - * @param {quat} a - Quaternion to calculate conjugate of. - * @returns {quat} out. - */ - static conjugate(out, a) { - out.x = -a.x; - out.y = -a.y; - out.z = -a.z; - out.w = a.w; - return out; - } - - /** - * Calculates the length of a quaternion. - * - * @param {quat} a - The quaternion. - * @returns {Number} Length of the quaternion. - */ - static magnitude(a) { - let x = a.x, - y = a.y, - z = a.z, - w = a.w; - return Math.sqrt(x * x + y * y + z * z + w * w); - } - - /** - *Alias of {@link quat.magnitude}. - */ - static mag(a) { - return quat.magnitude(a); - } - - /** - * Calculates the squared length of a quaternion. - * - * @param {quat} a - The quaternion. - * @returns {Number} Squared length of the quaternion. - */ - static squaredMagnitude(a) { - let x = a.x, - y = a.y, - z = a.z, - w = a.w; - return x * x + y * y + z * z + w * w; - } - - /** - *Alias of {@link quat.squaredMagnitude} - */ - static sqrMag(a) { - return quat.squaredMagnitude(a); - } - - /** - * Normalizes a quaternion. - * - * @param {quat} out - Quaternion to store result. - * @param {quat} a - Quaternion to normalize. - * @returns {quat} out. - * @function - */ - static normalize(out, a) { - let x = a.x, - y = a.y, - z = a.z, - w = a.w; - let len = x * x + y * y + z * z + w * w; - if (len > 0) { - len = 1 / Math.sqrt(len); - out.x = x * len; - out.y = y * len; - out.z = z * len; - out.w = w * len; - } - return out; - } - - /** - * Sets the specified quaternion with values corresponding to the given - * axes. Each axis is a vec3 and is expected to be unit length and - * perpendicular to all other specified axes. - * - * @param {quat} out - Quaternion to store result. - * @param {vec3} xAxis - Vector representing the local "right" direction. - * @param {vec3} yAxis - Vector representing the local "up" direction. - * @param {vec3} zAxis - Vector representing the viewing direction. - * @returns {quat} out. - */ - static fromAxes(out, xAxis, yAxis, zAxis) { - let fromAxesIIFE = (function () { - let matr = mat3.create(); - - return function (out, xAxis, yAxis, zAxis) { - mat3.set(matr, - xAxis.x, xAxis.y, xAxis.z, - yAxis.x, yAxis.y, yAxis.z, - zAxis.x, zAxis.y, zAxis.z - ); - return quat.normalize(out, quat.fromMat3(out, matr)); - }; - })(); - return fromAxesIIFE(out, xAxis, yAxis, zAxis); - } - - /** - * Calculates a quaternion from view direction and up direction - * - * @param {quat} out - Quaternion to store result. - * @param {vec3} view - View direction (must be normalized). - * @param {vec3} [up] - Up direction, default is (0,1,0) (must be normalized). - * - * @returns {quat} out. - */ - static fromViewUp(out, view, up) { - let fromViewUpIIFE = (function () { - let matr = mat3.create(); - - return function (out, view, up) { - mat3.fromViewUp(matr, view, up); - if (!matr) { - return null; - } - - return quat.normalize(out, quat.fromMat3(out, matr)); - }; - })(); - return fromViewUpIIFE(out, view, up); - } - - /** - * Sets a quaternion from the given angle and rotation axis, - * then returns it. - * - * @param {quat} out - Quaternion to store result. - * @param {vec3} axis - The axis around which to rotate. - * @param {Number} rad - The angle in radians. - * @returns {quat} out. - **/ - static fromAxisAngle(out, axis, rad) { - rad = rad * 0.5; - let s = Math.sin(rad); - out.x = s * axis.x; - out.y = s * axis.y; - out.z = s * axis.z; - out.w = Math.cos(rad); - return out; - } - - /** - * Creates a quaternion from the given 3x3 rotation matrix. - * - * NOTE: The resultant quaternion is not normalized, so you should be sure - * to re-normalize the quaternion yourself where necessary. - * - * @param {quat} out - Quaternion to store result. - * @param {mat3} m - The rotation matrix. - * @returns {quat} out. - * @function - */ - static fromMat3(out, m) { - // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm - - let m00 = m.m00, m01 = m.m03, m02 = m.m06, - m10 = m.m01, m11 = m.m04, m12 = m.m07, - m20 = m.m02, m21 = m.m05, m22 = m.m08; - - let trace = m00 + m11 + m22; - - if (trace > 0) { - let s = 0.5 / Math.sqrt(trace + 1.0); - - out.w = 0.25 / s; - out.x = (m21 - m12) * s; - out.y = (m02 - m20) * s; - out.z = (m10 - m01) * s; - - } else if ((m00 > m11) && (m00 > m22)) { - let s = 2.0 * Math.sqrt(1.0 + m00 - m11 - m22); - - out.w = (m21 - m12) / s; - out.x = 0.25 * s; - out.y = (m01 + m10) / s; - out.z = (m02 + m20) / s; - - } else if (m11 > m22) { - let s = 2.0 * Math.sqrt(1.0 + m11 - m00 - m22); - - out.w = (m02 - m20) / s; - out.x = (m01 + m10) / s; - out.y = 0.25 * s; - out.z = (m12 + m21) / s; - - } else { - let s = 2.0 * Math.sqrt(1.0 + m22 - m00 - m11); - - out.w = (m10 - m01) / s; - out.x = (m02 + m20) / s; - out.y = (m12 + m21) / s; - out.z = 0.25 * s; - } - - return out; - } - - /** - * Creates a quaternion from the given euler angle x, y, z. - * - * @param {quat} out - Quaternion to store result. - * @param {number} x - Angle to rotate around X axis in degrees. - * @param {number} y - Angle to rotate around Y axis in degrees. - * @param {number} z - Angle to rotate around Z axis in degrees. - * @returns {quat} out. - * @function - */ - static fromEuler(out, x, y, z) { - x *= halfToRad; - y *= halfToRad; - z *= halfToRad; - - let sx = Math.sin(x); - let cx = Math.cos(x); - let sy = Math.sin(y); - let cy = Math.cos(y); - let sz = Math.sin(z); - let cz = Math.cos(z); - - out.x = sx * cy * cz + cx * sy * sz; - out.y = cx * sy * cz + sx * cy * sz; - out.z = cx * cy * sz - sx * sy * cz; - out.w = cx * cy * cz - sx * sy * sz; - - return out; - } - - /** - * Convert a quaternion back to euler angle (in degrees). - * - * @param {vec3} out - Euler angle stored as a vec3 - * @param {number} q - the quaternion to be converted - * @returns {vec3} out. - */ - static toAxisX(out) { - - } - - /** - * Convert a quaternion back to euler angle (in degrees). - * - * @param {vec3} out - Euler angle stored as a vec3 - * @param {number} q - the quaternion to be converted - * @returns {vec3} out. - */ - static toEuler(out, q) { - let x = q.x, y = q.y, z = q.z, w = q.w; - let heading, attitude, bank; - let test = x * y + z * w; - if (test > 0.499999) { // singularity at north pole - heading = 2 * Math.atan2(x,w); - attitude = Math.PI/2; - bank = 0; - } - if (test < -0.499999) { // singularity at south pole - heading = -2 * Math.atan2(x,w); - attitude = - Math.PI/2; - bank = 0; - } - if(isNaN(heading)){ - let sqx = x*x; - let sqy = y*y; - let sqz = z*z; - heading = Math.atan2(2*y*w - 2*x*z , 1 - 2*sqy - 2*sqz); // heading - attitude = Math.asin(2*test); // attitude - bank = Math.atan2(2*x*w - 2*y*z , 1 - 2*sqx - 2*sqz); // bank - } - - // Keep angles [0..360]. - out.y = toDegree(heading) + (heading < 0 ? 360 : 0); - out.z = toDegree(attitude) + (attitude < 0 ? 360 : 0); - out.x = toDegree(bank) + (bank < 0 ? 360 : 0); - - return out; - } - - /** - * Returns string representation of a quaternion. - * - * @param {quat} a - The quaternion. - * @returns {String} - String representation of this quaternion. - */ - static str(a) { - return `quat(${a.x}, ${a.y}, ${a.z}, ${a.w})`; - } - - /** - * Store components of a quaternion into array. - * - * @param {Array} out - Array to store result. - * @param {quat} q - The quaternion. - * @returns {Array} out. - */ - static array(out, q) { - out[0] = q.x; - out[1] = q.y; - out[2] = q.z; - out[3] = q.w; - - return out; - } - - /** - * Returns whether the specified quaternions are equal. (Compared using ===) - * - * @param {quat} a - The first quaternion. - * @param {quat} b - The second quaternion. - * @returns {Boolean} True if the quaternions are equal, false otherwise. - */ - static exactEquals(a, b) { - return vec4.exactEquals(a, b); - } - - /** - * Returns whether the specified quaternions are approximately equal. - * - * @param {quat} a The first quaternion. - * @param {quat} b The second quaternion. - * @returns {Boolean} True if the quaternions are approximately equal, false otherwise. - */ - static equals(a, b) { - return vec4.equals(a, b); - } -} - -export default quat; diff --git a/cocos/core/vmath/vec3.js b/cocos/core/vmath/vec3.js deleted file mode 100644 index 2b5bcd5383..0000000000 --- a/cocos/core/vmath/vec3.js +++ /dev/null @@ -1,934 +0,0 @@ -import { EPSILON, random } from './utils'; - -/** - * Mathematical 3-dimensional vector. - * - * x, y, z is alias of the first, second, third component of vector, respectively. - */ -class vec3 { - /** - * Creates a vector, with components specified separately. - * - * @param {number} x - Value assigned to x component. - * @param {number} y - Value assigned to y component. - * @param {number} z - Value assigned to z component. - */ - constructor(x = 0, y = 0, z = 0) { - /** - * The x component. - * @type {number} - * */ - this.x = x; - - /** - * The y component. - * @type {number} - * */ - this.y = y; - - /** - * The z component. - * @type {number} - * */ - this.z = z; - } - - static UNIT_X = { x: 1, y: 0, z: 0 }; - static UNIT_Y = { x: 0, y: 1, z: 0 }; - static UNIT_Z = { x: 0, y: 0, z: 1 }; - static ZERO = { x: 0, y: 0, z: 0 }; - static ONE = { x: 1, y: 1, z: 1 }; - static NEG_ONE = { x: -1, y: -1, z: -1 }; - - /** - * Creates a vector, with components specified separately. - * - * @param {number} x - Value assigned to x component. - * @param {number} y - Value assigned to y component. - * @param {number} z - Value assigned to z component. - * @return {vec3} The newly created vector. - */ - static create(x = 0, y = 0, z = 0) { - return new vec3(x, y, z); - } - - /** - * Creates a zero vector. - * - * @return {vec3} The newly created vector. - */ - static zero(out) { - out.x = 0; - out.y = 0; - out.z = 0; - return out; - } - - /** - * Clone a vector. - * - * @param {vec3} a - Vector to clone. - * @returns {vec3} The newly created vector. - */ - static clone(a) { - return new vec3(a.x, a.y, a.z); - } - - /** - * Copy content of a vector into another. - * - * @param {vec3} out - The vector to modified. - * @param {vec3} a - The specified vector. - * @returns {vec3} out. - */ - static copy(out, a) { - out.x = a.x; - out.y = a.y; - out.z = a.z; - return out; - } - - /** - * Sets the components of a vector to the given values. - * - * @param {vec3} out - The vector to modified. - * @param {Number} x - Value set to x component. - * @param {Number} y - Value set to y component. - * @param {Number} z - Value set to z component. - * @returns {vec3} out. - */ - static set(out, x, y, z) { - out.x = x; - out.y = y; - out.z = z; - return out; - } - - /** - * Add two vectors. - * - * It doesn't matter that any amount of these parameters refer to same vector. - * - * @param {vec3} out - Vector to store result. - * @param {vec3} a - The first operand. - * @param {vec3} b - The second operand. - * @returns {vec3} out. - */ - static add(out, a, b) { - out.x = a.x + b.x; - out.y = a.y + b.y; - out.z = a.z + b.z; - return out; - } - - /** - * Subtract two vectors. - * - * It doesn't matter that any amount of these parameters refer to same vector. - * - * @param {vec3} out - Vector to store result. - * @param {vec3} a - The first operand. - * @param {vec3} b - The second operand. - * @returns {vec3} out. - */ - static subtract(out, a, b) { - out.x = a.x - b.x; - out.y = a.y - b.y; - out.z = a.z - b.z; - return out; - } - - /** - * Alias of {@link vec3.subtract}. - */ - static sub(out, a, b) { - return vec3.subtract(out, a, b); - } - - /** - * Performs multiply on each component of two vectors respectively. - * - * It doesn't matter that any amount of these parameters refer to same vector. - * - * @param {vec3} out - Vector to store result. - * @param {vec3} a - The first operand. - * @param {vec3} b - The second operand. - * @returns {vec3} out. - */ - static multiply(out, a, b) { - out.x = a.x * b.x; - out.y = a.y * b.y; - out.z = a.z * b.z; - return out; - } - - /** - * Alias of {@link vec3.multiply}. - */ - static mul(out, a, b) { - return vec3.multiply(out, a, b); - } - - /** - * Performs division on each component of two vectors respectively. - * - * It doesn't matter that any amount of these parameters refer to same vector. - * - * @param {vec3} out - Vector to store result. - * @param {vec3} a - The first operand. - * @param {vec3} b - The second operand. - * @returns {vec3} out. - */ - static divide(out, a, b) { - out.x = a.x / b.x; - out.y = a.y / b.y; - out.z = a.z / b.z; - return out; - } - - /** - * Alias of {@link vec3.divide}. - */ - static div(out, a, b) { - return vec3.divide(out, a, b); - } - - /** - * Performs Math.ceil on each component of a vector. - * - * It doesn't matter that any amount of these parameters refer to same vector. - * - * @param {vec3} out - Vector to store result. - * @param {vec3} a - Vector to perform operation. - * @returns {vec3} out. - */ - static ceil(out, a) { - out.x = Math.ceil(a.x); - out.y = Math.ceil(a.y); - out.z = Math.ceil(a.z); - return out; - } - - /** - * Performs Math.floor on each component of a vector. - * - * It doesn't matter that any amount of these parameters refer to same vector. - * - * @param {vec3} out - Vector to store result. - * @param {vec3} a - Vector to perform operation. - * @returns {vec3} out. - */ - static floor(out, a) { - out.x = Math.floor(a.x); - out.y = Math.floor(a.y); - out.z = Math.floor(a.z); - return out; - } - - /** - * Performs Math.min on each component of two vectors respectively. - * - * It doesn't matter that any amount of these parameters refer to same vector. - * - * @param {vec3} out - Vector to store result. - * @param {vec3} a - The first operand. - * @param {vec3} b - The second operand. - * @returns {vec3} out. - */ - static min(out, a, b) { - out.x = Math.min(a.x, b.x); - out.y = Math.min(a.y, b.y); - out.z = Math.min(a.z, b.z); - return out; - } - - /** - * Performs Math.min on each component of two vectors respectively. - * - * It doesn't matter that any amount of these parameters refer to same vector. - * - * @param {vec3} out - Vector to store result. - * @param {vec3} a - The first operand. - * @param {vec3} b - The second operand. - * @returns {vec3} out. - */ - static max(out, a, b) { - out.x = Math.max(a.x, b.x); - out.y = Math.max(a.y, b.y); - out.z = Math.max(a.z, b.z); - return out; - } - - /** - * Performs Math.round on each component of a vector. - * - * It doesn't matter that any amount of these parameters refer to same vector. - * - * @param {vec3} out - Vector to store result. - * @param {vec3} a - Vector to perform operation. - * @returns {vec3} out. - */ - static round(out, a) { - out.x = Math.round(a.x); - out.y = Math.round(a.y); - out.z = Math.round(a.z); - return out; - } - - /** - * Scales a vector with a number. - * - * @param {vec3} out - Vector to store result. - * @param {vec3} a - Vector to scale. - * @param {number} b - The scale number. - * @returns {vec3} out. - * */ - static scale(out, a, b) { - out.x = a.x * b; - out.y = a.y * b; - out.z = a.z * b; - return out; - } - - /** - * Add two vectors after scaling the second operand by a number. - * - * @param {vec3} out - Vector to store result. - * @param {vec3} a - The first operand. - * @param {vec3} b - The second operand. - * @param {number} scale - The scale number before adding. - * @returns {vec3} out. - */ - static scaleAndAdd(out, a, b, scale) { - out.x = a.x + (b.x * scale); - out.y = a.y + (b.y * scale); - out.z = a.z + (b.z * scale); - return out; - } - - /** - * Calculates the euclidian distance between two vectors. - * - * @param {vec3} a - The first operand. - * @param {vec3} b - The second operand. - * @returns {number} Distance between a and b. - */ - static distance(a, b) { - let x = b.x - a.x, - y = b.y - a.y, - z = b.z - a.z; - return Math.sqrt(x * x + y * y + z * z); - } - - /** - * Alias of {@link vec3.distance}. - */ - static dist(a, b) { - return vec3.distance(a, b); - } - - /** - * Calculates the squared euclidian distance between two vectors. - * - * @param {vec3} a - The first operand. - * @param {vec3} b - The second operand. - * @returns {number} Squared distance between a and b. - */ - static squaredDistance(a, b) { - let x = b.x - a.x, - y = b.y - a.y, - z = b.z - a.z; - return x * x + y * y + z * z; - } - - /** - * Alias of {@link vec3.squaredDistance}. - */ - static sqrDist(a, b) { - return vec3.squaredDistance(a, b); - } - - /** - * Calculates the length of a vector. - * - * @param {vec3} a - The vector. - * @returns {Number} Length of the vector. - */ - static magnitude(a) { - let x = a.x, - y = a.y, - z = a.z; - return Math.sqrt(x * x + y * y + z * z); - } - - /** - *Alias of {@link vec3.magnitude}. - */ - static mag(a) { - return vec3.magnitude(a); - } - - /** - * Calculates the squared length of a vector. - * - * @param {vec3} a - The vector. - * @returns {Number} Squared length of the vector. - */ - static squaredMagnitude(a) { - let x = a.x, - y = a.y, - z = a.z; - return x * x + y * y + z * z; - } - - /** - *Alias of {@link vec3.squaredMagnitude} - */ - static sqrMag(a) { - return vec3.squaredMagnitude(a); - } - - /** - * Negates each component of a vector. - * - * @param {vec3} out - Vector to store result. - * @param {vec3} a - Vector to negate. - * @returns {vec3} out. - */ - static negate(out, a) { - out.x = -a.x; - out.y = -a.y; - out.z = -a.z; - return out; - } - - /** - * Inverts the components of a vector. - * - * @param {vec3} out - Vector to store result. - * @param {vec3} a - Vector to invert. - * @returns {vec3} out. - */ - static inverse(out, a) { - out.x = 1.0 / a.x; - out.y = 1.0 / a.y; - out.z = 1.0 / a.z; - return out; - } - - /** - * Safely inverts the components of a vector. - * - * @param {vec3} out - Vector to store result. - * @param {vec3} a - Vector to invert. - * @returns {vec3} out. - */ - static inverseSafe(out, a) { - let x = a.x, - y = a.y, - z = a.z; - - if (Math.abs(x) < EPSILON) { - out.x = 0; - } else { - out.x = 1.0 / x; - } - - if (Math.abs(y) < EPSILON) { - out.y = 0; - } else { - out.y = 1.0 / y; - } - - if (Math.abs(z) < EPSILON) { - out.z = 0; - } else { - out.z = 1.0 / z; - } - - return out; - } - - /** - * Normalizes a vector. - * - * @param {vec3} out - Vector to store result. - * @param {vec3} a - Vector to normalize. - * @returns {vec3} out. - */ - static normalize(out, a) { - let x = a.x, - y = a.y, - z = a.z; - - let len = x * x + y * y + z * z; - if (len > 0) { - //TODO: evaluate use of glm_invsqrt here? - len = 1 / Math.sqrt(len); - out.x = x * len; - out.y = y * len; - out.z = z * len; - } - return out; - } - - /** - * Calculates the dot product of two vectors. - * - * @param {vec3} a - The first operand. - * @param {vec3} b - The second operand. - * @returns {Number} Dot product of a and b. - */ - static dot(a, b) { - return a.x * b.x + a.y * b.y + a.z * b.z; - } - - /** - * Calculates the cross product of two vectors. - * - * @param {vec3} out - Vector to store result. - * @param {vec3} a - The first operand. - * @param {vec3} b - The second operand. - * @returns {vec3} out. - */ - static cross(out, a, b) { - let ax = a.x, ay = a.y, az = a.z, - bx = b.x, by = b.y, bz = b.z; - - out.x = ay * bz - az * by; - out.y = az * bx - ax * bz; - out.z = ax * by - ay * bx; - return out; - } - - /** - * Performs a linear interpolation between two vectors. - * - * @param {vec3} out - Vector to store result. - * @param {vec3} a - The first operand. - * @param {vec3} b - The second operand. - * @param {Number} t - The interpolation coefficient. - * @returns {vec3} out. - */ - static lerp(out, a, b, t) { - let ax = a.x, - ay = a.y, - az = a.z; - out.x = ax + t * (b.x - ax); - out.y = ay + t * (b.y - ay); - out.z = az + t * (b.z - az); - return out; - } - - /** - * Performs a hermite interpolation with two control points. - * - * @param {vec3} out - Vector to store result. - * @param {vec3} a - The first operand. - * @param {vec3} b - The second operand. - * @param {vec3} c - The third operand. - * @param {vec3} d - The fourth operand. - * @param {Number} t - The interpolation coefficient. - * @returns {vec3} out. - */ - static hermite(out, a, b, c, d, t) { - let factorTimes2 = t * t, - factor1 = factorTimes2 * (2 * t - 3) + 1, - factor2 = factorTimes2 * (t - 2) + t, - factor3 = factorTimes2 * (t - 1), - factor4 = factorTimes2 * (3 - 2 * t); - - out.x = a.x * factor1 + b.x * factor2 + c.x * factor3 + d.x * factor4; - out.y = a.y * factor1 + b.y * factor2 + c.y * factor3 + d.y * factor4; - out.z = a.z * factor1 + b.z * factor2 + c.z * factor3 + d.z * factor4; - - return out; - } - - /** - * Performs a bezier interpolation with two control points. - * - * @param {vec3} out - Vector to store result. - * @param {vec3} a - The first operand. - * @param {vec3} b - The second operand. - * @param {vec3} c - The third operand. - * @param {vec3} d - The fourth operand. - * @param {Number} t - The interpolation coefficient. - * @returns {vec3} out. - */ - static bezier(out, a, b, c, d, t) { - let inverseFactor = 1 - t, - inverseFactorTimesTwo = inverseFactor * inverseFactor, - factorTimes2 = t * t, - factor1 = inverseFactorTimesTwo * inverseFactor, - factor2 = 3 * t * inverseFactorTimesTwo, - factor3 = 3 * factorTimes2 * inverseFactor, - factor4 = factorTimes2 * t; - - out.x = a.x * factor1 + b.x * factor2 + c.x * factor3 + d.x * factor4; - out.y = a.y * factor1 + b.y * factor2 + c.y * factor3 + d.y * factor4; - out.z = a.z * factor1 + b.z * factor2 + c.z * factor3 + d.z * factor4; - - return out; - } - - /** - * Generates a random vector uniformly distributed on a sphere centered at the origin. - * - * @param {vec3} out - Vector to store result. - * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit length vector will be returned. - * @returns {vec3} out. - */ - static random(out, scale) { - scale = scale || 1.0; - - let phi = random() * 2.0 * Math.PI; - let theta = Math.acos(random() * 2 - 1); - - out.x = Math.sin(theta) * Math.cos(phi) * scale; - out.y = Math.sin(theta) * Math.sin(phi) * scale; - out.z = Math.cos(theta) * scale; - return out; - } - - /** - * Transforms a point vector with a 4x4 matrix, - * i.e. 4th vector component is implicitly '1'. - * - * @param {vec3} out - Vector to store result. - * @param {vec3} a - Vector to transform. - * @param {mat4} m - The matrix. - * @returns {vec3} out. - */ - static transformMat4(out, a, m) { - let x = a.x, y = a.y, z = a.z, - rhw = m.m03 * x + m.m07 * y + m.m11 * z + m.m15; - rhw = rhw ? 1 / rhw : 1; - out.x = (m.m00 * x + m.m04 * y + m.m08 * z + m.m12) * rhw; - out.y = (m.m01 * x + m.m05 * y + m.m09 * z + m.m13) * rhw; - out.z = (m.m02 * x + m.m06 * y + m.m10 * z + m.m14) * rhw; - return out; - } - - /** - * Transforms a normal vector with a 4x4 matrix, - * i.e. 4th vector component is implicitly '0'. - * - * @param {vec3} out - Vector to store result. - * @param {vec3} a - Vector to transform. - * @param {mat4} m - The matrix. - * @returns {vec3} out. - */ - static transformMat4Normal(out, a, m) { - let x = a.x, y = a.y, z = a.z, - rhw = m.m03 * x + m.m07 * y + m.m11 * z; - rhw = rhw ? 1 / rhw : 1; - out.x = (m.m00 * x + m.m04 * y + m.m08 * z) * rhw; - out.y = (m.m01 * x + m.m05 * y + m.m09 * z) * rhw; - out.z = (m.m02 * x + m.m06 * y + m.m10 * z) * rhw; - return out; - } - - /** - * Transforms a vector with a 3x3 matrix. - * - * @param {vec3} out - Vector to store result. - * @param {vec3} a - Vector to transform. - * @param {mat3} m - The matrix. - * @returns {vec3} out. - */ - static transformMat3(out, a, m) { - let x = a.x, y = a.y, z = a.z; - out.x = x * m.m00 + y * m.m03 + z * m.m06; - out.y = x * m.m01 + y * m.m04 + z * m.m07; - out.z = x * m.m02 + y * m.m05 + z * m.m08; - return out; - } - - /** - * Transforms a vector with a quaternion. - * - * @param {vec3} out - Vector to store result. - * @param {vec3} a - Vector to transform. - * @param {quat} q - The quaternion. - * @returns {vec3} out. - */ - static transformQuat(out, a, q) { - // benchmarks: http://jsperf.com/quaternion-transform-vec3-implementations - - let x = a.x, y = a.y, z = a.z; - let qx = q.x, qy = q.y, qz = q.z, qw = q.w; - - // calculate quat * vec - let ix = qw * x + qy * z - qz * y; - let iy = qw * y + qz * x - qx * z; - let iz = qw * z + qx * y - qy * x; - let iw = -qx * x - qy * y - qz * z; - - // calculate result * inverse quat - out.x = ix * qw + iw * -qx + iy * -qz - iz * -qy; - out.y = iy * qw + iw * -qy + iz * -qx - ix * -qz; - out.z = iz * qw + iw * -qz + ix * -qy - iy * -qx; - return out; - } - - /** - * Rotates a 3D vector around the x-axis. - * @param {vec3} out - Vector to store result. - * @param {vec3} a - The point to rotate. - * @param {vec3} b - The origin of the rotation. - * @param {Number} c - The angle of rotation. - * @returns {vec3} out. - */ - static rotateX(out, a, b, c) { - // Translate point to the origin - let px = a.x - b.x; - let py = a.y - b.y; - let pz = a.z - b.z; - - //perform rotation - let rx = px; - let ry = py * Math.cos(c) - pz * Math.sin(c); - let rz = py * Math.sin(c) + pz * Math.cos(c); - - //translate to correct position - out.x = rx + b.x; - out.y = ry + b.y; - out.z = rz + b.z; - - return out; - } - - /** - * Rotates a 3D vector around the y-axis. - * @param {vec3} out - Vector to store result. - * @param {vec3} a - The point to rotate. - * @param {vec3} b - The origin of the rotation. - * @param {Number} c - The angle of rotation. - * @returns {vec3} out. - */ - static rotateY(out, a, b, c) { - //Translate point to the origin - let px = a.x - b.x; - let py = a.y - b.y; - let pz = a.z - b.z; - - //perform rotation - let rx = pz * Math.sin(c) + px * Math.cos(c); - let ry = py; - let rz = pz * Math.cos(c) - px * Math.sin(c); - - //translate to correct position - out.x = rx + b.x; - out.y = ry + b.y; - out.z = rz + b.z; - - return out; - } - - /** - * Rotates a 3D vector around the z-axis. - * @param {vec3} out - Vector to store result. - * @param {vec3} a - The point to rotate. - * @param {vec3} b - The origin of the rotation. - * @param {Number} c - The angle of rotation. - * @returns {vec3} out. - */ - static rotateZ(out, a, b, c) { - //Translate point to the origin - let px = a.x - b.x; - let py = a.y - b.y; - let pz = a.z - b.z; - - //perform rotation - let rx = px * Math.cos(c) - py * Math.sin(c); - let ry = px * Math.sin(c) + py * Math.cos(c); - let rz = pz; - - //translate to correct position - out.x = rx + b.x; - out.y = ry + b.y; - out.z = rz + b.z; - - return out; - } - - /** - * Returns string representation of a vector. - * - * @param {vec3} a - The vector. - * @returns {String} - String representation of this vector. - */ - static str(a) { - return `vec3(${a.x}, ${a.y}, ${a.z})`; - } - - /** - * Store components of a vector into array. - * - * @param {Array} out - Array to store result. - * @param {vec3} v - The vector. - * @returns {Array} out. - */ - static array(out, v) { - out[0] = v.x; - out[1] = v.y; - out[2] = v.z; - - return out; - } - - /** - * Returns whether the specified vectors are equal. (Compared using ===) - * - * @param {vec3} a - The first vector. - * @param {vec3} b - The second vector. - * @returns {Boolean} True if the vectors are equal, false otherwise. - */ - static exactEquals(a, b) { - return a.x === b.x && a.y === b.y && a.z === b.z; - } - - /** - * Returns whether the specified vectors are approximately equal. - * - * @param {vec3} a The first vector. - * @param {vec3} b The second vector. - * @returns {Boolean} True if the vectors are approximately equal, false otherwise. - */ - static equals(a, b) { - let a0 = a.x, a1 = a.y, a2 = a.z; - let b0 = b.x, b1 = b.y, b2 = b.z; - return (Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && - Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && - Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2))); - } - - /** - * Perform some operation over an array of vec3s. - * - * @param {Array} a the array of vectors to iterate over. - * @param {Number} stride Number of elements between the start of each vec3. If 0 assumes tightly packed. - * @param {Number} offset Number of elements to skip at the beginning of the array. - * @param {Number} count Number of vec3s to iterate over. If 0 iterates over entire array. - * @param {Function} fn Function to call for each vector in the array. - * @param {Object} [arg] additional argument to pass to fn. - * @returns {Array} a. - */ - static forEach(a, stride, offset, count, fn, arg) { - return vec3._forEach(a, stride, offset, count, fn, arg); - } - - /** - * Gets the angle between two vectors. - * @param {vec3} a - The first operand. - * @param {vec3} b - The second operand. - * @returns {Number} - The angle in radians. - */ - static angle(a, b) { - return vec3._angle(a, b); - } - - /** - * Projects a vector onto a plane represented by its normal. - * @param {vec3} out The result vector. - * @param {vec3} a The vector. - * @param {vec3} n The plane's normal. - */ - static projectOnPlane(out, a, n) { - return vec3.sub(out, a, vec3.project(out, a, n)); - } - - /** - * Projects a vector onto another vector. - * @param {vec3} out The result vector. - * @param {vec3} a The vector to project. - * @param {vec3} b The vector onto which the projection performs. - */ - static project(out, a, b) { - let sqrLen = vec3.squaredMagnitude(b); - if (sqrLen < 0.000001) - return vec3.set(out, 0, 0, 0); - else - return vec3.scale(out, b, vec3.dot(a, b) / sqrLen); - } -} - -/** -* Perform some operation over an array of vec3s. -* -* @param {Array} a the array of vectors to iterate over. -* @param {Number} stride Number of elements between the start of each vec3. If 0 assumes tightly packed. -* @param {Number} offset Number of elements to skip at the beginning of the array. -* @param {Number} count Number of vec3s to iterate over. If 0 iterates over entire array. -* @param {Function} fn Function to call for each vector in the array. -* @param {Object} [arg] additional argument to pass to fn. -* @returns {Array} a. -* @ignore. -*/ -vec3._forEach = (function () { - let vec = vec3.create(0, 0, 0); - - return function (a, stride, offset, count, fn, arg) { - let i, l; - if (!stride) { - stride = 3; - } - - if (!offset) { - offset = 0; - } - - if (count) { - l = Math.min((count * stride) + offset, a.length); - } else { - l = a.length; - } - - for (i = offset; i < l; i += stride) { - vec.x = a[i]; - vec.y = a[i + 1]; - vec.z = a[i + 2]; - fn(vec, vec, arg); - a[i] = vec.x; - a[i + 1] = vec.y; - a[i + 2] = vec.z; - } - - return a; - }; -})(); - -/** - * Get the angle between two 3D vectors. - * @param {vec3} a The first operand. - * @param {vec3} b The second operand. - * @returns {Number} The angle in radians. - * @ignore - */ -vec3._angle = (function () { - let tempA = vec3.create(0, 0, 0); - let tempB = vec3.create(0, 0, 0); - - return function (a, b) { - vec3.copy(tempA, a); - vec3.copy(tempB, b); - - vec3.normalize(tempA, tempA); - vec3.normalize(tempB, tempB); - - let cosine = vec3.dot(tempA, tempB); - - if (cosine > 1.0) { - return 0; - } - - if (cosine < -1.0) { - return Math.PI; - } - - return Math.acos(cosine); - }; -})(); - -export default vec3; \ No newline at end of file diff --git a/cocos/core/vmath/vec3.ts b/cocos/core/vmath/vec3.ts index 83dc826ed5..690d6fb21a 100644 --- a/cocos/core/vmath/vec3.ts +++ b/cocos/core/vmath/vec3.ts @@ -8,6 +8,13 @@ import { EPSILON, random } from './utils'; * x, y, z is alias of the first, second, third component of vector, respectively. */ export default class vec3 { + public static UNIT_X = { x: 1, y: 0, z: 0 }; + public static UNIT_Y = { x: 0, y: 1, z: 0 }; + public static UNIT_Z = { x: 0, y: 0, z: 1 }; + public static ZERO = { x: 0, y: 0, z: 0 }; + public static ONE = { x: 1, y: 1, z: 1 }; + public static NEG_ONE = { x: -1, y: -1, z: -1 }; + /** * Creates a vector, with components specified separately. *