diff --git a/cocos/core/vmath/quat.js b/cocos/core/vmath/quat.js new file mode 100644 index 0000000000..d5694a765c --- /dev/null +++ b/cocos/core/vmath/quat.js @@ -0,0 +1,834 @@ +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 new file mode 100644 index 0000000000..2b5bcd5383 --- /dev/null +++ b/cocos/core/vmath/vec3.js @@ -0,0 +1,934 @@ +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/gfx/device.ts b/cocos/gfx/device.ts index e4a1fa5c62..1c97dc83bf 100644 --- a/cocos/gfx/device.ts +++ b/cocos/gfx/device.ts @@ -47,6 +47,7 @@ export abstract class GFXDevice { public abstract copyBufferToTexture(buffer: ArrayBuffer, texture: GFXTexture, regions: GFXBufferTextureCopy[]); public abstract copyImageSourceToTexture(source: CanvasImageSource, texture: GFXTexture, regions: GFXBufferTextureCopy[]); + public abstract copyFramebufferToBuffer(srcFramebuffer: GFXFramebuffer, dstBuffer: ArrayBuffer, regions: GFXBufferTextureCopy[]); public get canvas(): HTMLCanvasElement { return this._canvas; diff --git a/cocos/gfx/framebuffer.ts b/cocos/gfx/framebuffer.ts index 580d033097..adb1751031 100644 --- a/cocos/gfx/framebuffer.ts +++ b/cocos/gfx/framebuffer.ts @@ -30,6 +30,10 @@ export abstract class GFXFramebuffer { return this._depthStencilView; } + public get isOffscreen(): boolean { + return this._isOffscreen; + } + protected _device: GFXDevice; protected _renderPass: GFXRenderPass | null = null; protected _colorViews: GFXTextureView[] = []; diff --git a/cocos/gfx/webgl/webgl-commands.ts b/cocos/gfx/webgl/webgl-commands.ts index c348a2eac1..0d5c4acec3 100644 --- a/cocos/gfx/webgl/webgl-commands.ts +++ b/cocos/gfx/webgl/webgl-commands.ts @@ -4,6 +4,11 @@ import { GFXFormatInfos, GFXFormat, WebGLEXT, GFXType, GFX_MAX_VERTEX_ATTRIBUTES import { WebGLGFXCommandAllocator } from "./webgl-command-allocator"; import { WebGLTexUnit } from "./webgl-state-cache"; +function CmpF32NotEuqal(a: number, b: number): boolean { + let c = a - b; + return (c > 0.000001 || c < -0.000001); +} + function GFXFormatToWebGLType(format: GFXFormat, device: WebGLGFXDevice): GLenum { switch (format) { case GFXFormat.R8: return WebGLRenderingContext.UNSIGNED_BYTE; @@ -1075,7 +1080,7 @@ export function WebGLCmdFuncCreateInputAssember(device: WebGLGFXDevice, gpuInput count: GFXFormatInfos[attrib.format].count, stride: gpuBuffer.stride, componentCount: WebGLGetComponentCount(glType), - isNormalized: (attrib.isNormalized !== undefined? attrib.isNormalized : false), + isNormalized: (attrib.isNormalized !== undefined ? attrib.isNormalized : false), isInstanced: (attrib.isInstanced !== undefined ? attrib.isInstanced : false), offset: offsets[stream], }; @@ -1657,8 +1662,9 @@ export function WebGLCmdFuncExecuteCmds(device: WebGLGFXDevice, cmdPackage: WebG } case WebGLRenderingContext.FLOAT: { for (let m = 0; m < glUniform.count; ++m, ++offset) { - if (vf32[offset] !== glUniform.view[m]) { - + let c: number = 0; + + if (CmpF32NotEuqal(vf32[offset], glUniform.view[m])) { let begin = glUniform.offset / 4; glUniform.view.set(vf32.subarray(begin, begin + glUniform.count)); gl.uniform1fv(glUniform.glLoc, glUniform.view); @@ -1670,8 +1676,8 @@ export function WebGLCmdFuncExecuteCmds(device: WebGLGFXDevice, cmdPackage: WebG case WebGLRenderingContext.FLOAT_VEC2: { for (let m = 0; m < glUniform.count; ++m, offset += 2) { let idx = 2 * m; - if (vf32[offset] !== glUniform.view[idx] || - vf32[offset + 1] !== glUniform.view[idx + 1]) { + if (CmpF32NotEuqal(vf32[offset], glUniform.view[idx]) || + CmpF32NotEuqal(vf32[offset + 1], glUniform.view[idx + 1])) { let begin = glUniform.offset / 4; glUniform.view.set(vf32.subarray(begin, begin + 2 * glUniform.count)); @@ -1684,9 +1690,9 @@ export function WebGLCmdFuncExecuteCmds(device: WebGLGFXDevice, cmdPackage: WebG case WebGLRenderingContext.FLOAT_VEC3: { for (let m = 0; m < glUniform.count; ++m, offset += 3) { let idx = 3 * m; - if (vf32[offset] !== glUniform.view[idx] || - vf32[offset + 1] !== glUniform.view[idx + 1] || - vf32[offset + 2] !== glUniform.view[idx + 2]) { + if (CmpF32NotEuqal(vf32[offset], glUniform.view[idx]) || + CmpF32NotEuqal(vf32[offset + 1], glUniform.view[idx + 1]) || + CmpF32NotEuqal(vf32[offset + 2], glUniform.view[idx + 2])) { let begin = glUniform.offset / 4; glUniform.view.set(vf32.subarray(begin, begin + 3 * glUniform.count)); @@ -1700,10 +1706,11 @@ export function WebGLCmdFuncExecuteCmds(device: WebGLGFXDevice, cmdPackage: WebG case WebGLRenderingContext.FLOAT_VEC4: { for (let m = 0; m < glUniform.count; ++m, offset += 4) { let idx = 4 * m; - if (vf32[offset] !== glUniform.view[idx] || - vf32[offset + 1] !== glUniform.view[idx + 1] || - vf32[offset + 2] !== glUniform.view[idx + 2] || - vf32[offset + 3] !== glUniform.view[idx + 3]) { + + if (CmpF32NotEuqal(vf32[offset], glUniform.view[idx]) || + CmpF32NotEuqal(vf32[offset + 1], glUniform.view[idx + 1]) || + CmpF32NotEuqal(vf32[offset + 2], glUniform.view[idx + 2]) || + CmpF32NotEuqal(vf32[offset + 3], glUniform.view[idx + 3])) { let begin = glUniform.offset / 4; glUniform.view.set(vf32.subarray(begin, begin + 4 * glUniform.count)); @@ -1716,10 +1723,10 @@ export function WebGLCmdFuncExecuteCmds(device: WebGLGFXDevice, cmdPackage: WebG case WebGLRenderingContext.FLOAT_MAT2: { for (let m = 0; m < glUniform.count; ++m, offset += 4) { let idx = 4 * m; - if (vf32[offset] !== glUniform.view[idx] || - vf32[offset + 1] !== glUniform.view[idx + 1] || - vf32[offset + 2] !== glUniform.view[idx + 2] || - vf32[offset + 3] !== glUniform.view[idx + 3]) { + if (CmpF32NotEuqal(vf32[offset], glUniform.view[idx]) || + CmpF32NotEuqal(vf32[offset + 1], glUniform.view[idx + 1]) || + CmpF32NotEuqal(vf32[offset + 2], glUniform.view[idx + 2]) || + CmpF32NotEuqal(vf32[offset + 3], glUniform.view[idx + 3])) { let begin = glUniform.offset / 4; glUniform.view.set(vf32.subarray(begin, begin + 4 * glUniform.count)); @@ -1732,15 +1739,15 @@ export function WebGLCmdFuncExecuteCmds(device: WebGLGFXDevice, cmdPackage: WebG case WebGLRenderingContext.FLOAT_MAT3: { for (let m = 0; m < glUniform.count; ++m, offset += 9) { let idx = 9 * m; - if (vf32[offset] !== glUniform.view[idx] || - vf32[offset + 1] !== glUniform.view[idx + 1] || - vf32[offset + 2] !== glUniform.view[idx + 2] || - vf32[offset + 3] !== glUniform.view[idx + 3] || - vf32[offset + 4] !== glUniform.view[idx + 4] || - vf32[offset + 5] !== glUniform.view[idx + 5] || - vf32[offset + 6] !== glUniform.view[idx + 6] || - vf32[offset + 7] !== glUniform.view[idx + 7] || - vf32[offset + 8] !== glUniform.view[idx + 8]) { + if (CmpF32NotEuqal(vf32[offset], glUniform.view[idx]) || + CmpF32NotEuqal(vf32[offset + 1], glUniform.view[idx + 1]) || + CmpF32NotEuqal(vf32[offset + 2], glUniform.view[idx + 2]) || + CmpF32NotEuqal(vf32[offset + 3], glUniform.view[idx + 3]) || + CmpF32NotEuqal(vf32[offset + 4], glUniform.view[idx + 4]) || + CmpF32NotEuqal(vf32[offset + 5], glUniform.view[idx + 5]) || + CmpF32NotEuqal(vf32[offset + 6], glUniform.view[idx + 6]) || + CmpF32NotEuqal(vf32[offset + 7], glUniform.view[idx + 7]) || + CmpF32NotEuqal(vf32[offset + 8], glUniform.view[idx + 8])) { let begin = glUniform.offset / 4; glUniform.view.set(vf32.subarray(begin, begin + 9 * glUniform.count)); @@ -1751,25 +1758,25 @@ export function WebGLCmdFuncExecuteCmds(device: WebGLGFXDevice, cmdPackage: WebG break; } case WebGLRenderingContext.FLOAT_MAT4: { - let offset = glUniform.offset; for (let m = 0; m < glUniform.count; ++m, offset += 16) { let idx = 16 * m; - if (vf32[offset] !== glUniform.view[idx] || - vf32[offset + 1] !== glUniform.view[idx + 1] || - vf32[offset + 2] !== glUniform.view[idx + 2] || - vf32[offset + 3] !== glUniform.view[idx + 3] || - vf32[offset + 4] !== glUniform.view[idx + 4] || - vf32[offset + 5] !== glUniform.view[idx + 5] || - vf32[offset + 6] !== glUniform.view[idx + 6] || - vf32[offset + 7] !== glUniform.view[idx + 7] || - vf32[offset + 8] !== glUniform.view[idx + 8] || - vf32[offset + 9] !== glUniform.view[idx + 9] || - vf32[offset + 10] !== glUniform.view[idx + 10] || - vf32[offset + 11] !== glUniform.view[idx + 11] || - vf32[offset + 12] !== glUniform.view[idx + 12] || - vf32[offset + 13] !== glUniform.view[idx + 13] || - vf32[offset + 14] !== glUniform.view[idx + 14] || - vf32[offset + 15] !== glUniform.view[idx + 15]) { + + if (CmpF32NotEuqal(vf32[offset], glUniform.view[idx]) || + CmpF32NotEuqal(vf32[offset + 1], glUniform.view[idx + 1]) || + CmpF32NotEuqal(vf32[offset + 2], glUniform.view[idx + 2]) || + CmpF32NotEuqal(vf32[offset + 3], glUniform.view[idx + 3]) || + CmpF32NotEuqal(vf32[offset + 4], glUniform.view[idx + 4]) || + CmpF32NotEuqal(vf32[offset + 5], glUniform.view[idx + 5]) || + CmpF32NotEuqal(vf32[offset + 6], glUniform.view[idx + 6]) || + CmpF32NotEuqal(vf32[offset + 7], glUniform.view[idx + 7]) || + CmpF32NotEuqal(vf32[offset + 8], glUniform.view[idx + 8]) || + CmpF32NotEuqal(vf32[offset + 9], glUniform.view[idx + 9]) || + CmpF32NotEuqal(vf32[offset + 10], glUniform.view[idx + 10]) || + CmpF32NotEuqal(vf32[offset + 11], glUniform.view[idx + 11]) || + CmpF32NotEuqal(vf32[offset + 12], glUniform.view[idx + 12]) || + CmpF32NotEuqal(vf32[offset + 13], glUniform.view[idx + 13]) || + CmpF32NotEuqal(vf32[offset + 14], glUniform.view[idx + 14]) || + CmpF32NotEuqal(vf32[offset + 15], glUniform.view[idx + 15])) { let begin = glUniform.offset / 4; glUniform.view.set(vf32.subarray(begin, begin + 16 * glUniform.count)); diff --git a/cocos/gfx/webgl/webgl-device.ts b/cocos/gfx/webgl/webgl-device.ts index b418eec772..306b1f346e 100644 --- a/cocos/gfx/webgl/webgl-device.ts +++ b/cocos/gfx/webgl/webgl-device.ts @@ -31,7 +31,7 @@ import { WebGLGFXFramebuffer } from './webgl-framebuffer'; import { WebGLGFXInputAssembler } from './webgl-input-assembler'; import { GFXWindow, GFXWindowInfo } from '../window'; import { WebGLGFXWindow } from './webgl-window'; -import { GFXBindingType, GFXFilter, GFXAddress, GFXTextureType, GFXTextureFlagBit, GFXTextureViewType, GFXBufferUsageBit, GFXQueueType, GFXFormat, GFXBufferTextureCopy, GFXMemoryUsageBit, GFXTextureLayout, GFXTextureSubres, GFXRect, GFXFormatInfos } from '../define'; +import { GFXBindingType, GFXFilter, GFXAddress, GFXTextureType, GFXTextureFlagBit, GFXTextureViewType, GFXBufferUsageBit, GFXQueueType, GFXFormat, GFXBufferTextureCopy, GFXMemoryUsageBit, GFXTextureLayout, GFXTextureSubres, GFXRect, GFXFormatInfos, GFXFormatSize } from '../define'; import { WebGLGFXBindingLayout } from './webgl-binding-layout'; const WebGLPrimitives: GLenum[] = [ @@ -408,6 +408,38 @@ export class WebGLGFXDevice extends GFXDevice { } } + public copyFramebufferToBuffer(srcFramebuffer: GFXFramebuffer, dstBuffer: ArrayBuffer, regions: GFXBufferTextureCopy[]) { + let gl = this._webGLRC; + let gpuFramebuffer = (srcFramebuffer).gpuFramebuffer; + + let curFBO = this.stateCache.glFramebuffer; + + if (this.stateCache.glFramebuffer !== gpuFramebuffer.glFramebuffer) { + gl.bindFramebuffer(WebGLRenderingContext.FRAMEBUFFER, gpuFramebuffer.glFramebuffer); + this.stateCache.glFramebuffer = gpuFramebuffer.glFramebuffer; + } + + let view = new Uint8Array(dstBuffer); + + for (let r = 0; r < regions.length; ++r) { + let region = regions[r]; + let buffOffset = region.buffOffset; + region.buffTexHeight * region.buffStride; + + let w = region.texExtent.width; + let h = region.texExtent.height; + + let memSize = GFXFormatSize(GFXFormat.RGBA8, w, h, 1); + let data = view.subarray(buffOffset, buffOffset + memSize); + + gl.readPixels(region.texOffset.x, region.texOffset.y, w, h, WebGLRenderingContext.RGBA, WebGLRenderingContext.UNSIGNED_BYTE, data); + } + + if (this.stateCache.glFramebuffer !== curFBO) { + gl.bindFramebuffer(WebGLRenderingContext.FRAMEBUFFER, curFBO); + this.stateCache.glFramebuffer = curFBO; + } + } + public get gl(): WebGLRenderingContext { return this._webGLRC; } diff --git a/cocos/gfx/webgl/webgl-framebuffer.ts b/cocos/gfx/webgl/webgl-framebuffer.ts index 9ea8bd8d62..2ca91f0ef0 100644 --- a/cocos/gfx/webgl/webgl-framebuffer.ts +++ b/cocos/gfx/webgl/webgl-framebuffer.ts @@ -38,8 +38,8 @@ export class WebGLGFXFramebuffer extends GFXFramebuffer { return this._device; } - public get gpuFramebuffer() : WebGLGPUFramebuffer | null { - return this._gpuFramebuffer; + public get gpuFramebuffer() : WebGLGPUFramebuffer { + return this._gpuFramebuffer; } private _gpuFramebuffer : WebGLGPUFramebuffer | null = null; diff --git a/cocos/pipeline/forward/forward-flow.ts b/cocos/pipeline/forward/forward-flow.ts index 67509a697b..100369cc7a 100644 --- a/cocos/pipeline/forward/forward-flow.ts +++ b/cocos/pipeline/forward/forward-flow.ts @@ -2,6 +2,7 @@ import { RenderFlow, RenderFlowInfo } from "../render-flow"; import { RenderPipeline } from "../render-pipeline"; // import { TestStage } from "./test-stage"; import { TestMaterialStage } from "./test-material-stage"; +import { TestStage } from "./test-stage"; export enum ForwardStagePriority { FORWARD = 0, @@ -22,7 +23,7 @@ export class ForwardFlow extends RenderFlow { this._priority = info.priority; let mainWindow = this._pipeline.root.mainWindow; - if(!mainWindow || !mainWindow.framebuffer) { + if (!mainWindow || !mainWindow.framebuffer) { return false; } @@ -40,6 +41,14 @@ export class ForwardFlow extends RenderFlow { framebuffer: mainWindow.framebuffer, }); + /* + this.createStage(TestStage, { + name: "TestStage", + priority: ForwardStagePriority.FORWARD, + framebuffer: mainWindow.framebuffer, + }); + */ + return true; } diff --git a/cocos/pipeline/forward/forward-pipeline.ts b/cocos/pipeline/forward/forward-pipeline.ts index ddc813292d..59634d6023 100644 --- a/cocos/pipeline/forward/forward-pipeline.ts +++ b/cocos/pipeline/forward/forward-pipeline.ts @@ -36,7 +36,7 @@ export class ForwardPipeline extends RenderPipeline { return false; } - this.addRenderPass(RenderPassStage.FORWARD, windowPass); + this.addRenderPass(RenderPassStage.DEFAULT, windowPass); // create UBOs this._globalUBO = this._root.device.createBuffer({ diff --git a/cocos/pipeline/forward/test-stage.ts b/cocos/pipeline/forward/test-stage.ts index 4b4ab54166..677bc3c90d 100644 --- a/cocos/pipeline/forward/test-stage.ts +++ b/cocos/pipeline/forward/test-stage.ts @@ -4,7 +4,6 @@ import { GFXCommandBuffer } from "../../gfx/command-buffer"; import { RenderView } from "../render-view"; import { GFXFramebuffer } from "../../gfx/framebuffer"; import { GFXCommandBufferType, GFXShaderType, GFXPrimitiveMode, GFXFormat, GFXTextureType, GFXTextureUsageBit, GFXTextureFlagBit, GFXTextureViewType, GFXBufferUsageBit, GFXMemoryUsageBit, GFXTextureLayout, GFXBufferTextureCopy, GFXAddress, GFXFilter, GFXBindingType, GFXType, GFXCullMode } from "../../gfx/define"; -import { GFXInputAssembler } from "../../gfx/input-assembler"; import { GFXPipelineState, GFXInputState, GFXRasterizerState, GFXDepthStencilState, GFXBlendState } from "../../gfx/pipeline-state"; import { GFXShader } from "../../gfx/shader"; import { GFXRenderPass } from "../../gfx/render-pass"; @@ -28,13 +27,13 @@ let _shader_vs = `#version 100 attribute vec2 a_position; attribute vec2 a_texCoord; -//uniform mat4 u_matWorldViewProj; +uniform mat4 u_matViewProj; varying vec2 v_texCoord; void main() { - gl_Position = vec4(a_position, 0.0, 1.0); - //gl_Position = u_matWorldViewProj * vec4(a_position, 0.0, 1.0); + //gl_Position = vec4(a_position, 0.0, 1.0); + gl_Position = u_matViewProj * vec4(a_position, 0.0, 1.0); v_texCoord = a_texCoord; }`; @@ -91,7 +90,16 @@ export class TestStage extends RenderStage { stages: [vsStage, fsStage], blocks: [{ binding: 0, name: "Global", members: [ - { name: "u_matWorldViewProj", type: GFXType.MAT4, count: 1 }, + { name: "u_time", type: GFXType.FLOAT4, count: 1 }, + { name: "u_screenSize", type: GFXType.FLOAT4, count: 1 }, + { name: "u_screenScale", type: GFXType.FLOAT4, count: 1 }, + { name: "u_matView", type: GFXType.MAT4, count: 1 }, + { name: "u_matViewInv", type: GFXType.MAT4, count: 1 }, + { name: "u_matProj", type: GFXType.MAT4, count: 1 }, + { name: "u_matProjInv", type: GFXType.MAT4, count: 1 }, + { name: "u_matViewProj", type: GFXType.MAT4, count: 1 }, + { name: "u_matViewProjInv", type: GFXType.MAT4, count: 1 }, + { name: "u_cameraPos", type: GFXType.FLOAT4, count: 1 }, ] }, { binding: 5, name: "UBO", members: [ @@ -108,6 +116,7 @@ export class TestStage extends RenderStage { this._bindingLayout = this._device.createBindingLayout({ bindings: [ + { binding: 0, type: GFXBindingType.UNIFORM_BUFFER, name: "Global" }, { binding: 5, type: GFXBindingType.UNIFORM_BUFFER, name: "UBO" }, { binding: 10, type: GFXBindingType.SAMPLER, name: "u_sampler" }, ], diff --git a/cocos/pipeline/render-pipeline.ts b/cocos/pipeline/render-pipeline.ts index f56eb8f420..6287654950 100644 --- a/cocos/pipeline/render-pipeline.ts +++ b/cocos/pipeline/render-pipeline.ts @@ -11,7 +11,7 @@ import vec4 from "../core/vmath/vec4"; import mat4 from "../core/vmath/mat4"; export enum RenderPassStage { - FORWARD = 100, + DEFAULT = 100, }; export class UBOGlobal { @@ -22,7 +22,7 @@ export class UBOGlobal { static MAT_VIEW_INV_OFFSET: number = UBOGlobal.MAT_VIEW_OFFSET + 16; static MAT_PROJ_OFFSET: number = UBOGlobal.MAT_VIEW_INV_OFFSET + 16; static MAT_PROJ_INV_OFFSET: number = UBOGlobal.MAT_PROJ_OFFSET + 16; - static MAT_VIEW_PROJ_OFFSET: number = UBOGlobal.MAT_VIEW_INV_OFFSET + 16; + static MAT_VIEW_PROJ_OFFSET: number = UBOGlobal.MAT_PROJ_INV_OFFSET + 16; static MAT_VIEW_PROJ_INV_OFFSET: number = UBOGlobal.MAT_VIEW_PROJ_OFFSET + 16; static CAMERA_POS_OFFSET: number = UBOGlobal.MAT_VIEW_PROJ_INV_OFFSET + 16; static COUNT: number = UBOGlobal.CAMERA_POS_OFFSET + 4; diff --git a/cocos/renderer/core/effect.ts b/cocos/renderer/core/effect.ts index b5780c163f..30dc97bd35 100644 --- a/cocos/renderer/core/effect.ts +++ b/cocos/renderer/core/effect.ts @@ -166,7 +166,7 @@ export class Effect { }; }); passInfo.shader = cc.game._programLib.getGFXShader(passInfo.program, defines || {}); - passInfo.renderPass = cc.director.root.pipeline.getRenderPass(passInfo.stage || RenderPassStage.FORWARD); + passInfo.renderPass = cc.director.root.pipeline.getRenderPass(passInfo.stage || RenderPassStage.DEFAULT); passInfo.blocks = programs[passInfo.program].blocks; passInfo.samplers = programs[passInfo.program].samplers; const pass = new Pass(cc.game._gfxDevice); diff --git a/cocos/renderer/core/pass.ts b/cocos/renderer/core/pass.ts index a5850a1d9f..7e3920337f 100644 --- a/cocos/renderer/core/pass.ts +++ b/cocos/renderer/core/pass.ts @@ -70,7 +70,7 @@ export class Pass { // internal data protected _programName: string = ''; protected _primitive: GFXPrimitiveMode = GFXPrimitiveMode.TRIANGLE_LIST; - protected _stage: RenderPassStage = RenderPassStage.FORWARD; + protected _stage: RenderPassStage = RenderPassStage.DEFAULT; protected _handleMap: Record = {}; protected _typeMap: Record = {}; protected _blocks: Block[] = []; diff --git a/cocos/renderer/scene/camera.ts b/cocos/renderer/scene/camera.ts index f973928401..78a8bd6b97 100644 --- a/cocos/renderer/scene/camera.ts +++ b/cocos/renderer/scene/camera.ts @@ -1,7 +1,6 @@ import { RenderScene } from "./render-scene"; import Node from "../../scene-graph/node"; -import vec3 from "../../core/vmath/vec3"; -import mat4 from "../../core/vmath/mat4"; +import { mat4, vec3, quat } from '../../core/vmath'; export enum CameraProjection { PERSPECTIVE, @@ -115,13 +114,28 @@ export class Camera { return this._matViewProj; } + public get position(): vec3 { + this._node.getWorldPosition(this._position); + return this._position; + } + + public set target(target: vec3) { + this._node.lookAt(target); + } + + public get direction(): vec3 { + quat.getRotation(this._rotation); + quat.transformQuat(this._direction, vec3.UNIT_Z, this._rotation); + return this._direction; + } + public set position(pos: vec3) { this._node.setWorldPosition(pos.x, pos.y, pos.z); } - public get position(): vec3 { - this._node.getWorldPosition(this._position); - return this._position; + public set direction(dir: vec3) { + quat.rotationTo(this._rotation, vec3.UNIT_Z, dir); + this._node.setRotation(this._rotation); } private _scene: RenderScene; @@ -138,4 +152,6 @@ export class Camera { private _matProj: mat4 = new mat4; private _matViewProj: mat4 = new mat4; private _position: vec3 = new vec3; + private _rotation: quat = new quat; + private _direction: vec3 = new vec3; }; diff --git a/playground/simple.html b/playground/simple.html index 8f4c14c512..66149dabee 100644 --- a/playground/simple.html +++ b/playground/simple.html @@ -35,8 +35,8 @@ const bcr = $body.getBoundingClientRect(); // cc.view.setCanvasSize(bcr.width, bcr.height); window.addEventListener('resize', () => { - const bcr = $body.getBoundingClientRect(); - cc.view.setCanvasSize(bcr.width, bcr.height); + const bcr = $body.getBoundingClientRect(); + cc.view.setCanvasSize(bcr.width, bcr.height); }); let root = cc.director.root; @@ -57,7 +57,9 @@ view.attach(scene.mainCamera); let camera = scene.mainCamera; - //camera.position = vec3(0.0, 0.0, 5.0); + camera.position = { x: 0.0, y: 0.0, z: 3.0 }; + camera.direction = { x: 0.0, y: 0.0, z: -1.0 }; + camera.update(); //todo initialization @@ -96,4 +98,4 @@ - + \ No newline at end of file