Skip to content

Commit adf1cee

Browse files
committed
Add mat4.orthoZO, which is appropriate for WebGPU's normalized device coordinates
1 parent 4f92b6f commit adf1cee

1 file changed

Lines changed: 47 additions & 2 deletions

File tree

src/mat4.js

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,7 +1621,9 @@ export function perspectiveFromFieldOfView(out, fov, near, far) {
16211621
}
16221622

16231623
/**
1624-
* Generates a orthogonal projection matrix with the given bounds
1624+
* Generates a orthogonal projection matrix with the given bounds.
1625+
* The near/far clip planes correspond to a normalized device coordinate Z range of [-1, 1],
1626+
* which matches WebGL/OpenGL's clip volume.
16251627
*
16261628
* @param {mat4} out mat4 frustum matrix will be written into
16271629
* @param {number} left Left bound of the frustum
@@ -1632,7 +1634,7 @@ export function perspectiveFromFieldOfView(out, fov, near, far) {
16321634
* @param {number} far Far bound of the frustum
16331635
* @returns {mat4} out
16341636
*/
1635-
export function ortho(out, left, right, bottom, top, near, far) {
1637+
export function orthoNO(out, left, right, bottom, top, near, far) {
16361638
let lr = 1 / (left - right);
16371639
let bt = 1 / (bottom - top);
16381640
let nf = 1 / (near - far);
@@ -1655,6 +1657,49 @@ export function ortho(out, left, right, bottom, top, near, far) {
16551657
return out;
16561658
}
16571659

1660+
/**
1661+
* Alias for {@link mat4.orthoNO}
1662+
* @function
1663+
*/
1664+
export const ortho = orthoNO;
1665+
1666+
/**
1667+
* Generates a orthogonal projection matrix with the given bounds.
1668+
* The near/far clip planes correspond to a normalized device coordinate Z range of [0, 1],
1669+
* which matches WebGPU/Vulkan/DirectX/Metal's clip volume.
1670+
*
1671+
* @param {mat4} out mat4 frustum matrix will be written into
1672+
* @param {number} left Left bound of the frustum
1673+
* @param {number} right Right bound of the frustum
1674+
* @param {number} bottom Bottom bound of the frustum
1675+
* @param {number} top Top bound of the frustum
1676+
* @param {number} near Near bound of the frustum
1677+
* @param {number} far Far bound of the frustum
1678+
* @returns {mat4} out
1679+
*/
1680+
export function orthoZO(out, left, right, bottom, top, near, far) {
1681+
let lr = 1 / (left - right);
1682+
let bt = 1 / (bottom - top);
1683+
let nf = 1 / (near - far);
1684+
out[0] = -2 * lr;
1685+
out[1] = 0;
1686+
out[2] = 0;
1687+
out[3] = 0;
1688+
out[4] = 0;
1689+
out[5] = -2 * bt;
1690+
out[6] = 0;
1691+
out[7] = 0;
1692+
out[8] = 0;
1693+
out[9] = 0;
1694+
out[10] = nf;
1695+
out[11] = 0;
1696+
out[12] = (left + right) * lr;
1697+
out[13] = (top + bottom) * bt;
1698+
out[14] = near * nf;
1699+
out[15] = 1;
1700+
return out;
1701+
}
1702+
16581703
/**
16591704
* Generates a look-at matrix with the given eye position, focal point, and up axis.
16601705
* If you want a matrix that actually makes an object look at another object, you should use targetTo instead.

0 commit comments

Comments
 (0)