@@ -789,7 +789,7 @@ export function perspective(fieldOfViewYInRadians: number, aspect: number, zNear
789
789
}
790
790
791
791
/**
792
- * Computes a 4-by-4 perspective transformation matrix given the angular height
792
+ * Computes a 4-by-4 reverse-z perspective transformation matrix given the angular height
793
793
* of the frustum, the aspect ratio, and the near and far clipping planes. The
794
794
* arguments define a frustum extending in the negative z direction. The given
795
795
* angle is the vertical angle of the frustum, and the horizontal angle is
@@ -831,13 +831,13 @@ export function perspective(fieldOfViewYInRadians: number, aspect: number, zNear
831
831
dst [ 13 ] = 0 ;
832
832
dst [ 15 ] = 0 ;
833
833
834
- if ( Number . isFinite ( zFar ) ) {
834
+ if ( zFar === Infinity ) {
835
+ dst [ 10 ] = 0 ;
836
+ dst [ 14 ] = zNear ;
837
+ } else {
835
838
const rangeInv = 1 / ( zFar - zNear ) ;
836
839
dst [ 10 ] = zNear * rangeInv ;
837
840
dst [ 14 ] = zFar * zNear * rangeInv ;
838
- } else {
839
- dst [ 10 ] = 0 ;
840
- dst [ 14 ] = zNear ;
841
841
}
842
842
843
843
return dst ;
@@ -929,6 +929,57 @@ export function frustum(left: number, right: number, bottom: number, top: number
929
929
return dst ;
930
930
}
931
931
932
+ /**
933
+ * Computes a 4-by-4 reverse-z perspective transformation matrix given the left, right,
934
+ * top, bottom, near and far clipping planes. The arguments define a frustum
935
+ * extending in the negative z direction. The arguments near and far are the
936
+ * distances to the near and far clipping planes. Note that near and far are not
937
+ * z coordinates, but rather they are distances along the negative z-axis. The
938
+ * matrix generated sends the viewing frustum to the unit box. We assume a unit
939
+ * box extending from -1 to 1 in the x and y dimensions and from 1 (-near) to 0 (-far) in the z
940
+ * dimension.
941
+ * @param left - The x coordinate of the left plane of the box.
942
+ * @param right - The x coordinate of the right plane of the box.
943
+ * @param bottom - The y coordinate of the bottom plane of the box.
944
+ * @param top - The y coordinate of the right plane of the box.
945
+ * @param near - The negative z coordinate of the near plane of the box.
946
+ * @param far - The negative z coordinate of the far plane of the box.
947
+ * @param dst - Output matrix. If not passed a new one is created.
948
+ * @returns The perspective projection matrix.
949
+ */
950
+ export function frustumReverseZ ( left : number , right : number , bottom : number , top : number , near : number , far = Infinity , dst ?: Mat4 ) : Mat4 {
951
+ dst = dst || new MatType ( 16 ) ;
952
+
953
+ const dx = ( right - left ) ;
954
+ const dy = ( top - bottom ) ;
955
+
956
+ dst [ 0 ] = 2 * near / dx ;
957
+ dst [ 1 ] = 0 ;
958
+ dst [ 2 ] = 0 ;
959
+ dst [ 3 ] = 0 ;
960
+ dst [ 4 ] = 0 ;
961
+ dst [ 5 ] = 2 * near / dy ;
962
+ dst [ 6 ] = 0 ;
963
+ dst [ 7 ] = 0 ;
964
+ dst [ 8 ] = ( left + right ) / dx ;
965
+ dst [ 9 ] = ( top + bottom ) / dy ;
966
+ dst [ 11 ] = - 1 ;
967
+ dst [ 12 ] = 0 ;
968
+ dst [ 13 ] = 0 ;
969
+ dst [ 15 ] = 0 ;
970
+
971
+ if ( far === Infinity ) {
972
+ dst [ 10 ] = 0 ;
973
+ dst [ 14 ] = near ;
974
+ } else {
975
+ const rangeInv = 1 / ( far - near ) ;
976
+ dst [ 10 ] = near * rangeInv ;
977
+ dst [ 14 ] = far * near * rangeInv ;
978
+ }
979
+
980
+ return dst ;
981
+ }
982
+
932
983
let xAxis : Vec3 ;
933
984
let yAxis : Vec3 ;
934
985
let zAxis : Vec3 ;
0 commit comments