@@ -386,71 +386,22 @@ public static Matrix4x4 GetJitteredOrthographicProjectionMatrix(Camera camera, V
386386 return Matrix4x4 . Ortho ( left , right , bottom , top , camera . nearClipPlane , camera . farClipPlane ) ;
387387 }
388388
389- // We can represent a projection matrix by using the tangents of the frustum half angles.
390- // The 'traditional' representation of the values in a projection matrix for
391- // left, right, top and bottom are that they represent values on the near clip plane.
392- // If we take the matrix element (0,0) as an example, it would be equal to
393- // (2 * clipNearPlane) / (right - left). We can divide the term by clipNearPlane to get
394- // 2 / ((right - left) / clipNearPlane), which gives us
395- // 2 / (right/clipNearPlane - left/clipNearPlane). And we can substitue
396- // tan(rightHalfAngle) = (right/clipNearPlane) and tan(leftHalfAngle) = (left/clipNearPlane).
397- // Our new term for (0,0) is 2/(rTan - lTan).
398- //
399- // Since we have the calculated value for (0,0), we can use that to solve for (rTan - lTan).
400- // (rTan - lTan) = 2 / proj(0,0)
401- // We can also get the value for (rTan + lTan), as it is the numerator for term (0,2).
402- // We have the denominator, so (rTan + lTan) = (rTan - lTan) * proj(0,2) = 2 * proj(0,2) / proj (0,0)
403- // We can add (rTan + lTan) and (rTan - lTan) to get 2 * rTan, which can gives us rTan.
404- // rTan = ((2 / proj(0,0)) + (2 * proj(0,2) / proj(0,0))) / 2 =>
405- // rTan = (1 + proj(0,2)) / proj(0,0)
406- //
407- // We can derive lTan via proj(0,0) = 2 / (rTan - lTan), which gives us
408- // lTan = rTan - 2/proj(0,0)
409- // If we substitute our derivation for rTan in here, we get the conveniently symmetric:
410- // lTan = ((1 + proj(0,2)) / proj(0,0)) - 2 / proj(0,0) =>
411- // lTan = (-1 + proj(0,2)) / proj(0,0)
412- //
413- // We can repeat these calculations for the top and bottom tangents as well.
414389 public static Matrix4x4 GenerateJitteredProjectionMatrixFromOriginal ( PostProcessRenderContext context , Matrix4x4 origProj , Vector2 jitter )
415390 {
416- var rTan = ( 1.0f + origProj [ 0 , 2 ] ) / origProj [ 0 , 0 ] ;
417- var lTan = ( - 1.0f + origProj [ 0 , 2 ] ) / origProj [ 0 , 0 ] ;
391+ FrustumPlanes planes = origProj . decomposeProjection ;
418392
419- var tTan = ( 1.0f + origProj [ 1 , 2 ] ) / origProj [ 1 , 1 ] ;
420- var bTan = ( - 1.0f + origProj [ 1 , 2 ] ) / origProj [ 1 , 1 ] ;
393+ float vertFov = Math . Abs ( planes . top ) + Math . Abs ( planes . bottom ) ;
394+ float horizFov = Math . Abs ( planes . left ) + Math . Abs ( planes . right ) ;
421395
422- float tanVertFov = Math . Abs ( tTan ) + Math . Abs ( bTan ) ;
423- float tanHorizFov = Math . Abs ( lTan ) + Math . Abs ( rTan ) ;
396+ var planeJitter = new Vector2 ( jitter . x * horizFov / context . singleEyeWidth ,
397+ jitter . y * vertFov / context . height ) ;
424398
425- jitter . x *= tanHorizFov / context . singleEyeWidth ;
426- jitter . y *= tanVertFov / context . height ;
399+ planes . left += planeJitter . x ;
400+ planes . right += planeJitter . x ;
401+ planes . top += planeJitter . y ;
402+ planes . bottom += planeJitter . y ;
427403
428- float left = jitter . x + lTan ;
429- float right = jitter . x + rTan ;
430- float top = jitter . y + tTan ;
431- float bottom = jitter . y + bTan ;
432-
433- var jitteredMatrix = new Matrix4x4 ( ) ;
434-
435- jitteredMatrix [ 0 , 0 ] = 2f / ( right - left ) ;
436- jitteredMatrix [ 0 , 1 ] = 0f ;
437- jitteredMatrix [ 0 , 2 ] = ( right + left ) / ( right - left ) ;
438- jitteredMatrix [ 0 , 3 ] = 0f ;
439-
440- jitteredMatrix [ 1 , 0 ] = 0f ;
441- jitteredMatrix [ 1 , 1 ] = 2f / ( top - bottom ) ;
442- jitteredMatrix [ 1 , 2 ] = ( top + bottom ) / ( top - bottom ) ;
443- jitteredMatrix [ 1 , 3 ] = 0f ;
444-
445- jitteredMatrix [ 2 , 0 ] = 0f ;
446- jitteredMatrix [ 2 , 1 ] = 0f ;
447- jitteredMatrix [ 2 , 2 ] = origProj [ 2 , 2 ] ;
448- jitteredMatrix [ 2 , 3 ] = origProj [ 2 , 3 ] ;
449-
450- jitteredMatrix [ 3 , 0 ] = 0f ;
451- jitteredMatrix [ 3 , 1 ] = 0f ;
452- jitteredMatrix [ 3 , 2 ] = - 1f ;
453- jitteredMatrix [ 3 , 3 ] = 0f ;
404+ var jitteredMatrix = Matrix4x4 . Frustum ( planes ) ;
454405
455406 return jitteredMatrix ;
456407 }
0 commit comments